Skip to content

Commit

Permalink
Version 0.6.0
Browse files Browse the repository at this point in the history
+ Lua logging module implemented
  • Loading branch information
igor725 committed Aug 15, 2022
1 parent 7cd899a commit 2847c0d
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 95 deletions.
12 changes: 6 additions & 6 deletions src/constants.hpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/emu/native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static std::vector<struct KeyEvent> 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();
Expand Down
8 changes: 5 additions & 3 deletions src/langctl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ typedef struct {
typedef std::map<std::string, LangMap> 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<typename ...Args>
std::string Get(std::string code, Args... ar) {
if (auto localLang = m_currLang) {
Expand Down
95 changes: 95 additions & 0 deletions src/lualog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "lualog.hpp"

#include "thirdparty\easyloggingpp.h"
#include <string>

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;
}
13 changes: 13 additions & 0 deletions src/lualog.hpp
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion src/luamenu.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}
};
52 changes: 0 additions & 52 deletions src/luascript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/menus/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ class MenuItemButton : public MenuItemDefault

class MenuTemporary : public MenuBase
{
void OnPop(void)
{
void OnPop(void) {
delete this;
}

Expand Down
5 changes: 4 additions & 1 deletion src/menus/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
54 changes: 29 additions & 25 deletions src/menus/scripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MenuScript *>(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())
Expand All @@ -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) {
Expand All @@ -47,52 +57,51 @@ 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"));
}

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 {
Expand Down Expand Up @@ -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;
}
1 change: 0 additions & 1 deletion src/menus/settings.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "thirdparty\easyloggingpp.h"
#include "thirdparty\scriptmenu.h"
#include "thirdparty\keyboard.h"
#include "menus\settings.hpp"
Expand Down
1 change: 0 additions & 1 deletion src/native/cache.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "thirdparty\LuaJIT\src\lua.hpp"
#include "thirdparty\easyloggingpp.h"
#include "native\types.hpp"
#include <map>

Expand Down
Loading

0 comments on commit 2847c0d

Please sign in to comment.