Skip to content

Commit

Permalink
feat: iphysenv#Status & #GetGravity
Browse files Browse the repository at this point in the history
also added small helper functions in lua.cpp
  • Loading branch information
shockpast committed Dec 8, 2024
1 parent a88969c commit 460f913
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 22 deletions.
32 changes: 32 additions & 0 deletions source/lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,36 @@ void lua::PushHook(const char* name)
g_Lua->ReferencePush(reference);
g_Lua->ReferenceFree(reference);
g_Lua->PushString(name);
}

void lua::ThrowError(const char* error, ...)
{
char buf[1024];

va_list args;
va_start(args, error);

vsnprintf(buf, sizeof(buf), error, args);
g_Lua->ThrowError(buf);

va_end(args);
}

void lua::StartTable()
{
g_Lua->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
g_Lua->CreateTable();
}

void lua::AddFunction(GarrysMod::Lua::CFunc func, const char* name)
{
g_Lua->PushString(name);
g_Lua->PushCFunction(func);
g_Lua->RawSet(-3);
}

void lua::FinishTable(const char* name)
{
g_Lua->SetField(-2, name);
g_Lua->Pop();
}
4 changes: 4 additions & 0 deletions source/lua.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace lua
{
void PushHook(const char* name);
void ThrowError(const char* error, ...);
void StartTable();
void AddFunction(GarrysMod::Lua::CFunc func, const char *name);
void FinishTable(const char *name);
}
55 changes: 43 additions & 12 deletions source/modules/vphysics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#include "symbols.h"
#include "detour.h"
#include "main.h"

typedef void (*IPhysicsEnvironment_Simulate_t)(void* _this, float deltaTime);
#include "lua.h"

IPhysics* _physics = NULL;
bool _shouldSimulate = true;
Expand All @@ -19,23 +18,57 @@ LUA_FUNCTION_STATIC(iphysenv_ShouldSimulate)
return 0;
}

LUA_FUNCTION_STATIC(iphysenv_Status)
{
LUA->PushBool(_shouldSimulate);
return 1;
}

LUA_FUNCTION_STATIC(iphysenv_SetGravity)
{
IPhysicsEnvironment* iphysenv = _physics->GetActiveEnvironmentByIndex(0);
const Vector& vec = LUA->GetVector(1);
if (iphysenv == NULL)
{
LUA->ThrowError("IPhysicsEnvironment couldn't be found, how?");
return 0;
}

if (!LUA->IsType(1, GarrysMod::Lua::Type::Vector))
{
lua::ThrowError("bad argument #1 to 'SetGravity' (Vector expected, got %s)", LUA->GetTypeName(1));
return 0;
}

const Vector& vec = LUA->GetVector(1);
iphysenv->SetGravity(vec);

return 0;
}

LUA_FUNCTION_STATIC(iphysenv_GetGravity)
{
IPhysicsEnvironment* iphysenv = _physics->GetActiveEnvironmentByIndex(0);
if (iphysenv == NULL)
{
LUA->ThrowError("IPhysicsEnvironment couldn't be found, how?");
return 0;
}

Vector vec = Vector(0, 0, 0);
iphysenv->GetGravity(&vec);

LUA->PushVector(vec);

return 1;
}

static Detouring::Hook detour_CPhysicsEnvironment_Simulate;
void hook_IPhysicsEnvironment_Simulate(IPhysicsEnvironment* _this, float deltaTime)
{
if (!_shouldSimulate)
return;

detour_CPhysicsEnvironment_Simulate.GetTrampoline<IPhysicsEnvironment_Simulate_t>()(_this, deltaTime);
detour_CPhysicsEnvironment_Simulate.GetTrampoline<symbols::vphysics_CPhysicsEnvironment_Simulate>()(_this, deltaTime);
}

void vphysics::initialize()
Expand All @@ -45,14 +78,12 @@ void vphysics::initialize()

detour::Create(&detour_CPhysicsEnvironment_Simulate, "CPhysicsEnvironment::Simulate", vphysics_loader.GetModule(), symbols::vphysics_CPhysicsEnvironment_SimulateSym, (void*)hook_IPhysicsEnvironment_Simulate, 0);

g_Lua->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
g_Lua->CreateTable();
g_Lua->PushCFunction(iphysenv_ShouldSimulate);
g_Lua->SetField(-2, "ShouldSimulate");
g_Lua->PushCFunction(iphysenv_SetGravity);
g_Lua->SetField(-2, "SetGravity");
g_Lua->SetField(-2, "iphysenv");
g_Lua->Pop();
lua::StartTable();
lua::AddFunction(iphysenv_ShouldSimulate, "ShouldSimulate");
lua::AddFunction(iphysenv_Status, "Status");
lua::AddFunction(iphysenv_SetGravity, "SetGravity");
lua::AddFunction(iphysenv_GetGravity, "GetGravity");
lua::FinishTable("iphysenv");

Msg("initialized 'iphysenv' module\n");
}
7 changes: 0 additions & 7 deletions source/symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ namespace symbols
NULL_SYMBOL,
};

const std::vector<Symbol> vphysics_CPhysicsEnvrionment_SetGravitySym = {
//
Symbol::FromName("_ZN19CPhysicsEnvironment10SetGravityERK6Vector"),
// todo
NULL_SYMBOL
};

//---------------------------------------------------------------------------------
// Purpose: tier0 Symbols
//---------------------------------------------------------------------------------
Expand Down
3 changes: 0 additions & 3 deletions source/symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ namespace symbols
typedef void (GMCOMMON_CALLING_CONVENTION* vphysics_CPhysicsEnvironment_Simulate)(void* _this, float deltaTime);
extern const std::vector<Symbol> vphysics_CPhysicsEnvironment_SimulateSym;

typedef void (GMCOMMON_CALLING_CONVENTION* vphysics_CPhysicsEnvrionment_SetGravity)(void* _this, const Vector& gravity);
extern const std::vector<Symbol> vphysics_CPhysicsEnvrionment_SetGravitySym;

//---------------------------------------------------------------------------------
// Purpose: tier0 Symbols
//---------------------------------------------------------------------------------
Expand Down

0 comments on commit 460f913

Please sign in to comment.