mirror of
https://github.com/antopilo/Nuake.git
synced 2026-09-04 11:35:06 +03:00
Can now have multiple instances of the same script without compile errors
This commit is contained in:
@@ -12,6 +12,7 @@ WrenVM* ScriptingEngine::m_WrenVM;
|
||||
|
||||
std::map<std::string, Ref<WrenScript>> ScriptingEngine::m_Scripts;
|
||||
std::map<std::string, Ref<ScriptModule>> ScriptingEngine::Modules;
|
||||
std::vector<std::string> ScriptingEngine::m_LoadedScripts;
|
||||
|
||||
void errorFn(WrenVM* vm, WrenErrorType errorType,
|
||||
const char* module, const int line,
|
||||
@@ -88,18 +89,44 @@ WrenLoadModuleResult myLoadModule(WrenVM* vm, const char* name)
|
||||
|
||||
Ref<WrenScript> ScriptingEngine::RegisterScript(const std::string& path, const std::string& mod)
|
||||
{
|
||||
std::string query = "import \"" + path + "\" for " + mod;
|
||||
wrenInterpret(m_WrenVM, "main", query.c_str());
|
||||
// Check if scripts has already been loaded.
|
||||
// You can't import the same module twice, otherwise, compile error.
|
||||
if (m_Scripts.find(path) == m_Scripts.end())
|
||||
{
|
||||
std::string query = "import \"" + path + "\" for " + mod;
|
||||
wrenInterpret(m_WrenVM, "main", query.c_str());
|
||||
}
|
||||
|
||||
return CreateRef<WrenScript>(path, mod);
|
||||
}
|
||||
|
||||
// Useful to check if a script has been imported, importing a script
|
||||
// twice gives a compile error.
|
||||
bool ScriptingEngine::IsScriptImported(const std::string& path)
|
||||
{
|
||||
for (auto& script : m_LoadedScripts)
|
||||
{
|
||||
if (script == path)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ScriptingEngine::ImportScript(const std::string& path)
|
||||
{
|
||||
if (IsScriptImported(path))
|
||||
return;
|
||||
m_LoadedScripts.push_back(path);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ScriptingEngine::RegisterModule(Ref<ScriptModule> scriptModule)
|
||||
{
|
||||
Modules[scriptModule->GetModuleName()] = scriptModule;
|
||||
scriptModule->RegisterModule(m_WrenVM);
|
||||
}
|
||||
|
||||
|
||||
void ScriptingEngine::InitScript(Ref<WrenScript> script)
|
||||
{
|
||||
script->CallInit();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "src/Core/Core.h"
|
||||
@@ -12,12 +13,15 @@ class ScriptingEngine
|
||||
{
|
||||
private:
|
||||
static WrenVM* m_WrenVM;
|
||||
|
||||
static std::vector<std::string> m_LoadedScripts;
|
||||
static std::map<std::string, Ref<WrenScript>> m_Scripts;
|
||||
static std::map<std::string, Ref<ScriptModule>> Modules;
|
||||
public:
|
||||
static Ref<WrenScript> RegisterScript(const std::string& path, const std::string& mod);
|
||||
static bool IsScriptImported(const std::string& path);
|
||||
static void ImportScript(const std::string& path);
|
||||
static void RegisterModule(Ref<ScriptModule> module);
|
||||
static void RegisterMethod(void* method, const std::string& module, const std::string& signature);
|
||||
static void InitScript(Ref<WrenScript> script);
|
||||
static void UpdateScript(Ref<WrenScript> script, Timestep timestep);
|
||||
static void ExitScript(Ref<WrenScript> script);
|
||||
@@ -25,6 +29,8 @@ public:
|
||||
static void RunCode(const std::string& code);
|
||||
static void Close();
|
||||
|
||||
|
||||
|
||||
static WrenForeignMethodFn bindForeignMethod(
|
||||
WrenVM* vm,
|
||||
const char* module,
|
||||
|
||||
@@ -6,18 +6,22 @@ WrenScript::WrenScript(const std::string& path, const std::string& mod, bool isE
|
||||
{
|
||||
WrenVM* vm = ScriptingEngine::GetWrenVM();
|
||||
|
||||
// Import statement
|
||||
std::string source = "import \"" + path + "\" for " + mod;
|
||||
|
||||
CompiledSuccesfully = true;
|
||||
|
||||
// Import file as module
|
||||
WrenInterpretResult result = wrenInterpret(vm, "main", source.c_str());
|
||||
if (result != WREN_RESULT_SUCCESS)
|
||||
CompiledSuccesfully = false;
|
||||
// Can't import twice the same script, otherwise gives a compile error.
|
||||
if (!ScriptingEngine::IsScriptImported(path))
|
||||
{
|
||||
std::string source = "import \"" + path + "\" for " + mod;
|
||||
WrenInterpretResult result = wrenInterpret(vm, "main", source.c_str());
|
||||
|
||||
if (!CompiledSuccesfully)
|
||||
return;
|
||||
if (result != WREN_RESULT_SUCCESS)
|
||||
CompiledSuccesfully = false;
|
||||
|
||||
if (!CompiledSuccesfully)
|
||||
return;
|
||||
|
||||
ScriptingEngine::ImportScript(path);
|
||||
}
|
||||
|
||||
// Get handle to class
|
||||
wrenEnsureSlots(vm, 1);
|
||||
|
||||
Reference in New Issue
Block a user