Skip to content

Commit

Permalink
Fix GModDataPack::AddOrUpdateFile detour not working
Browse files Browse the repository at this point in the history
  • Loading branch information
danielga committed Jun 5, 2022
1 parent 6718df8 commit 4be8b4d
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,8 @@ static std::unordered_set<std::string> whitelist_pathid = { "lsv", "lua", "data"

class GModDataPackProxy : public Detouring::ClassProxy<GModDataPack, GModDataPackProxy>
{
private:
static FunctionPointers::GModDataPack_AddOrUpdateFile_t AddOrUpdateFile_original;
static const char hook_name[];
static GarrysMod::Lua::ILuaBase *lua;

public:
static void Initialize( GarrysMod::Lua::ILuaBase *LUA )
void Initialize( GarrysMod::Lua::ILuaBase *LUA )
{
lua = LUA;

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

static void Deinitialize( GarrysMod::Lua::ILuaBase *LUA )
void Deinitialize( GarrysMod::Lua::ILuaBase *LUA )
{
UnHook( AddOrUpdateFile_original );
}

void AddOrUpdateFile( LuaFile *file, bool reload )
{
LuaHelpers::PushHookRun( lua, hook_name );
auto &self = Singleton;

LuaHelpers::PushHookRun( self.lua, "AddOrUpdateCSLuaFile" );

lua->PushString( file->path.c_str( ) );
lua->PushBool( reload );
self.lua->PushString( file->path.c_str( ) );
self.lua->PushBool( reload );

bool shouldcall = true;
if( LuaHelpers::CallHookRun( lua, 2, 1 ) )
shouldcall = !lua->IsType( -1, GarrysMod::Lua::Type::BOOL ) || lua->GetBool( -1 );
if( LuaHelpers::CallHookRun( self.lua, 2, 1 ) )
shouldcall = !self.lua->IsType( -1, GarrysMod::Lua::Type::BOOL ) || self.lua->GetBool( -1 );

lua->Pop( 1 );
self.lua->Pop( 1 );

if( shouldcall )
return Call( AddOrUpdateFile_original, file, reload );
return Call( self.AddOrUpdateFile_original, file, reload );
}

static GModDataPackProxy Singleton;

private:
FunctionPointers::GModDataPack_AddOrUpdateFile_t AddOrUpdateFile_original = nullptr;
GarrysMod::Lua::ILuaBase *lua = nullptr;
};

FunctionPointers::GModDataPack_AddOrUpdateFile_t GModDataPackProxy::AddOrUpdateFile_original = nullptr;
const char GModDataPackProxy::hook_name[] = "AddOrUpdateCSLuaFile";
GarrysMod::Lua::ILuaBase *GModDataPackProxy::lua = nullptr;
GModDataPackProxy GModDataPackProxy::Singleton;

static bool IsPathAllowed( std::string &filename )
{
Expand Down

0 comments on commit 4be8b4d

Please sign in to comment.