Skip to content

Commit 18cae77

Browse files
committed
Version 0.2.2
* Native cache completely rewritten (again) * Readonly attribute check fixed
1 parent 735ced6 commit 18cae77

File tree

10 files changed

+79
-43
lines changed

10 files changed

+79
-43
lines changed

build.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ SET RL_LIBS=user32.lib shell32.lib lua51.lib
2121
SET RL_SOURCES=src\*.cpp
2222
SET RL_LDFLAGS=/DLL /INCREMENTAL /LIBPATH:"%RL_LUAJIT_SOURCE_DIR%"
2323
SET RL_CFLAGS=/DELPP_NO_DEFAULT_LOG_FILE /DELPP_DISABLE_LOG_FILE_FROM_ARG ^
24-
/DELPP_THREAD_SAFE /DWIN32_LEAN_AND_MEAN /D_CRT_SECURE_NO_WARNINGS /W2 ^
24+
/DELPP_THREAD_SAFE /DWIN32_LEAN_AND_MEAN /D_CRT_SECURE_NO_WARNINGS /FC /W2 ^
2525
/Isrc\ /EHsc /MP /DLL /Foobjs\
2626

2727
IF "%1"=="standalone" (

src/base.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <windows.h>
1212
#include <map>
1313

14-
#ifndef REDLUA_STANDALONE
1514
std::map <std::string, LuaScript *> Scripts {};
1615
static BOOL HasConsole = false;
1716

@@ -54,18 +53,23 @@ void RedLuaMain(void) {
5453
}
5554

5655
LOG(INFO) << "Logger initialized";
56+
#ifndef REDLUA_STANDALONE
5757
auto menuController = new MenuController();
5858
auto mainMenu = CreateMainMenu(menuController);
5959
menuController->SetCurrentPosition(
6060
Settings.Read("menu_position", 0)
6161
);
6262
LOG(DEBUG) << "RedLua menu initialized";
63-
63+
#endif
6464
if(Settings.Read("auto_updates", false)) {
6565
LOG(DEBUG) << "Starting updates checker...";
6666
std::string data;
6767
if(UpdatesCtl.CheckRedLua(data))
68+
#ifdef REDLUA_STANDALONE
69+
LOG(INFO) << "New version " << data << " detected";
70+
#else
6871
CreateUpdateAlert(menuController, data);
72+
#endif
6973
else
7074
LOG(DEBUG) << "RedLua updater: " << data;
7175
if(UpdatesCtl.CheckNativeDB(data))
@@ -81,6 +85,7 @@ void RedLuaMain(void) {
8185
LOG(ERROR) << "Failed to load " REDLUA_NATIVES_FILE ": " << ret;
8286
}
8387

88+
#ifndef REDLUA_STANDALONE
8489
RedLuaScanScripts();
8590

8691
while(true) {
@@ -98,6 +103,7 @@ void RedLuaMain(void) {
98103

99104
WAIT(0);
100105
}
106+
#endif
101107
}
102108

103109
void RedLuaFinish(void) {
@@ -111,4 +117,3 @@ void RedLuaFinish(void) {
111117
if(HasConsole && !FreeConsole())
112118
LOG(ERROR) << "Failed to free the console";
113119
}
114-
#endif

src/constants.hpp

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

33
#define REDLUA_NAME "RedLua"
4-
#define REDLUA_VERSION "v0.2.1"
5-
#define REDLUA_VERSION_NUM 021
4+
#define REDLUA_VERSION "v0.2.2"
5+
#define REDLUA_VERSION_NUM 022
66
#define REDLUA_FULLNAME REDLUA_NAME " " REDLUA_VERSION
77

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

src/dllmain.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ BOOL DllMain(HMODULE hInstance, DWORD dwReason, LPVOID lpReserved) {
2424
RedLuaFinish();
2525
break;
2626
}
27+
#else
28+
switch(dwReason) {
29+
case DLL_PROCESS_ATTACH:
30+
RedLuaMain();
31+
break;
32+
case DLL_PROCESS_DETACH:
33+
RedLuaFinish();
34+
break;
35+
}
2736
#endif
2837

2938
return TRUE;

src/luanative.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,6 @@ static const luaL_Reg nativelib[] = {
101101
{NULL, NULL}
102102
};
103103

104-
void luaclose_native(lua_State *L) {
105-
auto it = ReferenceMap.find(L);
106-
if(it != ReferenceMap.end())
107-
ReferenceMap.erase(it);
108-
}
109-
110104
int luaopen_native(lua_State *L) {
111105
call_init(L);
112106
nativeobj_init(L);

src/luanative.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
#include "thirdparty\LuaJIT\src\lua.hpp"
44

5-
void luaclose_native(lua_State *L);
6-
75
#ifdef REDLUA_STANDALONE
86
#define luaopen_native luaopen_RedLua_native
97

src/native/cache.hpp

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
#pragma once
22

33
#include "thirdparty\LuaJIT\src\lua.hpp"
4+
#include "thirdparty\easyloggingpp.h"
45
#include "native\types.hpp"
56
#include <map>
67

7-
typedef struct _RefMap {int ns, nc;} RefMap;
8-
std::map<lua_State *, RefMap> ReferenceMap {};
9-
std::map<int, std::map<NativeType, std::map<NativeCacheField, int>>> NativeCache {};
8+
#define LUANATIVE_CACHE "NativeCache"
9+
#define GLOBAL_NATIVECACHE "REDLUA_CACHE"
1010

11-
static int from_cache(int cache_ref, NativeType type, NativeCacheField id) {
12-
if(NativeCache.find(cache_ref) == NativeCache.end())
13-
return 0;
14-
if(NativeCache[cache_ref].find(type) == NativeCache[cache_ref].end())
11+
typedef std::map<NativeType, std::map<NativeCacheField, int>> NativeCacheMap;
12+
13+
typedef struct _NativeCache {
14+
int ref;
15+
NativeCacheMap *map;
16+
} NativeCache;
17+
18+
static NativeCache *get_native_cache(lua_State *L, int cache_ref) {
19+
if(cache_ref == -2) return nullptr;
20+
if(cache_ref == -1) lua_getfield(L, LUA_REGISTRYINDEX, GLOBAL_NATIVECACHE);
21+
else lua_rawgeti(L, LUA_REGISTRYINDEX, cache_ref);
22+
auto nc = (NativeCache *)luaL_checkudata(L, -1, LUANATIVE_CACHE);
23+
lua_pop(L, 2);
24+
return nc;
25+
}
26+
27+
static int from_cache(NativeCacheMap &map, NativeType type, NativeCacheField id) {
28+
if(map.find(type) == map.end())
1529
return 0;
16-
if(NativeCache[cache_ref][type].find(id) == NativeCache[cache_ref][type].end())
30+
if(map[type].find(id) == map[type].end())
1731
return 0;
1832

19-
return NativeCache[cache_ref][type][id];
33+
return map[type][id];
2034
}
2135

2236
static bool search_in_cache
2337
(
24-
lua_State *L, int *cache_ref, NativeCacheField *cache_id,
38+
lua_State *L, NativeCache *cache_obj, NativeCacheField *cache_id,
2539
NativeType type, NativeData id, int *cached
2640
) {
27-
if(*cache_ref > NATIVECACHE_DISABLE) { // Если cache_ref меньше или равен -2, значит кеширование выключено
28-
*cache_id = *cache_id == NATIVEDATA_INVAL ? id : *cache_id,
29-
*cache_ref = *cache_ref > 0 ? *cache_ref : ReferenceMap[L].nc;
30-
if(*cache_id == NATIVEDATA_INVAL || *cache_ref < 0)
31-
luaL_error(L, "Invalid cache request");
32-
lua_rawgeti(L, LUA_REGISTRYINDEX, *cache_ref);
33-
if((*cached = from_cache(*cache_ref, type, *cache_id)) > 0) {
41+
if(cache_obj) {
42+
*cache_id = *cache_id == NATIVEDATA_INVAL ? id : *cache_id;
43+
lua_rawgeti(L, LUA_REGISTRYINDEX, cache_obj->ref);
44+
if((*cached = from_cache(*cache_obj->map, type, *cache_id)) > 0) {
3445
lua_rawgeti(L, -1, *cached);
3546
if(!lua_isnil(L, -1)) {
3647
lua_remove(L, -2);
@@ -46,22 +57,39 @@ static bool search_in_cache
4657
return false;
4758
}
4859

49-
static void save_to_cache(lua_State *L, int cache_ref, int cache_id, NativeType type, NativeData id, int cached) {
50-
if(cache_ref > NATIVECACHE_DISABLE) {
60+
static void save_to_cache(lua_State *L, NativeCache *cache_obj, int cache_id, NativeType type, NativeData id, int cached) {
61+
if(cache_obj) {
5162
// Сохраняем в кеш
5263
lua_pushvalue(L, -1);
5364
// Если cached установлена в ненулевое значение, значит был промах кеша,
5465
// заполняем дыру в Lua кеше новой юзердатой с теми же параметрами
5566
if(!cached) cached = (int)lua_objlen(L, -3) + 1;
5667
lua_rawseti(L, -3, cached);
57-
NativeCache[cache_ref][type][cache_id] = cached;
68+
(*cache_obj->map)[type][cache_id] = cached;
5869
}
5970
}
6071

72+
static int ncache_gc(lua_State *L) {
73+
auto nc = (NativeCache *)luaL_checkudata(L, 1, LUANATIVE_CACHE);
74+
if(nc->map) delete nc->map;
75+
return 0;
76+
}
77+
6178
static void create_luacache(lua_State *L) {
6279
lua_createtable(L, 0, 0);
6380
lua_createtable(L, 0, 1);
6481
lua_pushstring(L, "v");
6582
lua_setfield(L, -2, "__mode");
6683
lua_setmetatable(L, -2);
84+
85+
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
86+
auto nc = (NativeCache *)lua_newuserdata(L, sizeof(NativeCache));
87+
if(luaL_newmetatable(L, LUANATIVE_CACHE)) {
88+
lua_pushcfunction(L, ncache_gc);
89+
lua_setfield(L, -2, "__gc");
90+
}
91+
92+
lua_setmetatable(L, -2);
93+
nc->map = new NativeCacheMap();
94+
nc->ref = ref;
6795
}

src/native/call.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static int global_index(lua_State *L) {
207207
if(nspace == nullptr) return 1; // Возвращаем тот nil, что нам дал rawget выше
208208

209209
lua_pop(L, 1);
210-
lua_rawgeti(L, LUA_REGISTRYINDEX, ReferenceMap[L].ns);
210+
lua_getfield(L, LUA_REGISTRYINDEX, "REDLUA_NAMESPACE");
211211
lua_rawgeti(L, -1, 1);
212212
int mt_top = (int)lua_objlen(L, -1) + 1;
213213
lua_rawgeti(L, -2, 2);
@@ -235,7 +235,7 @@ static void call_init(lua_State *L) {
235235
lua_setfield(L, -2, "__metatable");
236236
luaL_setfuncs(L, nspacemeta, 0);
237237
lua_setmetatable(L, -2);
238-
ReferenceMap[L].ns = luaL_ref(L, LUA_REGISTRYINDEX);
238+
lua_setfield(L, LUA_REGISTRYINDEX, "REDLUA_NAMESPACE");
239239

240240
lua_createtable(L, 0, 1);
241241
lua_pushcfunction(L, global_index);

src/native/object.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ static NativeObject *push_cached_lightobjectlink
4444
int cache_ref = -1, NativeCacheField cache_id = NATIVEDATA_INVAL
4545
) {
4646
int cached = 0;
47-
if(search_in_cache(L, &cache_ref, &cache_id, type, -1, &cached))
47+
NativeCache *cache = get_native_cache(L, cache_ref);
48+
if(search_in_cache(L, cache, &cache_id, type, -1, &cached))
4849
return (NativeObject *)lua_touserdata(L, -1);
4950

5051
auto no = (NativeObject *)lua_newuserdata(L, sizeof(NativeObject));
5152
NATIVEOBJECT_INITLIGHT(no, type, false, 1, *ptr);
5253
luaL_setmetatable(L, LUANATIVE_OBJECT);
5354

54-
save_to_cache(L, cache_ref, cache_id, type, -1, cached);
55+
save_to_cache(L, cache, cache_id, type, -1, cached);
5556
return no;
5657
}
5758

@@ -62,14 +63,15 @@ static NativeObject *push_cached_fullobject
6263
int cache_ref = -1, NativeCacheField cache_id = NATIVEDATA_INVAL
6364
) {
6465
int cached = 0;
65-
if(search_in_cache(L, &cache_ref, &cache_id, type, id, &cached))
66+
NativeCache *cache = get_native_cache(L, cache_ref);
67+
if(search_in_cache(L, cache, &cache_id, type, id, &cached))
6668
return (NativeObject *)lua_touserdata(L, -1);
6769

6870
auto no = (NativeObject *)lua_newuserdata(L, sizeof(NativeObject));
69-
NATIVEOBJECT_INIT(no, type, false, ReferenceMap[L].nc == cache_ref, 1, id);
71+
NATIVEOBJECT_INIT(no, type, false, cache_ref == -1, 1, id);
7072
luaL_setmetatable(L, LUANATIVE_OBJECT);
7173

72-
save_to_cache(L, cache_ref, cache_id, type, id, cached);
74+
save_to_cache(L, cache, cache_id, type, id, cached);
7375
return no;
7476
}
7577

@@ -165,6 +167,7 @@ static int native_tostring(lua_State *L) {
165167

166168
static int native_newindex(lua_State *L) {
167169
auto no = (NativeObject *)luaL_checkudata(L, 1, LUANATIVE_OBJECT);
170+
luaL_argcheck(L, !no->hdr.isReadOnly, 1, "readonly object");
168171
if(lua_type(L, 2) == LUA_TSTRING && no->hdr.type == NTYPE_VECTOR3)
169172
return vector_newindex(L, no, *lua_tostring(L, 2));
170173
uint idx = (uint)luaL_checkinteger(L, 2);
@@ -342,5 +345,5 @@ static void nativeobj_init(lua_State *L) {
342345
luaL_setfuncs(L, nativeobj, 0);
343346

344347
create_luacache(L);
345-
ReferenceMap[L].nc = luaL_ref(L, LUA_REGISTRYINDEX);
348+
lua_setfield(L, LUA_REGISTRYINDEX, GLOBAL_NATIVECACHE);
346349
}

src/redlua.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ class LuaScript {
9494
if(LookForFunc("OnStop"))
9595
CallFunc(0, 0);
9696

97-
luaclose_native(L);
9897
lua_close(L);
9998
}
10099

0 commit comments

Comments
 (0)