From 2847c0d69e53094f325c5cb3f988e7aa6527919c Mon Sep 17 00:00:00 2001 From: igor725 Date: Mon, 15 Aug 2022 22:12:34 +0300 Subject: [PATCH] Version 0.6.0 + Lua logging module implemented --- src/constants.hpp | 12 +++--- src/emu/native.cpp | 2 +- src/langctl.hpp | 8 ++-- src/lualog.cpp | 95 ++++++++++++++++++++++++++++++++++++++++++ src/lualog.hpp | 13 ++++++ src/luamenu.cpp | 1 - src/luascript.cpp | 2 + src/luascript.hpp | 52 ----------------------- src/menus/helpers.hpp | 3 +- src/menus/main.cpp | 5 ++- src/menus/scripts.cpp | 54 +++++++++++++----------- src/menus/settings.cpp | 1 - src/native/cache.hpp | 1 - src/updatesctl.hpp | 4 +- 14 files changed, 158 insertions(+), 95 deletions(-) create mode 100644 src/lualog.cpp create mode 100644 src/lualog.hpp diff --git a/src/constants.hpp b/src/constants.hpp index 6b3369c..8f9088d 100644 --- a/src/constants.hpp +++ b/src/constants.hpp @@ -1,16 +1,16 @@ #pragma once #define REDLUA_NAME "RedLua" -#define REDLUA_VERSION "v0.5.0" -#define REDLUA_VERSION_NUM 050 +#define REDLUA_VERSION "v0.6.0" +#define REDLUA_VERSION_NUM 060 #define REDLUA_FULLNAME REDLUA_NAME " " REDLUA_VERSION #ifdef REDLUA_GTAV -#define REDLUA_GAMECODE "gta5" -#define REDLUA_HOTKEY_DEFAULT 0x73 +# define REDLUA_GAMECODE "gta5" +# define REDLUA_HOTKEY_DEFAULT 0x73 #else -#define REDLUA_GAMECODE "rdr3" -#define REDLUA_HOTKEY_DEFAULT 0x76 +# define REDLUA_GAMECODE "rdr3" +# define REDLUA_HOTKEY_DEFAULT 0x76 #endif #define REDLUA_TAGS_URL "https://api.github.com/repos/igor725/RedLua/tags" diff --git a/src/emu/native.cpp b/src/emu/native.cpp index ec6682d..cc44fa2 100644 --- a/src/emu/native.cpp +++ b/src/emu/native.cpp @@ -18,7 +18,7 @@ static std::vector VirtualPress {}; void emu_scriptWait(DWORD ms) { Sleep(100); if (VirtualPress.size() > 0) { - struct KeyEvent &ke = VirtualPress.back(); + auto &ke = VirtualPress.back(); OnKeyboardMessage(ke.key, ke.repeats, ke.scanCode, ke.isExtended, ke.isWithAlt, ke.wasDownBefore, ke.isUpNow); VirtualPress.pop_back(); diff --git a/src/langctl.hpp b/src/langctl.hpp index 9c04ec1..39c7ec1 100644 --- a/src/langctl.hpp +++ b/src/langctl.hpp @@ -17,11 +17,13 @@ typedef struct { typedef std::map LangsMap; class LangCtl { - LangsMap m_lngMap = {}; - LangMap *m_currLang = nullptr, - *m_defaultLang = nullptr; + LangsMap m_lngMap; + LangMap *m_currLang, + *m_defaultLang; public: + LangCtl() : m_lngMap({}), m_currLang(nullptr), m_defaultLang(nullptr) {}; + template std::string Get(std::string code, Args... ar) { if (auto localLang = m_currLang) { diff --git a/src/lualog.cpp b/src/lualog.cpp new file mode 100644 index 0000000..2efdc9d --- /dev/null +++ b/src/lualog.cpp @@ -0,0 +1,95 @@ +#include "lualog.hpp" + +#include "thirdparty\easyloggingpp.h" +#include + +static int tocppstring(lua_State *L, std::string &logstr) { + char pointer[20]; + lua_Number tempd; + int tempi; + + for (int i = 1; i <= lua_gettop(L); i++) { + switch (lua_type(L, i)) { + case LUA_TNIL: + logstr.append("nil"); + break; + case LUA_TBOOLEAN: + logstr.append(lua_toboolean(L, i) ? "true" : "false"); + break; + case LUA_TNUMBER: + tempd = lua_tonumber(L, i); + tempi = (int)tempd; + if (tempi == tempd) + logstr.append(std::to_string(tempi)); + else + logstr.append(std::to_string(tempd)); + break; + case LUA_TSTRING: + logstr.append(lua_tostring(L, i)); + break; + default: + if (luaL_callmeta(L, i, "__tostring")) { + if (!lua_isstring(L, -1)) + luaL_error(L, "'__tostring' must return a string"); + logstr.append(lua_tostring(L, -1)); + lua_pop(L, 1); + } else { + logstr.append(luaL_typename(L, i)); + std::snprintf(pointer, 20, ": %p", lua_topointer(L, i)); + logstr.append(pointer); + } + break; + } + + logstr.append(", "); + } + + if (!logstr.empty()) + (logstr.pop_back(), logstr.pop_back()); + + return 0; +} + +static int log_info(lua_State *L) { + std::string logstr; + tocppstring(L, logstr); + LOG(INFO) << logstr; + return 1; +} + +static int log_warn(lua_State *L) { + std::string logstr; + tocppstring(L, logstr); + LOG(WARNING) << logstr; + return 1; +} + +static int log_debug(lua_State *L) { + std::string logstr; + tocppstring(L, logstr); + LOG(DEBUG) << logstr; + return 1; +} + +static int log_error(lua_State *L) { + std::string logstr; + tocppstring(L, logstr); + LOG(ERROR) << logstr; + return 1; +} + +const luaL_Reg loglib[] = { + {"info", log_info}, + {"warn", log_warn}, + {"debug", log_debug}, + {"error", log_error}, + + {NULL, NULL} +}; + +int luaopen_log(lua_State *L) { + luaL_newlib(L, loglib); + lua_pushcfunction(L, log_info); + lua_setglobal(L, "print"); + return 1; +} diff --git a/src/lualog.hpp b/src/lualog.hpp new file mode 100644 index 0000000..f4d5e63 --- /dev/null +++ b/src/lualog.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "thirdparty\LuaJIT\src\lua.hpp" +#ifdef REDLUA_STANDALONE +#define luaopen_log luaopen_RedLua_log + +extern "C" { + __declspec(dllexport) +#endif +int luaopen_log(lua_State *L); +#ifdef REDLUA_STANDALONE +} +#endif diff --git a/src/luamenu.cpp b/src/luamenu.cpp index 4f4848d..056de80 100644 --- a/src/luamenu.cpp +++ b/src/luamenu.cpp @@ -1,7 +1,6 @@ #include "luamenu.hpp" #include "natives.hpp" #include "thirdparty\scriptmenu.h" -#include "thirdparty\easyloggingpp.h" class MenuLua : public MenuBase { MenuLua **m_self; lua_State *m_L; diff --git a/src/luascript.cpp b/src/luascript.cpp index 8c2ac48..4ea2a34 100644 --- a/src/luascript.cpp +++ b/src/luascript.cpp @@ -3,12 +3,14 @@ #include "luamisc.hpp" #include "luamenu.hpp" #include "lualang.hpp" +#include "lualog.hpp" const luaL_Reg redlibs[] = { {"native", luaopen_native}, {"misc", luaopen_misc}, {"menu", luaopen_menu}, {"lang", luaopen_lang}, + {"log", luaopen_log}, {NULL, NULL} }; diff --git a/src/luascript.hpp b/src/luascript.hpp index 9d0ee30..4736bb6 100644 --- a/src/luascript.hpp +++ b/src/luascript.hpp @@ -10,55 +10,6 @@ extern const luaL_Reg redlibs[]; -static int log_print(lua_State *L) { - std::string logstr; - char pointer[20]; - lua_Number tempd; - int tempi; - - for (int i = 1; i <= lua_gettop(L); i++) { - switch (lua_type(L, i)) { - case LUA_TNIL: - logstr.append("nil"); - break; - case LUA_TBOOLEAN: - logstr.append(lua_toboolean(L, i) ? "true" : "false"); - break; - case LUA_TNUMBER: - tempd = lua_tonumber(L, i); - tempi = (int)tempd; - if (tempi == tempd) - logstr.append(std::to_string(tempi)); - else - logstr.append(std::to_string(tempd)); - break; - case LUA_TSTRING: - logstr.append(lua_tostring(L, i)); - break; - default: - if (luaL_callmeta(L, i, "__tostring")) { - if (!lua_isstring(L, -1)) - luaL_error(L, "'__tostring' must return a string"); - logstr.append(lua_tostring(L, -1)); - lua_pop(L, 1); - } else { - logstr.append(luaL_typename(L, i)); - std::snprintf(pointer, 20, ": %p", lua_topointer(L, i)); - logstr.append(pointer); - } - break; - } - - logstr.append(", "); - } - - if (logstr.length() > 0) { - (logstr.pop_back(), logstr.pop_back()); - LOG(INFO) << logstr; - } - return 0; -} - class LuaScript { private: lua_State *L; @@ -95,9 +46,6 @@ class LuaScript { lua_setfield(L, -2, "cpath"); } lua_pop(L, 1); - - lua_pushcfunction(L, log_print); - lua_setglobal(L, "print"); } ~LuaScript(void) { diff --git a/src/menus/helpers.hpp b/src/menus/helpers.hpp index b7d4419..400af0d 100644 --- a/src/menus/helpers.hpp +++ b/src/menus/helpers.hpp @@ -33,8 +33,7 @@ class MenuItemButton : public MenuItemDefault class MenuTemporary : public MenuBase { - void OnPop(void) - { + void OnPop(void) { delete this; } diff --git a/src/menus/main.cpp b/src/menus/main.cpp index ec8af4b..937c191 100644 --- a/src/menus/main.cpp +++ b/src/menus/main.cpp @@ -12,7 +12,10 @@ MenuBase *CreateMainMenu(MenuController *controller) { controller->RegisterMenu(menu); menu->AddItem(new MenuItemButton(Lng.Get("core.main.scripts"), [](auto ctl) { - ctl->PushMenu(CreateScriptsList(ctl)); + if(Scripts.size() > 0) + ctl->PushMenu(CreateScriptsList(ctl)); + else + ctl->SetStatusText(Lng.Get("core.scripts.nf")); })); menu->AddItem(new MenuItemButton(Lng.Get("core.main.refr"), [](auto ctl) { if (RedLuaScanScripts()) diff --git a/src/menus/scripts.cpp b/src/menus/scripts.cpp index eb345cf..2cd3b36 100644 --- a/src/menus/scripts.cpp +++ b/src/menus/scripts.cpp @@ -15,10 +15,20 @@ class MenuScript : public MenuTemporary { } }; -class MenuItemStatus : public MenuItemDefault { +class MenuItemGenLua : public MenuItemDefault { +public: + MenuItemGenLua(void) : MenuItemDefault("") {}; + MenuItemGenLua(std::string caption) : MenuItemDefault(caption) {}; + + LuaScript *GetScript(void) { + return dynamic_cast(GetMenu())->GetScript(); + } +}; + +class MenuItemStatus : public MenuItemGenLua { std::string GetCaption(void) { - LuaScript *scr = ((MenuScript *)GetMenu())->GetScript(); std::string state; + auto scr = GetScript(); if (scr->IsEnabled()) state = Lng.Get("core.script.state1"); else if (scr->HasError()) @@ -30,10 +40,10 @@ class MenuItemStatus : public MenuItemDefault { public: MenuItemStatus() - : MenuItemDefault("") {} + : MenuItemGenLua() {} }; -class MenuItemUsage : public MenuItemDefault { +class MenuItemUsage : public MenuItemGenLua { std::string m_usage = Lng.Get("core.script.rvlusage"); std::string GetCaption(void) { @@ -47,13 +57,12 @@ class MenuItemUsage : public MenuItemDefault { public: MenuItemUsage() - : MenuItemDefault("") {} + : MenuItemGenLua() {} }; -class MenuItemReload : public MenuItemDefault { +class MenuItemReload : public MenuItemGenLua { void OnSelect(void) { - LuaScript *script = ((MenuScript *)GetMenu())->GetScript(); - if (script->Load()) + if (GetScript()->Load()) SetStatusText(Lng.Get("core.script.nfy.relsc")); else SetStatusText(Lng.Get("core.script.nfy.relfl")); @@ -61,38 +70,38 @@ class MenuItemReload : public MenuItemDefault { public: MenuItemReload(std::string title) - : MenuItemDefault(title) {} + : MenuItemGenLua(title) {} }; -class MenuItemUnload : public MenuItemDefault { +class MenuItemUnload : public MenuItemGenLua { void OnSelect(void) { - LuaScript *script = ((MenuScript *)GetMenu())->GetScript(); + auto scr = GetScript(); for (auto it = Scripts.begin(); it != Scripts.end();) { - if (it->second == script) { + if (it->second == scr) { it = Scripts.erase(it); } else { it++; } } - delete script; + delete scr; SetStatusText(Lng.Get("core.script.nfy.unlsucc")); GetMenu()->GetController()->PopMenu(2); } public: MenuItemUnload(std::string title) - : MenuItemDefault(title) {} + : MenuItemGenLua(title) {} }; -class MenuItemToggle : public MenuItemDefault { +class MenuItemToggle : public MenuItemGenLua { void OnSelect(void) { - LuaScript *script = ((MenuScript *)GetMenu())->GetScript(); - script->SetEnabled(!script->IsEnabled()); + auto scr = GetScript(); + scr->SetEnabled(!scr->IsEnabled()); } public: MenuItemToggle(std::string title) - : MenuItemDefault(title) {} + : MenuItemGenLua(title) {} }; class MenuItemScript : public MenuItemDefault { @@ -128,12 +137,7 @@ class MenuItemScript : public MenuItemDefault { MenuTemporary *CreateScriptsList(MenuController *controller) { auto menu = new MenuTemporary(new MenuItemListTitle(Lng.Get("core.main.scripts"))); controller->RegisterMenu(menu); - - if (Scripts.size() > 0) { - for (auto &x : Scripts) - menu->AddItem(new MenuItemScript(x.first, x.second)); - } else - menu->AddItem(new MenuItemDefault(Lng.Get("core.scripts.nf"))); - + for (auto &x : Scripts) + menu->AddItem(new MenuItemScript(x.first, x.second)); return menu; } diff --git a/src/menus/settings.cpp b/src/menus/settings.cpp index 9656da4..0273b77 100644 --- a/src/menus/settings.cpp +++ b/src/menus/settings.cpp @@ -1,4 +1,3 @@ -#include "thirdparty\easyloggingpp.h" #include "thirdparty\scriptmenu.h" #include "thirdparty\keyboard.h" #include "menus\settings.hpp" diff --git a/src/native/cache.hpp b/src/native/cache.hpp index c5a93ef..f42b83c 100644 --- a/src/native/cache.hpp +++ b/src/native/cache.hpp @@ -1,7 +1,6 @@ #pragma once #include "thirdparty\LuaJIT\src\lua.hpp" -#include "thirdparty\easyloggingpp.h" #include "native\types.hpp" #include diff --git a/src/updatesctl.hpp b/src/updatesctl.hpp index ccfba26..a7e7d6f 100644 --- a/src/updatesctl.hpp +++ b/src/updatesctl.hpp @@ -18,8 +18,8 @@ class UpdatesController { ERR_NO_UPDATES }; - UpdatesController::Returns CheckRedLua(std::string &vername); - UpdatesController::Returns CheckNativeDB(bool force_update = false); + Returns CheckRedLua(std::string &vername); + Returns CheckNativeDB(bool force_update = false); void Stop(void); };