/**
 * vim: set ts=4 :
 * =============================================================================
 * SourceMod BinTools Extension
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
 * =============================================================================
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, version 3.0, as published by the
 * Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see .
 *
 * As a special exception, AlliedModders LLC gives you permission to link the
 * code of this program (as well as its derivative works) to "Half-Life 2," the
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
 * by the Valve Corporation.  You must obey the GNU General Public License in
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
 * this exception to all derivative works.  AlliedModders LLC defines further
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
 * or .
 *
 * Version: $Id$
 */
#include "extension.h"
#include "CallWrapper.h"
#include "CallMaker.h"
CallWrapper::CallWrapper(const SourceHook::ProtoInfo *protoInfo)
{
	m_AddrCodeBase = NULL;
	m_AddrCallee = NULL;
	unsigned int argnum = protoInfo->numOfParams;
	m_Info = *protoInfo;
	m_Info.paramsPassInfo = new SourceHook::PassInfo[argnum + 1];
	memcpy((void *)m_Info.paramsPassInfo, protoInfo->paramsPassInfo, sizeof(SourceHook::PassInfo) * (argnum+1));
	if (argnum)
	{
		m_Params = new PassEncode[argnum];
		for (size_t i=0; iFreePageMemory(m_AddrCodeBase);
		m_AddrCodeBase = NULL;
	}
	delete this;
}
CallConvention CallWrapper::GetCallConvention()
{
	/* Need to convert to a SourceMod convention for bcompat */
	return GetSMCallConvention((SourceHook::ProtoInfo::CallConvention)m_Info.convention);
}
const PassEncode *CallWrapper::GetParamInfo(unsigned int num)
{
	if (num + 1 > GetParamCount())
	{
		return NULL;
	}
	return &m_Params[num];
}
const PassInfo *CallWrapper::GetReturnInfo()
{
	return m_RetParam;
}
unsigned int CallWrapper::GetParamCount()
{
	return m_Info.numOfParams;
}
void CallWrapper::Execute(void *vParamStack, void *retBuffer)
{
	typedef void (*CALL_EXECUTE)(void *, void *);
	CALL_EXECUTE fn = (CALL_EXECUTE)GetCodeBaseAddr();
	fn(vParamStack, retBuffer);
}
void CallWrapper::SetMemFuncInfo(const SourceHook::MemFuncInfo *funcInfo)
{
	m_FuncInfo = *funcInfo;
}
SourceHook::MemFuncInfo *CallWrapper::GetMemFuncInfo()
{
	return &m_FuncInfo;
}
void CallWrapper::SetCalleeAddr(void *addr)
{
	m_AddrCallee = addr;
}
void CallWrapper::SetCodeBaseAddr(void *addr)
{
	m_AddrCodeBase = addr;
}
void *CallWrapper::GetCalleeAddr()
{
	return m_AddrCallee;
}
void *CallWrapper::GetCodeBaseAddr()
{
	return m_AddrCodeBase;
}
const SourceHook::PassInfo *CallWrapper::GetSHReturnInfo()
{
	return &(m_Info.retPassInfo);
}
SourceHook::ProtoInfo::CallConvention CallWrapper::GetSHCallConvention()
{
	return (SourceHook::ProtoInfo::CallConvention)m_Info.convention;
}
const SourceHook::PassInfo * CallWrapper::GetSHParamInfo(unsigned int num)
{
	if (num + 1 > GetParamCount())
	{
		return NULL;
	}
	return &(m_Info.paramsPassInfo[num+1]);
}
unsigned int CallWrapper::GetParamOffset(unsigned int num)
{
	assert(num < GetParamCount() && num >= 0);
	return m_Params[num].offset;
}