2021-09-03 05:11:04 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <fstream>
|
|
|
|
#include <map>
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
#include "ScriptTypes.h"
|
|
|
|
#include "debugger.h"
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
#pragma once
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
#define SCRIPTSYS_SCRIPTS_DIR "Scripts\\"
|
|
|
|
#define SCRIPTSYS_MODULES_DIR "Scripts\\modules\\"
|
2017-08-18 05:08:22 +00:00
|
|
|
|
|
|
|
class CScriptSystem
|
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
typedef std::map<JSInstanceName, CScriptInstance*> JSInstanceMap;
|
2021-10-11 03:08:48 +00:00
|
|
|
typedef std::vector<JSAppCallback> JSAppCallbackList;
|
2021-09-03 05:11:04 +00:00
|
|
|
typedef std::map<JSInstanceName, JSInstanceStatus> JSInstanceStatusMap;
|
|
|
|
typedef std::vector<JSSysCommand> JSSysCommandQueue;
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-10-11 03:08:48 +00:00
|
|
|
struct JSQueuedCallbackAdd
|
|
|
|
{
|
|
|
|
JSAppHookID hookId;
|
|
|
|
JSAppCallback callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct JSQueuedCallbackRemove
|
|
|
|
{
|
|
|
|
JSAppHookID hookId;
|
|
|
|
JSAppCallbackID callbackId;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum { JS_CPU_CB_RANGE_CACHE_SIZE = 256 };
|
|
|
|
|
|
|
|
struct JSCpuCbListInfo
|
|
|
|
{
|
|
|
|
size_t numCallbacks;
|
|
|
|
uint32_t minAddrStart;
|
|
|
|
uint32_t maxAddrEnd;
|
|
|
|
size_t numRangeCacheEntries;
|
|
|
|
bool bRangeCacheExceeded;
|
|
|
|
struct {
|
|
|
|
uint32_t addrStart;
|
|
|
|
uint32_t addrEnd;
|
|
|
|
} rangeCache[JS_CPU_CB_RANGE_CACHE_SIZE];
|
|
|
|
};
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
HANDLE m_hThread;
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
CriticalSection m_CmdQueueCS;
|
|
|
|
JSSysCommandQueue m_CmdQueue;
|
|
|
|
HANDLE m_hCmdEvent;
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
CriticalSection m_InstancesCS;
|
|
|
|
JSInstanceMap m_Instances;
|
2021-10-11 03:08:48 +00:00
|
|
|
JSAppCallbackList m_AppCallbackHooks[JS_NUM_APP_HOOKS];
|
2021-09-03 05:11:04 +00:00
|
|
|
JSAppCallbackID m_NextAppCallbackId;
|
2021-10-11 03:08:48 +00:00
|
|
|
|
|
|
|
std::vector<JSQueuedCallbackRemove> m_CbRemoveQueue;
|
|
|
|
std::vector<JSQueuedCallbackAdd> m_CbAddQueue;
|
|
|
|
|
|
|
|
volatile size_t m_AppCallbackCount;
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-10-11 03:08:48 +00:00
|
|
|
volatile JSCpuCbListInfo m_CpuExecCbInfo;
|
|
|
|
volatile JSCpuCbListInfo m_CpuReadCbInfo;
|
|
|
|
volatile JSCpuCbListInfo m_CpuWriteCbInfo;
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
CriticalSection m_UIStateCS;
|
|
|
|
JSInstanceStatusMap m_UIInstanceStatus;
|
|
|
|
stdstr m_UILog;
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
CDebuggerUI* m_Debugger;
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
std::set<std::string> m_AutorunList;
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
stdstr m_InstallDirFullPath;
|
|
|
|
stdstr m_ScriptsDirFullPath;
|
|
|
|
stdstr m_ModulesDirFullPath;
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
public:
|
|
|
|
CScriptSystem(CDebuggerUI* debugger);
|
|
|
|
~CScriptSystem();
|
|
|
|
|
|
|
|
CDebuggerUI* Debugger();
|
|
|
|
|
|
|
|
void StartScript(const char* instanceName, const char* path);
|
|
|
|
void StopScript(const char* instanceName);
|
|
|
|
void Input(const char* instanceName, const char* code);
|
|
|
|
|
|
|
|
stdstr InstallDirPath();
|
|
|
|
stdstr ScriptsDirPath();
|
|
|
|
stdstr ModulesDirPath();
|
|
|
|
|
|
|
|
JSInstanceStatus GetStatus(const char* instanceName);
|
|
|
|
void NotifyStatus(const char* instanceName, JSInstanceStatus status);
|
|
|
|
void ConsoleLog(const char* format, ...);
|
|
|
|
void ConsolePrint(const char* format, ...);
|
|
|
|
void ConsoleClear();
|
|
|
|
stdstr GetConsoleBuffer();
|
|
|
|
|
|
|
|
void PostCMethodCall(const char* instanceName, void* dukThisHeapPtr, duk_c_function func,
|
|
|
|
JSDukArgSetupFunc argSetupFunc = nullptr,
|
|
|
|
void* argSetupParam = nullptr,
|
|
|
|
size_t argSetupParamSize = 0);
|
|
|
|
|
|
|
|
bool HaveAppCallbacks(JSAppHookID hookId);
|
2021-10-11 03:08:48 +00:00
|
|
|
|
|
|
|
// Note: Unguarded for speed, shouldn't matter
|
|
|
|
inline bool HaveAppCallbacks() { return m_AppCallbackCount != 0; }
|
|
|
|
|
|
|
|
inline bool HaveCpuExecCallbacks(uint32_t address)
|
|
|
|
{
|
|
|
|
return HaveCpuCallbacks(m_CpuExecCbInfo, m_AppCallbackHooks[JS_HOOK_CPU_EXEC], address);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool HaveCpuReadCallbacks(uint32_t address)
|
|
|
|
{
|
|
|
|
return HaveCpuCallbacks(m_CpuReadCbInfo, m_AppCallbackHooks[JS_HOOK_CPU_READ], address);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool HaveCpuWriteCallbacks(uint32_t address)
|
|
|
|
{
|
|
|
|
return HaveCpuCallbacks(m_CpuWriteCbInfo, m_AppCallbackHooks[JS_HOOK_CPU_WRITE], address);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateCpuCbListInfo(volatile JSCpuCbListInfo& info, JSAppCallbackList& callbacks);
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
void DoMouseEvent(JSAppHookID hookId, int x, int y, DWORD uMsg = (DWORD)-1);
|
2021-10-11 03:08:48 +00:00
|
|
|
|
|
|
|
JSAppCallbackID QueueAddAppCallback(JSAppHookID hookId, JSAppCallback callback);
|
|
|
|
void QueueRemoveAppCallback(JSAppHookID hookId, JSAppCallbackID callbackId);
|
|
|
|
void InvokeAppCallbacks(JSAppHookID hookId, void* env = nullptr);
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
void ExecAutorunList();
|
|
|
|
std::set<std::string>& AutorunList();
|
|
|
|
void LoadAutorunList();
|
|
|
|
void SaveAutorunList();
|
2021-10-11 03:08:48 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
private:
|
2021-10-11 03:08:48 +00:00
|
|
|
inline bool HaveCpuCallbacks(volatile JSCpuCbListInfo& info, JSAppCallbackList& callbacks, uint32_t address)
|
|
|
|
{
|
|
|
|
if (info.numCallbacks == 0 || address < info.minAddrStart || address > info.maxAddrEnd)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!info.bRangeCacheExceeded)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < info.numRangeCacheEntries; i++)
|
|
|
|
{
|
|
|
|
if (address >= info.rangeCache[i].addrStart &&
|
|
|
|
address <= info.rangeCache[i].addrEnd)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGuard guard(m_InstancesCS);
|
|
|
|
|
|
|
|
for (JSAppCallback& callback : callbacks)
|
|
|
|
{
|
|
|
|
if (address >= callback.m_Params.addrStart &&
|
|
|
|
address <= callback.m_Params.addrEnd)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
void InitDirectories();
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
void PostCommand(JSSysCommandID id, stdstr paramA = "", stdstr paramB = "", void* paramC = nullptr);
|
2019-04-29 06:28:12 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
static DWORD WINAPI ThreadProc(void* _this);
|
|
|
|
void ThreadProc();
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
bool ProcessCommand(JSSysCommand& cmd);
|
|
|
|
bool ProcessCommandQueue(std::vector<JSSysCommand>& queue);
|
|
|
|
void PullCommands(JSSysCommandID id, std::vector<JSSysCommand>& out);
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
void OnStartScript(const char* name, const char* path);
|
|
|
|
void OnStopScript(const char* name);
|
|
|
|
void OnInput(const char* name, const char* code);
|
|
|
|
void OnCMethodCall(const char* name, JSSysCMethodCall* methodCall);
|
|
|
|
void OnSweep(bool bIfDone);
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
bool RawRemoveInstance(const char* key);
|
2021-10-11 03:08:48 +00:00
|
|
|
|
|
|
|
void RawAddAppCallback(JSAppHookID hookId, JSAppCallback& callback);
|
|
|
|
void RawRemoveAppCallback(JSAppHookID hookId, JSAppCallbackID callbackId);
|
|
|
|
void RefreshCallbackMaps();
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
static stdstr FixStringReturns(const char* str);
|
2021-03-18 03:39:02 +00:00
|
|
|
};
|