From 404b04570452fe24676b0294fdd77878806a904b Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 17 Feb 2019 13:11:08 +0500 Subject: Minor lua-api improvement --- src/AssetManager.cpp | 53 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 16 deletions(-) (limited to 'src') 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 loadedScripts; std::vector failedScripts; AssetTreeNode *node = AssetManager::GetAssetByAssetName("/"); for (auto &it : node->childs) { for (auto &child : it->childs) { - if (child->name == "init") { - AssetScript *asset = dynamic_cast(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(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) { -- cgit v1.2.3