Skip to content

Commit

Permalink
feat: SAR version warning
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisAMJ committed Dec 2, 2024
1 parent daa8ec5 commit b23954d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
19 changes: 19 additions & 0 deletions src/Features/Hud/CheatWarn.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "Event.hpp"
#include "Features/Hud/Hud.hpp"
#include "Features/Updater.hpp"
#include "Modules/Scheme.hpp"
#include "Modules/Surface.hpp"
#include "Modules/Client.hpp"
Expand All @@ -16,6 +18,7 @@ struct Cheat {
const char *fix;
};

int g_update_status = 2; // assume no update
static Cheat g_cheats[] = {
{ +[]() {
return sv_cheats.GetBool();
Expand Down Expand Up @@ -62,6 +65,10 @@ static Cheat g_cheats[] = {
{ +[]() {
return fabsf(engine->GetIPT() - 1.0f / sar.game->Tickrate()) > 0.00001f;
}, "tickrate is not default", "remove '-tickrate' from the game launch options" },

{ +[]() {
return g_update_status == 0;
}, "SAR is not up-to-date", "run 'sar_update'" }
};

class CheatWarnHud : public Hud {
Expand Down Expand Up @@ -150,4 +157,16 @@ class CheatWarnHud : public Hud {
}
};

static std::thread g_worker;
ON_INIT {
if (g_worker.joinable()) g_worker.join();
g_worker = std::thread(checkUpdate, Channel::Release, false, [](int status) {
g_update_status = status;
});
}

ON_EVENT(SAR_UNLOAD) {
if (g_worker.joinable()) g_worker.join();
}

CheatWarnHud cheatWarnHud;
32 changes: 15 additions & 17 deletions src/Features/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ struct SarVersion {

#define DL_SAR_HOST "https://dl.sar.portal2.sr"

enum class Channel {
Release,
PreRelease,
Canary,
};

static std::optional<SarVersion> getVersionComponents(const char *str) {
SarVersion v = {0, 0, false};
v.pre = UINT_MAX;
Expand Down Expand Up @@ -84,7 +78,7 @@ static std::optional<SarVersion> getVersionComponents(const char *str) {
return v;
}

static bool isNewerVersion(std::string& verStr) {
static bool isNewerVersion(std::string& verStr, bool print) {
auto version = getVersionComponents(verStr.c_str());
auto current = getVersionComponents(SAR_VERSION);

Expand All @@ -95,7 +89,7 @@ static bool isNewerVersion(std::string& verStr) {

if (!current || current->canary) {
// Otherwise, canary versions shouldn't downgrade to releases
THREAD_PRINT("Cannot compare version numbers on non-release version\n");
if (print) THREAD_PRINT("Cannot compare version numbers on non-release version\n");
return false;
}

Expand Down Expand Up @@ -275,23 +269,27 @@ static std::string createTempPath(const char *filename) {
return p.string();
}

void checkUpdate(Channel channel) {
void checkUpdate(Channel channel, bool print, std::function<void(int)> callback) {
std::string name, dlUrl, pdbUrl;

THREAD_PRINT("Querying for latest version...\n");
if (print) THREAD_PRINT("Querying for latest version...\n");

if (!getLatestVersion(&name, &dlUrl, &pdbUrl, channel)) {
THREAD_PRINT("An error occurred retrieving latest version\n");
if (print) THREAD_PRINT("An error occurred retrieving latest version\n");
callback(1); // RC=1 indicates failure to check for updates
return;
}

THREAD_PRINT("Latest version is %s\n", name.c_str());
if (print) THREAD_PRINT("Latest version is %s\n", name.c_str());

if (!isNewerVersion(name)) {
THREAD_PRINT("You're all up-to-date!\n");
bool isNewer = isNewerVersion(name, print);
if (!isNewer) {
if (print) THREAD_PRINT("You're all up-to-date!\n");
} else {
THREAD_PRINT("Update with sar_update, or at %s\n", dlUrl.c_str());
if (print) THREAD_PRINT("Update with sar_update, or at %s\n", dlUrl.c_str());
}

callback(isNewer ? 0 : 2); // RC=0 indicates update available, RC=2 otherwise
}

void doUpdate(Channel channel, int successAction, bool force) {
Expand All @@ -304,7 +302,7 @@ void doUpdate(Channel channel, int successAction, bool force) {
return;
}

if (!force && !isNewerVersion(name)) {
if (!force && !isNewerVersion(name, true)) {
THREAD_PRINT("You're already up-to-date!\n");
return;
}
Expand Down Expand Up @@ -396,7 +394,7 @@ CON_COMMAND(sar_check_update, "sar_check_update [release|pre|canary] - check whe
}

if (g_worker.joinable()) g_worker.join();
g_worker = std::thread(checkUpdate, channel);
g_worker = std::thread(checkUpdate, channel, true, [](int) {});
}

CON_COMMAND(sar_update, "sar_update [release|pre|canary] [exit|restart] [force] - update SAR to the latest version. If exit is given, exit the game upon successful update; if force is given, always re-install, even if it may be a downgrade\n") {
Expand Down
8 changes: 8 additions & 0 deletions src/Features/Updater.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
#pragma once

enum class Channel {
Release,
PreRelease,
Canary,
};

void checkUpdate(Channel channel, bool print, std::function<void(int)> cb);

0 comments on commit b23954d

Please sign in to comment.