Skip to content
This repository has been archived by the owner on Dec 20, 2019. It is now read-only.

Commit

Permalink
Various GameRules code change
Browse files Browse the repository at this point in the history
- The extension now removes gamerules hooks on map end
- Plugins can now hook gamerules props change.
- Ported inc file to sourcemod 1.7+ syntax
- Fixed typo on gamerules natives
  • Loading branch information
Kenzzer committed Aug 27, 2017
1 parent 1efc052 commit de0b28e
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*.la
*.a

extension/build/

Debug*/
Release*/
*.suo
Expand Down
Binary file modified addons/sourcemod/extensions/sendproxy.ext.2.tf2.dll
Binary file not shown.
Binary file modified addons/sourcemod/extensions/sendproxy.ext.2.tf2.so
Binary file not shown.
51 changes: 30 additions & 21 deletions addons/sourcemod/scripting/include/sendproxy.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,46 @@ enum SendPropType {
Prop_Max
};

funcenum SendProxyCallback
typeset SendProxyCallback
{
Action:public(entity, const String:PropName[], &iValue, element), //Prop_Int
Action:public(entity, const String:PropName[], &Float:flValue, element), //Prop_Float
Action:public(entity, const String:PropName[], String:modifiedValue[4096], element), //Prop_String
Action:public(entity, const String:PropName[], Float:vecValues[3], element), //Prop_Vector
function Action (int entity, const char[] PropName, int &iValue, int element); //Prop_Int
function Action (int entity, const char[] PropName, float &flValue, int element); //Prop_Float
function Action (int entity, const char[] PropName, char modifiedValue[4096], int element); //Prop_String
function Action (int entity, const char[] PropName, float vecValues[3], int element); //Prop_Vector
};

funcenum SendProxyCallbackGamerules
typeset SendProxyCallbackGamerules
{
Action:public(const String:PropName[], &iValue, element), //Prop_Int
Action:public(const String:PropName[], &Float:flValue, element), //Prop_Float
Action:public(const String:PropName[], String:modifiedValue[4096], element), //Prop_String
Action:public(const String:PropName[], Float:vecValues[3], element), //Prop_Vector
function Action (const char[] PropName, int &iValue, int element); //Prop_Int
function Action (const char[] PropName, float &flValue, int element); //Prop_Float
function Action (const char[] PropName, char modifiedValue[4096], int element); //Prop_String
function Action (const char[] PropName, float vecValues[3], int element); //Prop_Vector
};

funcenum PropChangedCallback
typeset PropChangedCallback
{
public(entity, const String:PropName[], const String:oldValue[], const String:newValue[]),
function void(int entity, const char[] PropName, const char[] oldValue, const char[] newValue));
};

typeset GameRulesPropChangedCallback
{
function void(const char[] PropName, const char[] oldValue, const char[] newValue);
};

//Returns true upon success, false upon failure
native bool:SendProxy_Hook(entity, String:propname[], SendPropType:proptype, SendProxyCallback:callback);
native bool:SendProxy_HookGamerules(String:propname[], SendPropType:proptype, SendProxyCallbackGamerules:callback);
native bool:SendProxy_HookArrayProp(entity, const String:name[], element, SendPropType:type, SendProxyCallback:callback);
native bool:SendProxy_UnhookArrayProp(entity, const String:name[], element, SendPropType:type, SendProxyCallback:callback);
native bool:SendProxy_Unhook(entity, String:propname[], SendProxyCallback:callback);
native bool:SendProxy_IsHooked(entity, String:propname[]);
native bool SendProxy_Hook(int entity, char[] propname, SendPropType proptype, SendProxyCallback callback);
native bool SendProxy_HookGameRules(char[] propname, SendPropType proptype, SendProxyCallbackGamerules callback);
native bool SendProxy_HookArrayProp(int entity, const char[] name, int element, SendPropType: ype, SendProxyCallback callback);
native bool SendProxy_UnhookArrayProp(int entity, const char[] name, int element, SendPropType type, SendProxyCallback callback);
native bool SendProxy_Unhook(int entity, char[] propname, SendProxyCallback callback);
native bool SendProxy_UnhookGameRules(char[] propname, SendProxyCallbackGamerules callback);
native bool SendProxy_IsHooked(int entity, char[] propname);
native bool SendProxy_IsHookedGameRules(char[] propname);

native bool:SendProxy_HookPropChange(entity, const String:name[], PropChangedCallback:callback);
native SendProxy_UnhookPropChange(entity, const String:name[], PropChangedCallback:callback);
native bool SendProxy_HookPropChange(entity, const char[] name, PropChangedCallback callback);
native bool SendProxy_HookPropChangeGameRules(const char[] name, GameRulesPropChangedCallback callback);
native void SendProxy_UnhookPropChange(entity, const char[] name, PropChangedCallback callback);
native void SendProxy_UnhookPropChangeGameRules(const char[] name, GameRulesPropChangedCallback callback);

