Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the version checker optional #59

Merged
merged 4 commits into from
May 13, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 45 additions & 8 deletions MySQLOO/source/GMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
#define MYSQLOO_VERSION "9"
#define MYSQLOO_MINOR_VERSION "6"

// Variable to hold the reference to the version check ConVar object
int iVersionCheckConVar = NULL;
viral32111 marked this conversation as resolved.
Show resolved Hide resolved

GMOD_MODULE_CLOSE() {
// Free the version check ConVar object reference
LUA->ReferenceFree(iVersionCheckConVar);

/* Deletes all the remaining luaobjects when the server changes map
*/
for (auto query : LuaObjectBase::luaRemovalObjects) {
Expand Down Expand Up @@ -122,14 +128,30 @@ static int fetchFailed(lua_State* state) {
static int doVersionCheck(lua_State* state) {
GarrysMod::Lua::ILuaBase* LUA = state->luabase;
LUA->SetState(state);
LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
LUA->GetField(-1, "http");
LUA->GetField(-1, "Fetch");
LUA->PushString("https://raw.githubusercontent.com/FredyH/MySQLOO/master/minorversion.txt");
LUA->PushCFunction(fetchSuccessful);
LUA->PushCFunction(fetchFailed);
LUA->Call(3, 0);
LUA->Pop(2);

// Check if the reference to the ConVar object is set
if (iVersionCheckConVar != NULL) {
// Retrieve the value of the ConVar
LUA->ReferencePush(iVersionCheckConVar); // Push the ConVar object
LUA->GetField(-1, "GetInt"); // Push the name of the function
LUA->ReferencePush(iVersionCheckConVar); // Push the ConVar object as the first self argument
LUA->Call(1, 1); // Call with 1 argument and 1 return
int iVersionCheckEnabled = (int)LUA->GetNumber(-1); // Retrieve the returned value

// Check if the version check convar is set to 1
if (iVersionCheckEnabled == 1) {
// Execute the HTTP request
LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
LUA->GetField(-1, "http");
LUA->GetField(-1, "Fetch");
LUA->PushString("https://raw.githubusercontent.com/FredyH/MySQLOO/master/minorversion.txt");
LUA->PushCFunction(fetchSuccessful);
LUA->PushCFunction(fetchFailed);
LUA->Call(3, 0);
LUA->Pop(2);
}
}

return 0;
}

Expand Down Expand Up @@ -174,6 +196,21 @@ GMOD_MODULE_OPEN() {

LUA->SetField(-2, "mysqloo");
LUA->Pop();

LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB); // Push the global table
// Create the version check ConVar
LUA->GetField(-1, "CreateConVar");
LUA->PushString("sv_mysqloo_versioncheck"); // Name
LUA->PushString("1"); // Default value
LUA->PushNumber(128); // FCVAR flags
LUA->PushString("Enable or disable the MySQLOO update checker."); // Help text
LUA->PushNumber(0); // Min value
LUA->PushNumber(1); // Max value
LUA->Call(6, 1); // Call with 6 arguments and 1 result
iVersionCheckConVar = LUA->ReferenceCreate(); // Store the created ConVar object as a global variable
LUA->Pop(); // Pop the global table

runInTimer(LUA, 5, doVersionCheck);

return 1;
}