Skip to content

Commit 111e68e

Browse files
committed
util: implement basics for future version checking
1 parent bf49ca4 commit 111e68e

File tree

8 files changed

+82
-34
lines changed

8 files changed

+82
-34
lines changed

ghostinj-dll/source/dll.cpp

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,46 +52,35 @@
5252
#include <dlfcn.h>
5353
#endif
5454

55+
#ifdef ARCHITECTURE_X86
56+
#define PLATFORM_EXTENSION "linux"
57+
#else
58+
#define PLATFORM_EXTENSION "linux64"
59+
#endif
60+
61+
void UpdateHolyLib()
62+
{
63+
if ( std::filesystem::exists( "garrysmod/lua/bin/gmsv_holylib_" PLATFORM_EXTENSION "_updated.so" ) )
64+
{
65+
printf( "Found a updated holylib version.\n" );
66+
std::filesystem::rename( "garrysmod/lua/bin/gmsv_holylib_" PLATFORM_EXTENSION ".so", "garrysmod/lua/bin/gmsv_holylib_" PLATFORM_EXTENSION "_previous.so" );
67+
std::filesystem::rename( "garrysmod/lua/bin/gmsv_holylib_" PLATFORM_EXTENSION "_updated.so", "garrysmod/lua/bin/gmsv_holylib_" PLATFORM_EXTENSION ".so" );
68+
printf( "Updated HolyLib\n" );
69+
}
70+
}
71+
5572
void* ghostinj2 = NULL;
5673
void* holylib = NULL;
5774
typedef void ( *plugin_main )();
5875
void Load()
5976
{
6077
printf( "--- HolyLib-GhostInj Loading ---\n" );
6178

62-
#ifdef ARCHITECTURE_X86
63-
if ( std::filesystem::exists( "garrysmod/lua/bin/gmsv_holylib_linux_updated.so" ) )
64-
{
65-
printf( "Found a updated holylib version.\n" );
66-
if ( std::filesystem::remove( "garrysmod/lua/bin/gmsv_holylib_linux.so" ) )
67-
{
68-
std::filesystem::rename( "garrysmod/lua/bin/gmsv_holylib_linux_updated.so", "garrysmod/lua/bin/gmsv_holylib_linux.so" );
69-
printf( "Updated HolyLib\n" );
70-
} else {
71-
printf( "Failed to delete old HolyLib version!\n" );
72-
}
73-
}
79+
UpdateHolyLib();
7480

7581
holylib = dlopen( "garrysmod/lua/bin/gmsv_holylib_linux.so", RTLD_NOW );
7682
if ( !holylib )
7783
printf( "Failed to open gmsv_holylib_linux.so (%s)\n", dlerror() );
78-
#else
79-
if ( std::filesystem::exists( "garrysmod/lua/bin/gmsv_holylib_linux64_updated.so" ) )
80-
{
81-
printf( "Found a updated holylib version.\n" );
82-
if ( std::filesystem::remove( "garrysmod/lua/bin/gmsv_holylib_linux64.so" ) )
83-
{
84-
std::filesystem::rename( "garrysmod/lua/bin/gmsv_holylib_linux64_updated.so", "garrysmod/lua/bin/gmsv_holylib_linux64.so" );
85-
printf( "Updated HolyLib\n" );
86-
} else {
87-
printf( "Failed to delete old HolyLib version!\n" );
88-
}
89-
}
90-
91-
holylib = dlopen( "garrysmod/lua/bin/gmsv_holylib_linux64.so", RTLD_NOW );
92-
if ( !holylib )
93-
printf( "Failed to open gmsv_holylib_linux64.so (%s)\n", dlerror() );
94-
#endif
9584

9685
plugin_main plugin = reinterpret_cast< plugin_main >( dlsym( holylib, "HolyLib_PreLoad" ) );
9786
if ( !plugin ) {

source/_prebuildtools/_compilefiles.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ local function CompileVerionFile()
7676
--local additional = file and file:read("*l") or "0"
7777

7878
local versionFile = [[
79-
// This is a generated file! & This will change on every run so don't include it unless you want cache misses & compiles to take ages.
79+
// This is a generated file! & This will change on every run and don't include it unless you want cache misses & compiles to take ages.
8080
#pragma once
8181
82+
#ifndef _ALLOWVERSIONFILE
83+
#error "Idiot, you included the wrong file! Use versioninfo.h, not _versioninfo.h!"
84+
#endif
85+
8286
#define HOLYLIB_BUILD_BRANCH "]] .. branch .. [["
8387
#define HOLYLIB_BUILD_RUN_NUMBER "]] .. run_number .. [["
8488
]]

source/_versioninfo.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#define _ALLOWVERSIONFILE
12
#include "_versioninfo.h"
3+
#undef _ALLOWVERSIONFILE
24

35
// This file will always have a cache miss, so we make this as simple as possible to not become a slow down in the compile.
46

@@ -11,6 +13,11 @@ const char* HolyLib_GetPluginDescription()
1113
#endif
1214
}
1315

16+
const char* HolyLib_GetVersion()
17+
{
18+
return "0.8";
19+
}
20+
1421
const char* HolyLib_GetRunNumber()
1522
{
1623
return HOLYLIB_BUILD_RUN_NUMBER;

source/holylib.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "module.h"
33
#include "util.h"
44
#include "plugin.h"
5+
#include "versioninfo.h"
56

67
// memdbgon must be the last include file in a .cpp file!!!
78
#include "tier0/memdbgon.h"
@@ -45,6 +46,26 @@ class CHolyLib : public IHolyLib
4546
{
4647
return g_pHolyUtil;
4748
}
49+
50+
virtual const char* GetPluginDescription()
51+
{
52+
return HolyLib_GetPluginDescription();
53+
}
54+
55+
virtual const char* GetVersion()
56+
{
57+
return HolyLib_GetVersion();
58+
}
59+
60+
virtual const char* GetBranch()
61+
{
62+
return HolyLib_GetBranch();
63+
}
64+
65+
virtual const char* GetRunNumber()
66+
{
67+
return HolyLib_GetRunNumber();
68+
}
4869
};
4970

5071
static CHolyLib s_HolyLib;

source/plugin.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "vprof.h"
1212
#include "server.h"
1313
#include "holylua.h"
14+
#include "versioninfo.h"
1415

1516
struct edict_t;
1617
#include "playerinfomanager.h"
@@ -206,7 +207,6 @@ void CServerPlugin::UnPause(void)
206207
//---------------------------------------------------------------------------------
207208
// Purpose: the name of this plugin, returned in "plugin_print" command
208209
//---------------------------------------------------------------------------------
209-
extern const char* HolyLib_GetPluginDescription();
210210
const char* CServerPlugin::GetPluginDescription(void)
211211
{
212212
return HolyLib_GetPluginDescription();
@@ -256,7 +256,6 @@ void CServerPlugin::ServerActivate(edict_t *pEdictList, int edictCount, int clie
256256

257257
Lua::ServerInit();
258258
g_pModuleManager.ServerActivate(pEdictList, edictCount, clientMax);
259-
Util::CheckVersion();
260259
}
261260

262261
//---------------------------------------------------------------------------------
@@ -372,7 +371,6 @@ GMOD_MODULE_OPEN()
372371
else
373372
g_pModuleManager.SetModuleRealm(Module_Realm::MENU);
374373

375-
Util::CheckVersion();
376374
g_pModuleManager.MarkAsBinaryModule();
377375
Lua::SetManualShutdown();
378376
g_HolyLibServerPlugin.Load(NULL, NULL); // Yes. I don't like it but I can't get thoes fancy interfaces.

source/public/iholylib.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ class IHolyLib
3434

3535
// For thoes who don't want to get it by the interface
3636
virtual IHolyUtil* GetHolyUtil() = 0;
37+
38+
// Information about the plugin like version, branch & description
39+
virtual const char* GetPluginDescription() = 0;
40+
virtual const char* GetVersion() = 0;
41+
virtual const char* GetRunNumber() = 0;
42+
virtual const char* GetBranch() = 0;
3743
};
3844

3945
#define INTERFACEVERSION_HOLYLIB "IHOLYLIB001"

source/util.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "httplib.h"
12
#include "util.h"
23
#include "GarrysMod/Lua/LuaObject.h"
34
#include <string>
@@ -11,6 +12,7 @@
1112
#include "toolframework/itoolentity.h"
1213
#include "GarrysMod/IGet.h"
1314
#include <lua.h>
15+
#include "versioninfo.h"
1416

1517
// memdbgon must be the last include file in a .cpp file!!!
1618
#include "tier0/memdbgon.h"
@@ -561,9 +563,22 @@ bool Util::ShouldLoad()
561563
return true;
562564
}
563565

564-
void Util::CheckVersion()
566+
void Util::CheckVersion() // This is called only when holylib is initially loaded! (ToDo: Allow people to disable this!)
565567
{
566568
// ToDo: Implement this someday
569+
httplib::Client pClient("http://holylib.raphaelit7.com");
570+
571+
httplib::Headers headers = {
572+
{ "HolyLib_Branch", HolyLib_GetBranch() },
573+
{ "HolyLib_RunNumber", HolyLib_GetRunNumber() },
574+
{ "HolyLib_Version", HolyLib_GetVersion() }
575+
};
576+
577+
auto res = pClient.Get("/api/check_version");
578+
if (res->status == 200)
579+
{
580+
Msg(PROJECT_NAME ": %s", res->body.c_str());
581+
}
567582
}
568583

569584
static bool g_bUtilInit = false;
@@ -573,6 +588,7 @@ void Util::Load()
573588
return;
574589

575590
g_bUtilInit = true;
591+
Util::CheckVersion();
576592
IConfig* pConVarConfig = g_pConfigSystem->LoadConfig("garrysmod/holylib/cfg/convars.json");
577593
if (pConVarConfig)
578594
{

source/versioninfo.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// You should use this file instead of the _versioninfo.h since this one won't be generated on each run ensuring no cache misses!
2+
#pragma once
3+
4+
extern const char* HolyLib_GetPluginDescription();
5+
extern const char* HolyLib_GetVersion();
6+
extern const char* HolyLib_GetRunNumber();
7+
extern const char* HolyLib_GetBranch();

0 commit comments

Comments
 (0)