From ea084165bf2f414a49a559ddf5921b9073fc464c Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Tue, 25 Jul 2023 00:49:20 +1000 Subject: [PATCH] Optimize entityPortrait: Drawable > LuaTable instead of Drawable > Json > LuaTable --- source/core/StarLua.cpp | 12 ++++++++---- source/core/StarLua.hpp | 4 +++- source/core/StarLuaConverters.hpp | 6 +++--- source/game/StarCommandProcessor.cpp | 4 +++- source/game/StarWorldServer.cpp | 1 + source/game/scripting/StarWorldLuaBindings.cpp | 15 +++++---------- source/game/scripting/StarWorldLuaBindings.hpp | 3 ++- 7 files changed, 25 insertions(+), 20 deletions(-) diff --git a/source/core/StarLua.cpp b/source/core/StarLua.cpp index 901e875..41355b1 100644 --- a/source/core/StarLua.cpp +++ b/source/core/StarLua.cpp @@ -461,7 +461,7 @@ ByteArray LuaEngine::compile(ByteArray const& contents, String const& name) { LuaString LuaEngine::createString(String const& str) { lua_checkstack(m_state, 1); - if (m_nullTerminated) + if (m_nullTerminated > 0) lua_pushstring(m_state, str.utf8Ptr()); else lua_pushlstring(m_state, str.utf8Ptr(), str.utf8Size()); @@ -556,6 +556,10 @@ LuaNullEnforcer LuaEngine::nullTerminate() { return LuaNullEnforcer(*this); } +void LuaEngine::setNullTerminated(bool nullTerminated) { + m_nullTerminated = nullTerminated ? 0 : INT_MIN; +} + LuaEngine* LuaEngine::luaEnginePtr(lua_State* state) { return (*reinterpret_cast(lua_getextraspace(state))); } @@ -723,7 +727,7 @@ char const* LuaEngine::stringPtr(int handleIndex) { } size_t LuaEngine::stringLength(int handleIndex) { - if (m_nullTerminated) + if (m_nullTerminated > 0) return strlen(lua_tostring(m_handleThread, handleIndex)); else { size_t len = 0; @@ -733,7 +737,7 @@ size_t LuaEngine::stringLength(int handleIndex) { } String LuaEngine::string(int handleIndex) { - if (m_nullTerminated) + if (m_nullTerminated > 0) return String(lua_tostring(m_handleThread, handleIndex)); else { size_t len = 0; @@ -743,7 +747,7 @@ String LuaEngine::string(int handleIndex) { } StringView LuaEngine::stringView(int handleIndex) { - if (m_nullTerminated) + if (m_nullTerminated > 0) return StringView(lua_tostring(m_handleThread, handleIndex)); else { size_t len = 0; diff --git a/source/core/StarLua.hpp b/source/core/StarLua.hpp index 0deb76f..792dc09 100644 --- a/source/core/StarLua.hpp +++ b/source/core/StarLua.hpp @@ -600,6 +600,8 @@ public: // Enforce null-terminated string conversion as long as the returned enforcer object is in scope. LuaNullEnforcer nullTerminate(); + // Disables null-termination enforcement + void setNullTerminated(bool nullTerminated); private: friend struct LuaDetail::LuaHandle; @@ -729,7 +731,7 @@ private: uint64_t m_instructionCount; unsigned m_recursionLevel; unsigned m_recursionLimit; - unsigned m_nullTerminated; + int m_nullTerminated; HashMap, shared_ptr> m_profileEntries; }; diff --git a/source/core/StarLuaConverters.hpp b/source/core/StarLuaConverters.hpp index 72ab763..3270d16 100644 --- a/source/core/StarLuaConverters.hpp +++ b/source/core/StarLuaConverters.hpp @@ -15,16 +15,16 @@ template struct LuaConverter> : LuaConverter { static LuaValue from(LuaEngine& engine, LuaNullTermWrapper&& v) { auto enforcer = engine.nullTerminate(); - return LuaConverter::from(std::forward(v)); + return LuaConverter::from(engine, std::forward(v)); } static LuaValue from(LuaEngine& engine, LuaNullTermWrapper const& v) { auto enforcer = engine.nullTerminate(); - return LuaConverter::from(v); + return LuaConverter::from(engine, v); } static LuaNullTermWrapper to(LuaEngine& engine, LuaValue const& v) { - return LuaConverter::to(v); + return LuaConverter::to(engine, v); } }; diff --git a/source/game/StarCommandProcessor.cpp b/source/game/StarCommandProcessor.cpp index 0c7a8c8..4775a1e 100644 --- a/source/game/StarCommandProcessor.cpp +++ b/source/game/StarCommandProcessor.cpp @@ -30,7 +30,9 @@ CommandProcessor::CommandProcessor(UniverseServer* universe) m_scriptComponent.addCallbacks("universe", LuaBindings::makeUniverseServerCallbacks(m_universe)); m_scriptComponent.addCallbacks("CommandProcessor", makeCommandCallbacks()); m_scriptComponent.setScripts(jsonToStringList(assets->json("/universe_server.config:commandProcessorScripts"))); - m_scriptComponent.setLuaRoot(make_shared()); + auto luaRoot = make_shared(); + luaRoot->luaEngine().setNullTerminated(false); + m_scriptComponent.setLuaRoot(luaRoot); m_scriptComponent.init(); } diff --git a/source/game/StarWorldServer.cpp b/source/game/StarWorldServer.cpp index 0685e9e..87b9ba3 100644 --- a/source/game/StarWorldServer.cpp +++ b/source/game/StarWorldServer.cpp @@ -1238,6 +1238,7 @@ void WorldServer::init(bool firstTime) { m_damageManager = make_shared(this, ServerConnectionId); m_wireProcessor = make_shared(m_worldStorage); m_luaRoot = make_shared(); + m_luaRoot->luaEngine().setNullTerminated(false); m_luaRoot->tuneAutoGarbageCollection(m_serverConfig.getFloat("luaGcPause"), m_serverConfig.getFloat("luaGcStepMultiplier")); m_sky = make_shared(m_worldTemplate->skyParameters(), false); diff --git a/source/game/scripting/StarWorldLuaBindings.cpp b/source/game/scripting/StarWorldLuaBindings.cpp index 087f88c..4645eb7 100644 --- a/source/game/scripting/StarWorldLuaBindings.cpp +++ b/source/game/scripting/StarWorldLuaBindings.cpp @@ -491,10 +491,10 @@ namespace LuaBindings { callbacks.registerCallbackWithSignature, EntityId>("entityGender", bind(WorldEntityCallbacks::entityGender, world, _1)); callbacks.registerCallbackWithSignature, EntityId>("entityName", bind(WorldEntityCallbacks::entityName, world, _1)); callbacks.registerCallbackWithSignature, EntityId, Maybe>("entityDescription", bind(WorldEntityCallbacks::entityDescription, world, _1, _2)); - callbacks.registerCallbackWithSignature, EntityId, String>("entityPortrait", bind(WorldEntityCallbacks::entityPortrait, world, _1, _2)); + callbacks.registerCallbackWithSignature>>, EntityId, String>("entityPortrait", bind(WorldEntityCallbacks::entityPortrait, world, _1, _2)); callbacks.registerCallbackWithSignature, EntityId, String>("entityHandItem", bind(WorldEntityCallbacks::entityHandItem, world, _1, _2)); callbacks.registerCallbackWithSignature("entityHandItemDescriptor", bind(WorldEntityCallbacks::entityHandItemDescriptor, world, _1, _2)); - callbacks.registerCallbackWithSignature, EntityId>("entityUniqueId", bind(WorldEntityCallbacks::entityUniqueId, world, _1)); + callbacks.registerCallbackWithSignature>, EntityId>("entityUniqueId", bind(WorldEntityCallbacks::entityUniqueId, world, _1)); callbacks.registerCallbackWithSignature>("getObjectParameter", bind(WorldEntityCallbacks::getObjectParameter, world, _1, _2, _3)); callbacks.registerCallbackWithSignature>("getNpcScriptParameter", bind(WorldEntityCallbacks::getNpcScriptParameter, world, _1, _2, _3)); callbacks.registerCallbackWithSignature, EntityId>("objectSpaces", bind(WorldEntityCallbacks::objectSpaces, world, _1)); @@ -1421,14 +1421,9 @@ namespace LuaBindings { return {}; } - LuaNullTermWrapper> WorldEntityCallbacks::entityPortrait(World* world, EntityId entityId, String const& portraitMode) { - auto entity = world->entity(entityId); - - if (auto portraitEntity = as(entity)) { - PortraitMode mode = PortraitModeNames.getLeft(portraitMode); - auto drawables = portraitEntity->portrait(mode); - return drawables.transformed(mem_fn(&Drawable::toJson)); - } + LuaNullTermWrapper>> WorldEntityCallbacks::entityPortrait(World* world, EntityId entityId, String const& portraitMode) { + if (auto portraitEntity = as(world->entity(entityId))) + return portraitEntity->portrait(PortraitModeNames.getLeft(portraitMode)); return {}; } diff --git a/source/game/scripting/StarWorldLuaBindings.hpp b/source/game/scripting/StarWorldLuaBindings.hpp index e2d082c..e308ded 100644 --- a/source/game/scripting/StarWorldLuaBindings.hpp +++ b/source/game/scripting/StarWorldLuaBindings.hpp @@ -5,6 +5,7 @@ #include "StarRect.hpp" #include "StarPoly.hpp" #include "StarColor.hpp" +#include "StarDrawable.hpp" #include "StarGameTypes.hpp" #include "StarCollisionBlock.hpp" #include "StarLua.hpp" @@ -122,7 +123,7 @@ namespace LuaBindings { Maybe entityGender(World* world, EntityId entityId); Maybe entityName(World* world, EntityId entityId); Maybe entityDescription(World* world, EntityId entityId, Maybe const& species); - LuaNullTermWrapper> entityPortrait(World* world, EntityId entityId, String const& portraitMode); + LuaNullTermWrapper>> entityPortrait(World* world, EntityId entityId, String const& portraitMode); Maybe entityHandItem(World* world, EntityId entityId, String const& handName); Json entityHandItemDescriptor(World* world, EntityId entityId, String const& handName); LuaNullTermWrapper> entityUniqueId(World* world, EntityId entityId);