Skip to content

Commit a8b05e3

Browse files
committed
More fixes and improvements
* native.new now supports types that are longer than 4 bytes * native.newvec renamed to native.vector * NativeCache improved * Passing strings to natives fixed * Treat hash as int * Natives can now take cdata values + NativeObject:topointer() method added
1 parent 3b2cbd9 commit a8b05e3

File tree

8 files changed

+162
-138
lines changed

8 files changed

+162
-138
lines changed

src/luanative.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,31 @@
22

33
#include "luanative.hpp"
44

5-
#include "native/object.hpp"
6-
#include "native/types.hpp"
7-
#include "native/typemap.hpp"
8-
#include "native/call.hpp"
9-
#include "native/db.hpp"
5+
#include "native\object.hpp"
6+
#include "native\types.hpp"
7+
#include "native\typemap.hpp"
8+
#include "native\call.hpp"
9+
#include "native\db.hpp"
1010

1111
NativeNamespaces Natives;
1212

1313
static int native_call(lua_State *L) {
1414
if(Natives.size() == 0)
1515
luaL_error(L, "Natives database is empty or was loaded incorrectly");
16-
NativeMeth *_meth = native_search(
16+
17+
NativeMeth *meth = native_search(
1718
luaL_checkstring(L, 1),
1819
luaL_checkstring(L, 2)
1920
);
20-
if(_meth == nullptr)
21+
if(meth == nullptr)
2122
luaL_error(L, "Method not found");
2223

23-
native_prepare(L, _meth, lua_gettop(L) - 2);
24-
return push_value(L, _meth->ret, nativeCall());
24+
native_prepare(L, meth, lua_gettop(L) - 2);
25+
return native_perform(L, meth);
2526
}
2627

2728
static int native_info(lua_State *L) {
28-
auto *meth = native_search(
29+
auto meth = native_search(
2930
luaL_checkstring(L, 1),
3031
luaL_checkstring(L, 2)
3132
);
@@ -61,24 +62,25 @@ static int native_new(lua_State *L) {
6162
NativeTypeInfo &type = get_type_info(type_idx);
6263
luaL_argcheck(L, type_idx > NTYPE_VOID && !type.isPointer, 1, "unsupported type passed");
6364

64-
uint size = (uint)luaL_checkinteger(L, 2);
65-
auto *hdr = (NativeObjectHeader *)lua_newuserdata(L, NATHDR_SZ + (size * 4));
66-
NATHDR_INIT(*hdr, type_idx + 1, size, false);
65+
uint count = (uint)luaL_checkinteger(L, 2);
66+
uint objsize = (count * type.size);
67+
auto hdr = (NativeObjectHeader *)lua_newuserdata(L, NATHDR_SZ + objsize);
68+
NATHDR_INIT(*hdr, type_idx + 1, count, false);
6769
luaL_setmetatable(L, LUANATIVE_OBJECT);
68-
memset(&(hdr[1]), 0, size);
70+
memset(&(hdr[1]), 0, objsize);
6971
return 1;
7072
}
7173

7274
#define VTN(idx) (float)lua_tonumber(L, idx)
73-
static int native_newvec(lua_State *L) {
75+
static int native_vector(lua_State *L) {
7476
Vector3 pos {VTN(1), VTN(2), VTN(3)};
7577
push_vector(L, &pos);
7678
return 1;
7779
}
7880

7981
#define WORLDGETALL(T, TN) { \
80-
BYTE *ptr = (BYTE *)luaL_checkudata(L, 1, LUANATIVE_OBJECT); \
81-
auto *hdr = (NativeObjectHeader *)ptr; ptr += sizeof(NativeObjectHeader); \
82+
auto ptr = (char *)luaL_checkudata(L, 1, LUANATIVE_OBJECT); \
83+
auto hdr = (NativeObjectHeader *)ptr; ptr += sizeof(NativeObjectHeader); \
8284
luaL_argcheck(L, hdr->type != T, 1, "not a" #TN " pool"); \
8385
lua_pushinteger(L, worldGetAll##TN((int *)ptr, hdr->size)); \
8486
return 1; \
@@ -93,7 +95,7 @@ static const luaL_Reg nativelib[] = {
9395
{"info", native_info},
9496

9597
{"new", native_new},
96-
{"newvector", native_newvec},
98+
{"vector", native_vector},
9799

98100
{"allobjects", native_allobjs},
99101
{"allpeds", native_allpeds},

src/native/cache.hpp

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

33
#include "thirdparty\luajit\src\lua.hpp"
4-
#include "native/types.hpp"
4+
#include "native\types.hpp"
55
#include <map>
66

77
typedef struct _RefMap {int ns, nc;} RefMap;
88
std::map<lua_State *, RefMap> ReferenceMap {};
9-
std::map<int, std::map<NativeType, std::map<int, int>>> NativeCache {};
9+
std::map<int, std::map<NativeType, std::map<NativeData, int>>> NativeCache {};
1010

11-
static int from_cache(int cache_ref, NativeType type, int id) {
11+
static int from_cache(int cache_ref, NativeType type, NativeData id) {
1212
if(NativeCache.find(cache_ref) == NativeCache.end())
1313
return 0;
1414
if(NativeCache[cache_ref].find(type) == NativeCache[cache_ref].end())

src/native/call.hpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
#include "thirdparty\ScriptHook\inc\nativeCaller.h"
44
#include "thirdparty\ScriptHook\inc\types.h"
55

6-
#include "native/db.hpp"
7-
#include "native/cache.hpp"
8-
#include "native/object.hpp"
6+
#include "native\db.hpp"
7+
#include "native\cache.hpp"
8+
#include "native\object.hpp"
99

1010
#define IS_PTR_OF(t1, ti1, t2, ti2) (ti1.isPointer && (((t1) - (t2)) == 1 || ((t1) - (ti2).superType) == 1))
1111

12-
static void get_value(lua_State *L, int idx, NativeType extype, PUINT64 val) {
12+
static void native_prepare_arg(lua_State *L, int idx, NativeType extype, PUINT64 val) {
1313
union {
1414
float f;
1515
UINT64 u64;
@@ -26,19 +26,23 @@ static void get_value(lua_State *L, int idx, NativeType extype, PUINT64 val) {
2626
extype = NTYPE_BOOL;
2727
break;
2828
case LUA_TSTRING:
29-
extype = NTYPE_STRING;
30-
break;
29+
*val = (UINT64)lua_tostring(L, idx);
30+
return;
3131
case LUA_TUSERDATA:
3232
extype = NTYPE_ANY;
3333
break;
3434
default:
3535
extype = NTYPE_VOID;
3636
break;
3737
}
38+
} else if(extype == NTYPE_ANYPTR && lua_type(L, idx) == 10) { // В "Any*" передана LUA_TCDATA
39+
*val = (UINT64)lua_topointer(L, idx);
40+
return;
3841
}
3942

4043
switch(extype) {
4144
case NTYPE_INT:
45+
case NTYPE_HASH:
4246
*val = luaL_checkinteger(L, idx);
4347
return;
4448
case NTYPE_FLOAT:
@@ -53,7 +57,7 @@ static void get_value(lua_State *L, int idx, NativeType extype, PUINT64 val) {
5357
return;
5458
}
5559

56-
auto *no = (NativeObject *)luaL_checkudata(L, idx, LUANATIVE_OBJECT);
60+
auto no = (NativeObject *)luaL_checkudata(L, idx, LUANATIVE_OBJECT);
5761
NativeTypeInfo &nti_obj = get_type_info(no->hdr.type),
5862
&nti_exp = get_type_info(extype);
5963
luaL_argcheck(L, !nti_exp.isPointer || !no->hdr.readonly, idx,
@@ -84,7 +88,7 @@ static void native_prepare(lua_State *L, NativeMeth *meth, int nargs) {
8488
if(extype == NTYPE_FLOAT && methargs - i >= 3) {
8589
if(meth->params[i + 1].type == NTYPE_FLOAT
8690
&& meth->params[i + 2].type == NTYPE_FLOAT) {
87-
auto *vec = (PUINT64)to_vector(L, idx);
91+
auto vec = (PUINT64)to_vector(L, idx);
8892
if(vec != nullptr) {
8993
for(int j = 0; j < 3; j++, i++)
9094
nativePush(vec[j]);
@@ -95,16 +99,21 @@ static void native_prepare(lua_State *L, NativeMeth *meth, int nargs) {
9599

96100
if(extype != NTYPE_VECTOR3) {
97101
UINT64 value;
98-
get_value(L, idx, extype, &value);
102+
native_prepare_arg(L, idx, extype, &value);
99103
nativePush(value);
100104
} else {
101-
auto *vec = (PUINT64)check_vector(L, idx);
105+
auto vec = (PUINT64)check_vector(L, idx);
102106
for(int j = 0; j < 3; j++)
103107
nativePush(vec[j]);
104108
}
105109
}
106110
}
107111

112+
static int native_perform(lua_State *L, NativeMeth *meth) {
113+
// TODO: Ещё какая-нибудь фигня???
114+
return push_value(L, meth->ret, nativeCall());
115+
}
116+
108117
static int generic_newindex(lua_State *L) {
109118
luaL_error(L, "Attempt to modify readonly object");
110119
return 0;
@@ -123,7 +132,7 @@ static int meth_call(lua_State *L) {
123132
NativeMeth *_meth = native_search(lua_tostring(L, -1), lua_tostring(L, -3));
124133
if(!_meth) luaL_error(L, "Method not found");
125134
native_prepare(L, _meth, nargs - 2);
126-
return push_value(L, _meth->ret, nativeCall());
135+
return native_perform(L, _meth);
127136
}
128137

129138
static const luaL_Reg methmeta[] = {

src/native/db.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include "thirdparty\json.hpp"
4-
#include "native/types.hpp"
4+
#include "native\types.hpp"
55
#include <fstream>
66

77
using json = nlohmann::json;

0 commit comments

Comments
 (0)