2017-08-18 05:08:22 +00:00
|
|
|
#include <stdafx.h>
|
|
|
|
|
|
|
|
#include "ScriptHook.h"
|
|
|
|
#include "ScriptInstance.h"
|
|
|
|
#include "ScriptSystem.h"
|
|
|
|
|
2018-11-21 08:57:50 +00:00
|
|
|
int CScriptHook::Add(CScriptInstance* scriptInstance, void* heapptr, uint32_t param, uint32_t param2,
|
|
|
|
uint32_t param3, uint32_t param4, bool bOnce)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2017-09-13 12:00:53 +00:00
|
|
|
JSCALLBACK jsCallback;
|
|
|
|
jsCallback.scriptInstance = scriptInstance;
|
|
|
|
jsCallback.heapptr = heapptr;
|
|
|
|
jsCallback.callbackId = m_ScriptSystem->GetNextCallbackId();
|
|
|
|
jsCallback.param = param;
|
|
|
|
jsCallback.param2 = param2;
|
2018-11-21 08:57:50 +00:00
|
|
|
jsCallback.param3 = param3;
|
|
|
|
jsCallback.param4 = param4;
|
2017-09-13 12:00:53 +00:00
|
|
|
jsCallback.bOnce = bOnce;
|
|
|
|
m_Callbacks.push_back(jsCallback);
|
|
|
|
return jsCallback.callbackId;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CScriptHook::InvokeById(int callbackId)
|
|
|
|
{
|
2017-09-13 12:00:53 +00:00
|
|
|
int nCallbacks = m_Callbacks.size();
|
|
|
|
for (int i = 0; i < nCallbacks; i++)
|
|
|
|
{
|
|
|
|
if (m_Callbacks[i].callbackId == callbackId)
|
|
|
|
{
|
|
|
|
m_Callbacks[i].scriptInstance->Invoke(m_Callbacks[i].heapptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CScriptHook::InvokeByParam(uint32_t param)
|
|
|
|
{
|
2017-09-13 12:00:53 +00:00
|
|
|
int nCallbacks = m_Callbacks.size();
|
|
|
|
for (int i = 0; i < nCallbacks; i++)
|
|
|
|
{
|
|
|
|
if (m_Callbacks[i].param == param)
|
|
|
|
{
|
|
|
|
m_Callbacks[i].scriptInstance->Invoke(m_Callbacks[i].heapptr, param);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 21:24:11 +00:00
|
|
|
void CScriptHook::InvokeByAddressInRange(uint32_t address)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2017-09-13 12:00:53 +00:00
|
|
|
int nCallbacks = m_Callbacks.size();
|
|
|
|
for (int i = 0; i < nCallbacks; i++)
|
|
|
|
{
|
2018-12-09 21:24:11 +00:00
|
|
|
if (address == m_Callbacks[i].param || (address >= m_Callbacks[i].param && address < m_Callbacks[i].param2))
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2018-12-09 21:24:11 +00:00
|
|
|
m_Callbacks[i].scriptInstance->Invoke(m_Callbacks[i].heapptr, address);
|
2017-09-13 12:00:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 21:24:11 +00:00
|
|
|
void CScriptHook::InvokeByAddressInRange_MaskedOpcode(uint32_t pc, uint32_t opcode)
|
2018-11-21 08:57:50 +00:00
|
|
|
{
|
|
|
|
int nCallbacks = m_Callbacks.size();
|
|
|
|
for (int i = 0; i < nCallbacks; i++)
|
|
|
|
{
|
2018-12-09 21:24:11 +00:00
|
|
|
if (pc == m_Callbacks[i].param || (pc >= m_Callbacks[i].param && pc < m_Callbacks[i].param2))
|
2018-11-21 08:57:50 +00:00
|
|
|
{
|
2018-12-09 21:24:11 +00:00
|
|
|
if ((m_Callbacks[i].param3 & m_Callbacks[i].param4) == (opcode & m_Callbacks[i].param4))
|
2018-11-21 08:57:50 +00:00
|
|
|
{
|
2018-12-09 21:24:11 +00:00
|
|
|
m_Callbacks[i].scriptInstance->Invoke(m_Callbacks[i].heapptr, pc);
|
2018-11-21 08:57:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-09 21:24:11 +00:00
|
|
|
void CScriptHook::InvokeByAddressInRange_GPRValue(uint32_t pc)
|
|
|
|
{
|
|
|
|
int nCallbacks = m_Callbacks.size();
|
|
|
|
|
|
|
|
for (int i = 0; i < nCallbacks; i++)
|
|
|
|
{
|
|
|
|
uint32_t startAddress = m_Callbacks[i].param;
|
|
|
|
uint32_t endAddress = m_Callbacks[i].param2;
|
|
|
|
uint32_t registers = m_Callbacks[i].param3;
|
|
|
|
uint32_t value = m_Callbacks[i].param4;
|
|
|
|
|
|
|
|
if (!(pc == startAddress || (pc >= startAddress && pc < endAddress)))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int nReg = 0; nReg < 32; nReg++)
|
|
|
|
{
|
|
|
|
if (registers & (1 << nReg))
|
|
|
|
{
|
|
|
|
if (value == g_Reg->m_GPR[nReg].UW[0])
|
|
|
|
{
|
|
|
|
m_Callbacks[i].scriptInstance->Invoke2(m_Callbacks[i].heapptr, pc, nReg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 05:08:22 +00:00
|
|
|
void CScriptHook::InvokeAll()
|
|
|
|
{
|
2017-09-13 12:00:53 +00:00
|
|
|
int nCallbacks = m_Callbacks.size();
|
|
|
|
for (int i = 0; i < nCallbacks; i++)
|
|
|
|
{
|
|
|
|
m_Callbacks[i].scriptInstance->Invoke(m_Callbacks[i].heapptr);
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CScriptHook::RemoveById(int callbackId)
|
|
|
|
{
|
2017-09-13 12:00:53 +00:00
|
|
|
int nCallbacks = m_Callbacks.size();
|
|
|
|
for (int i = 0; i < nCallbacks; i++)
|
|
|
|
{
|
|
|
|
if (m_Callbacks[i].callbackId == callbackId)
|
|
|
|
{
|
|
|
|
m_Callbacks.erase(m_Callbacks.begin() + i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CScriptHook::RemoveByParam(uint32_t param)
|
|
|
|
{
|
2017-09-13 12:00:53 +00:00
|
|
|
int nCallbacks = m_Callbacks.size();
|
|
|
|
for (int i = 0; i < nCallbacks; i++)
|
|
|
|
{
|
|
|
|
if (m_Callbacks[i].param == param)
|
|
|
|
{
|
|
|
|
m_Callbacks.erase(m_Callbacks.begin() + i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CScriptHook::RemoveByInstance(CScriptInstance* scriptInstance)
|
|
|
|
{
|
2017-09-13 12:00:53 +00:00
|
|
|
int lastIndex = m_Callbacks.size() - 1;
|
|
|
|
for (int i = lastIndex; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (m_Callbacks[i].scriptInstance == scriptInstance)
|
|
|
|
{
|
|
|
|
m_Callbacks.erase(m_Callbacks.begin() + i);
|
|
|
|
}
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CScriptHook::HasContext(CScriptInstance* scriptInstance)
|
|
|
|
{
|
2017-09-13 12:00:53 +00:00
|
|
|
for (size_t i = 0; i < m_Callbacks.size(); i++)
|
|
|
|
{
|
|
|
|
if (m_Callbacks[i].scriptInstance == scriptInstance)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CScriptHook::CScriptHook(CScriptSystem* scriptSystem)
|
|
|
|
{
|
2017-09-13 12:00:53 +00:00
|
|
|
m_ScriptSystem = scriptSystem;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CScriptHook::~CScriptHook()
|
|
|
|
{
|
2017-09-13 12:00:53 +00:00
|
|
|
m_Callbacks.clear();
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|