diff --git a/code/framework/src/scripting/engines/engine.h b/code/framework/src/scripting/engines/engine.h index 0abf5a4c9..8aa491334 100644 --- a/code/framework/src/scripting/engines/engine.h +++ b/code/framework/src/scripting/engines/engine.h @@ -21,6 +21,7 @@ namespace Framework::Scripting::Engines { virtual EngineError Shutdown() = 0; virtual void Update() = 0; + virtual bool PreloadGamemode(std::string) = 0; virtual bool LoadGamemode(std::string) = 0; virtual bool UnloadGamemode(std::string) = 0; diff --git a/code/framework/src/scripting/engines/node/engine.cpp b/code/framework/src/scripting/engines/node/engine.cpp index bcddae0c3..457b13d1f 100644 --- a/code/framework/src/scripting/engines/node/engine.cpp +++ b/code/framework/src/scripting/engines/node/engine.cpp @@ -122,13 +122,13 @@ namespace Framework::Scripting::Engines::Node { _nextFileWatchUpdate = Utils::Time::Add(Utils::Time::GetTimePoint(), _fileWatchUpdatePeriod); } - // Notify the resource + // Notify the gamemode, if loaded if(_gamemodeLoaded){ InvokeEvent(Events[EventIDs::RESOURCE_UPDATED]); } } - bool Engine::LoadGamemode(std::string mainPath) { + bool Engine::LoadGamemodePackageFile(std::string mainPath){ // If gamemmode is already loaded, don't load it again if(_gamemodeLoaded) { return false; @@ -164,11 +164,28 @@ namespace Framework::Scripting::Engines::Node { return false; } } + return true; } catch (nlohmann::detail::type_error &err) { Logging::GetLogger(FRAMEWORK_INNER_SCRIPTING)->error("The gamemode package.json is not valid:\n\t{}", err.what()); return false; } + return true; + } + + bool Engine::PreloadGamemode(std::string mainPath){ + if(LoadGamemodePackageFile(mainPath)){ + if(LoadGamemode(mainPath)){ + return WatchGamemodeChanges(mainPath); + } + } + return false; + } + + bool Engine::LoadGamemode(std::string mainPath) { + if(_gamemodeLoaded){ + return false; + } // Make sure the specified entrypoint is valid cppfs::FileHandle entryPointFile = cppfs::fs::open(mainPath + "/" + _gamemodeMetadata.entrypoint); @@ -211,12 +228,12 @@ namespace Framework::Scripting::Engines::Node { node::LoadEnvironment(_gamemodeEnvironment, bootstrap_code); // Compile the gamemode - CompileGamemode(content, entryPointFile.path()); - RunGamemode(); + CompileGamemodeScript(content, entryPointFile.path()); + RunGamemodeScript(); // Invoke the gamemode loaded event InvokeEvent(Events[EventIDs::RESOURCE_LOADED]); - + _gamemodeLoaded = true; return true; } @@ -251,7 +268,7 @@ namespace Framework::Scripting::Engines::Node { return true; } - bool Engine::CompileGamemode(const std::string &str, const std::string &path) { + bool Engine::CompileGamemodeScript(const std::string &str, const std::string &path) { v8::Isolate::Scope isolateScope(_isolate); v8::HandleScope handleScope(_isolate); @@ -279,7 +296,7 @@ namespace Framework::Scripting::Engines::Node { return true; } - bool Engine::RunGamemode() { + bool Engine::RunGamemodeScript() { if (_gamemodeScript.IsEmpty()) { Logging::GetLogger(FRAMEWORK_INNER_SCRIPTING)->debug("Invalid gamemode script object"); return false; @@ -297,25 +314,23 @@ namespace Framework::Scripting::Engines::Node { return true; } - bool Engine::WatchGamemodeChanges() { - /*cppfs::FileHandle dir = cppfs::fs::open(_path); + bool Engine::WatchGamemodeChanges(std::string path) { + cppfs::FileHandle dir = cppfs::fs::open(path); if (!dir.isDirectory()) { return false; } Logging::GetLogger(FRAMEWORK_INNER_SCRIPTING)->debug("Watching '{}' changes", dir.path().c_str()); _watcher.add(dir, cppfs::FileCreated | cppfs::FileRemoved | cppfs::FileModified | cppfs::FileAttrChanged, cppfs::Recursive); - _watcher.addHandler([this](cppfs::FileHandle &fh, cppfs::FileEvent ev) { - Logging::GetLogger(FRAMEWORK_INNER_SCRIPTING)->debug("Resource '{}' is reloaded due to the file changes", _name); + _watcher.addHandler([this, path](cppfs::FileHandle &fh, cppfs::FileEvent ev) { + Logging::GetLogger(FRAMEWORK_INNER_SCRIPTING)->debug("Gamemode is reloaded due to the file changes"); // Close the resource first, we'll start with a clean slate - Shutdown(); - - if (LoadPackageFile()) { - Init(); + if(this->IsGamemodeLoaded() && UnloadGamemode(path)){ + LoadGamemode(path); } }); _nextFileWatchUpdate = Utils::Time::Add(Utils::Time::GetTimePoint(), _fileWatchUpdatePeriod); - return true;*/ + return true; } } // namespace Framework::Scripting::Engines::Node diff --git a/code/framework/src/scripting/engines/node/engine.h b/code/framework/src/scripting/engines/node/engine.h index 45b77a644..264d7c199 100644 --- a/code/framework/src/scripting/engines/node/engine.h +++ b/code/framework/src/scripting/engines/node/engine.h @@ -75,11 +75,14 @@ namespace Framework::Scripting::Engines::Node { EngineError Shutdown() override; void Update() override; + bool PreloadGamemode(std::string) override; bool LoadGamemode(std::string) override; bool UnloadGamemode(std::string) override; - bool CompileGamemode(const std::string &, const std::string &); - bool RunGamemode(); - bool WatchGamemodeChanges(); + + bool LoadGamemodePackageFile(std::string); + bool CompileGamemodeScript(const std::string &, const std::string &); + bool RunGamemodeScript(); + bool WatchGamemodeChanges(std::string); template void InvokeEvent(const std::string name, Args &&...args) { @@ -134,6 +137,10 @@ namespace Framework::Scripting::Engines::Node { return _context.Get(_isolate); } + bool IsGamemodeLoaded() const { + return _gamemodeLoaded; + } + void SetProcessArguments(int argc, char **argv) override {} void SetModName(std::string name) override { diff --git a/code/framework/src/scripting/module.cpp b/code/framework/src/scripting/module.cpp index dd314bf62..d4bc7f078 100644 --- a/code/framework/src/scripting/module.cpp +++ b/code/framework/src/scripting/module.cpp @@ -80,7 +80,7 @@ namespace Framework::Scripting { return false; } - return _engine->LoadGamemode("gamemode"); + return _engine->PreloadGamemode("gamemode"); } bool Module::UnloadGamemode(){