Skip to content

Commit 2b4e4fb

Browse files
committed
redo for the modern age
1 parent 8d9377c commit 2b4e4fb

File tree

4 files changed

+107
-67
lines changed

4 files changed

+107
-67
lines changed

loader/src/loader/updater.cpp

Lines changed: 45 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "ModMetadataImpl.hpp"
88
#include <Geode/utils/string.hpp>
99

10+
#include "../server/Server.hpp"
11+
1012
using namespace geode::prelude;
1113

1214
static std::unordered_map<std::string, web::WebTask> RUNNING_REQUESTS {};
@@ -23,10 +25,12 @@ updater::LoaderUpdateEvent::LoaderUpdateEvent(
2325

2426
updater::LoaderUpdateFilter::LoaderUpdateFilter() = default;
2527

28+
bool s_isNewUpdateDownloaded = false;
29+
30+
/*
2631
// cache for the json of the latest github release to avoid hitting
2732
// the github api too much
2833
std::optional<matjson::Value> s_latestGithubRelease;
29-
bool s_isNewUpdateDownloaded = false;
3034
3135
void updater::fetchLatestGithubRelease(
3236
const std::function<void(matjson::Value const&)>& then,
@@ -86,34 +90,24 @@ void updater::fetchLatestGithubRelease(
8690
)
8791
);
8892
}
93+
*/
8994

9095
void updater::downloadLatestLoaderResources() {
9196
log::debug("Downloading latest resources", Loader::get()->getVersion().toVString());
92-
fetchLatestGithubRelease(
93-
[](matjson::Value const& raw) {
94-
auto root = checkJson(raw, "[]");
95-
96-
// find release asset
97-
for (auto& obj : root.needs("assets").items()) {
98-
if (obj.needs("name").get<std::string>() == "resources.zip") {
99-
updater::tryDownloadLoaderResources(
100-
obj.needs("browser_download_url").get<std::string>(),
101-
false
102-
);
103-
return;
104-
}
105-
}
10697

107-
ResourceDownloadEvent(
108-
UpdateFailed("Unable to find resources in latest GitHub release")
109-
).post();
110-
},
111-
[](std::string const& info) {
112-
ResourceDownloadEvent(
113-
UpdateFailed("Unable to download resources: " + info)
114-
).post();
115-
},
116-
true
98+
server::getLatestLoaderVersion().listen(
99+
[](Result<server::ServerLoaderVersion, server::ServerError>* res) {
100+
if (res->ok()) {
101+
auto& release = res->unwrap();
102+
updater::downloadLoaderUpdate(
103+
fmt::format("https://github.com/geode-sdk/geode/releases/download/{0}/geode-{0}-{1}.zip", release.tag, GEODE_PLATFORM_SHORT_IDENTIFIER_NOARCH)
104+
);
105+
} else {
106+
ResourceDownloadEvent(
107+
UpdateFailed("Unable to download resources: " + res->unwrapErr().details)
108+
).post();
109+
}
110+
}
117111
);
118112
}
119113

@@ -351,56 +345,40 @@ void updater::downloadLoaderUpdate(std::string const& url) {
351345

352346
void updater::checkForLoaderUpdates() {
353347
// Check for updates in the background
354-
fetchLatestGithubRelease(
355-
[](matjson::Value const& raw) {
356-
auto root = checkJson(raw, "[]");
348+
server::getLatestLoaderVersion().listen(
349+
[](Result<server::ServerLoaderVersion, server::ServerError>* res) {
350+
if (res->ok()) {
351+
auto& release = res->unwrap();
352+
auto ver = VersionInfo::parse(release.tag).unwrapOrDefault();
357353

358-
VersionInfo ver { 0, 0, 0 };
359-
root.needs("tag_name").into(ver);
354+
log::info("Latest Geode version is {}", ver.toVString());
355+
Mod::get()->setSavedValue("latest-version-auto-update-check", ver.toVString());
360356

361-
log::info("Latest Geode version is {}", ver.toVString());
362-
Mod::get()->setSavedValue("latest-version-auto-update-check", ver.toVString());
357+
// make sure release is newer
358+
if (ver <= Loader::get()->getVersion()) {
359+
if(ver <= VersionInfo(2, 0, 0, VersionTag(VersionTag::Beta, 1))) {
360+
log::warn("Invalid loader version detected, resetting update check time");
363361

364-
// make sure release is newer
365-
if (ver <= Loader::get()->getVersion()) {
366-
if(ver <= VersionInfo(2, 0, 0, VersionTag(VersionTag::Beta, 1))) {
367-
log::warn("Invalid loader version detected, resetting update check time");
368-
369-
Mod::get()->setSavedValue("last-modified-auto-update-check", std::string());
362+
Mod::get()->setSavedValue("last-modified-auto-update-check", std::string());
363+
}
364+
return;
370365
}
371-
return;
372-
}
373-
374-
// don't auto-update major versions when not on forward compat
375-
if (!Loader::get()->isForwardCompatMode() && ver.getMajor() > Loader::get()->getVersion().getMajor()) {
376-
return;
377-
}
378366

379-
// find release asset
380-
for (auto& obj : root.needs("assets").items()) {
381-
if (string::endsWith(
382-
obj.needs("name").get<std::string>(),
383-
fmt::format("{}.zip", PlatformID::toShortString(GEODE_PLATFORM_TARGET, true))
384-
)) {
385-
updater::downloadLoaderUpdate(
386-
obj.needs("browser_download_url").get<std::string>()
387-
);
367+
// don't auto-update major versions when not on forward compat
368+
if (!Loader::get()->isForwardCompatMode() && ver.getMajor() > Loader::get()->getVersion().getMajor()) {
388369
return;
389370
}
390-
}
391-
392-
log::error("Failed to find release asset for " GEODE_PLATFORM_NAME);
393-
LoaderUpdateEvent(
394-
UpdateFailed("Unable to find release asset for " GEODE_PLATFORM_NAME)
395-
).post();
396371

397-
Mod::get()->setSavedValue("last-modified-auto-update-check", std::string());
398-
},
399-
[](std::string const& info) {
400-
log::error("Failed to fetch updates {}", info);
401-
LoaderUpdateEvent(
402-
UpdateFailed("Unable to check for updates: " + info)
403-
).post();
372+
// find release asset
373+
updater::downloadLoaderUpdate(
374+
fmt::format("https://github.com/geode-sdk/geode/releases/download/{0}/geode-{0}-{1}.zip", release.tag, GEODE_PLATFORM_SHORT_IDENTIFIER_NOARCH)
375+
);
376+
} else {
377+
log::error("Failed to fetch updates {}", res->unwrapErr().details);
378+
LoaderUpdateEvent(
379+
UpdateFailed("Unable to check for updates: " + res->unwrapErr().details)
380+
).post();
381+
}
404382
}
405383
);
406384
}

loader/src/loader/updater.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ namespace geode::updater {
4343
void downloadLoaderResources(bool useLatestRelease = false);
4444
void downloadLatestLoaderResources();
4545
void downloadLoaderUpdate(std::string const& url);
46+
47+
/*
4648
void fetchLatestGithubRelease(
4749
const std::function<void(matjson::Value const&)>& then,
4850
std::function<void(std::string const&)> expect,
4951
bool force = false
5052
);
53+
*/
5154

5255
bool verifyLoaderResources();
5356
void checkForLoaderUpdates();

loader/src/server/Server.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,20 @@ Result<ServerModsList> ServerModsList::parse(matjson::Value const& raw) {
529529
return payload.ok(list);
530530
}
531531

532+
Result<ServerLoaderVersion> ServerLoaderVersion::parse(matjson::Value const& raw) {
533+
auto root = checkJson(raw, "ServerLoaderVersion");
534+
535+
auto res = ServerLoaderVersion();
536+
root.needs("version").into(res.version);
537+
root.needs("tag").into(res.tag);
538+
root.needs("commit_hash").into(res.commitHash);
539+
540+
auto gd_obj = root.needs("gd");
541+
gd_obj.needs(GEODE_PLATFORM_SHORT_IDENTIFIER).into(res.gameVersion);
542+
543+
return root.ok(res);
544+
}
545+
532546
ModMetadata ServerModMetadata::latestVersion() const {
533547
return this->versions.front().metadata;
534548
}
@@ -890,6 +904,40 @@ ServerRequest<std::vector<ServerModUpdate>> server::checkAllUpdates(bool useCach
890904
);
891905
}
892906

907+
ServerRequest<ServerLoaderVersion> server::getLatestLoaderVersion(bool useCache) {
908+
if (useCache) {
909+
return getCache<getLatestLoaderVersion>().get();
910+
}
911+
912+
auto req = web::WebRequest();
913+
req.userAgent(getServerUserAgent());
914+
915+
req.param("platform", GEODE_PLATFORM_SHORT_IDENTIFIER_NOARCH);
916+
req.param("gd", GEODE_GD_VERSION_STR);
917+
918+
return req.get(formatServerURL("/loader/versions/latest")).map(
919+
[](web::WebResponse* response) -> Result<ServerLoaderVersion, ServerError> {
920+
if (response->ok()) {
921+
// Parse payload
922+
auto payload = parseServerPayload(*response);
923+
if (!payload) {
924+
return Err(payload.unwrapErr());
925+
}
926+
// Parse response
927+
auto r = ServerLoaderVersion::parse(payload.unwrap());
928+
if (!r) {
929+
return Err(ServerError(response->code(), "Unable to parse response: {}", r.unwrapErr()));
930+
}
931+
return Ok(r.unwrap());
932+
}
933+
return Err(parseServerError(*response));
934+
},
935+
[](web::WebProgress* progress) {
936+
return parseServerProgress(*progress, "Checking for loader updates");
937+
}
938+
);
939+
}
940+
893941
void server::clearServerCaches(bool clearGlobalCaches) {
894942
getCache<&getMods>().clear();
895943
getCache<&getMod>().clear();
@@ -899,6 +947,7 @@ void server::clearServerCaches(bool clearGlobalCaches) {
899947
if (clearGlobalCaches) {
900948
getCache<&getTags>().clear();
901949
getCache<&checkAllUpdates>().clear();
950+
getCache<&getLatestLoaderVersion>().clear();
902951
}
903952
}
904953

loader/src/server/Server.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ namespace server {
103103
static Result<ServerModsList> parse(matjson::Value const& json);
104104
};
105105

106+
struct ServerLoaderVersion final {
107+
std::string version;
108+
std::string tag;
109+
std::string commitHash;
110+
std::string gameVersion;
111+
112+
static Result<ServerLoaderVersion> parse(matjson::Value const& json);
113+
};
114+
106115
enum class ModsSort {
107116
Downloads,
108117
RecentlyUpdated,
@@ -178,6 +187,7 @@ namespace server {
178187
);
179188

180189
ServerRequest<std::vector<ServerModUpdate>> checkAllUpdates(bool useCache = true);
190+
ServerRequest<ServerLoaderVersion> getLatestLoaderVersion(bool useCache = true);
181191

182192
void clearServerCaches(bool clearGlobalCaches = false);
183193
}

0 commit comments

Comments
 (0)