Skip to content

Commit 2847c0d

Browse files
committed
Version 0.6.0
+ Lua logging module implemented
1 parent 7cd899a commit 2847c0d

14 files changed

Lines changed: 158 additions & 95 deletions

src/constants.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#pragma once
22

33
#define REDLUA_NAME "RedLua"
4-
#define REDLUA_VERSION "v0.5.0"
5-
#define REDLUA_VERSION_NUM 050
4+
#define REDLUA_VERSION "v0.6.0"
5+
#define REDLUA_VERSION_NUM 060
66
#define REDLUA_FULLNAME REDLUA_NAME " " REDLUA_VERSION
77

88
#ifdef REDLUA_GTAV
9-
#define REDLUA_GAMECODE "gta5"
10-
#define REDLUA_HOTKEY_DEFAULT 0x73
9+
# define REDLUA_GAMECODE "gta5"
10+
# define REDLUA_HOTKEY_DEFAULT 0x73
1111
#else
12-
#define REDLUA_GAMECODE "rdr3"
13-
#define REDLUA_HOTKEY_DEFAULT 0x76
12+
# define REDLUA_GAMECODE "rdr3"
13+
# define REDLUA_HOTKEY_DEFAULT 0x76
1414
#endif
1515

1616
#define REDLUA_TAGS_URL "https://api.github.com/repos/igor725/RedLua/tags"

