From 6e96e0fb84a7be813d02ef741080f434bbb09323 Mon Sep 17 00:00:00 2001 From: Peace-Maker Date: Tue, 13 Dec 2016 17:37:12 -0700 Subject: [PATCH] Don't use std::list at all in DynamicHooks Switch to amtl Vector. --- DynamicHooks/convention.h | 19 +++++++--------- DynamicHooks/conventions/x86MsCdecl.cpp | 16 +++++++------- DynamicHooks/conventions/x86MsCdecl.h | 4 ++-- DynamicHooks/conventions/x86MsStdcall.cpp | 18 ++++++++-------- DynamicHooks/conventions/x86MsStdcall.h | 4 ++-- DynamicHooks/conventions/x86MsThiscall.cpp | 22 +++++++++---------- DynamicHooks/conventions/x86MsThiscall.h | 4 ++-- DynamicHooks/hook.cpp | 12 +++++------ DynamicHooks/manager.cpp | 25 ++++++++++++++-------- DynamicHooks/manager.h | 4 ++-- DynamicHooks/registers.cpp | 8 +++---- DynamicHooks/registers.h | 7 +++--- dynhooks_sourcepawn.cpp | 19 +++++++--------- 13 files changed, 81 insertions(+), 81 deletions(-) diff --git a/DynamicHooks/convention.h b/DynamicHooks/convention.h index 2e86018..461d338 100644 --- a/DynamicHooks/convention.h +++ b/DynamicHooks/convention.h @@ -34,11 +34,8 @@ // ============================================================================ // >> INCLUDES // ============================================================================ -#include -#include -#include - #include "registers.h" +#include // ============================================================================ // >> DataType_t @@ -150,13 +147,13 @@ public: @param : The return type of the function. */ - ICallingConvention(std::vector vecArgTypes, DataTypeSized_t returnType, int iAlignment=4) + ICallingConvention(ke::Vector &vecArgTypes, DataTypeSized_t returnType, int iAlignment=4) { - m_vecArgTypes = vecArgTypes; - std::vector::iterator it = m_vecArgTypes.begin(); - for (; it != m_vecArgTypes.end(); it++) + m_vecArgTypes = ke::Move(vecArgTypes); + + for (size_t i=0; i < m_vecArgTypes.length(); i++) { - DataTypeSized_t &type = *it; + DataTypeSized_t &type = m_vecArgTypes[i]; if (!type.size) type.size = GetDataTypeSize(type); } @@ -170,7 +167,7 @@ public: This should return a list of Register_t values. These registers will be saved for later access. */ - virtual std::list GetRegisters() = 0; + virtual ke::Vector GetRegisters() = 0; /* Returns the number of bytes that should be added to the stack to clean up. @@ -208,7 +205,7 @@ public: virtual void ReturnPtrChanged(CRegisters* pRegisters, void* pReturnPtr) = 0; public: - std::vector m_vecArgTypes; + ke::Vector m_vecArgTypes; DataTypeSized_t m_returnType; int m_iAlignment; }; diff --git a/DynamicHooks/conventions/x86MsCdecl.cpp b/DynamicHooks/conventions/x86MsCdecl.cpp index 7729408..637f8bf 100644 --- a/DynamicHooks/conventions/x86MsCdecl.cpp +++ b/DynamicHooks/conventions/x86MsCdecl.cpp @@ -38,7 +38,7 @@ // ============================================================================ // >> x86MsCdecl // ============================================================================ -x86MsCdecl::x86MsCdecl(std::vector vecArgTypes, DataTypeSized_t returnType, int iAlignment) : +x86MsCdecl::x86MsCdecl(ke::Vector &vecArgTypes, DataTypeSized_t returnType, int iAlignment) : ICallingConvention(vecArgTypes, returnType, iAlignment) { if (m_returnType.size > 4) @@ -59,22 +59,22 @@ x86MsCdecl::~x86MsCdecl() } } -std::list x86MsCdecl::GetRegisters() +ke::Vector x86MsCdecl::GetRegisters() { - std::list registers; + ke::Vector registers; - registers.push_back(ESP); + registers.append(ESP); if (m_returnType.type == DATA_TYPE_FLOAT || m_returnType.type == DATA_TYPE_DOUBLE) { - registers.push_back(ST0); + registers.append(ST0); } else { - registers.push_back(EAX); + registers.append(EAX); if (m_pReturnBuffer) { - registers.push_back(EDX); + registers.append(EDX); } } @@ -90,7 +90,7 @@ int x86MsCdecl::GetArgStackSize() { int iArgStackSize = 0; - for (unsigned int i = 0; i < m_vecArgTypes.size(); i++) + for (unsigned int i = 0; i < m_vecArgTypes.length(); i++) { iArgStackSize += m_vecArgTypes[i].size; } diff --git a/DynamicHooks/conventions/x86MsCdecl.h b/DynamicHooks/conventions/x86MsCdecl.h index cfda15e..c3b829d 100644 --- a/DynamicHooks/conventions/x86MsCdecl.h +++ b/DynamicHooks/conventions/x86MsCdecl.h @@ -63,10 +63,10 @@ Return values: class x86MsCdecl: public ICallingConvention { public: - x86MsCdecl(std::vector vecArgTypes, DataTypeSized_t returnType, int iAlignment=4); + x86MsCdecl(ke::Vector &vecArgTypes, DataTypeSized_t returnType, int iAlignment=4); ~x86MsCdecl(); - virtual std::list GetRegisters(); + virtual ke::Vector GetRegisters(); virtual int GetPopSize(); virtual int GetArgStackSize(); virtual void** GetStackArgumentPtr(CRegisters* pRegisters); diff --git a/DynamicHooks/conventions/x86MsStdcall.cpp b/DynamicHooks/conventions/x86MsStdcall.cpp index 3db8236..c2e284a 100644 --- a/DynamicHooks/conventions/x86MsStdcall.cpp +++ b/DynamicHooks/conventions/x86MsStdcall.cpp @@ -38,7 +38,7 @@ // ============================================================================ // >> x86MsStdcall // ============================================================================ -x86MsStdcall::x86MsStdcall(std::vector vecArgTypes, DataTypeSized_t returnType, int iAlignment) : +x86MsStdcall::x86MsStdcall(ke::Vector &vecArgTypes, DataTypeSized_t returnType, int iAlignment) : ICallingConvention(vecArgTypes, returnType, iAlignment) { if (m_returnType.size > 4) @@ -59,22 +59,22 @@ x86MsStdcall::~x86MsStdcall() } } -std::list x86MsStdcall::GetRegisters() +ke::Vector x86MsStdcall::GetRegisters() { - std::list registers; + ke::Vector registers; - registers.push_back(ESP); + registers.append(ESP); if (m_returnType.type == DATA_TYPE_FLOAT || m_returnType.type == DATA_TYPE_DOUBLE) { - registers.push_back(ST0); + registers.append(ST0); } else { - registers.push_back(EAX); + registers.append(EAX); if (m_pReturnBuffer) { - registers.push_back(EDX); + registers.append(EDX); } } @@ -85,7 +85,7 @@ int x86MsStdcall::GetPopSize() { int iPopSize = 0; - for(unsigned int i=0; i < m_vecArgTypes.size(); i++) + for(unsigned int i=0; i < m_vecArgTypes.length(); i++) { iPopSize += m_vecArgTypes[i].size; } @@ -97,7 +97,7 @@ int x86MsStdcall::GetArgStackSize() { int iArgStackSize = 0; - for (unsigned int i = 0; i < m_vecArgTypes.size(); i++) + for (unsigned int i = 0; i < m_vecArgTypes.length(); i++) { iArgStackSize += m_vecArgTypes[i].size; } diff --git a/DynamicHooks/conventions/x86MsStdcall.h b/DynamicHooks/conventions/x86MsStdcall.h index 51cb42f..2d1c46f 100644 --- a/DynamicHooks/conventions/x86MsStdcall.h +++ b/DynamicHooks/conventions/x86MsStdcall.h @@ -63,10 +63,10 @@ Return values: class x86MsStdcall: public ICallingConvention { public: - x86MsStdcall(std::vector vecArgTypes, DataTypeSized_t returnType, int iAlignment=4); + x86MsStdcall(ke::Vector &vecArgTypes, DataTypeSized_t returnType, int iAlignment=4); ~x86MsStdcall(); - virtual std::list GetRegisters(); + virtual ke::Vector GetRegisters(); virtual int GetPopSize(); virtual int GetArgStackSize(); virtual void** GetStackArgumentPtr(CRegisters* pRegisters); diff --git a/DynamicHooks/conventions/x86MsThiscall.cpp b/DynamicHooks/conventions/x86MsThiscall.cpp index c599db9..4ea6386 100644 --- a/DynamicHooks/conventions/x86MsThiscall.cpp +++ b/DynamicHooks/conventions/x86MsThiscall.cpp @@ -38,7 +38,7 @@ // ============================================================================ // >> x86MsThiscall // ============================================================================ -x86MsThiscall::x86MsThiscall(std::vector vecArgTypes, DataTypeSized_t returnType, int iAlignment) : +x86MsThiscall::x86MsThiscall(ke::Vector &vecArgTypes, DataTypeSized_t returnType, int iAlignment) : ICallingConvention(vecArgTypes, returnType, iAlignment) { if (m_returnType.size > 4) @@ -59,23 +59,23 @@ x86MsThiscall::~x86MsThiscall() } } -std::list x86MsThiscall::GetRegisters() +ke::Vector x86MsThiscall::GetRegisters() { - std::list registers; + ke::Vector registers; - registers.push_back(ESP); - registers.push_back(ECX); + registers.append(ESP); + registers.append(ECX); if (m_returnType.type == DATA_TYPE_FLOAT || m_returnType.type == DATA_TYPE_DOUBLE) { - registers.push_back(ST0); + registers.append(ST0); } else { - registers.push_back(EAX); + registers.append(EAX); if (m_pReturnBuffer) { - registers.push_back(EDX); + registers.append(EDX); } } @@ -89,7 +89,7 @@ int x86MsThiscall::GetPopSize() //int iPopSize = GetDataTypeSize(DATA_TYPE_POINTER, m_iAlignment); int iPopSize = 0; - for(unsigned int i=0; i < m_vecArgTypes.size(); i++) + for(unsigned int i=0; i < m_vecArgTypes.length(); i++) { iPopSize += m_vecArgTypes[i].size; } @@ -101,7 +101,7 @@ int x86MsThiscall::GetArgStackSize() { int iArgStackSize = 0; - for (unsigned int i = 0; i < m_vecArgTypes.size(); i++) + for (unsigned int i = 0; i < m_vecArgTypes.length(); i++) { iArgStackSize += m_vecArgTypes[i].size; } @@ -158,4 +158,4 @@ void x86MsThiscall::ReturnPtrChanged(CRegisters* pRegisters, void* pReturnPtr) memcpy(pRegisters->m_eax, m_pReturnBuffer, 4); memcpy(pRegisters->m_edx, (void *) ((unsigned long) m_pReturnBuffer + 4), 4); } -} \ No newline at end of file +} diff --git a/DynamicHooks/conventions/x86MsThiscall.h b/DynamicHooks/conventions/x86MsThiscall.h index 1c5490b..fc0e99c 100644 --- a/DynamicHooks/conventions/x86MsThiscall.h +++ b/DynamicHooks/conventions/x86MsThiscall.h @@ -64,10 +64,10 @@ Return values: class x86MsThiscall: public ICallingConvention { public: - x86MsThiscall(std::vector vecArgTypes, DataTypeSized_t returnType, int iAlignment=4); + x86MsThiscall(ke::Vector &vecArgTypes, DataTypeSized_t returnType, int iAlignment=4); ~x86MsThiscall(); - virtual std::list GetRegisters(); + virtual ke::Vector GetRegisters(); virtual int GetPopSize(); virtual int x86MsThiscall::GetArgStackSize(); virtual void** GetStackArgumentPtr(CRegisters* pRegisters); diff --git a/DynamicHooks/hook.cpp b/DynamicHooks/hook.cpp index 26f7757..a0706d4 100644 --- a/DynamicHooks/hook.cpp +++ b/DynamicHooks/hook.cpp @@ -316,10 +316,10 @@ void CHook::Write_CallHandler(sp::MacroAssembler& masm, HookType_t type) void CHook::Write_SaveRegisters(sp::MacroAssembler& masm) { - std::list vecRegistersToSave = m_pCallingConvention->GetRegisters(); - for(std::list::iterator it=vecRegistersToSave.begin(); it != vecRegistersToSave.end(); it++) + ke::Vector vecRegistersToSave = m_pCallingConvention->GetRegisters(); + for(size_t i = 0; i < vecRegistersToSave.length(); i++) { - switch(*it) + switch(vecRegistersToSave[i]) { // ======================================================================== // >> 8-bit General purpose registers @@ -378,10 +378,10 @@ void CHook::Write_SaveRegisters(sp::MacroAssembler& masm) void CHook::Write_RestoreRegisters(sp::MacroAssembler& masm) { - std::list vecRegistersToSave = m_pCallingConvention->GetRegisters(); - for(std::list::iterator it=vecRegistersToSave.begin(); it != vecRegistersToSave.end(); it++) + ke::Vector vecRegistersToSave = m_pCallingConvention->GetRegisters(); + for (size_t i = 0; i < vecRegistersToSave.length(); i++) { - switch(*it) + switch (vecRegistersToSave[i]) { // ======================================================================== // >> 8-bit General purpose registers diff --git a/DynamicHooks/manager.cpp b/DynamicHooks/manager.cpp index b82ac73..390e8bb 100644 --- a/DynamicHooks/manager.cpp +++ b/DynamicHooks/manager.cpp @@ -50,17 +50,24 @@ CHook* CHookManager::HookFunction(void* pFunc, ICallingConvention* pConvention) } pHook = new CHook(pFunc, pConvention); - m_Hooks.push_back(pHook); + m_Hooks.append(pHook); return pHook; } void CHookManager::UnhookFunction(void* pFunc) { - CHook* pHook = FindHook(pFunc); - if (pHook) + if (!pFunc) + return; + + for (size_t i = 0; i < m_Hooks.length(); i++) { - m_Hooks.remove(pHook); - delete pHook; + CHook* pHook = m_Hooks[i]; + if (pHook->m_pFunc == pFunc) + { + m_Hooks.remove(i); + delete pHook; + return; + } } } @@ -69,9 +76,9 @@ CHook* CHookManager::FindHook(void* pFunc) if (!pFunc) return NULL; - for(std::list::iterator it=m_Hooks.begin(); it != m_Hooks.end(); it++) + for(size_t i = 0; i < m_Hooks.length(); i++) { - CHook* pHook = *it; + CHook* pHook = m_Hooks[i]; if (pHook->m_pFunc == pFunc) return pHook; } @@ -80,8 +87,8 @@ CHook* CHookManager::FindHook(void* pFunc) void CHookManager::UnhookAllFunctions() { - for(std::list::iterator it=m_Hooks.begin(); it != m_Hooks.end(); it++) - delete *it; + for(size_t i = 0; i < m_Hooks.length(); i++) + delete m_Hooks[i]; m_Hooks.clear(); } diff --git a/DynamicHooks/manager.h b/DynamicHooks/manager.h index 6899b62..22b06a9 100644 --- a/DynamicHooks/manager.h +++ b/DynamicHooks/manager.h @@ -34,9 +34,9 @@ // ============================================================================ // >> INCLUDES // ============================================================================ -#include #include "hook.h" #include "convention.h" +#include // ============================================================================ @@ -68,7 +68,7 @@ public: void UnhookAllFunctions(); public: - std::list m_Hooks; + ke::Vector m_Hooks; }; diff --git a/DynamicHooks/registers.cpp b/DynamicHooks/registers.cpp index 04812fc..e6fc4d7 100644 --- a/DynamicHooks/registers.cpp +++ b/DynamicHooks/registers.cpp @@ -30,7 +30,7 @@ #include "registers.h" -CRegisters::CRegisters(std::list registers) +CRegisters::CRegisters(ke::Vector registers) { // ======================================================================== // >> 8-bit General purpose registers @@ -360,11 +360,11 @@ CRegisters::~CRegisters() DeleteRegister(m_st7); } -CRegister* CRegisters::CreateRegister(std::list& registers, Register_t reg, int iSize) +CRegister* CRegisters::CreateRegister(ke::Vector& registers, Register_t reg, int iSize) { - for(std::list::iterator it=registers.begin(); it != registers.end(); it++) + for(size_t i = 0; i < registers.length(); i++) { - if ((*it) == reg) + if (registers[i] == reg) { return new CRegister(iSize); } diff --git a/DynamicHooks/registers.h b/DynamicHooks/registers.h index 08083bb..6b7193d 100644 --- a/DynamicHooks/registers.h +++ b/DynamicHooks/registers.h @@ -35,8 +35,7 @@ // ============================================================================ // >> INCLUDES // ============================================================================ -#include -#include +#include // ============================================================================ @@ -261,11 +260,11 @@ public: class CRegisters { public: - CRegisters(std::list registers); + CRegisters(ke::Vector registers); ~CRegisters(); private: - CRegister* CreateRegister(std::list& registers, Register_t reg, int iSize); + CRegister* CreateRegister(ke::Vector& registers, Register_t reg, int iSize); void DeleteRegister(CRegister* pRegister); public: diff --git a/dynhooks_sourcepawn.cpp b/dynhooks_sourcepawn.cpp index d573f09..f06a537 100644 --- a/dynhooks_sourcepawn.cpp +++ b/dynhooks_sourcepawn.cpp @@ -182,14 +182,14 @@ void CleanupDetours() ICallingConvention *ConstructCallingConvention(HookSetup *setup) { - std::vector vecArgTypes; + ke::Vector vecArgTypes; for (size_t i = 0; i < setup->params.size(); i++) { ParamInfo &info = setup->params[i]; DataTypeSized_t type; type.type = DynamicHooks_ConvertParamTypeFrom(info.type); type.size = info.size; - vecArgTypes.push_back(type); + vecArgTypes.append(type); } DataTypeSized_t returnType; @@ -235,7 +235,7 @@ bool HandleDetour(HookType_t hookType, CHook* pDetour) HookParamsStruct *paramStruct = NULL; Handle_t pHndl = BAD_HANDLE; - int argNum = pDetour->m_pCallingConvention->m_vecArgTypes.size(); + int argNum = pDetour->m_pCallingConvention->m_vecArgTypes.length(); MRESReturn finalRet = MRES_Ignored; ke::AutoPtr finalRetBuf(new uint8_t[pDetour->m_pCallingConvention->m_returnType.size]); @@ -338,8 +338,6 @@ bool HandleDetour(HookType_t hookType, CHook* pDetour) break; } } - // TODO: Introduce that override concept in dyndetours. - // This doesn't call the original function at the moment, but just returns the given return value. tempRet = MRES_Override; pWrapper->UpdateParamsFromStruct(paramStruct); break; @@ -348,8 +346,6 @@ bool HandleDetour(HookType_t hookType, CHook* pDetour) { if (returnStruct->isChanged) { - // TODO: Introduce that override concept in dyndetours. - // This doesn't call the original function at the moment, but just returns the given return value. tempRet = MRES_Override; if (pWrapper->returnType == ReturnType_String || pWrapper->returnType == ReturnType_Int || pWrapper->returnType == ReturnType_Bool) { @@ -429,6 +425,7 @@ bool HandleDetour(HookType_t hookType, CHook* pDetour) } } + // If we want to use our own return value, write it back. if (finalRet >= MRES_Override) { void* pPtr = pDetour->m_pCallingConvention->GetReturnPtr(pDetour->m_pRegisters); @@ -540,8 +537,8 @@ HookParamsStruct *CDynamicHooksSourcePawn::GetParamStruct() params->dg = this; size_t paramsSize = this->m_pDetour->m_pCallingConvention->GetArgStackSize(); - std::vector &argTypes = m_pDetour->m_pCallingConvention->m_vecArgTypes; - int numArgs = argTypes.size(); + ke::Vector &argTypes = m_pDetour->m_pCallingConvention->m_vecArgTypes; + int numArgs = argTypes.length(); params->orgParams = (void **)malloc(paramsSize); params->newParams = (void **)malloc(paramsSize); @@ -568,8 +565,8 @@ void CDynamicHooksSourcePawn::UpdateParamsFromStruct(HookParamsStruct *params) if (!params) return; - std::vector &argTypes = m_pDetour->m_pCallingConvention->m_vecArgTypes; - int numArgs = argTypes.size(); + ke::Vector &argTypes = m_pDetour->m_pCallingConvention->m_vecArgTypes; + int numArgs = argTypes.length(); int firstArg = 0; if (callConv == CallConv_THISCALL)