Skip to content

Commit 4be8b4d

Browse files
committed
Fix GModDataPack::AddOrUpdateFile detour not working
1 parent 6718df8 commit 4be8b4d

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

source/main.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,8 @@ static std::unordered_set<std::string> whitelist_pathid = { "lsv", "lua", "data"
6161

6262
class GModDataPackProxy : public Detouring::ClassProxy<GModDataPack, GModDataPackProxy>
6363
{
64-
private:
65-
static FunctionPointers::GModDataPack_AddOrUpdateFile_t AddOrUpdateFile_original;
66-
static const char hook_name[];
67-
static GarrysMod::Lua::ILuaBase *lua;
68-
6964
public:
70-
static void Initialize( GarrysMod::Lua::ILuaBase *LUA )
65+
void Initialize( GarrysMod::Lua::ILuaBase *LUA )
7166
{
7267
lua = LUA;
7368

@@ -79,34 +74,38 @@ class GModDataPackProxy : public Detouring::ClassProxy<GModDataPack, GModDataPac
7974
LUA->ThrowError( "failed to hook GModDataPack::AddOrUpdateFile" );
8075
}
8176

82-
static void Deinitialize( GarrysMod::Lua::ILuaBase *LUA )
77+
void Deinitialize( GarrysMod::Lua::ILuaBase *LUA )
8378
{
8479
UnHook( AddOrUpdateFile_original );
8580
}
8681

8782
void AddOrUpdateFile( LuaFile *file, bool reload )
8883
{
89-
LuaHelpers::PushHookRun( lua, hook_name );
84+
auto &self = Singleton;
85+
86+
LuaHelpers::PushHookRun( self.lua, "AddOrUpdateCSLuaFile" );
9087

91-
lua->PushString( file->path.c_str( ) );
92-
lua->PushBool( reload );
88+
self.lua->PushString( file->path.c_str( ) );
89+
self.lua->PushBool( reload );
9390

9491
bool shouldcall = true;
95-
if( LuaHelpers::CallHookRun( lua, 2, 1 ) )
96-
shouldcall = !lua->IsType( -1, GarrysMod::Lua::Type::BOOL ) || lua->GetBool( -1 );
92+
if( LuaHelpers::CallHookRun( self.lua, 2, 1 ) )
93+
shouldcall = !self.lua->IsType( -1, GarrysMod::Lua::Type::BOOL ) || self.lua->GetBool( -1 );
9794

98-
lua->Pop( 1 );
95+
self.lua->Pop( 1 );
9996

10097
if( shouldcall )
101-
return Call( AddOrUpdateFile_original, file, reload );
98+
return Call( self.AddOrUpdateFile_original, file, reload );
10299
}
103100

104101
static GModDataPackProxy Singleton;
102+
103+
private:
104+
FunctionPointers::GModDataPack_AddOrUpdateFile_t AddOrUpdateFile_original = nullptr;
105+
GarrysMod::Lua::ILuaBase *lua = nullptr;
105106
};
106107

107-
FunctionPointers::GModDataPack_AddOrUpdateFile_t GModDataPackProxy::AddOrUpdateFile_original = nullptr;
108-
const char GModDataPackProxy::hook_name[] = "AddOrUpdateCSLuaFile";
109-
GarrysMod::Lua::ILuaBase *GModDataPackProxy::lua = nullptr;
108+
GModDataPackProxy GModDataPackProxy::Singleton;
110109

111110
static bool IsPathAllowed( std::string &filename )
112111
{

0 commit comments

Comments
 (0)