@@ -15,6 +15,7 @@ TOSHI_NAMESPACE_USING
1515TDEFINE_CLASS ( AModLoaderTask );
1616
1717extern const T2CommandLine* g_pCommandLine;
18+ extern TBOOL g_bCheckUpdates;
1819
1920void AModLoaderTask::AGUI2MainPostRenderCallback ()
2021{
@@ -79,100 +80,111 @@ void AModLoaderTask::LoadMods()
7980{
8081 auto pchCommandLine = GetCommandLineA ();
8182
82- TINFO ( " Checking mods versions are up to date...\n " );
83-
84- // Update mods
85- for ( const auto & entry : std::filesystem::directory_iterator ( GetModsDirectory () ) )
83+ if ( g_bCheckUpdates )
8684 {
87- if ( entry.path ().extension ().compare ( L" .dll" ) == 0 )
88- {
89- HMODULE hModule = LoadLibraryExW ( entry.path ().native ().c_str (), NULL , DONT_RESOLVE_DLL_REFERENCES );
90- auto fnGetModAutoUpdateURL = TREINTERPRETCAST ( t_GetModAutoUpdateURL, GetProcAddress ( hModule, " GetModAutoUpdateURL" ) );
91- auto fnGetVersion = TREINTERPRETCAST ( t_GetModVersion, GetProcAddress ( hModule, " GetModVersion" ) );
92- auto fnGetName = TREINTERPRETCAST ( t_GetModName, GetProcAddress ( hModule, " GetModName" ) );
85+ TINFO ( " Checking mods versions are up to date...\n " );
9386
94- // Skip mods that don't have auto updates enabled
95- if ( !fnGetModAutoUpdateURL || !fnGetVersion )
87+ // Update mods
88+ for ( const auto & entry : std::filesystem::directory_iterator ( GetModsDirectory () ) )
89+ {
90+ if ( entry.path ().extension ().compare ( L" .dll" ) == 0 )
9691 {
97- FreeLibrary ( hModule );
98- continue ;
99- }
92+ HMODULE hModule = LoadLibraryExW ( entry.path ().native ().c_str (), NULL , DONT_RESOLVE_DLL_REFERENCES );
93+ auto fnGetModAutoUpdateURL = TREINTERPRETCAST ( t_GetModAutoUpdateURL, GetProcAddress ( hModule, " GetModAutoUpdateURL" ) );
94+ auto fnGetVersion = TREINTERPRETCAST ( t_GetModVersion, GetProcAddress ( hModule, " GetModVersion" ) );
95+ auto fnGetName = TREINTERPRETCAST ( t_GetModName, GetProcAddress ( hModule, " GetModName" ) );
10096
101- // Obtain the update url
102- TString8 updateInfoURL = fnGetModAutoUpdateURL ();
103- TString8 strModName = ( fnGetName ) ? fnGetName () : " Unknown Mod" ;
104- TVersion modVersion = fnGetVersion ();
105-
106- // We don't need the dll to be attached anymore
107- FreeLibrary ( hModule );
108-
109- if ( !updateInfoURL.Length () ) continue ;
110-
111- TINFO ( " Checking for updates of the '%s' mod...\n " , strModName.GetString () );
112-
113- UpdateManager::VersionInfo versionInfo;
114- TBOOL bOutDated = UpdateManager::CheckVersion ( updateInfoURL.GetString (), modVersion, &versionInfo );
115-
116- if ( bOutDated )
117- {
118- TINFO ( " New version (v%u.%u) of the '%s' (v%u.%u) mod is available\n " , versionInfo.uiVersion .Parts .Major , versionInfo.uiVersion .Parts .Minor , strModName.GetString (), modVersion.Parts .Major , modVersion.Parts .Minor );
119-
120- TString8 strDescription = TString8::VarArgs (
121- " New version of the '%s' mod is available!\n\n "
122- " Current version: v%u.%u\n "
123- " Latest version: v%u.%u\n\n "
124- " Would you like to update now?" ,
125- strModName.GetString (),
126- modVersion.Parts .Major ,
127- modVersion.Parts .Minor ,
128- versionInfo.uiVersion .Parts .Major ,
129- versionInfo.uiVersion .Parts .Minor
130- );
131-
132- const TINT msgBoxResult = MessageBoxA ( NULL , strDescription.GetString (), " Barnyard ModLoader" , MB_YESNO );
133-
134- if ( msgBoxResult == IDYES )
97+ // Skip mods that don't have auto updates enabled
98+ if ( !fnGetModAutoUpdateURL || !fnGetVersion )
13599 {
136- TINFO ( " User agreed to update mod\n " );
100+ FreeLibrary ( hModule );
101+ continue ;
102+ }
137103
138- AHTTPClient httpClient;
139- httpClient.Create ( versionInfo.strUpdateUrl .GetString () );
140- httplib::Result downloadResult = httpClient.Get ();
104+ // Obtain the update url
105+ TString8 updateInfoURL = fnGetModAutoUpdateURL ();
106+ TString8 strModName = ( fnGetName ) ? fnGetName () : " Unknown Mod" ;
107+ TVersion modVersion = fnGetVersion ();
141108
142- if ( downloadResult.error () != httplib::Error::Success ||
143- downloadResult->status != 200 )
144- {
145- TERROR ( " Unable to download new version of the mod\n " );
146- TASSERT ( TFALSE );
147- MessageBoxA ( NULL , " Unable to download new version of the mod! Canceling..." , " Barnyard ModLoader" , MB_OK );
148- continue ;
149- }
109+ // We don't need the dll to be attached anymore
110+ FreeLibrary ( hModule );
150111
151- TINFO ( " Downloaded new version of the mod! \n " ) ;
112+ if ( !updateInfoURL. Length () ) continue ;
152113
153- HANDLE hFile = :: CreateFileW ( entry. path (). native (). c_str (), GENERIC_WRITE, FILE_SHARE_WRITE, NULL , CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
114+ TINFO ( " Checking for updates of the '%s' mod... \n " , strModName. GetString () );
154115
155- if ( hFile == INVALID_HANDLE_VALUE )
156- {
157- TERROR ( " Unable to open file for writing. Error: %u\n " , GetLastError () );
158- TASSERT ( TFALSE );
159- MessageBoxA ( NULL , " Unable to open file for writing! Canceling..." , " Barnyard ModLoader" , MB_OK );
160- continue ;
161- }
116+ UpdateManager::VersionInfo versionInfo;
117+ TBOOL bOutDated = UpdateManager::CheckVersion ( updateInfoURL.GetString (), modVersion, &versionInfo );
162118
163- DWORD dwNumWritten;
164- BOOL bWritten = WriteFile ( hFile, downloadResult->body .c_str (), downloadResult->body .size (), &dwNumWritten, NULL );
165-
166- if ( !bWritten || dwNumWritten != downloadResult->body .size () )
119+ if ( bOutDated )
120+ {
121+ TINFO ( " New version (v%u.%u) of the '%s' (v%u.%u) mod is available\n " , versionInfo.uiVersion .Parts .Major , versionInfo.uiVersion .Parts .Minor , strModName.GetString (), modVersion.Parts .Major , modVersion.Parts .Minor );
122+
123+ TString8 strDescription = TString8::VarArgs (
124+ " New version of the '%s' mod is available!\n\n "
125+ " Current version: v%u.%u\n "
126+ " Latest version: v%u.%u\n\n "
127+ " Would you like to update now?" ,
128+ strModName.GetString (),
129+ modVersion.Parts .Major ,
130+ modVersion.Parts .Minor ,
131+ versionInfo.uiVersion .Parts .Major ,
132+ versionInfo.uiVersion .Parts .Minor
133+ );
134+
135+ const TINT msgBoxResult = MessageBoxA ( NULL , strDescription.GetString (), " Barnyard ModLoader" , MB_YESNO );
136+
137+ if ( msgBoxResult == IDYES )
167138 {
168- TERROR ( " An unknown error has occured during updating the mod. Error: %u\n " , GetLastError () );
169- TASSERT ( TFALSE );
170- MessageBoxA ( NULL , " An unknown error has occured during updating the mod." , " Barnyard ModLoader" , MB_OK );
171- }
172-
173- ::CloseHandle ( hFile );
139+ TINFO ( " User agreed to update mod\n " );
140+
141+ AHTTPClient httpClient;
142+ httpClient.Create ( versionInfo.strUpdateUrl .GetString () );
143+ httplib::Result downloadResult = httpClient.Get ();
174144
175- TINFO ( " Successfully updated the mod!\n " );
145+ // Redirect once
146+ if ( downloadResult->get_header_value_count ( " location" ) )
147+ {
148+ httpClient.Create ( downloadResult->get_header_value ( " location" ).c_str () );
149+ downloadResult = httpClient.Get ();
150+ }
151+
152+ if ( downloadResult.error () != httplib::Error::Success ||
153+ downloadResult->status != 200 )
154+ {
155+ TERROR ( " Unable to download new version of the mod\n " );
156+ TASSERT ( TFALSE );
157+ MessageBoxA ( NULL , " Unable to download new version of the mod! Canceling..." , " Barnyard ModLoader" , MB_OK );
158+ continue ;
159+ }
160+
161+ TINFO ( " Downloaded new version of the mod!\n " );
162+
163+ HANDLE hFile = ::CreateFileW ( entry.path ().native ().c_str (), GENERIC_WRITE, FILE_SHARE_WRITE, NULL , CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
164+
165+ if ( hFile == INVALID_HANDLE_VALUE )
166+ {
167+ TERROR ( " Unable to open file for writing. Error: %u\n " , GetLastError () );
168+ TASSERT ( TFALSE );
169+ MessageBoxA ( NULL , " Unable to open file for writing! Canceling..." , " Barnyard ModLoader" , MB_OK );
170+ continue ;
171+ }
172+
173+ DWORD dwNumWritten;
174+ BOOL bWritten = WriteFile ( hFile, downloadResult->body .c_str (), downloadResult->body .size (), &dwNumWritten, NULL );
175+
176+ if ( !bWritten || dwNumWritten != downloadResult->body .size () )
177+ {
178+ TERROR ( " An unknown error has occured during updating the mod. Error: %u\n " , GetLastError () );
179+ TASSERT ( TFALSE );
180+ MessageBoxA ( NULL , " An unknown error has occured during updating the mod." , " Barnyard ModLoader" , MB_OK );
181+ }
182+
183+ ::CloseHandle ( hFile );
184+
185+ TINFO ( " Successfully updated the mod!\n " );
186+ MessageBoxA ( NULL , " The mod was successfully updated!" , " Barnyard ModLoader" , MB_OK );
187+ }
176188 }
177189 }
178190 }
0 commit comments