diff --git a/CMakeLists.txt b/CMakeLists.txt index 46760e5..c33afec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ set(LUNARIUM_SRC "src/graphics/gui/logGui.cpp" "src/graphics/gui/luaConsole.cpp" "src/scripting/scriptManager.cpp" +"src/scripting/coreAPI.cpp" "src/assets/types/image.cpp" ) diff --git a/src/core/core.cpp b/src/core/core.cpp index 1ca251a..b44596c 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace lunarium { @@ -62,7 +63,10 @@ namespace lunarium mpInstance->mState.SaveToFile("lunarium_state.xml"); // Shutdown subsystems - // LuaConsole::FreeInstance(); + CoreAPI::FreeInstance(); + ScriptManager::FreeInstance(); + + LuaConsole::FreeInstance(); LogGui::FreeInstance(); GUI::GetInstance().Shutdown(); GUI::FreeInstance(); @@ -220,6 +224,14 @@ namespace lunarium "Could not initialized the LUA script manager: %s", result.Description); } + CoreAPI& capi = CoreAPI::GetInstance(); + result = capi.Initialize(scriptMan); + if (Failed(result)) + { + Logger::Log(LogCategory::CORE, LogLevel::WARNING, + "Could not initialized the LUA Core API: %s", result.Description); + } + mbIsInit = true; } @@ -296,6 +308,8 @@ namespace lunarium if (LuaConsole::GetInstance().GetNewCommand(command)) { Logger::Log(LogCategory::CORE, LogLevel::INFO, "New LUA command: %s", command.c_str()); + OpRes result = ScriptManager::RunScript(command.c_str()); + // NOTE: Ignoring script result } } diff --git a/src/graphics/gui/luaConsole.cpp b/src/graphics/gui/luaConsole.cpp index eb41028..ff6375e 100644 --- a/src/graphics/gui/luaConsole.cpp +++ b/src/graphics/gui/luaConsole.cpp @@ -97,7 +97,7 @@ namespace lunarium ImGui::SetScrollHereY(1.0f); ImGui::End(); - if (Core::Input().IsKeyPressed(KeyCode::RETURN) && strlen(mBuffer) > 1) + if (Core::Input().IsKeyPressed(KeyCode::RETURN, true) && strlen(mBuffer) > 1) { mCommandHistory.push_back(mBuffer); memset(mBuffer, 0, LUA_CON_BUFFER_SIZE); diff --git a/src/scripting/coreAPI.cpp b/src/scripting/coreAPI.cpp new file mode 100644 index 0000000..46a267d --- /dev/null +++ b/src/scripting/coreAPI.cpp @@ -0,0 +1,61 @@ +/****************************************************************************** +* File - coreAPI.cpp +* Author - Joey Pollack +* Date - 2021/09/23 (y/m/d) +* Mod Date - 2021/09/23 (y/m/d) +* Description - class with all static methods that expose core functionality to LUA +* Need an init function that takes a reference to a LUA state so that it +* can expose the static functions to LUA. +******************************************************************************/ + +#include "coreAPI.h" +#include "scriptManager.h" +#include +#include + +namespace lunarium +{ + + CoreAPI* CoreAPI::mpInstance = nullptr; + CoreAPI& CoreAPI::GetInstance() + { + if (mpInstance == nullptr) + { + mpInstance = new CoreAPI; + } + + return *mpInstance; + } + + void CoreAPI::FreeInstance() + { + delete mpInstance; + mpInstance = nullptr; + } + + OpRes CoreAPI::Initialize(ScriptManager& sman) + { + mCat = sman.mCat; + + // Register methods + sman.mState["SetWindowSize"] = &CoreAPI::SetWindowSize; + sman.mState["Log"] = &CoreAPI::Log; + + // return OpRes::Fail("CoreAPI::Initialize not implemented yet!"); + return OpRes::OK(); + } + + //////////////////////////////////////////////////////////// + // API + //////////////////////////////////////////////////////////// + + void CoreAPI::SetWindowSize(int w, int h) + { + Core::MainWindow().Resize(w, h); + } + + void CoreAPI::Log(int level, const char* msg) + { + Logger::Log(mpInstance->mCat, level, msg); + } +} \ No newline at end of file diff --git a/src/scripting/coreAPI.h b/src/scripting/coreAPI.h index 11be601..acf0341 100644 --- a/src/scripting/coreAPI.h +++ b/src/scripting/coreAPI.h @@ -11,13 +11,30 @@ #ifndef CORE_API_H_ #define CORE_API_H_ +#include + + namespace lunarium { + class ScriptManager; + class CoreAPI { public: + static CoreAPI& GetInstance(); + static void FreeInstance(); + + OpRes Initialize(ScriptManager& sman); + + private: + static CoreAPI* mpInstance; + uint32_t mCat; + + public: // API + + static void SetWindowSize(int w, int h); + static void Log(int level, const char* msg); - }; } diff --git a/src/scripting/scriptManager.cpp b/src/scripting/scriptManager.cpp index a880f21..317d305 100644 --- a/src/scripting/scriptManager.cpp +++ b/src/scripting/scriptManager.cpp @@ -9,6 +9,7 @@ ******************************************************************************/ #include "scriptManager.h" +#include namespace lunarium { @@ -31,9 +32,33 @@ namespace lunarium OpRes ScriptManager::Initialize() { + mCat = Logger::RegisterCategory("LUA"); + mState.open_libraries(sol::lib::base, sol::lib::package); // mState.script("print('test puppy says: bark bark bark!')"); return OpRes::OK(); } + + OpRes ScriptManager::RunScript(const char* code) + { + if (!mpInstance) + { + return OpRes::Fail("ScriptManager::RunScript failed because the instance is null!"); + } + + // TODO: Use the state to run the script and check for errors + auto result = mpInstance->mState.script(code, [](lua_State*, sol::protected_function_result pfr) { + return pfr; + }); + + if (!result.valid()) + { + sol::error err = result; + Logger::Log(mpInstance->mCat, LogLevel::WARNING, "LUA script has errors: %s", err.what()); + return OpRes::Fail(err.what()); + } + + return OpRes::OK(); + } } \ No newline at end of file diff --git a/src/scripting/scriptManager.h b/src/scripting/scriptManager.h index daa60c8..ea2f788 100644 --- a/src/scripting/scriptManager.h +++ b/src/scripting/scriptManager.h @@ -23,10 +23,14 @@ namespace lunarium static void FreeInstance(); OpRes Initialize(); + static OpRes RunScript(const char* code); private: static ScriptManager* mpInstance; sol::state mState; + uint32_t mCat; + + friend class CoreAPI; }; }