summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2019-02-17 09:11:08 +0100
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2019-04-21 12:55:48 +0200
commit404b04570452fe24676b0294fdd77878806a904b (patch)
treeb4f6e8293239641874d835675ef7291edd81b041
parentAdded lua-support (diff)
downloadAltCraft-404b04570452fe24676b0294fdd77878806a904b.tar
AltCraft-404b04570452fe24676b0294fdd77878806a904b.tar.gz
AltCraft-404b04570452fe24676b0294fdd77878806a904b.tar.bz2
AltCraft-404b04570452fe24676b0294fdd77878806a904b.tar.lz
AltCraft-404b04570452fe24676b0294fdd77878806a904b.tar.xz
AltCraft-404b04570452fe24676b0294fdd77878806a904b.tar.zst
AltCraft-404b04570452fe24676b0294fdd77878806a904b.zip
-rw-r--r--cwd/assets/altcraft/init.lua1
-rw-r--r--cwd/assets/altcraft/scripts/init.lua12
-rw-r--r--src/AssetManager.cpp53
3 files changed, 49 insertions, 17 deletions
diff --git a/cwd/assets/altcraft/init.lua b/cwd/assets/altcraft/init.lua
deleted file mode 100644
index a3ea3c6..0000000
--- a/cwd/assets/altcraft/init.lua
+++ /dev/null
@@ -1 +0,0 @@
-print("Hello") \ No newline at end of file
diff --git a/cwd/assets/altcraft/scripts/init.lua b/cwd/assets/altcraft/scripts/init.lua
new file mode 100644
index 0000000..d48ecd0
--- /dev/null
+++ b/cwd/assets/altcraft/scripts/init.lua
@@ -0,0 +1,12 @@
+plug = {
+ name = 'altcraft',
+ displayName = "AltCraft Core Plugin",
+ onLoad = nil,
+ onUnload = nil,
+}
+
+function plug:onLoad ()
+ print("Loaded "..self.name.."-plugin!")
+end
+
+AC:RegisterPlugin(plug) \ No newline at end of file
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index 1ae9929..361e2ef 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -103,34 +103,55 @@ void LoadTextures() {
void LoadScripts() {
lua.open_libraries(sol::lib::base, sol::lib::table);
+ lua["AC"] = lua.create_table();
+ lua["plugins"] = lua.create_table();
+ lua["AC"]["RegisterPlugin"].set_function([&](sol::table &self, sol::table &plugin) {
+ std::string pluginName;
+ try {
+ pluginName = plugin["name"];
+ lua["plugins"][pluginName] = plugin;
+ LOG(INFO) << "Loading plugin " << (lua["plugins"][pluginName]["displayName"] ? lua["plugins"][pluginName]["displayName"] : pluginName);
+ if (lua["plugins"][pluginName]["onLoad"])
+ lua["plugins"][pluginName]["onLoad"].call(lua["plugins"][pluginName]);
+ } catch (sol::error &e) {
+ if (pluginName.empty())
+ return;
+
+ LOG(ERROR) << "Plugin " << pluginName << " loading failed: " << e.what();
+ lua["plugins"][pluginName] = sol::lua_nil;
+ }
+ });
- LOG(INFO) << "Loading lua-init-scripts";
+ LOG(INFO) << "Loading Lua...";
std::vector<std::string> loadedScripts;
std::vector<std::string> failedScripts;
AssetTreeNode *node = AssetManager::GetAssetByAssetName("/");
for (auto &it : node->childs) {
for (auto &child : it->childs) {
- if (child->name == "init") {
- AssetScript *asset = dynamic_cast<AssetScript *>(child->asset.get());
- if (!asset) {
- LOG(ERROR) << "Unrecognised script file /" << it->name;
- continue;
- }
- try {
- lua.script(asset->code);
- }
- catch (sol::error &e) {
- LOG(ERROR) << "LUA init-script " << child->name << " failed: " << e.what();
- failedScripts.push_back(it->name);
- continue;
+ if (child->name == "scripts") {
+ for (auto &script : child->childs)
+ {
+ AssetScript *asset = dynamic_cast<AssetScript *>(script->asset.get());
+ if (!asset) {
+ LOG(ERROR) << "Unrecognised script file /" << it->name;
+ continue;
+ }
+ try {
+ lua.script(asset->code);
+ }
+ catch (sol::error &e) {
+ LOG(ERROR) << "LUA script " << it->name << "/" << child->name << "/" << script->name << " parsing failed: " << e.what();
+ failedScripts.push_back(it->name);
+ continue;
+ }
+ loadedScripts.push_back(it->name);
}
- loadedScripts.push_back(it->name);
}
}
}
- LOG(INFO) << "Lua loaded: " << loadedScripts.size() << " failed: " << failedScripts.size();
+ LOG(INFO) << "Lua loaded: " << loadedScripts.size() << " failed: " << failedScripts.size();
}
void WalkDirEntry(const fs::directory_entry &dirEntry, AssetTreeNode *node) {