77#include " ModMetadataImpl.hpp"
88#include < Geode/utils/string.hpp>
99
10+ #include " ../server/Server.hpp"
11+
1012using namespace geode ::prelude;
1113
1214static std::unordered_map<std::string, web::WebTask> RUNNING_REQUESTS {};
@@ -23,10 +25,12 @@ updater::LoaderUpdateEvent::LoaderUpdateEvent(
2325
2426updater::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
2833std::optional<matjson::Value> s_latestGithubRelease;
29- bool s_isNewUpdateDownloaded = false ;
3034
3135void updater::fetchLatestGithubRelease(
3236 const std::function<void(matjson::Value const&)>& then,
@@ -86,34 +90,24 @@ void updater::fetchLatestGithubRelease(
8690 )
8791 );
8892}
93+ */
8994
9095void 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
352346void 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}
0 commit comments