summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/AssetManager.cpp42
-rw-r--r--src/AssetManager.hpp1
-rw-r--r--src/Plugin.cpp81
-rw-r--r--src/Plugin.hpp11
-rw-r--r--src/Render.cpp17
5 files changed, 113 insertions, 39 deletions
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index 361e2ef..66e7767 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -11,9 +11,9 @@
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_PNG
#include <stb_image.h>
-#include <sol.hpp>
#include "Utility.hpp"
+#include "Plugin.hpp"
namespace fs = std::experimental::filesystem::v1;
@@ -25,7 +25,6 @@ std::map<BlockId, std::string> blockIdToBlockName;
std::unique_ptr<AssetTreeNode> assetTree;
std::unique_ptr<TextureAtlas> atlas;
std::map<BlockId, BlockFaces> blockIdToBlockFaces;
-sol::state lua;
void LoadIds();
void LoadAssets();
@@ -62,6 +61,8 @@ void AssetManager::InitAssetManager()
LoadIds();
ParseBlockModels();
+
+ PluginSystem::Init();
LoadScripts();
}
@@ -102,30 +103,6 @@ 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...";
- std::vector<std::string> loadedScripts;
- std::vector<std::string> failedScripts;
-
AssetTreeNode *node = AssetManager::GetAssetByAssetName("/");
for (auto &it : node->childs) {
for (auto &child : it->childs) {
@@ -137,21 +114,12 @@ void LoadScripts() {
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);
+ PluginSystem::Execute(asset->code);
}
}
}
}
-
- LOG(INFO) << "Lua loaded: " << loadedScripts.size() << " failed: " << failedScripts.size();
+ LOG(INFO) << "Scripts loaded";
}
void WalkDirEntry(const fs::directory_entry &dirEntry, AssetTreeNode *node) {
diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp
index 97e1d63..bf948b3 100644
--- a/src/AssetManager.hpp
+++ b/src/AssetManager.hpp
@@ -10,7 +10,6 @@
#include <glm/vec4.hpp>
#include <glm/vec3.hpp>
#include <glm/mat4x4.hpp>
-#include <sol_forward.hpp>
#include "Vector.hpp"
#include "Block.hpp"
diff --git a/src/Plugin.cpp b/src/Plugin.cpp
new file mode 100644
index 0000000..40ff82f
--- /dev/null
+++ b/src/Plugin.cpp
@@ -0,0 +1,81 @@
+#include "Plugin.hpp"
+
+#include <vector>
+
+#include <easylogging++.h>
+#include <sol.hpp>
+
+struct Plugin {
+ const std::string name;
+ const std::string displayName;
+ const std::function<void()> onLoad;
+ const std::function<void()> onUnload;
+ const std::function<void(std::string)> onChangeState;
+};
+
+
+std::vector<Plugin> plugins;
+sol::state lua;
+
+
+namespace PluginApi {
+
+ void RegisterPlugin(sol::table &self, sol::table &plugin) {
+ Plugin nativePlugin {
+ plugin["name"].get_or<std::string>("75"),
+ plugin["displayName"].get_or<std::string>(""),
+ plugin["onLoad"].get_or(std::function<void()>()),
+ plugin["onUnload"].get_or(std::function<void()>()),
+ plugin["onChangeState"].get_or(std::function<void(std::string)>())
+ };
+ plugins.push_back(nativePlugin);
+ nativePlugin.onLoad();
+
+ LOG(INFO) << "Loaded plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name);
+ }
+
+ void LogWarning(sol::table &self, std::string text) {
+ LOG(WARNING) << text;
+ }
+
+}
+
+void PluginSystem::Init()
+{
+ LOG(INFO) << "Initializing plugin system";
+ for (Plugin &plugin : plugins) {
+ if (plugin.onUnload)
+ plugin.onUnload();
+ }
+
+ plugins.clear();
+ lua = sol::state();
+ lua.open_libraries();
+
+ sol::table apiTable = lua["AC"].get_or_create<sol::table>();
+
+ apiTable["RegisterPlugin"] = PluginApi::RegisterPlugin;
+ apiTable["LogWarning"] = PluginApi::LogWarning;
+}
+
+void PluginSystem::Execute(const std::string &luaCode)
+{
+ try {
+ lua.safe_script(luaCode);
+ } catch (sol::error &e) {
+ LOG(ERROR) << e.what();
+ }
+}
+
+void PluginSystem::CallOnChangeState(std::string newState)
+{
+ for (Plugin &plugin : plugins) {
+ if (plugin.onChangeState)
+ try {
+ plugin.onChangeState(newState);
+ }
+ catch (sol::error &e) {
+ LOG(ERROR) << e.what();
+ }
+ }
+}
diff --git a/src/Plugin.hpp b/src/Plugin.hpp
new file mode 100644
index 0000000..a5f75e1
--- /dev/null
+++ b/src/Plugin.hpp
@@ -0,0 +1,11 @@
+#pragma once
+
+#include <string>
+
+namespace PluginSystem {
+ void Init();
+
+ void Execute(const std::string &luaCode);
+
+ void CallOnChangeState(std::string newState);
+} \ No newline at end of file
diff --git a/src/Render.cpp b/src/Render.cpp
index cf108e4..f86e5ec 100644
--- a/src/Render.cpp
+++ b/src/Render.cpp
@@ -14,6 +14,7 @@
#include "RendererWorld.hpp"
#include "Settings.hpp"
#include "Framebuffer.hpp"
+#include "Plugin.hpp"
Render::Render(unsigned int windowWidth, unsigned int windowHeight,
std::string windowTitle)
@@ -71,7 +72,9 @@ Render::~Render() {
Settings::WriteDouble("brightness", fieldBrightness);
Settings::WriteDouble("resolutionScale", fieldResolutionScale);
Settings::Save();
-
+
+ PluginSystem::Init();
+
framebuffer.reset();
ImGui_ImplSdlGL3_Shutdown();
SDL_GL_DeleteContext(glContext);
@@ -717,13 +720,25 @@ void Render::InitEvents() {
switch (GlobalState::GetState()) {
case State::Playing:
SetMouseCapture(true);
+ PluginSystem::CallOnChangeState("Playing");
break;
case State::InitialLoading:
+ PluginSystem::CallOnChangeState("InitialLoading");
+ break;
case State::MainMenu:
+ PluginSystem::CallOnChangeState("MainMenu");
+ break;
case State::Loading:
+ PluginSystem::CallOnChangeState("Loading");
+ break;
case State::Paused:
+ PluginSystem::CallOnChangeState("Paused");
+ break;
case State::Inventory:
+ PluginSystem::CallOnChangeState("Inventory");
+ break;
case State::Chat:
+ PluginSystem::CallOnChangeState("Chat");
SetMouseCapture(false);
break;
}