diff --git a/res/modules/internal/bytearray.lua b/res/modules/internal/bytearray.lua index c7c756f1c..739bb7cea 100644 --- a/res/modules/internal/bytearray.lua +++ b/res/modules/internal/bytearray.lua @@ -215,6 +215,13 @@ FFIBytearray = { } table.merge(FFIBytearray, bytearray_methods) +local function FFIBytearray_as_ptr(bytes) + if type(bytes) == "cdata" then + return tostring(bytes.bytes):sub(27), bytes.size + end + return "0" +end + local function FFIBytearray_as_string(bytes) local t = type(bytes) if t == "cdata" then @@ -283,6 +290,7 @@ local FFIU32view = create_FFIview_class("FFIU32view", "uint32_t", 4) return { FFIBytearray = setmetatable(FFIBytearray, FFIBytearray), FFIBytearray_as_string = FFIBytearray_as_string, + FFIBytearray_as_ptr = FFIBytearray_as_ptr, FFIU16view = FFIU16view, FFII16view = FFII16view, FFIU32view = FFIU32view, diff --git a/res/scripts/stdmin.lua b/res/scripts/stdmin.lua index 905c626e7..98d937b1a 100644 --- a/res/scripts/stdmin.lua +++ b/res/scripts/stdmin.lua @@ -303,6 +303,7 @@ require "core:internal/extensions/string" local bytearray = require "core:internal/bytearray" Bytearray = bytearray.FFIBytearray Bytearray_as_string = bytearray.FFIBytearray_as_string +Bytearray_as_ptr = bytearray.FFIBytearray_as_ptr U16view = bytearray.FFIU16view I16view = bytearray.FFII16view U32view = bytearray.FFIU32view diff --git a/src/constants.hpp b/src/constants.hpp index f43314f4a..f0dc24d40 100644 --- a/src/constants.hpp +++ b/src/constants.hpp @@ -5,6 +5,8 @@ #include #include +#define VC_LUA_DIRECT_BYTEARRAY_ACCESS + inline constexpr int ENGINE_VERSION_MAJOR = 0; inline constexpr int ENGINE_VERSION_MINOR = 32; diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index a7881bef7..88fa0f021 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -833,11 +833,22 @@ namespace lua { return tolstring(L, idx); } pushvalue(L, idx); +#ifdef VC_LUA_DIRECT_BYTEARRAY_ACCESS + requireglobal(L, "Bytearray_as_ptr"); + pushvalue(L, -2); + call(L, 1, 2); + auto view = tolstring(L, -2); + uint64_t size = touinteger(L, -1); + auto ptr = (const char*)std::stoull(std::string(view), nullptr, 16); + pop(L, 3); + return std::string_view(ptr, size); +#else requireglobal(L, "Bytearray_as_string"); pushvalue(L, -2); call(L, 1, 1); auto view = tolstring(L, -1); pop(L, 2); return view; +#endif // VC_LUA_DIRECT_BYTEARRAY_ACCESS } }