From eab83c296c64e7c0692a3617d63e4cd70befa20d Mon Sep 17 00:00:00 2001 From: kelson8 Date: Wed, 2 Apr 2025 14:48:29 -0400 Subject: [PATCH] Add lua to project for testing, add test.lua script My lua test in here so far just outputs a message to cout. I will expand this for something later. Add defines.cpp for lua scripts and other items. --- lua_scripts/test.lua | 1 + src/Main.cpp | 18 ++++++ src/test/lua_test.cpp | 143 ++++++++++++++++++++++++++++++++++++++++++ src/test/lua_test.h | 59 +++++++++++++++++ src/util/defines.cpp | 9 +++ src/util/defines.h | 14 +++++ 6 files changed, 244 insertions(+) create mode 100644 lua_scripts/test.lua create mode 100644 src/test/lua_test.cpp create mode 100644 src/test/lua_test.h create mode 100644 src/util/defines.cpp diff --git a/lua_scripts/test.lua b/lua_scripts/test.lua new file mode 100644 index 0000000..891740c --- /dev/null +++ b/lua_scripts/test.lua @@ -0,0 +1 @@ +--print_msg("Test in Cout ImGui C++") \ No newline at end of file diff --git a/src/Main.cpp b/src/Main.cpp index 5b46328..5e0b9e1 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -20,6 +20,8 @@ int main(int, char**) #include #include +#include "defines.h" + // Main code // I moved the DirectX9 code into the test/directx9_test.cpp file, @@ -34,6 +36,15 @@ int main(int, char**) // New #include +#ifdef LUA_TEST +#include "lua_test.h" +//extern "C" { +//#include +//#include +//#include +//} +#endif + // https://stackoverflow.com/questions/14954876/how-to-create-a-message-box-with-user-defined-buttons-in-c /* @@ -57,6 +68,13 @@ typedef VOID(*DLLPROC) (LPTSTR); int main(int, char**) { #define _TEST1 + // Load lua +#ifdef LUA_TEST + LuaTest luaTest = LuaTest(); + luaTest.InitLua(); + +#endif //LUA_TEST + // Load a test dll // Get path to the dll, use current path // https://stackoverflow.com/questions/61382829/dynamically-use-a-dll-from-a-console-application diff --git a/src/test/lua_test.cpp b/src/test/lua_test.cpp new file mode 100644 index 0000000..63d9a58 --- /dev/null +++ b/src/test/lua_test.cpp @@ -0,0 +1,143 @@ +#include "lua_test.h" + +#include +#include + +/// +/// Set the lua state, and load the lua libraries +/// +LuaTest::LuaTest() { + m_luaVM = luaL_newstate(); + + // Load LUA libraries, TODO is this needed in here? + luaopen_base(m_luaVM); + luaopen_math(m_luaVM); + luaopen_string(m_luaVM); + luaopen_table(m_luaVM); + luaopen_debug(m_luaVM); + luaopen_utf8(m_luaVM); + luaopen_os(m_luaVM); + +} + +/// +/// Check lua globals, made this into a function +/// +/// +/// +/// +bool +LuaTest::CheckLua(lua_State* L, int r) +{ + // Check if the value is valid + if (r != LUA_OK) { + std::string errorMsg = lua_tostring(L, -1); + std::cout << errorMsg << std::endl; + return false; + } /*else { + std::cout << errorMsg << std::endl; + }*/ + + return true; +} + +// All functions used in lua scripts need to be in 'extern "C" {}' blocks +// When defining a lua function in here, make it like this: +/* + int FunctionNameLua(lua_State* L) + + // The return can be how many variables you want this to use. + // For the LogMessageToConsoleLua function, it has a return value of 1 because it + // only takes the string from the lua file like this: + // print_msg("This will display in cout") + + return 1; + + // If this has multiple variables needed, such as for coordinates in a game + // You can change this to 3 for an X, Y, Z + // set_coordinates(x, y, z); + return 3; + + +* +*/ +extern "C" +{ + /// + /// Log a message to cout using lua + /// + /// + /// + int LogMessageToConsoleLua(lua_State* L) + { + std::string messageToLog = (std::string)lua_tostring(L, 1); + std::cout << messageToLog << std::endl; + + return 1; + } + +} + +/// +/// I had to add this to the class to fix it. +/// I got this working, I had to add lua_State *L and get the lua state +/// To move this back into the init function if needed: +/// Rename the 'L' variable in each lua_register to 'm_luaVM' +/// +void +LuaTest::RegisterLuaFunctions(lua_State* L) +{ + // Register functions like this + lua_register(L, "print_msg", LogMessageToConsoleLua); +} + +/// +/// TODO Setup events to run, I may need to add a delay to some of this. +/// This would go in the main loop if I decide to use events in this ImGui test. +/// +#ifdef LUA_EVENT_TEST +void +LuaTest::LuaEvents() +{ + Defines defines = Defines(); + LuaTest luaTest = LuaTest(); + + luaTest.RegisterLuaFunctions(m_luaVM); + + if (CheckLua(m_luaVM, luaL_dofile(m_luaVM, defines.luaEventsScript.c_str()))) + { + } + else { + std::cout << "The lua file " << defines.luaEventsScript << "Couldn't be loaded!" << std::endl; + } + +} +#endif + + +/// +/// This runs the init lua function, but it can be run multiple times +/// +void +LuaTest::InitLua() +{ + Defines defines = Defines(); + LuaTest luaTest = LuaTest(); + + + //lua_State *L = LuaTest::GetInstance().GetLuaState(); + + luaTest.RegisterLuaFunctions(m_luaVM); + + // This seems to work for error handling if the file doesn't exist + if (CheckLua(m_luaVM, luaL_dofile(m_luaVM, defines.luaScript.c_str()))) + { + //std::cout << defines.luaScript << " exists!" << std::endl; + } + else { + std::cout << defines.luaScript << " doesn't exist!" << std::endl; + } + + +} + diff --git a/src/test/lua_test.h b/src/test/lua_test.h new file mode 100644 index 0000000..f9e30f7 --- /dev/null +++ b/src/test/lua_test.h @@ -0,0 +1,59 @@ +#pragma once + + +#include "defines.h" +#ifdef LUA_TEST +extern "C" { +#include +#include +#include +} +#endif + + +class LuaTest +{ +private: + // Set the lua state + lua_State* m_luaVM; + + // Register the lua functions + void RegisterLuaFunctions(lua_State* L); +public: + + LuaTest(); + void InitLua(); + + bool CheckLua(lua_State* L, int r); + + // Run the custom event system I am working on in Lua +#ifdef LUA_EVENT_TEST + void LuaEvents(); +#endif + + // These work for getting the m_luaVM variable in the private class above. + + // Return the instance of LuaTest + static LuaTest& GetInstance() + { + static LuaTest instance; + return instance; + } + + // Get the lua state, can be used like this: + // lua_State *L = LuaTest::GetInstance().GetLuaState(); + lua_State* GetLuaState() + { + return m_luaVM; + } + + // Close the lua vm + ~LuaTest() + { + if (m_luaVM) + { + lua_close(m_luaVM); + } + } +}; + diff --git a/src/util/defines.cpp b/src/util/defines.cpp new file mode 100644 index 0000000..828eff1 --- /dev/null +++ b/src/util/defines.cpp @@ -0,0 +1,9 @@ +#include "defines.h" +#include "misc_util.h" +#include "file_functions.h" + +std::string currentDirectory = FileFunctions::CurrentDirectory(); +std::string luaScriptsDirectory = currentDirectory + "\\lua_scripts\\"; + +std::string Defines::luaScript = luaScriptsDirectory + "test.lua"; +std::string Defines::luaEventsScript = luaScriptsDirectory + "kcnet-events.lua"; \ No newline at end of file diff --git a/src/util/defines.h b/src/util/defines.h index 95a74be..537948c 100644 --- a/src/util/defines.h +++ b/src/util/defines.h @@ -3,6 +3,16 @@ #if _WIN32 #include #endif + +#include +#include + +// Enable lua test +#define LUA_TEST + +// Enable the lua event test, this is disabled until I'm ready to use it. +//#define LUA_EVENT_TEST + // // TODO Fix this to work in the OpenGL test @@ -22,6 +32,10 @@ public: const wchar_t* window_title = TITLE; const wchar_t* main_menu_title = MAIN_MENU_TITLE_; const char* imgui_window_name = IMGUI_WINDOW_NAME_; + + // Lua script files + static std::string luaScript; + static std::string luaEventsScript; };