add direct-scripting-data-access setting

This commit is contained in:
MihailRis 2026-05-24 13:24:58 +03:00
parent dc9ae6e99e
commit 24a2c35f41
5 changed files with 31 additions and 25 deletions

View file

@ -5,6 +5,7 @@
#include <iostream>
#include "util/stringutil.hpp"
#include "engine/Engine.hpp"
using namespace lua;
@ -369,3 +370,29 @@ void lua::remove_environment(State* L, int id) {
pushnil(L);
store_env(L, id);
}
std::string_view lua::bytearray_as_string(lua::State* L, int idx) {
const auto& settings = scripting::engine->getSettings();
if (type(L, idx) == LUA_TSTRING) {
return tolstring(L, idx);
}
pushvalue(L, idx);
if (settings.system.directScriptingDataAccess.get()) {
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;
}
}

View file

@ -828,27 +828,5 @@ namespace lua {
return create_bytearray(L, bytes.data(), bytes.size());
}
inline std::string_view bytearray_as_string(lua::State* L, int idx) {
if (type(L, idx) == LUA_TSTRING) {
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
}
std::string_view bytearray_as_string(lua::State* L, int idx);
}