2019-05-12 21:18:05 -04:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "ScriptManager.h"
|
|
|
|
#include "ScriptHost.h"
|
|
|
|
#include "DebugBreakHelper.h"
|
|
|
|
#include "Debugger.h"
|
|
|
|
#include "CpuTypes.h"
|
|
|
|
#include "Console.h"
|
|
|
|
#include "DebugHud.h"
|
|
|
|
|
|
|
|
ScriptManager::ScriptManager(Debugger* debugger)
|
|
|
|
{
|
|
|
|
_debugger = debugger;
|
|
|
|
_hasScript = false;
|
|
|
|
_nextScriptId = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ScriptManager::LoadScript(string name, string content, int32_t scriptId)
|
|
|
|
{
|
|
|
|
DebugBreakHelper helper(_debugger);
|
|
|
|
auto lock = _scriptLock.AcquireSafe();
|
|
|
|
|
2021-03-10 11:13:28 -05:00
|
|
|
if(scriptId < 0) {
|
2019-05-12 21:18:05 -04:00
|
|
|
shared_ptr<ScriptHost> script(new ScriptHost(_nextScriptId++));
|
|
|
|
script->LoadScript(name, content, _debugger);
|
|
|
|
_scripts.push_back(script);
|
|
|
|
_hasScript = true;
|
|
|
|
return script->GetScriptId();
|
2021-03-10 11:13:28 -05:00
|
|
|
} else {
|
|
|
|
auto result = std::find_if(_scripts.begin(), _scripts.end(), [=](shared_ptr<ScriptHost> &script) {
|
2019-05-12 21:18:05 -04:00
|
|
|
return script->GetScriptId() == scriptId;
|
|
|
|
});
|
2021-03-10 11:13:28 -05:00
|
|
|
if(result != _scripts.end()) {
|
2019-05-12 21:18:05 -04:00
|
|
|
//Send a ScriptEnded event before reloading the code
|
|
|
|
(*result)->ProcessEvent(EventType::ScriptEnded);
|
|
|
|
|
|
|
|
(*result)->LoadScript(name, content, _debugger);
|
|
|
|
return scriptId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptManager::RemoveScript(int32_t scriptId)
|
|
|
|
{
|
|
|
|
DebugBreakHelper helper(_debugger);
|
|
|
|
auto lock = _scriptLock.AcquireSafe();
|
2021-03-10 11:13:28 -05:00
|
|
|
_scripts.erase(std::remove_if(_scripts.begin(), _scripts.end(), [=](const shared_ptr<ScriptHost>& script) {
|
|
|
|
if(script->GetScriptId() == scriptId) {
|
2019-05-12 21:18:05 -04:00
|
|
|
//Send a ScriptEnded event before unloading the script
|
|
|
|
script->ProcessEvent(EventType::ScriptEnded);
|
|
|
|
_debugger->GetConsole()->GetDebugHud()->ClearScreen();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}), _scripts.end());
|
|
|
|
_hasScript = _scripts.size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* ScriptManager::GetScriptLog(int32_t scriptId)
|
|
|
|
{
|
|
|
|
auto lock = _scriptLock.AcquireSafe();
|
2021-03-10 11:13:28 -05:00
|
|
|
for(shared_ptr<ScriptHost> &script : _scripts) {
|
|
|
|
if(script->GetScriptId() == scriptId) {
|
2019-05-12 21:18:05 -04:00
|
|
|
return script->GetLog();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptManager::ProcessEvent(EventType type)
|
|
|
|
{
|
2021-03-10 11:13:28 -05:00
|
|
|
if(_hasScript) {
|
|
|
|
for(shared_ptr<ScriptHost> &script : _scripts) {
|
2019-05-12 21:18:05 -04:00
|
|
|
script->ProcessEvent(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 11:13:28 -05:00
|
|
|
void ScriptManager::ProcessMemoryOperation(uint32_t address, uint8_t &value, MemoryOperationType type, CpuType cpuType)
|
2019-05-12 21:18:05 -04:00
|
|
|
{
|
2021-03-10 11:13:28 -05:00
|
|
|
if(_hasScript) {
|
|
|
|
for(shared_ptr<ScriptHost> &script : _scripts) {
|
2020-05-18 16:10:53 -04:00
|
|
|
script->ProcessMemoryOperation(address, value, type, cpuType);
|
2019-05-12 21:18:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|