3 * =============================================================================
4 * SourceMod BinTools Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
33 #include "CallMaker.h"
34 #include "jit_compile.h"
35 #include "extension.h"
37 /* SourceMod <==> SourceHook type conversion functions */
39 SourceHook::ProtoInfo::CallConvention GetSHCallConvention(SourceMod::CallConvention cv)
41 if (cv < SourceMod::CallConv_ThisCall || cv > SourceMod::CallConv_Cdecl)
43 return SourceHook::ProtoInfo::CallConv_Unknown;
46 return (SourceHook::ProtoInfo::CallConvention)(cv + 1);
49 SourceMod::CallConvention GetSMCallConvention(SourceHook::ProtoInfo::CallConvention cv)
51 assert(cv >= SourceHook::ProtoInfo::CallConv_ThisCall || cv <= SourceHook::ProtoInfo::CallConv_Cdecl);
53 return (SourceMod::CallConvention)(cv - 1);
56 SourceHook::PassInfo::PassType GetSHPassType(SourceMod::PassType type)
58 if (type < SourceMod::PassType_Basic || type > SourceMod::PassType_Object)
60 return SourceHook::PassInfo::PassType_Unknown;
63 return (SourceHook::PassInfo::PassType)(type + 1);
66 SourceMod::PassType GetSMPassType(int type)
68 /* SourceMod doesn't provide an Unknown type so we it must be an error */
69 assert(type >= SourceHook::PassInfo::PassType_Basic && type <= SourceHook::PassInfo::PassType_Object);
71 return (SourceMod::PassType)(type - 1);
74 void GetSMPassInfo(SourceMod::PassInfo *out, const SourceHook::PassInfo *in)
77 out->flags = in->flags;
78 out->type = GetSMPassType(in->type);
81 ICallWrapper *CallMaker::CreateCall(void *address,
83 const PassInfo *retInfo,
84 const PassInfo paramInfo[],
85 unsigned int numParams)
87 SourceHook::CProtoInfoBuilder protoInfo(GetSHCallConvention(cv));
89 for (unsigned int i=0; i<numParams; i++)
91 protoInfo.AddParam(paramInfo[i].size, GetSHPassType(paramInfo[i].type), paramInfo[i].flags,
92 NULL, NULL, NULL, NULL);
97 protoInfo.SetReturnType(retInfo->size, GetSHPassType(retInfo->type), retInfo->flags,
98 NULL, NULL, NULL, NULL);
102 protoInfo.SetReturnType(0, SourceHook::PassInfo::PassType_Unknown, 0,
103 NULL, NULL, NULL, NULL);
106 return g_CallMaker2.CreateCall(address, &(*protoInfo));
109 ICallWrapper *CallMaker::CreateVCall(unsigned int vtblIdx,
110 unsigned int vtblOffs,
111 unsigned int thisOffs,
112 const PassInfo *retInfo,
113 const PassInfo paramInfo[],
114 unsigned int numParams)
116 SourceHook::MemFuncInfo info;
117 info.isVirtual = true;
118 info.vtblindex = vtblIdx;
119 info.vtbloffs = vtblOffs;
120 info.thisptroffs = thisOffs;
122 SourceHook::CProtoInfoBuilder protoInfo(SourceHook::ProtoInfo::CallConv_ThisCall);
124 for (unsigned int i=0; i<numParams; i++)
126 protoInfo.AddParam(paramInfo[i].size, GetSHPassType(paramInfo[i].type), paramInfo[i].flags,
127 NULL, NULL, NULL, NULL);
132 protoInfo.SetReturnType(retInfo->size, GetSHPassType(retInfo->type), retInfo->flags,
133 NULL, NULL, NULL, NULL);
137 protoInfo.SetReturnType(0, SourceHook::PassInfo::PassType_Unknown, 0,
138 NULL, NULL, NULL, NULL);
142 return g_CallMaker2.CreateVirtualCall(&(*protoInfo), &info);
145 ICallWrapper *CallMaker2::CreateCall(void *address, const SourceHook::ProtoInfo *protoInfo)
147 CallWrapper *pWrapper = new CallWrapper(protoInfo);
148 pWrapper->SetCalleeAddr(address);
150 void *addr = JIT_CallCompile(pWrapper, FuncAddr_Direct);
151 pWrapper->SetCodeBaseAddr(addr);
156 ICallWrapper *CallMaker2::CreateVirtualCall(const SourceHook::ProtoInfo *protoInfo,
157 const SourceHook::MemFuncInfo *info)
159 CallWrapper *pWrapper = new CallWrapper(protoInfo);
160 pWrapper->SetMemFuncInfo(info);
162 void *addr = JIT_CallCompile(pWrapper, FuncAddr_VTable);
163 pWrapper->SetCodeBaseAddr(addr);
168 #if defined HOOKING_ENABLED
169 IHookWrapper *CallMaker2::CreateVirtualHook(SourceHook::ISourceHook *pSH,
170 const SourceHook::ProtoInfo *protoInfo,
171 const SourceHook::MemFuncInfo *info,
172 VIRTUAL_HOOK_PROTO f)
174 HookWrapper *pWrapper = new HookWrapper(pSH, protoInfo, const_cast<SourceHook::MemFuncInfo *>(info), (void *)f);
176 void *addr = JIT_HookCompile(pWrapper);
177 pWrapper->SetHookWrpAddr(addr);
179 ICallWrapper *callWrap = CreateVirtualCall(protoInfo, info);
180 pWrapper->SetCallWrapperAddr(callWrap);