summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2019-05-19 12:25:03 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2019-05-19 12:25:03 +0200
commit35e786c2b4632f92518c8881db650ba63beecd5c (patch)
tree831345bd105884b65349afae04dddd88b1ec4bb8
parentImplemented GameState lua-api (diff)
downloadAltCraft-35e786c2b4632f92518c8881db650ba63beecd5c.tar
AltCraft-35e786c2b4632f92518c8881db650ba63beecd5c.tar.gz
AltCraft-35e786c2b4632f92518c8881db650ba63beecd5c.tar.bz2
AltCraft-35e786c2b4632f92518c8881db650ba63beecd5c.tar.lz
AltCraft-35e786c2b4632f92518c8881db650ba63beecd5c.tar.xz
AltCraft-35e786c2b4632f92518c8881db650ba63beecd5c.tar.zst
AltCraft-35e786c2b4632f92518c8881db650ba63beecd5c.zip
-rw-r--r--cwd/assets/altcraft/scripts/blocks.lua2
-rw-r--r--cwd/assets/altcraft/scripts/init.lua3
-rw-r--r--src/AssetManager.cpp9
-rw-r--r--src/Plugin.cpp23
4 files changed, 35 insertions, 2 deletions
diff --git a/cwd/assets/altcraft/scripts/blocks.lua b/cwd/assets/altcraft/scripts/blocks.lua
new file mode 100644
index 0000000..ad00742
--- /dev/null
+++ b/cwd/assets/altcraft/scripts/blocks.lua
@@ -0,0 +1,2 @@
+print("Hello from altcraft/blocks!")
+return "CONTENT OF ac/blocks" \ No newline at end of file
diff --git a/cwd/assets/altcraft/scripts/init.lua b/cwd/assets/altcraft/scripts/init.lua
index de58b90..f001017 100644
--- a/cwd/assets/altcraft/scripts/init.lua
+++ b/cwd/assets/altcraft/scripts/init.lua
@@ -32,5 +32,8 @@ function plugin.onTick (deltaTime)
end
end
+blocks = require("altcraft/blocks")
+print("From init: "..blocks)
+
AC.RegisterPlugin(plugin)
plugin = nil \ No newline at end of file
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index eb3186a..be69dd0 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -110,6 +110,9 @@ void LoadScripts() {
if (child->name == "scripts") {
for (auto &script : child->childs)
{
+ if (script->name != "init")
+ continue;
+
AssetScript *asset = dynamic_cast<AssetScript *>(script->asset.get());
if (!asset) {
LOG(ERROR) << "Unrecognised script file /" << it->name;
@@ -118,9 +121,11 @@ void LoadScripts() {
try {
PluginSystem::Execute(asset->code, true);
}
- catch (std::exception& e) {
+ catch (std::exception & e) {
LOG(ERROR) << "Failed loading script '" << script->name << "' in '" << it->name << "'";
}
+
+ break;
}
}
}
@@ -171,7 +176,7 @@ void ParseAsset(AssetTreeNode &node) {
return;
}
- if (node.name == "init") {
+ if (node.parent->name == "scripts") {
ParseAssetScript(node);
return;
}
diff --git a/src/Plugin.cpp b/src/Plugin.cpp
index 4ef1240..6acfb08 100644
--- a/src/Plugin.cpp
+++ b/src/Plugin.cpp
@@ -9,6 +9,7 @@
#include "GameState.hpp"
#include "Game.hpp"
#include "Event.hpp"
+#include "AssetManager.hpp"
struct Plugin {
@@ -61,6 +62,24 @@ namespace PluginApi {
}
}
+int LoadFileRequire(lua_State* L) {
+ std::string path = sol::stack::get<std::string>(L);
+
+ std::string package = path.substr(0, path.find('/'));
+ std::string script = path.substr(path.find('/') + 1);
+
+ std::string scriptPath = "/" + package + "/scripts/" + script;
+
+ AssetScript *asset = AssetManager::GetAsset<AssetScript>(scriptPath);
+ if (!asset) {
+ sol::stack::push(L, "Module '" + scriptPath + "' not found");
+ return 1;
+ }
+
+ luaL_loadbuffer(L, asset->code.data(), asset->code.size(), path.c_str());
+ return 1;
+}
+
void PluginSystem::Init() {
OPTICK_EVENT();
LOG(INFO) << "Initializing plugin system";
@@ -73,6 +92,10 @@ void PluginSystem::Init() {
lua = sol::state();
lua.open_libraries();
+ lua["package"]["searchers"] = lua.create_table_with(
+ 1, LoadFileRequire
+ );
+
lua.new_usertype<Entity>("Entity",
"pos", &Entity::pos);