Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/scripting
diff options
context:
space:
mode:
Diffstat (limited to 'source/game/scripting')
-rw-r--r--source/game/scripting/StarLuaRoot.cpp76
-rw-r--r--source/game/scripting/StarLuaRoot.hpp8
2 files changed, 59 insertions, 25 deletions
diff --git a/source/game/scripting/StarLuaRoot.cpp b/source/game/scripting/StarLuaRoot.cpp
index 4d6e8cf..d41e8fc 100644
--- a/source/game/scripting/StarLuaRoot.cpp
+++ b/source/game/scripting/StarLuaRoot.cpp
@@ -5,16 +5,10 @@ namespace Star {
LuaRoot::LuaRoot() {
auto& root = Root::singleton();
-
- m_luaEngine = LuaEngine::create(root.configuration()->get("safeScripts").toBool());
-
- m_luaEngine->setRecursionLimit(root.configuration()->get("scriptRecursionLimit").toUInt());
- m_luaEngine->setInstructionLimit(root.configuration()->get("scriptInstructionLimit").toUInt());
- m_luaEngine->setProfilingEnabled(root.configuration()->get("scriptProfilingEnabled").toBool());
- m_luaEngine->setInstructionMeasureInterval(root.configuration()->get("scriptInstructionMeasureInterval").toUInt());
-
m_scriptCache = make_shared<ScriptCache>();
+ restart();
+
m_rootReloadListener = make_shared<CallbackListener>([cache = m_scriptCache]() {
cache->clear();
});
@@ -24,6 +18,40 @@ LuaRoot::LuaRoot() {
}
LuaRoot::~LuaRoot() {
+ shutdown();
+}
+
+void LuaRoot::loadScript(String const& assetPath) {
+ m_scriptCache->loadScript(*m_luaEngine, assetPath);
+}
+
+bool LuaRoot::scriptLoaded(String const& assetPath) const {
+ return m_scriptCache->scriptLoaded(assetPath);
+}
+
+void LuaRoot::unloadScript(String const& assetPath) {
+ m_scriptCache->unloadScript(assetPath);
+}
+
+void LuaRoot::restart() {
+ shutdown();
+
+ auto& root = Root::singleton();
+
+ m_luaEngine = LuaEngine::create(root.configuration()->get("safeScripts").toBool());
+
+ m_luaEngine->setRecursionLimit(root.configuration()->get("scriptRecursionLimit").toUInt());
+ m_luaEngine->setInstructionLimit(root.configuration()->get("scriptInstructionLimit").toUInt());
+ m_luaEngine->setProfilingEnabled(root.configuration()->get("scriptProfilingEnabled").toBool());
+ m_luaEngine->setInstructionMeasureInterval(root.configuration()->get("scriptInstructionMeasureInterval").toUInt());
+}
+
+void LuaRoot::shutdown() {
+ clearScriptCache();
+
+ if (!m_luaEngine)
+ return;
+
auto profile = m_luaEngine->getProfile();
if (!profile.empty()) {
profile.sort([](auto const& a, auto const& b) {
@@ -56,18 +84,8 @@ LuaRoot::~LuaRoot() {
Logger::info("Writing lua profile {}", filename);
File::writeFile(profileSummary, path);
}
-}
-void LuaRoot::loadScript(String const& assetPath) {
- m_scriptCache->loadScript(*m_luaEngine, assetPath);
-}
-
-bool LuaRoot::scriptLoaded(String const& assetPath) const {
- return m_scriptCache->scriptLoaded(assetPath);
-}
-
-void LuaRoot::unloadScript(String const& assetPath) {
- m_scriptCache->unloadScript(assetPath);
+ m_luaEngine.reset();
}
LuaContext LuaRoot::createContext(String const& script) {
@@ -91,33 +109,43 @@ LuaContext LuaRoot::createContext(StringList const& scriptPaths) {
for (auto const& scriptPath : scriptPaths)
cache->loadContextScript(newContext, scriptPath);
+ for (auto const& callbackPair : m_luaCallbacks)
+ newContext.setCallbacks(callbackPair.first, callbackPair.second);
+
return newContext;
}
void LuaRoot::collectGarbage(Maybe<unsigned> steps) {
- m_luaEngine->collectGarbage(steps);
+ if (m_luaEngine)
+ m_luaEngine->collectGarbage(steps);
}
void LuaRoot::setAutoGarbageCollection(bool autoGarbageColleciton) {
- m_luaEngine->setAutoGarbageCollection(autoGarbageColleciton);
+ if (m_luaEngine)
+ m_luaEngine->setAutoGarbageCollection(autoGarbageColleciton);
}
void LuaRoot::tuneAutoGarbageCollection(float pause, float stepMultiplier) {
- m_luaEngine->tuneAutoGarbageCollection(pause, stepMultiplier);
+ if (m_luaEngine)
+ m_luaEngine->tuneAutoGarbageCollection(pause, stepMultiplier);
}
size_t LuaRoot::luaMemoryUsage() const {
- return m_luaEngine->memoryUsage();
+ return m_luaEngine ? m_luaEngine->memoryUsage() : 0;
}
size_t LuaRoot::scriptCacheMemoryUsage() const {
- return m_scriptCache->memoryUsage();
+ return m_luaEngine ? m_scriptCache->memoryUsage() : 0;
}
void LuaRoot::clearScriptCache() const {
return m_scriptCache->clear();
}
+void LuaRoot::addCallbacks(String const& groupName, LuaCallbacks const& callbacks) {
+ m_luaCallbacks[groupName] = callbacks;
+}
+
LuaEngine& LuaRoot::luaEngine() const {
return *m_luaEngine;
}
diff --git a/source/game/scripting/StarLuaRoot.hpp b/source/game/scripting/StarLuaRoot.hpp
index 7335952..a26cf04 100644
--- a/source/game/scripting/StarLuaRoot.hpp
+++ b/source/game/scripting/StarLuaRoot.hpp
@@ -21,6 +21,9 @@ public:
bool scriptLoaded(String const& assetPath) const;
void unloadScript(String const& assetPath);
+ void restart();
+ void shutdown();
+
// A script context can be created from the combination of several scripts,
// the functions / data in each script will be loaded in order, so that later
// specified scripts will overwrite previous ones.
@@ -39,8 +42,9 @@ public:
size_t scriptCacheMemoryUsage() const;
void clearScriptCache() const;
- LuaEngine& luaEngine() const;
+ void addCallbacks(String const& groupName, LuaCallbacks const& callbacks);
+ LuaEngine& luaEngine() const;
private:
class ScriptCache {
public:
@@ -57,7 +61,9 @@ private:
};
LuaEnginePtr m_luaEngine;
+ StringMap<LuaCallbacks> m_luaCallbacks;
shared_ptr<ScriptCache> m_scriptCache;
+
ListenerPtr m_rootReloadListener;
String m_storageDirectory;