extensions/bintools/CallMaker.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 2504 74fea7db93e4
permissions -rw-r--r--
Update CS:GO team score gamedata.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod BinTools Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <stdio.h>
    33 #include "CallMaker.h"
    34 #include "jit_compile.h"
    35 #include "extension.h"
    36 
    37 /* SourceMod <==> SourceHook type conversion functions */
    38 
    39 SourceHook::ProtoInfo::CallConvention GetSHCallConvention(SourceMod::CallConvention cv)
    40 {
    41 	if (cv < SourceMod::CallConv_ThisCall || cv > SourceMod::CallConv_Cdecl)
    42 	{
    43 		return SourceHook::ProtoInfo::CallConv_Unknown;
    44 	}
    45 
    46 	return (SourceHook::ProtoInfo::CallConvention)(cv + 1);
    47 }
    48 
    49 SourceMod::CallConvention GetSMCallConvention(SourceHook::ProtoInfo::CallConvention cv)
    50 {
    51 	assert(cv >= SourceHook::ProtoInfo::CallConv_ThisCall || cv <= SourceHook::ProtoInfo::CallConv_Cdecl);
    52 
    53 	return (SourceMod::CallConvention)(cv - 1);
    54 }
    55 
    56 SourceHook::PassInfo::PassType GetSHPassType(SourceMod::PassType type)
    57 {
    58 	if (type < SourceMod::PassType_Basic || type > SourceMod::PassType_Object)
    59 	{
    60 		return SourceHook::PassInfo::PassType_Unknown;
    61 	}
    62 
    63 	return (SourceHook::PassInfo::PassType)(type + 1);
    64 }
    65 
    66 SourceMod::PassType GetSMPassType(int type)
    67 {
    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);
    70 
    71 	return (SourceMod::PassType)(type - 1);
    72 }
    73 
    74 void GetSMPassInfo(SourceMod::PassInfo *out, const SourceHook::PassInfo *in)
    75 {
    76 	out->size = in->size;
    77 	out->flags = in->flags;
    78 	out->type = GetSMPassType(in->type);
    79 }
    80 
    81 ICallWrapper *CallMaker::CreateCall(void *address,
    82 						 CallConvention cv,
    83 						 const PassInfo *retInfo,
    84 						 const PassInfo paramInfo[],
    85 						 unsigned int numParams)
    86 {
    87 	SourceHook::CProtoInfoBuilder protoInfo(GetSHCallConvention(cv));
    88 
    89 	for (unsigned int i=0; i<numParams; i++)
    90 	{
    91 		protoInfo.AddParam(paramInfo[i].size, GetSHPassType(paramInfo[i].type), paramInfo[i].flags,
    92 			NULL, NULL, NULL, NULL);
    93 	}
    94 
    95 	if (retInfo)
    96 	{
    97 		protoInfo.SetReturnType(retInfo->size, GetSHPassType(retInfo->type), retInfo->flags,
    98 			NULL, NULL, NULL, NULL);
    99 	}
   100 	else
   101 	{
   102 		protoInfo.SetReturnType(0, SourceHook::PassInfo::PassType_Unknown, 0,
   103 			NULL, NULL, NULL, NULL);
   104 	}
   105 
   106 	return g_CallMaker2.CreateCall(address, &(*protoInfo));
   107 }
   108 
   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)
   115 {
   116 	SourceHook::MemFuncInfo info;
   117 	info.isVirtual = true;
   118 	info.vtblindex = vtblIdx;
   119 	info.vtbloffs = vtblOffs;
   120 	info.thisptroffs = thisOffs;
   121 
   122 	SourceHook::CProtoInfoBuilder protoInfo(SourceHook::ProtoInfo::CallConv_ThisCall);
   123 
   124 	for (unsigned int i=0; i<numParams; i++)
   125 	{
   126 		protoInfo.AddParam(paramInfo[i].size, GetSHPassType(paramInfo[i].type), paramInfo[i].flags,
   127 			NULL, NULL, NULL, NULL);
   128 	}
   129 
   130 	if (retInfo)
   131 	{
   132 		protoInfo.SetReturnType(retInfo->size, GetSHPassType(retInfo->type), retInfo->flags,
   133 			NULL, NULL, NULL, NULL);
   134 	}
   135 	else
   136 	{
   137 		protoInfo.SetReturnType(0, SourceHook::PassInfo::PassType_Unknown, 0,
   138 			NULL, NULL, NULL, NULL);
   139 	}
   140 	
   141 
   142 	return g_CallMaker2.CreateVirtualCall(&(*protoInfo), &info);
   143 }
   144 
   145 ICallWrapper *CallMaker2::CreateCall(void *address, const SourceHook::ProtoInfo *protoInfo)
   146 {
   147 	CallWrapper *pWrapper = new CallWrapper(protoInfo);
   148 	pWrapper->SetCalleeAddr(address);
   149 
   150 	void *addr = JIT_CallCompile(pWrapper, FuncAddr_Direct);
   151 	pWrapper->SetCodeBaseAddr(addr);
   152 
   153 	return pWrapper;
   154 }
   155 
   156 ICallWrapper *CallMaker2::CreateVirtualCall(const SourceHook::ProtoInfo *protoInfo, 
   157 											const SourceHook::MemFuncInfo *info)
   158 {
   159 	CallWrapper *pWrapper = new CallWrapper(protoInfo);
   160 	pWrapper->SetMemFuncInfo(info);
   161 
   162 	void *addr = JIT_CallCompile(pWrapper, FuncAddr_VTable);
   163 	pWrapper->SetCodeBaseAddr(addr);
   164 
   165 	return pWrapper;
   166 }
   167 
   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)
   173 {
   174 	HookWrapper *pWrapper = new HookWrapper(pSH, protoInfo, const_cast<SourceHook::MemFuncInfo *>(info), (void *)f);
   175 
   176 	void *addr = JIT_HookCompile(pWrapper);
   177 	pWrapper->SetHookWrpAddr(addr);
   178 
   179 	ICallWrapper *callWrap = CreateVirtualCall(protoInfo, info);
   180 	pWrapper->SetCallWrapperAddr(callWrap);
   181 	
   182 	return pWrapper;
   183 }
   184 #endif
   185