#if !defined REQUIRE_EXTENSIONS
public __ext_sendproxymanager_SetNTVOptional()
Expand All @@ -56,7 +65,7 @@ public __ext_sendproxymanager_SetNTVOptional()
}
#endif

public Extension:__ext_sendproxymanager =
public Extension __ext_sendproxymanager =
{
name = "SendProxy Manager",
file = "sendproxy.ext",
Expand Down
117 changes: 101 additions & 16 deletions extension/extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,33 @@ IGameConfig *g_pGameConf = NULL;
ISDKTools *g_pSDKTools = NULL;

static cell_t Native_Hook(IPluginContext* pContext, const cell_t* params);
static cell_t Native_HookGamerules(IPluginContext* pContext, const cell_t* params);
static cell_t Native_HookGameRules(IPluginContext* pContext, const cell_t* params);
static cell_t Native_Unhook(IPluginContext* pContext, const cell_t* params);
static cell_t Native_UnhookGamerules(IPluginContext* pContext, const cell_t* params);
static cell_t Native_UnhookGameRules(IPluginContext* pContext, const cell_t* params);
static cell_t Native_IsHooked(IPluginContext* pContext, const cell_t* params);
static cell_t Native_IsHookedGameRules(IPluginContext* pContext, const cell_t* params);
static cell_t Native_HookArrayProp(IPluginContext* pContext, const cell_t* params);
static cell_t Native_UnhookArrayProp(IPluginContext* pContext, const cell_t* params);
static cell_t Native_HookPropChange(IPluginContext* pContext, const cell_t* params);
static cell_t Native_HookPropChangeGameRules(IPluginContext* pContext, const cell_t* params);
static cell_t Native_UnhookPropChange(IPluginContext* pContext, const cell_t* params);
static cell_t Native_UnhookPropChangeGameRules(IPluginContext* pContext, const cell_t* params);

const char *g_szGameRulesProxy;

const sp_nativeinfo_t g_MyNatives[] = {
{"SendProxy_Hook", Native_Hook},
{"SendProxy_HookGamerules", Native_HookGamerules},
{"SendProxy_HookGameRules", Native_HookGameRules},
{"SendProxy_HookArrayProp", Native_HookArrayProp},
{"SendProxy_UnhookArrayProp", Native_UnhookArrayProp},
{"SendProxy_Unhook", Native_Unhook},
{"SendProxy_UnhookGamerules", Native_UnhookGamerules},
{"SendProxy_UnhookGameRules", Native_UnhookGameRules},
{"SendProxy_IsHooked", Native_IsHooked},
{"SendProxy_IsHookedGameRules", Native_IsHookedGameRules},
{"SendProxy_HookPropChange", Native_HookPropChange},
{"SendProxy_HookPropChangeGameRules", Native_HookPropChangeGameRules},
{"SendProxy_UnhookPropChange", Native_UnhookPropChange},
{"SendProxy_UnhookPropChangeGameRules", Native_UnhookPropChangeGameRules},
{NULL, NULL},
};

Expand Down Expand Up @@ -190,19 +196,19 @@ void Hook_GameFrame(bool simulating)
}
}
}
void *pGamerules = NULL;
//Gamerules hooks
for (int i = 0; i < g_ChangeHooksGamerules.Count(); i++)
static void *pGamerules = NULL;
if (!pGamerules)
{
if (!pGamerules)
pGamerules = g_pSDKTools->GetGameRules();
if(!pGamerules)
{
pGamerules = g_pSDKTools->GetGameRules();
if(!pGamerules)
{
g_pSM->LogError(myself, "CRITICAL ERROR: Could not get gamerules pointer!");
return;
}
g_pSM->LogError(myself, "CRITICAL ERROR: Could not get gamerules pointer!");
return;
}
}
//Gamerules hooks
for (int i = 0; i < g_ChangeHooksGamerules.Count(); i++)
{
switch(g_ChangeHooksGamerules[i].PropType)
{
case Prop_Int:
Expand Down Expand Up @@ -319,6 +325,15 @@ void SendProxyManager::SDK_OnUnload()
plsys->RemovePluginsListener(&g_SendProxyManager);
}

void SendProxyManager::OnCoreMapEnd()
{
for (int i = 0; i < g_HooksGamerules.Count(); i++)
{
UnhookProxyGamerules(i);
i--;
}
}

bool SendProxyManager::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
GET_V_IFACE_ANY(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
Expand Down Expand Up @@ -827,6 +842,22 @@ static cell_t Native_UnhookPropChange(IPluginContext* pContext, const cell_t* pa
}
return 1;
}

static cell_t Native_UnhookPropChangeGameRules(IPluginContext* pContext, const cell_t* params)
{
char* name;
pContext->LocalToString(params[1], &name);
IPluginFunction *callback = pContext->GetFunctionById(params[2]);
sm_sendprop_info_t info;
gamehelpers->FindSendPropInfo(g_szGameRulesProxy, name, &info);

for (int i = 0; i < g_ChangeHooksGamerules.Count(); i++)
{
if (g_ChangeHooksGamerules[i].pCallback == callback && g_ChangeHooksGamerules[i].pVar == info.prop)
g_ChangeHooksGamerules.Remove(i--);
}
return 1;
}

static cell_t Native_HookPropChange(IPluginContext* pContext, const cell_t* params)
{
Expand Down Expand Up @@ -867,6 +898,47 @@ static cell_t Native_HookPropChange(IPluginContext* pContext, const cell_t* para
return 1;
}

static cell_t Native_HookPropChangeGameRules(IPluginContext* pContext, const cell_t* params)
{
char* name;
pContext->LocalToString(params[1], &name);
IPluginFunction *callback = pContext->GetFunctionById(params[2]);
SendProp *pProp = NULL;
PropChangeHookGamerules hook;
sm_sendprop_info_t info;
gamehelpers->FindSendPropInfo(g_szGameRulesProxy, name, &info);

pProp = info.prop;
int offset = info.actual_offset;
SendPropType type = pProp->GetType();

static void *pGamerules = NULL;
if (!pGamerules)
{
pGamerules = g_pSDKTools->GetGameRules();
if (!pGamerules)
{
g_pSM->LogError(myself, "CRITICAL ERROR: Could not get gamerules pointer!");
return 0;
}
}

switch (type)
{
case DPT_Int: hook.PropType = Prop_Int; hook.iLastValue = *(int*)((unsigned char*)pGamerules + offset); break;
case DPT_Float: hook.PropType = Prop_Float; hook.flLastValue = *(float*)((unsigned char*)pGamerules + offset); break;
case DPT_String: hook.PropType = Prop_String; hook.szLastValue = *(const char*)((unsigned char*)pGamerules + offset); break;
default: return pContext->ThrowNativeError("Prop type %d is not yet supported", type);
}

hook.Offset = offset;
hook.pVar = pProp;
hook.pCallback = callback;

g_ChangeHooksGamerules.AddToTail(hook);
return 1;
}

static cell_t Native_Hook(IPluginContext* pContext, const cell_t* params)
{
if (params[1] < 0 || params[1] >= 2048)
Expand Down Expand Up @@ -959,7 +1031,7 @@ static cell_t Native_Hook(IPluginContext* pContext, const cell_t* params)
return 1;
}

static cell_t Native_HookGamerules(IPluginContext* pContext, const cell_t* params)
static cell_t Native_HookGameRules(IPluginContext* pContext, const cell_t* params)
{
char* name;
pContext->LocalToString(params[1], &name);
Expand Down Expand Up @@ -1142,7 +1214,7 @@ static cell_t Native_Unhook(IPluginContext* pContext, const cell_t* params)
return 0;
}

static cell_t Native_UnhookGamerules(IPluginContext* pContext, const cell_t* params)//To-do break all the gamerules hook on map end.
static cell_t Native_UnhookGameRules(IPluginContext* pContext, const cell_t* params)//To-do break all the gamerules hook on map end.
{
char *propName;
pContext->LocalToString(params[1], &propName);
Expand Down Expand Up @@ -1170,4 +1242,17 @@ static cell_t Native_IsHooked(IPluginContext* pContext, const cell_t* params)
return 1;
}
return 0;
}

static cell_t Native_IsHookedGameRules(IPluginContext* pContext, const cell_t* params)
{
char *propName;
pContext->LocalToString(params[1], &propName);

for (int i = 0; i < g_HooksGamerules.Count(); i++)
{
if (strcmp(propName, g_HooksGamerules[i].pVar->GetName()) == 0)
return 1;
}
return 0;
}
3 changes: 1 addition & 2 deletions extension/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class PropChangeHookGamerules
SendProp* pVar;
int PropType;
unsigned int Offset;
int objectID;
};

void GlobalProxy(const SendProp *pProp, const void *pStructBase, const void* pData, DVariant *pOut, int iElement, int objectID);
Expand All @@ -114,7 +113,7 @@ class SendProxyManager : public SDKExtension, public IPluginsListener
virtual void SDK_OnUnload();
virtual void SDK_OnAllLoaded();


virtual void OnCoreMapEnd();
//virtual void SDK_OnPauseChange(bool paused);

//virtual bool QueryRunning(char *error, size_t maxlength);
Expand Down

0 comments on commit de0b28e

Please sign in to comment.