project64/Source/Project64/UserInterface/Debugger/ScriptHook.cpp

145 lines
3.6 KiB
C++
Raw Normal View History

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
{
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;
jsCallback.bOnce = bOnce;
m_Callbacks.push_back(jsCallback);
return jsCallback.callbackId;
2017-08-18 05:08:22 +00:00
}
void CScriptHook::InvokeById(int callbackId)
{
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)
{
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
}
void CScriptHook::InvokeByParamInRange(uint32_t param)
{
int nCallbacks = m_Callbacks.size();
for (int i = 0; i < nCallbacks; i++)
{
if (param == m_Callbacks[i].param || (param >= m_Callbacks[i].param && param < m_Callbacks[i].param2))
{
m_Callbacks[i].scriptInstance->Invoke(m_Callbacks[i].heapptr, param);
return;
}
}
2017-08-18 05:08:22 +00:00
}
2018-11-21 08:57:50 +00:00
void CScriptHook::InvokeByParamInRangeWithMaskedValue(uint32_t param, uint32_t value)
{
int nCallbacks = m_Callbacks.size();
for (int i = 0; i < nCallbacks; i++)
{
if (param == m_Callbacks[i].param || (param >= m_Callbacks[i].param && param < m_Callbacks[i].param2))
{
if ((m_Callbacks[i].param3 & m_Callbacks[i].param4) == (value & m_Callbacks[i].param4))
{
m_Callbacks[i].scriptInstance->Invoke(m_Callbacks[i].heapptr, param);
return;
}
}
}
}
2017-08-18 05:08:22 +00:00
void CScriptHook::InvokeAll()
{
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)
{
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)
{
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)
{
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)
{
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)
{
m_ScriptSystem = scriptSystem;
2017-08-18 05:08:22 +00:00
}
CScriptHook::~CScriptHook()
{
m_Callbacks.clear();
2017-08-18 05:08:22 +00:00
}