src/emu/native.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static std::vector<struct KeyEvent> VirtualPress {};
1818
void emu_scriptWait(DWORD ms) {
1919
Sleep(100);
2020
if (VirtualPress.size() > 0) {
21-
struct KeyEvent &ke = VirtualPress.back();
21+
auto &ke = VirtualPress.back();
2222
OnKeyboardMessage(ke.key, ke.repeats, ke.scanCode,
2323
ke.isExtended, ke.isWithAlt, ke.wasDownBefore, ke.isUpNow);
2424
VirtualPress.pop_back();

src/langctl.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ typedef struct {
1717
typedef std::map<std::string, LangMap> LangsMap;
1818

1919
class LangCtl {
20-
LangsMap m_lngMap = {};
21-
LangMap *m_currLang = nullptr,
22-
*m_defaultLang = nullptr;
20+
LangsMap m_lngMap;
21+
LangMap *m_currLang,
22+
*m_defaultLang;
2323

2424
public:
25+
LangCtl() : m_lngMap({}), m_currLang(nullptr), m_defaultLang(nullptr) {};
26+
2527
template<typename ...Args>
2628
std::string Get(std::string code, Args... ar) {
2729
if (auto localLang = m_currLang) {

src/lualog.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "lualog.hpp"
2+
3+
#include "thirdparty\easyloggingpp.h"
4+
#include <string>
5+
6+
static int tocppstring(lua_State *L, std::string &logstr) {
7+
char pointer[20];
8+
lua_Number tempd;
9+
int tempi;
10+
11+
for (int i = 1; i <= lua_gettop(L); i++) {
12+
switch (lua_type(L, i)) {
13+
case LUA_TNIL:
14+
logstr.append("nil");
15+
break;
16+
case LUA_TBOOLEAN:
17+
logstr.append(lua_toboolean(L, i) ? "true" : "false");
18+
break;
19+
case LUA_TNUMBER:
20+
tempd = lua_tonumber(L, i);
21+
tempi = (int)tempd;
22+
if (tempi == tempd)
23+
logstr.append(std::to_string(tempi));
24+
else
25+
logstr.append(std::to_string(tempd));
26+
break;
27+
case LUA_TSTRING:
28+
logstr.append(lua_tostring(L, i));
29+
break;
30+
default:
31+
if (luaL_callmeta(L, i, "__tostring")) {
32+
if (!lua_isstring(L, -1))
33+
luaL_error(L, "'__tostring' must return a string");
34+
logstr.append(lua_tostring(L, -1));
35+
lua_pop(L, 1);
36+
} else {
37+
logstr.append(luaL_typename(L, i));
38+
std::snprintf(pointer, 20, ": %p", lua_topointer(L, i));
39+
logstr.append(pointer);
40+
}
41+
break;
42+
}
43+
44+
logstr.append(", ");
45+
}
46+
47+
if (!logstr.empty())
48+
(logstr.pop_back(), logstr.pop_back());
49+
50+
return 0;
51+
}
52+
53+
static int log_info(lua_State *L) {
54+
std::string logstr;
55+
tocppstring(L, logstr);
56+
LOG(INFO) << logstr;
57+
return 1;
58+
}
59+
60+
static int log_warn(lua_State *L) {
61+
std::string logstr;
62+
tocppstring(L, logstr);
63+
LOG(WARNING) << logstr;
64+
return 1;
65+
}
66+
67+
static int log_debug(lua_State *L) {
68+
std::string logstr;
69+
tocppstring(L, logstr);
70+
LOG(DEBUG) << logstr;
71+
return 1;
72+
}
73+
74+
static int log_error(lua_State *L) {
75+
std::string logstr;
76+
tocppstring(L, logstr);
77+
LOG(ERROR) << logstr;
78+
return 1;
79+
}
80+
81+
const luaL_Reg loglib[] = {
82+
{"info", log_info},
83+
{"warn", log_warn},
84+
{"debug", log_debug},
85+
{"error", log_error},
86+
87+
{NULL, NULL}
88+
};
89+
90+
int luaopen_log(lua_State *L) {
91+
luaL_newlib(L, loglib);
92+
lua_pushcfunction(L, log_info);
93+
lua_setglobal(L, "print");
94+
return 1;
95+
}

src/lualog.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include "thirdparty\LuaJIT\src\lua.hpp"
4+
#ifdef REDLUA_STANDALONE
5+
#define luaopen_log luaopen_RedLua_log
6+
7+
extern "C" {
8+
__declspec(dllexport)
9+
#endif
10+
int luaopen_log(lua_State *L);
11+
#ifdef REDLUA_STANDALONE
12+
}
13+
#endif

src/luamenu.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "luamenu.hpp"
22
#include "natives.hpp"
33
#include "thirdparty\scriptmenu.h"
4-
#include "thirdparty\easyloggingpp.h"
54

65
class MenuLua : public MenuBase {
76
MenuLua **m_self; lua_State *m_L;

src/luascript.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#include "luamisc.hpp"
44
#include "luamenu.hpp"
55
#include "lualang.hpp"
6+
#include "lualog.hpp"
67

78
const luaL_Reg redlibs[] = {
89
{"native", luaopen_native},
910
{"misc", luaopen_misc},
1011
{"menu", luaopen_menu},
1112
{"lang", luaopen_lang},
13+
{"log", luaopen_log},
1214

1315
{NULL, NULL}
1416
};

src/luascript.hpp

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,6 @@
1010

1111
extern const luaL_Reg redlibs[];
1212

13-
static int log_print(lua_State *L) {
14-
std::string logstr;
15-
char pointer[20];
16-
lua_Number tempd;
17-
int tempi;
18-
19-
for (int i = 1; i <= lua_gettop(L); i++) {
20-
switch (lua_type(L, i)) {
21-
case LUA_TNIL:
22-
logstr.append("nil");
23-
break;
24-
case LUA_TBOOLEAN:
25-
logstr.append(lua_toboolean(L, i) ? "true" : "false");
26-
break;
27-
case LUA_TNUMBER:
28-
tempd = lua_tonumber(L, i);
29-
tempi = (int)tempd;
30-
if (tempi == tempd)
31-
logstr.append(std::to_string(tempi));
32-
else
33-
logstr.append(std::to_string(tempd));
34-
break;
35-
case LUA_TSTRING:
36-
logstr.append(lua_tostring(L, i));
37-
break;
38-
default:
39-
if (luaL_callmeta(L, i, "__tostring")) {
40-
if (!lua_isstring(L, -1))
41-
luaL_error(L, "'__tostring' must return a string");
42-
logstr.append(lua_tostring(L, -1));
43-
lua_pop(L, 1);
44-
} else {
45-
logstr.append(luaL_typename(L, i));
46-
std::snprintf(pointer, 20, ": %p", lua_topointer(L, i));
47-
logstr.append(pointer);
48-
}
49-
break;
50-
}
51-
52-
logstr.append(", ");
53-
}
54-
55-
if (logstr.length() > 0) {
56-
(logstr.pop_back(), logstr.pop_back());
57-
LOG(INFO) << logstr;
58-
}
59-
return 0;
60-
}
61-
6213
class LuaScript {
6314
private:
6415
lua_State *L;
@@ -95,9 +46,6 @@ class LuaScript {
9546
lua_setfield(L, -2, "cpath");
9647
}
9748
lua_pop(L, 1);
98-
99-
lua_pushcfunction(L, log_print);
100-
lua_setglobal(L, "print");
10149
}
10250

10351
~LuaScript(void) {

src/menus/helpers.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ class MenuItemButton : public MenuItemDefault
3333

3434
class MenuTemporary : public MenuBase
3535
{
36-
void OnPop(void)
37-
{
36+
void OnPop(void) {
3837
delete this;
3938
}
4039

src/menus/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ MenuBase *CreateMainMenu(MenuController *controller) {
1212
controller->RegisterMenu(menu);
1313

1414
menu->AddItem(new MenuItemButton(Lng.Get("core.main.scripts"), [](auto ctl) {
15-
ctl->PushMenu(CreateScriptsList(ctl));
15+
if(Scripts.size() > 0)
16+
ctl->PushMenu(CreateScriptsList(ctl));
17+
else
18+
ctl->SetStatusText(Lng.Get("core.scripts.nf"));
1619
}));
1720
menu->AddItem(new MenuItemButton(Lng.Get("core.main.refr"), [](auto ctl) {
1821
if (RedLuaScanScripts())

0 commit comments

Comments
 (0)