diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-07-25 00:49:20 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-07-25 00:49:20 +1000 |
commit | ea084165bf2f414a49a559ddf5921b9073fc464c (patch) | |
tree | c8f1263d9e11600f3632df2d0202cb884673c97e | |
parent | 8547c56ba4607e1c4efac483f97f01b8ba8c3a01 (diff) |
Optimize entityPortrait: Drawable > LuaTable instead of Drawable > Json > LuaTable
-rw-r--r-- | source/core/StarLua.cpp | 12 | ||||
-rw-r--r-- | source/core/StarLua.hpp | 4 | ||||
-rw-r--r-- | source/core/StarLuaConverters.hpp | 6 | ||||
-rw-r--r-- | source/game/StarCommandProcessor.cpp | 4 | ||||
-rw-r--r-- | source/game/StarWorldServer.cpp | 1 | ||||
-rw-r--r-- | source/game/scripting/StarWorldLuaBindings.cpp | 15 | ||||
-rw-r--r-- | 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<LuaEngine**>(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<tuple<String, unsigned>, shared_ptr<LuaProfileEntry>> 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 <typename T> struct LuaConverter<LuaNullTermWrapper<T>> : LuaConverter<T> { static LuaValue from(LuaEngine& engine, LuaNullTermWrapper<T>&& v) { auto enforcer = engine.nullTerminate(); - return LuaConverter<T>::from(std::forward<T>(v)); + return LuaConverter<T>::from(engine, std::forward<T>(v)); } static LuaValue from(LuaEngine& engine, LuaNullTermWrapper<T> const& v) { auto enforcer = engine.nullTerminate(); - return LuaConverter<T>::from(v); + return LuaConverter<T>::from(engine, v); } static LuaNullTermWrapper<T> to(LuaEngine& engine, LuaValue const& v) { - return LuaConverter<T>::to(v); + return LuaConverter<T>::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<LuaRoot>()); + auto luaRoot = make_shared<LuaRoot>(); + 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<DamageManager>(this, ServerConnectionId); m_wireProcessor = make_shared<WireProcessor>(m_worldStorage); m_luaRoot = make_shared<LuaRoot>(); + m_luaRoot->luaEngine().setNullTerminated(false); m_luaRoot->tuneAutoGarbageCollection(m_serverConfig.getFloat("luaGcPause"), m_serverConfig.getFloat("luaGcStepMultiplier")); m_sky = make_shared<Sky>(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<Maybe<String>, EntityId>("entityGender", bind(WorldEntityCallbacks::entityGender, world, _1)); callbacks.registerCallbackWithSignature<Maybe<String>, EntityId>("entityName", bind(WorldEntityCallbacks::entityName, world, _1)); callbacks.registerCallbackWithSignature<Maybe<String>, EntityId, Maybe<String>>("entityDescription", bind(WorldEntityCallbacks::entityDescription, world, _1, _2)); - callbacks.registerCallbackWithSignature<Maybe<JsonArray>, EntityId, String>("entityPortrait", bind(WorldEntityCallbacks::entityPortrait, world, _1, _2)); + callbacks.registerCallbackWithSignature<LuaNullTermWrapper<Maybe<List<Drawable>>>, EntityId, String>("entityPortrait", bind(WorldEntityCallbacks::entityPortrait, world, _1, _2)); callbacks.registerCallbackWithSignature<Maybe<String>, EntityId, String>("entityHandItem", bind(WorldEntityCallbacks::entityHandItem, world, _1, _2)); callbacks.registerCallbackWithSignature<Json, EntityId, String>("entityHandItemDescriptor", bind(WorldEntityCallbacks::entityHandItemDescriptor, world, _1, _2)); - callbacks.registerCallbackWithSignature<Maybe<String>, EntityId>("entityUniqueId", bind(WorldEntityCallbacks::entityUniqueId, world, _1)); + callbacks.registerCallbackWithSignature<LuaNullTermWrapper<Maybe<String>>, EntityId>("entityUniqueId", bind(WorldEntityCallbacks::entityUniqueId, world, _1)); callbacks.registerCallbackWithSignature<Json, EntityId, String, Maybe<Json>>("getObjectParameter", bind(WorldEntityCallbacks::getObjectParameter, world, _1, _2, _3)); callbacks.registerCallbackWithSignature<Json, EntityId, String, Maybe<Json>>("getNpcScriptParameter", bind(WorldEntityCallbacks::getNpcScriptParameter, world, _1, _2, _3)); callbacks.registerCallbackWithSignature<List<Vec2I>, EntityId>("objectSpaces", bind(WorldEntityCallbacks::objectSpaces, world, _1)); @@ -1421,14 +1421,9 @@ namespace LuaBindings { return {}; } - LuaNullTermWrapper<Maybe<JsonArray>> WorldEntityCallbacks::entityPortrait(World* world, EntityId entityId, String const& portraitMode) { - auto entity = world->entity(entityId); - - if (auto portraitEntity = as<PortraitEntity>(entity)) { - PortraitMode mode = PortraitModeNames.getLeft(portraitMode); - auto drawables = portraitEntity->portrait(mode); - return drawables.transformed(mem_fn(&Drawable::toJson)); - } + LuaNullTermWrapper<Maybe<List<Drawable>>> WorldEntityCallbacks::entityPortrait(World* world, EntityId entityId, String const& portraitMode) { + if (auto portraitEntity = as<PortraitEntity>(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<String> entityGender(World* world, EntityId entityId); Maybe<String> entityName(World* world, EntityId entityId); Maybe<String> entityDescription(World* world, EntityId entityId, Maybe<String> const& species); - LuaNullTermWrapper<Maybe<JsonArray>> entityPortrait(World* world, EntityId entityId, String const& portraitMode); + LuaNullTermWrapper<Maybe<List<Drawable>>> entityPortrait(World* world, EntityId entityId, String const& portraitMode); Maybe<String> entityHandItem(World* world, EntityId entityId, String const& handName); Json entityHandItemDescriptor(World* world, EntityId entityId, String const& handName); LuaNullTermWrapper<Maybe<String>> entityUniqueId(World* world, EntityId entityId); |