Skip to content

Commit 25a7281

Browse files
committed
[ModCore] Add -no-updates start parameter
1 parent e132173 commit 25a7281

3 files changed

Lines changed: 117 additions & 85 deletions

File tree

SDK/BYModCore/Source/AModLoaderTask.cpp

Lines changed: 93 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ TOSHI_NAMESPACE_USING
1515
TDEFINE_CLASS( AModLoaderTask );
1616

1717
extern const T2CommandLine* g_pCommandLine;
18+
extern TBOOL g_bCheckUpdates;
1819

1920
void 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
}

SDK/BYModCore/Source/UpdateManager.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,23 @@ void UpdateManager::AskAutoUpdate()
5050
{
5151
TINFO( "User agreed to update the ModCore\n" );
5252

53+
// Allocate console so user can see something during update
54+
AllocConsole();
55+
FILE* fDummy;
56+
freopen_s( &fDummy, "CONOUT$", "w", stdout );
57+
printf( "Downloading the update...\n" );
58+
5359
AHTTPClient httpClient;
5460
httpClient.Create( versionInfo.strUpdateUrl.GetString() );
5561
httplib::Result downloadResult = httpClient.Get();
5662

63+
// Redirect once
64+
if ( downloadResult->get_header_value_count( "location" ) )
65+
{
66+
httpClient.Create( downloadResult->get_header_value( "location" ).c_str() );
67+
downloadResult = httpClient.Get();
68+
}
69+
5770
if ( downloadResult.error() != httplib::Error::Success ||
5871
downloadResult->status != 200 )
5972
{

SDK/BYModCore/Source/main.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626

2727
TOSHI_NAMESPACE_USING
2828

29-
HMODULE hModuleCore;
30-
3129
static T2CommandLine g_CommandLine;
3230
const T2CommandLine* g_pCommandLine = &g_CommandLine;
3331

32+
TBOOL g_bCheckUpdates = TTRUE;
33+
3434
const char* GetModsDirectory()
3535
{
3636
return "Mods\\";
@@ -113,7 +113,6 @@ DWORD APIENTRY DllMain( HMODULE hModule, DWORD reason, LPVOID reserved )
113113
AllocConsole();
114114
FILE* fDummy;
115115
freopen_s( &fDummy, "CONOUT$", "w", stdout );
116-
hModuleCore = hModule;
117116
#endif
118117

119118
TUtil::TOSHIParams toshiParams;
@@ -134,9 +133,17 @@ DWORD APIENTRY DllMain( HMODULE hModule, DWORD reason, LPVOID reserved )
134133

135134
SetConsoleCtrlHandler( exit_handler, TRUE );
136135

136+
// Auto updates
137+
g_bCheckUpdates = !g_CommandLine.HasParameter( "-no-updates" );
138+
137139
UpdateManager::CleanUp();
138-
UpdateManager::AskAutoUpdate();
139140

141+
if ( g_bCheckUpdates )
142+
UpdateManager::AskAutoUpdate();
143+
else
144+
TINFO( "Auto updates are disabled with command line argument\n" );
145+
146+
// Create thread
140147
TINFO( "Log system was successfully initialised!\n" );
141148
TINFO( "Starting BYModCore thread...\n" );
142149

0 commit comments

Comments
 (0)