From e4a76730708f470712fa1e7b7c482c10664d644e Mon Sep 17 00:00:00 2001 From: viral32111 <19510403+viral32111@users.noreply.github.com> Date: Sat, 2 May 2020 16:01:33 +0100 Subject: [PATCH 1/4] Made the version checker optional The version checker can now be toggled via the sv_mysqloo_versioncheck console variable. --- MySQLOO/source/GMModule.cpp | 55 +++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/MySQLOO/source/GMModule.cpp b/MySQLOO/source/GMModule.cpp index accce64..a8ca859 100644 --- a/MySQLOO/source/GMModule.cpp +++ b/MySQLOO/source/GMModule.cpp @@ -7,6 +7,9 @@ #define MYSQLOO_VERSION "9" #define MYSQLOO_MINOR_VERSION "6" +// Variable to hold the reference to the version check ConVar object +int iVersionCheckConVar = NULL; + GMOD_MODULE_CLOSE() { /* Deletes all the remaining luaobjects when the server changes map */ @@ -122,14 +125,35 @@ 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) { + LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB); // Push the global table + // 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); + } + + // Free the version check ConVar object reference + LUA->ReferenceFree(iVersionCheckConVar); + LUA->Pop(); // Pop the global table + } + return 0; } @@ -174,6 +198,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; } From 6dc8600a605012eefe0bb90b7c77395b70e162fc Mon Sep 17 00:00:00 2001 From: viral32111 <19510403+viral32111@users.noreply.github.com> Date: Sat, 2 May 2020 22:03:05 +0100 Subject: [PATCH 2/4] Free reference in module close function https://github.com/FredyH/MySQLOO/pull/59#discussion_r419006371 --- MySQLOO/source/GMModule.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MySQLOO/source/GMModule.cpp b/MySQLOO/source/GMModule.cpp index a8ca859..46781f3 100644 --- a/MySQLOO/source/GMModule.cpp +++ b/MySQLOO/source/GMModule.cpp @@ -11,6 +11,9 @@ int iVersionCheckConVar = NULL; 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) { @@ -148,9 +151,6 @@ static int doVersionCheck(lua_State* state) { LUA->Call(3, 0); LUA->Pop(2); } - - // Free the version check ConVar object reference - LUA->ReferenceFree(iVersionCheckConVar); LUA->Pop(); // Pop the global table } From 220015d0ede4ae8333119560add75a985388e0ee Mon Sep 17 00:00:00 2001 From: viral32111 <19510403+viral32111@users.noreply.github.com> Date: Sat, 2 May 2020 22:03:47 +0100 Subject: [PATCH 3/4] Don't push global table to stack https://github.com/FredyH/MySQLOO/pull/59#discussion_r419006529 --- MySQLOO/source/GMModule.cpp | 40 ++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/MySQLOO/source/GMModule.cpp b/MySQLOO/source/GMModule.cpp index 46781f3..43ec2f4 100644 --- a/MySQLOO/source/GMModule.cpp +++ b/MySQLOO/source/GMModule.cpp @@ -131,27 +131,25 @@ static int doVersionCheck(lua_State* state) { // Check if the reference to the ConVar object is set if (iVersionCheckConVar != NULL) { - LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB); // Push the global table - // 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); - } - LUA->Pop(); // Pop the global table + // 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; From f2a38cdb6736c8f19202e51a26afaf455f42b95f Mon Sep 17 00:00:00 2001 From: viral32111 <19510403+viral32111@users.noreply.github.com> Date: Wed, 13 May 2020 23:35:55 +0100 Subject: [PATCH 4/4] Replaced hungarian notation variable identifiers https://github.com/FredyH/MySQLOO/pull/59#discussion_r424718877 --- MySQLOO/source/GMModule.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/MySQLOO/source/GMModule.cpp b/MySQLOO/source/GMModule.cpp index 43ec2f4..37fbe6f 100644 --- a/MySQLOO/source/GMModule.cpp +++ b/MySQLOO/source/GMModule.cpp @@ -8,11 +8,11 @@ #define MYSQLOO_MINOR_VERSION "6" // Variable to hold the reference to the version check ConVar object -int iVersionCheckConVar = NULL; +int versionCheckConVar = NULL; GMOD_MODULE_CLOSE() { // Free the version check ConVar object reference - LUA->ReferenceFree(iVersionCheckConVar); + LUA->ReferenceFree(versionCheckConVar); /* Deletes all the remaining luaobjects when the server changes map */ @@ -130,16 +130,16 @@ static int doVersionCheck(lua_State* state) { LUA->SetState(state); // Check if the reference to the ConVar object is set - if (iVersionCheckConVar != NULL) { + if (versionCheckConVar != NULL) { // Retrieve the value of the ConVar - LUA->ReferencePush(iVersionCheckConVar); // Push the ConVar object + LUA->ReferencePush(versionCheckConVar); // 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->ReferencePush(versionCheckConVar); // 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 + int versionCheckEnabled = (int)LUA->GetNumber(-1); // Retrieve the returned value // Check if the version check convar is set to 1 - if (iVersionCheckEnabled == 1) { + if (versionCheckEnabled == 1) { // Execute the HTTP request LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB); LUA->GetField(-1, "http"); @@ -207,7 +207,7 @@ GMOD_MODULE_OPEN() { 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 + versionCheckConVar = LUA->ReferenceCreate(); // Store the created ConVar object as a global variable LUA->Pop(); // Pop the global table runInTimer(LUA, 5, doVersionCheck);