Skip to content

Instantly share code, notes, and snippets.

@hydrogen-mvm
Last active July 21, 2019 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hydrogen-mvm/02e463231b9e1948de893ed4d0a01736 to your computer and use it in GitHub Desktop.
Save hydrogen-mvm/02e463231b9e1948de893ed4d0a01736 to your computer and use it in GitHub Desktop.
DHook Detour String Parameter Bug
/**
* Steps to reproduce:
*
* 1. Put the gamedata file named mvm_fixes.txt (included at the bottom of the sp file).
* 2. Open TF2 srcds and let the map load. (This was only tested on TF2.)
* 3. Run command sm_init to initialize the detour (can't do it on plugin start or it will crash the server).
* 4. Run command sm_create_ent to replicate the bug.
*
* You should see a message like this: Attempted to create unknown entity type ۅgic_relay!
*
* This was tested on Windows SRCDS although it didn't work on Linux SRCDS either.
* Earlier a few months back it worked fine on Windows SRCDS but failed on Linux SRCDS. Now it fails on both.
**/
#include <sourcemod>
#include <sdktools>
#include <dhooks>
#pragma semicolon 1
#pragma newdecls required
bool g_Enabled;
public void OnPluginStart()
{
RegServerCmd("sm_init", Command_InitDetour);
RegServerCmd("sm_create_ent", Command_CreateEnt);
}
public MRESReturn CreateEntityByName_Dtor(Handle hReturn, Handle hParams)
{
char strClass[PLATFORM_MAX_PATH];
DHookGetParamString(hParams, 1, strClass, sizeof(strClass));
PrintToServer("PRE CreateEntityByName(%s)", strClass);
DHookSetParamString(hParams, 1, strClass); // Literally the most worthless detour, we copy back the original string parameter
return MRES_ChangedHandled;
}
public Action Command_CreateEnt(int nArgs)
{
if (!g_Enabled)
{
PrintToServer("Enable the detour first with sm_init!");
return Plugin_Handled;
}
CreateEntityByName("logic_relay");
PrintToServer("Dispatched logic_relay entity.");
return Plugin_Handled;
}
public Action Command_InitDetour(int nArgs) // Must init this off-hand or else we'll crash the server on map load
{
if (g_Enabled)
{
PrintToServer("This detour is already enabled!");
return Plugin_Handled;
}
Handle hConf = LoadGameConfigFile("mvm_fixes");
// CBaseEntity *CreateEntityByName(const char *classname, int iForceEdictIndex);
Handle hCreateEntityByName = DHookCreateDetour(Address_Null, CallConv_CDECL, ReturnType_CBaseEntity, ThisPointer_Ignore);
DHookAddParam(hCreateEntityByName, HookParamType_CharPtr); // *classname
DHookAddParam(hCreateEntityByName, HookParamType_Int); //iForceEdictIndex
if (!DHookSetFromConf(hCreateEntityByName, hConf, SDKConf_Signature, "CreateEntityByName"))
SetFailState("Failed to load CreateEntityByName signature from gamedata");
DHookEnableDetour(hCreateEntityByName, false, CreateEntityByName_Dtor);
PrintToServer("Detour initialized!");
g_Enabled = true;
delete hConf;
return Plugin_Handled;
}
// Gamedata file below:
/*
"Games"
{
"tf"
{
"Signatures"
{
"CreateEntityByName"
{
"library" "server"
"windows" "\x55\x8B\xEC\x56\x8B\x75\x0C\x83\xFE\xFF"
"linux" "@_Z18CreateEntityByNamePKci"
}
}
}
}
*/
"Games"
{
"tf"
{
"Signatures"
{
"CreateEntityByName"
{
"library" "server"
"windows" "\x55\x8B\xEC\x56\x8B\x75\x0C\x83\xFE\xFF"
"linux" "@_Z18CreateEntityByNamePKci"
}
}
}
}
Screenshot of the *INCORRECT* behavior: https://steamuserimages-a.akamaihd.net/ugc/787485099409968598/E3A4D6063CA4CF7FF8DB4E0D7800BEF8FACC6EBB/
This is the CORRECT behavior: https://steamuserimages-a.akamaihd.net/ugc/787485099409977930/49F38E643A507F1528BA0D282E5EDD0D10B3D211/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment