Skip to content

Commit

Permalink
Reinstall file watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Segfaultd committed Nov 14, 2023
1 parent a662d87 commit ce7cbe1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
1 change: 1 addition & 0 deletions code/framework/src/scripting/engines/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
47 changes: 31 additions & 16 deletions code/framework/src/scripting/engines/node/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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
13 changes: 10 additions & 3 deletions code/framework/src/scripting/engines/node/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename... Args>
void InvokeEvent(const std::string name, Args &&...args) {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion code/framework/src/scripting/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace Framework::Scripting {
return false;
}

return _engine->LoadGamemode("gamemode");
return _engine->PreloadGamemode("gamemode");
}

bool Module::UnloadGamemode(){
Expand Down

0 comments on commit ce7cbe1

Please sign in to comment.