Skip to content

Commit

Permalink
Fix creating new class function breaking existing custom bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
FortyTwoFortyTwo committed Jan 13, 2025
1 parent bc9aaa0 commit 5414b2f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
SourceMod plugin that exposes many VScript features to make use of it. Currently supports L4D2 and TF2.

## Builds
All builds can be found in [releases](https://github.com/FortyTwoFortyTwo/VScript/releases) page, auto-built on every commits done in master branch.
All builds can be found in [releases](https://github.com/FortyTwoFortyTwo/VScript/releases) page, auto-built on every commits done in main branch.

## Requirements
- At least SourceMod version 1.12.0.6924
Expand Down
2 changes: 1 addition & 1 deletion scripting/vscript.sp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "include/vscript.inc"

#define PLUGIN_VERSION "1.9.1"
#define PLUGIN_VERSION "1.9.2"
#define PLUGIN_VERSION_REVISION "manual"

char g_sOperatingSystem[16];
Expand Down
5 changes: 3 additions & 2 deletions scripting/vscript/binding.sp
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ void Binding_SetCustom(VScriptFunction pFunction)
Function_SetBinding(pFunction, g_pCustomBinding);
}

void Binding_Delete(VScriptFunction pFunction)
bool Binding_Delete(VScriptFunction pFunction)
{
int iIndex = g_aBindingInfos.FindValue(pFunction, BindingInfo::pFunction);
if (iIndex == -1)
return;
return false;

BindingInfo info;
g_aBindingInfos.GetArray(iIndex, info);
delete info.hSDKCall;
g_aBindingInfos.Erase(iIndex);
return true;
}

public MRESReturn Binding_Detour(DHookReturn hReturn, DHookParam hParam)
Expand Down
19 changes: 15 additions & 4 deletions scripting/vscript/class.sp
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,24 @@ VScriptFunction Class_GetFunctionFromName(VScriptClass pClass, const char[] sNam

VScriptFunction Class_CreateFunction(VScriptClass pClass)
{
Address pFunctionBindings = pClass + view_as<Address>(g_iClassDesc_FunctionBindings);
int iFunctionCount = LoadFromAddress(pClass + view_as<Address>(g_iClassDesc_FunctionBindings) + view_as<Address>(0x0C), NumberType_Int32);
int iFunctionCount = Class_GetFunctionCount(pClass);

// Clear any binding detours we have first before moving over to new memory
ArrayList aList = new ArrayList();
for (int i = 0; i < iFunctionCount; i++)
if (Binding_Delete(Class_GetFunctionFromIndex(pClass, i)))
aList.Push(i);

Address pFunctionBindings = pClass + view_as<Address>(g_iClassDesc_FunctionBindings);
Memory_UtlVectorSetSize(pFunctionBindings, g_iFunctionBinding_sizeof, iFunctionCount + 1);

Address pData = LoadFromAddress(pClass + view_as<Address>(g_iClassDesc_FunctionBindings), NumberType_Int32);
VScriptFunction pFunction = view_as<VScriptFunction>(pData + view_as<Address>(g_iFunctionBinding_sizeof * iFunctionCount));
// Re-detour any bindings
for (int i = 0; i < aList.Length; i++)
Binding_SetCustom(Class_GetFunctionFromIndex(pClass, aList.Get(i)));

delete aList;

VScriptFunction pFunction = Class_GetFunctionFromIndex(pClass, iFunctionCount);
Function_Init(pFunction, true);
return pFunction;
}
Expand Down

0 comments on commit 5414b2f

Please sign in to comment.