2022-09-21 05:16:07 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
#include "Debugger.h"
|
|
|
|
#include "Project64\UserInterface\DiscordRPC.h"
|
2022-09-21 05:16:07 +00:00
|
|
|
#include "ScriptAPI/ScriptAPI.h"
|
|
|
|
#include "ScriptInstance.h"
|
|
|
|
#include "ScriptSystem.h"
|
|
|
|
#include "ScriptTypes.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include <sys/stat.h>
|
2021-09-03 05:11:04 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CScriptSystem::CScriptSystem(CDebuggerUI * debugger) :
|
2021-09-03 05:11:04 +00:00
|
|
|
m_Debugger(debugger),
|
|
|
|
m_NextAppCallbackId(0),
|
2021-10-11 03:08:48 +00:00
|
|
|
m_AppCallbackCount(0),
|
|
|
|
m_CpuExecCbInfo({}),
|
|
|
|
m_CpuReadCbInfo({}),
|
|
|
|
m_CpuWriteCbInfo({})
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
InitDirectories();
|
|
|
|
|
|
|
|
m_hCmdEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
|
|
|
|
m_hThread = CreateThread(nullptr, 0, ThreadProc, this, 0, nullptr);
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
CScriptSystem::~CScriptSystem()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
PostCommand(JS_CMD_SHUTDOWN);
|
|
|
|
WaitForSingleObject(m_hThread, INFINITE);
|
|
|
|
CloseHandle(m_hThread);
|
|
|
|
CloseHandle(m_hCmdEvent);
|
|
|
|
}
|
2017-09-13 12:00:53 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
JSInstanceStatus CScriptSystem::GetStatus(const char * name)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
CGuard guard(m_UIStateCS);
|
|
|
|
if (m_UIInstanceStatus.count(name) == 0)
|
|
|
|
{
|
|
|
|
return JS_STATUS_STOPPED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return m_UIInstanceStatus[name];
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 12:00:53 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::NotifyStatus(const char * name, JSInstanceStatus status)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
CGuard guard(m_UIStateCS);
|
|
|
|
if (status == JS_STATUS_STOPPED)
|
|
|
|
{
|
|
|
|
m_UIInstanceStatus.erase(name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_UIInstanceStatus[name] = status;
|
|
|
|
}
|
|
|
|
m_Debugger->Debug_RefreshScriptsWindow();
|
|
|
|
}
|
2017-09-13 12:00:53 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::ConsoleLog(const char * format, ...)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
CGuard guard(m_UIStateCS);
|
2017-09-13 12:00:53 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2017-09-13 12:00:53 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
int size = vsnprintf(nullptr, 0, format, args) + 1;
|
2022-09-21 05:16:07 +00:00
|
|
|
char * str = new char[size];
|
2021-09-03 05:11:04 +00:00
|
|
|
vsnprintf(str, size, format, args);
|
2017-09-13 12:00:53 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
stdstr formattedMsg = FixStringReturns(str) + "\r\n";
|
2022-09-21 05:16:07 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
m_Debugger->Debug_LogScriptsWindow(formattedMsg.c_str());
|
|
|
|
m_UILog += formattedMsg;
|
2017-09-13 12:00:53 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
delete[] str;
|
|
|
|
va_end(args);
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::ConsolePrint(const char * format, ...)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
CGuard guard(m_UIStateCS);
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
|
|
int size = vsnprintf(nullptr, 0, format, args) + 1;
|
2022-09-21 05:16:07 +00:00
|
|
|
char * str = new char[size];
|
2021-09-03 05:11:04 +00:00
|
|
|
vsnprintf(str, size, format, args);
|
2022-09-21 05:16:07 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
stdstr formattedMsg = FixStringReturns(str);
|
2022-09-21 05:16:07 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
m_Debugger->Debug_LogScriptsWindow(formattedMsg.c_str());
|
|
|
|
m_UILog += formattedMsg;
|
|
|
|
|
|
|
|
delete[] str;
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CScriptSystem::ConsoleClear()
|
|
|
|
{
|
|
|
|
CGuard guard(m_UIStateCS);
|
|
|
|
m_UILog.clear();
|
|
|
|
m_Debugger->Debug_ClearScriptsWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
stdstr CScriptSystem::GetConsoleBuffer()
|
|
|
|
{
|
|
|
|
CGuard guard(m_UIStateCS);
|
|
|
|
return stdstr(m_UILog);
|
|
|
|
}
|
|
|
|
|
|
|
|
stdstr CScriptSystem::InstallDirPath()
|
|
|
|
{
|
|
|
|
return m_InstallDirFullPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
stdstr CScriptSystem::ScriptsDirPath()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
return m_ScriptsDirFullPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
stdstr CScriptSystem::ModulesDirPath()
|
|
|
|
{
|
|
|
|
return m_ModulesDirFullPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CScriptSystem::InitDirectories()
|
|
|
|
{
|
|
|
|
m_InstallDirFullPath = (std::string)CPath(CPath::MODULE_DIRECTORY);
|
|
|
|
m_ScriptsDirFullPath = m_InstallDirFullPath + SCRIPTSYS_SCRIPTS_DIR;
|
|
|
|
m_ModulesDirFullPath = m_InstallDirFullPath + SCRIPTSYS_MODULES_DIR;
|
|
|
|
|
|
|
|
if (!PathFileExistsA(m_ScriptsDirFullPath.c_str()))
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
CreateDirectoryA(m_ScriptsDirFullPath.c_str(), nullptr);
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
if (!PathFileExistsA(m_ModulesDirFullPath.c_str()))
|
|
|
|
{
|
|
|
|
CreateDirectoryA(m_ModulesDirFullPath.c_str(), nullptr);
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::StartScript(const char * name, const char * path)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
PostCommand(JS_CMD_START_SCRIPT, name, path);
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::StopScript(const char * name)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
PostCommand(JS_CMD_STOP_SCRIPT, name);
|
|
|
|
}
|
2019-04-29 06:28:12 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::Input(const char * name, const char * code)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
PostCommand(JS_CMD_INPUT, name, code);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CScriptSystem::HaveAppCallbacks(JSAppHookID hookId)
|
|
|
|
{
|
|
|
|
CGuard guard(m_InstancesCS);
|
|
|
|
|
2022-09-26 02:31:54 +00:00
|
|
|
return (hookId < JS_NUM_APP_HOOKS && m_AppCallbackHooks[hookId].size() > 0);
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::InvokeAppCallbacks(JSAppHookID hookId, void * env)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
CGuard guard(m_InstancesCS);
|
2022-09-21 05:16:07 +00:00
|
|
|
|
|
|
|
JSAppCallbackList & callbacks = m_AppCallbackHooks[hookId];
|
2022-01-12 20:36:30 +00:00
|
|
|
size_t numCallbacks = callbacks.size();
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
bool bNeedSweep = false;
|
2019-04-29 06:28:12 +00:00
|
|
|
|
2022-01-12 20:36:30 +00:00
|
|
|
for (size_t i = 0; i < numCallbacks; i++)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
JSAppCallback & callback = callbacks[i];
|
|
|
|
CScriptInstance * instance = callback.m_Instance;
|
2022-01-12 20:36:30 +00:00
|
|
|
|
2022-09-26 02:31:54 +00:00
|
|
|
if (callback.m_bDisabled || instance->IsStopping() || m_StopsIssued.count(instance->Name()) != 0 || !callback.m_ConditionFunc(&callback, env))
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-12 20:36:30 +00:00
|
|
|
instance->RawInvokeAppCallback(callback, env);
|
2021-09-03 05:11:04 +00:00
|
|
|
|
2022-01-12 20:36:30 +00:00
|
|
|
if (instance->GetRefCount() == 0)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
2022-01-12 20:36:30 +00:00
|
|
|
bNeedSweep = true;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
if (bNeedSweep)
|
|
|
|
{
|
|
|
|
PostCommand(JS_CMD_SWEEP);
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::UpdateCpuCbListInfo(volatile JSCpuCbListInfo & info, JSAppCallbackList & callbacks)
|
2021-10-11 03:08:48 +00:00
|
|
|
{
|
|
|
|
uint32_t minAddrStart = 0;
|
|
|
|
uint32_t maxAddrEnd = 0;
|
|
|
|
int numCacheEntries = 0;
|
|
|
|
bool bCacheExceeded = false;
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
for (JSAppCallback & callback : callbacks)
|
2021-10-11 03:08:48 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < numCacheEntries; i++)
|
|
|
|
{
|
|
|
|
// combine adjacent/overlapping ranges
|
2022-09-26 02:31:54 +00:00
|
|
|
if ((callback.m_Params.addrStart >= info.rangeCache[i].addrStart && callback.m_Params.addrStart <= info.rangeCache[i].addrEnd + 1) || (callback.m_Params.addrEnd >= info.rangeCache[i].addrStart - 1 && callback.m_Params.addrEnd <= info.rangeCache[i].addrEnd))
|
2021-10-11 03:08:48 +00:00
|
|
|
{
|
|
|
|
info.rangeCache[i].addrStart = min(info.rangeCache[i].addrStart, callback.m_Params.addrStart);
|
|
|
|
info.rangeCache[i].addrEnd = max(info.rangeCache[i].addrEnd, callback.m_Params.addrEnd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == numCacheEntries)
|
|
|
|
{
|
|
|
|
if (i == JS_CPU_CB_RANGE_CACHE_SIZE)
|
|
|
|
{
|
|
|
|
bCacheExceeded = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info.rangeCache[i].addrStart = callback.m_Params.addrStart;
|
|
|
|
info.rangeCache[i].addrEnd = callback.m_Params.addrEnd;
|
|
|
|
numCacheEntries++;
|
|
|
|
}
|
|
|
|
}
|
2022-09-21 05:16:07 +00:00
|
|
|
|
2021-10-11 03:08:48 +00:00
|
|
|
if (callback.m_Params.addrStart < minAddrStart)
|
|
|
|
{
|
|
|
|
minAddrStart = callback.m_Params.addrStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback.m_Params.addrEnd > maxAddrEnd)
|
|
|
|
{
|
|
|
|
maxAddrEnd = callback.m_Params.addrEnd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info.numRangeCacheEntries = numCacheEntries;
|
|
|
|
info.bRangeCacheExceeded = bCacheExceeded;
|
|
|
|
info.numCallbacks = callbacks.size();
|
|
|
|
info.minAddrStart = minAddrStart;
|
|
|
|
info.maxAddrEnd = maxAddrEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CScriptSystem::RefreshCallbackMaps()
|
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
for (JSAppCallbackList & callbacks : m_AppCallbackHooks)
|
2021-10-11 03:08:48 +00:00
|
|
|
{
|
2021-12-28 07:31:19 +00:00
|
|
|
JSAppCallbackList::iterator it = callbacks.begin();
|
2021-10-11 03:08:48 +00:00
|
|
|
|
2021-12-28 07:31:19 +00:00
|
|
|
while (it != callbacks.end())
|
|
|
|
{
|
|
|
|
if (it->m_bDisabled)
|
|
|
|
{
|
2022-01-12 20:36:30 +00:00
|
|
|
it = callbacks.erase(it);
|
2021-12-28 07:31:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 03:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UpdateCpuCbListInfo(m_CpuExecCbInfo, m_AppCallbackHooks[JS_HOOK_CPU_EXEC]);
|
|
|
|
UpdateCpuCbListInfo(m_CpuReadCbInfo, m_AppCallbackHooks[JS_HOOK_CPU_READ]);
|
|
|
|
UpdateCpuCbListInfo(m_CpuWriteCbInfo, m_AppCallbackHooks[JS_HOOK_CPU_WRITE]);
|
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
void CScriptSystem::DoMouseEvent(JSAppHookID hookId, int x, int y, DWORD uMsg)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
int button = -1;
|
2019-12-25 00:41:20 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
switch (uMsg)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
case WM_LBUTTONDOWN:
|
|
|
|
case WM_LBUTTONUP:
|
|
|
|
button = 0;
|
|
|
|
break;
|
|
|
|
case WM_MBUTTONDOWN:
|
|
|
|
case WM_MBUTTONUP:
|
|
|
|
button = 1;
|
|
|
|
break;
|
|
|
|
case WM_RBUTTONDOWN:
|
|
|
|
case WM_RBUTTONUP:
|
|
|
|
button = 2;
|
|
|
|
break;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2019-04-29 06:28:12 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
JSHookMouseEnv env = {x, y, button};
|
|
|
|
InvokeAppCallbacks(hookId, (void *)&env);
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::PostCMethodCall(const char * name, void * dukThisHeapPtr, duk_c_function func,
|
|
|
|
JSDukArgSetupFunc argSetupFunc, void * argSetupParam, size_t argSetupParamSize)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
CGuard guard(m_CmdQueueCS);
|
|
|
|
// Will be deleted by command handler
|
2022-09-21 05:16:07 +00:00
|
|
|
JSSysCMethodCall * methodCall = new JSSysCMethodCall(dukThisHeapPtr, func, argSetupFunc, argSetupParam, argSetupParamSize);
|
|
|
|
PostCommand(JS_CMD_C_METHOD_CALL, name, "", (void *)methodCall);
|
2021-09-03 05:11:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::PostCommand(JSSysCommandID id, stdstr paramA, stdstr paramB, void * paramC)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
CGuard guard(m_CmdQueueCS);
|
2022-09-21 05:16:07 +00:00
|
|
|
JSSysCommand cmd = {id, paramA, paramB, paramC};
|
2022-01-12 20:36:30 +00:00
|
|
|
|
|
|
|
if (id == JS_CMD_STOP_SCRIPT)
|
|
|
|
{
|
|
|
|
JSInstanceName instName = paramA;
|
|
|
|
if (m_StopsIssued.count(instName) != 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_StopsIssued.insert(instName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
m_CmdQueue.push_back(cmd);
|
|
|
|
SetEvent(m_hCmdEvent);
|
|
|
|
}
|
2019-04-29 06:28:12 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
DWORD CScriptSystem::ThreadProc(void * _this)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
((CScriptSystem *)_this)->ThreadProc();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CScriptSystem::ThreadProc()
|
|
|
|
{
|
|
|
|
std::vector<JSSysCommand> queue;
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
while (true)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
WaitForSingleObject(m_hCmdEvent, INFINITE);
|
|
|
|
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
CGuard guard(m_CmdQueueCS);
|
|
|
|
queue = m_CmdQueue;
|
|
|
|
m_CmdQueue.clear();
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
if (!ProcessCommandQueue(queue))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::OnStartScript(const char * name, const char * path)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
if (m_Instances.count(name) != 0)
|
|
|
|
{
|
|
|
|
ConsoleLog("[SCRIPTSYS]: error: START_SCRIPT aborted; '%s' is already instanced", name);
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2019-04-29 06:28:12 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CScriptInstance * inst = new CScriptInstance(this, name);
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
NotifyStatus(name, JS_STATUS_STARTING);
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
if (inst->Run(path) && inst->GetRefCount() > 0)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
m_Instances[name] = inst;
|
|
|
|
NotifyStatus(name, JS_STATUS_STARTED);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NotifyStatus(name, JS_STATUS_STOPPED);
|
|
|
|
delete inst;
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::OnStopScript(const char * name)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
if (m_Instances.count(name) == 0)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
ConsoleLog("[SCRIPTSYS]: error: STOP_SCRIPT aborted; instance '%s' does not exist", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-12 20:36:30 +00:00
|
|
|
m_StopsIssued.erase(name);
|
2021-09-03 05:11:04 +00:00
|
|
|
RawRemoveInstance(name);
|
|
|
|
NotifyStatus(name, JS_STATUS_STOPPED);
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::OnInput(const char * name, const char * code)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
if (m_Instances.count(name) == 0)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
ConsoleLog("[SCRIPTSYS]: error: INPUT aborted; instance '%s' does not exist", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CScriptInstance * inst = m_Instances[name];
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
inst->RawConsoleInput(code);
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
if (!inst->IsStopping() && inst->GetRefCount() == 0)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
NotifyStatus(name, JS_STATUS_STOPPED);
|
|
|
|
RawRemoveInstance(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::OnCMethodCall(const char * name, JSSysCMethodCall * methodCall)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
if (m_Instances.count(name) == 0)
|
|
|
|
{
|
|
|
|
ConsoleLog("[SCRIPTSYS]: error: method call aborted; instance '%s' doesn't exist", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_Instances.count(name) == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CScriptInstance * inst = m_Instances[name];
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
inst->RawCMethodCall(methodCall->m_DukThisHeapPtr, methodCall->m_Func, methodCall->m_ArgSetupFunc, methodCall->m_ArgSetupParam);
|
|
|
|
|
|
|
|
if (!inst->IsStopping() && inst->GetRefCount() == 0)
|
|
|
|
{
|
|
|
|
NotifyStatus(name, JS_STATUS_STOPPED);
|
|
|
|
RawRemoveInstance(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CScriptSystem::OnSweep(bool bIfDone)
|
|
|
|
{
|
|
|
|
JSInstanceMap::iterator it = m_Instances.begin();
|
2022-09-21 05:16:07 +00:00
|
|
|
while (it != m_Instances.end())
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
CScriptInstance *& inst = it->second;
|
|
|
|
if (!bIfDone || inst->GetRefCount() == 0)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
NotifyStatus(inst->Name().c_str(), JS_STATUS_STOPPED);
|
|
|
|
delete inst;
|
2022-01-12 20:36:30 +00:00
|
|
|
it = m_Instances.erase(it);
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2021-09-03 05:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
bool CScriptSystem::RawRemoveInstance(const char * name)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
if (m_Instances.count(name) == 0)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CScriptInstance *& inst = m_Instances[name];
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
if (inst->IsStopping())
|
|
|
|
{
|
|
|
|
return false;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
inst->SetStopping(true);
|
|
|
|
inst->StopRegisteredWorkers();
|
2022-09-21 05:16:07 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
std::vector<JSSysCommand> pendingCalls;
|
|
|
|
PullCommands(JS_CMD_C_METHOD_CALL, pendingCalls);
|
|
|
|
ProcessCommandQueue(pendingCalls);
|
|
|
|
|
|
|
|
delete inst;
|
|
|
|
m_Instances.erase(name);
|
|
|
|
return true;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
JSAppCallbackID CScriptSystem::RawAddAppCallback(JSAppHookID hookId, JSAppCallback & callback)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
if (hookId >= JS_NUM_APP_HOOKS)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-12-28 07:31:19 +00:00
|
|
|
return JS_INVALID_CALLBACK;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2021-09-03 05:11:04 +00:00
|
|
|
|
2021-12-28 07:31:19 +00:00
|
|
|
callback.m_Instance->IncRefCount();
|
|
|
|
callback.m_CallbackId = m_NextAppCallbackId++;
|
2021-10-11 03:08:48 +00:00
|
|
|
m_AppCallbackHooks[hookId].push_back(callback);
|
2021-09-03 05:11:04 +00:00
|
|
|
m_AppCallbackCount++;
|
2022-01-12 20:36:30 +00:00
|
|
|
RefreshCallbackMaps();
|
2021-12-28 07:31:19 +00:00
|
|
|
return callback.m_CallbackId;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 03:08:48 +00:00
|
|
|
void CScriptSystem::RawRemoveAppCallback(JSAppHookID hookId, JSAppCallbackID callbackId)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-12-28 07:31:19 +00:00
|
|
|
JSAppCallbackList::iterator it = m_AppCallbackHooks[hookId].begin();
|
|
|
|
while (it != m_AppCallbackHooks[hookId].end())
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-10-11 03:08:48 +00:00
|
|
|
if (it->m_CallbackId == callbackId)
|
|
|
|
{
|
|
|
|
m_AppCallbackCount--;
|
2021-12-28 07:31:19 +00:00
|
|
|
it->m_Instance->DecRefCount();
|
|
|
|
it->m_bDisabled = true;
|
2021-10-11 03:08:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-12-28 07:31:19 +00:00
|
|
|
|
|
|
|
it++;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2022-01-12 20:36:30 +00:00
|
|
|
RefreshCallbackMaps();
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
void CScriptSystem::ExecAutorunList()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
LoadAutorunList();
|
|
|
|
|
|
|
|
std::istringstream joinedNames(g_Settings->LoadStringVal(Debugger_AutorunScripts));
|
|
|
|
std::string scriptName;
|
|
|
|
while (std::getline(joinedNames, scriptName, '|'))
|
|
|
|
{
|
|
|
|
std::string scriptPath = m_ScriptsDirFullPath + scriptName;
|
|
|
|
ConsoleLog("[SCRIPTSYS]: autorun '%s'", scriptName.c_str());
|
|
|
|
StartScript(scriptName.c_str(), scriptPath.c_str());
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
std::set<std::string> & CScriptSystem::AutorunList()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
return m_AutorunList;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
void CScriptSystem::LoadAutorunList()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
m_AutorunList.clear();
|
|
|
|
|
|
|
|
std::istringstream joinedNames(g_Settings->LoadStringVal(Debugger_AutorunScripts));
|
|
|
|
std::string scriptName;
|
|
|
|
|
|
|
|
while (std::getline(joinedNames, scriptName, '|'))
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
m_AutorunList.insert(scriptName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CScriptSystem::SaveAutorunList()
|
|
|
|
{
|
|
|
|
std::string joinedNames = "";
|
|
|
|
|
|
|
|
std::set<std::string>::iterator it;
|
|
|
|
for (it = m_AutorunList.begin(); it != m_AutorunList.end(); it++)
|
|
|
|
{
|
|
|
|
if (it != m_AutorunList.begin())
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
joinedNames += "|";
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2021-09-03 05:11:04 +00:00
|
|
|
joinedNames += *it;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
g_Settings->SaveString(Debugger_AutorunScripts, joinedNames.c_str());
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CDebuggerUI * CScriptSystem::Debugger()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
return m_Debugger;
|
2019-12-01 10:10:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
stdstr CScriptSystem::FixStringReturns(const char * str)
|
2019-12-01 10:10:36 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
stdstr fstr = str;
|
|
|
|
size_t pos = 0;
|
|
|
|
while ((pos = fstr.find("\n", pos)) != stdstr::npos)
|
|
|
|
{
|
|
|
|
fstr.replace(pos, 1, "\r\n");
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
return fstr;
|
2019-12-01 10:10:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
bool CScriptSystem::ProcessCommandQueue(std::vector<JSSysCommand> & queue)
|
2019-12-01 10:10:36 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
std::vector<JSSysCommand>::iterator it;
|
|
|
|
for (it = queue.begin(); it != queue.end(); it++)
|
2019-12-01 10:10:36 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
if (!ProcessCommand(*it))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
bool CScriptSystem::ProcessCommand(JSSysCommand & cmd)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
CGuard guard(m_InstancesCS);
|
|
|
|
|
|
|
|
switch (cmd.id)
|
|
|
|
{
|
|
|
|
case JS_CMD_START_SCRIPT:
|
|
|
|
OnStartScript(cmd.paramA.c_str(), cmd.paramB.c_str());
|
|
|
|
break;
|
|
|
|
case JS_CMD_STOP_SCRIPT:
|
|
|
|
OnStopScript(cmd.paramA.c_str());
|
|
|
|
break;
|
|
|
|
case JS_CMD_INPUT:
|
|
|
|
OnInput(cmd.paramA.c_str(), cmd.paramB.c_str());
|
|
|
|
break;
|
|
|
|
case JS_CMD_C_METHOD_CALL:
|
2022-09-21 05:16:07 +00:00
|
|
|
{
|
|
|
|
JSSysCMethodCall * methodCall = (JSSysCMethodCall *)cmd.paramC;
|
|
|
|
OnCMethodCall(cmd.paramA.c_str(), methodCall);
|
|
|
|
delete methodCall;
|
|
|
|
}
|
|
|
|
break;
|
2021-09-03 05:11:04 +00:00
|
|
|
case JS_CMD_SWEEP:
|
|
|
|
OnSweep(true);
|
|
|
|
break;
|
|
|
|
case JS_CMD_SHUTDOWN:
|
|
|
|
OnSweep(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-11 03:08:48 +00:00
|
|
|
RefreshCallbackMaps();
|
2021-09-03 05:11:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptSystem::PullCommands(JSSysCommandID id, std::vector<JSSysCommand> & out)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
CGuard guard(m_CmdQueueCS);
|
|
|
|
|
|
|
|
std::vector<JSSysCommand>::iterator it;
|
|
|
|
for (it = m_CmdQueue.begin(); it != m_CmdQueue.end();)
|
|
|
|
{
|
|
|
|
if ((*it).id == id)
|
|
|
|
{
|
|
|
|
out.push_back(*it);
|
|
|
|
it = m_CmdQueue.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it++;
|
|
|
|
}
|
2019-12-01 10:10:36 +00:00
|
|
|
}
|
|
|
|
}
|