Optimize entityPortrait: Drawable > LuaTable instead of Drawable > Json > LuaTable

This commit is contained in:
Kae 2023-07-25 00:49:20 +10:00
parent 8547c56ba4
commit ea084165bf
7 changed files with 25 additions and 20 deletions

View File

@ -461,7 +461,7 @@ ByteArray LuaEngine::compile(ByteArray const& contents, String const& name) {
LuaString LuaEngine::createString(String const& str) { LuaString LuaEngine::createString(String const& str) {
lua_checkstack(m_state, 1); lua_checkstack(m_state, 1);
if (m_nullTerminated) if (m_nullTerminated > 0)
lua_pushstring(m_state, str.utf8Ptr()); lua_pushstring(m_state, str.utf8Ptr());
else else
lua_pushlstring(m_state, str.utf8Ptr(), str.utf8Size()); lua_pushlstring(m_state, str.utf8Ptr(), str.utf8Size());
@ -556,6 +556,10 @@ LuaNullEnforcer LuaEngine::nullTerminate() {
return LuaNullEnforcer(*this); return LuaNullEnforcer(*this);
} }
void LuaEngine::setNullTerminated(bool nullTerminated) {
m_nullTerminated = nullTerminated ? 0 : INT_MIN;
}
LuaEngine* LuaEngine::luaEnginePtr(lua_State* state) { LuaEngine* LuaEngine::luaEnginePtr(lua_State* state) {
return (*reinterpret_cast<LuaEngine**>(lua_getextraspace(state))); return (*reinterpret_cast<LuaEngine**>(lua_getextraspace(state)));
} }
@ -723,7 +727,7 @@ char const* LuaEngine::stringPtr(int handleIndex) {
} }
size_t LuaEngine::stringLength(int handleIndex) { size_t LuaEngine::stringLength(int handleIndex) {
if (m_nullTerminated) if (m_nullTerminated > 0)
return strlen(lua_tostring(m_handleThread, handleIndex)); return strlen(lua_tostring(m_handleThread, handleIndex));
else { else {
size_t len = 0; size_t len = 0;
@ -733,7 +737,7 @@ size_t LuaEngine::stringLength(int handleIndex) {
} }
String LuaEngine::string(int handleIndex) { String LuaEngine::string(int handleIndex) {
if (m_nullTerminated) if (m_nullTerminated > 0)
return String(lua_tostring(m_handleThread, handleIndex)); return String(lua_tostring(m_handleThread, handleIndex));
else { else {
size_t len = 0; size_t len = 0;
@ -743,7 +747,7 @@ String LuaEngine::string(int handleIndex) {
} }
StringView LuaEngine::stringView(int handleIndex) { StringView LuaEngine::stringView(int handleIndex) {
if (m_nullTerminated) if (m_nullTerminated > 0)
return StringView(lua_tostring(m_handleThread, handleIndex)); return StringView(lua_tostring(m_handleThread, handleIndex));
else { else {
size_t len = 0; size_t len = 0;

View File

@ -600,6 +600,8 @@ public:
// Enforce null-terminated string conversion as long as the returned enforcer object is in scope. // Enforce null-terminated string conversion as long as the returned enforcer object is in scope.
LuaNullEnforcer nullTerminate(); LuaNullEnforcer nullTerminate();
// Disables null-termination enforcement
void setNullTerminated(bool nullTerminated);
private: private:
friend struct LuaDetail::LuaHandle; friend struct LuaDetail::LuaHandle;
@ -729,7 +731,7 @@ private:
uint64_t m_instructionCount; uint64_t m_instructionCount;
unsigned m_recursionLevel; unsigned m_recursionLevel;
unsigned m_recursionLimit; unsigned m_recursionLimit;
unsigned m_nullTerminated; int m_nullTerminated;
HashMap<tuple<String, unsigned>, shared_ptr<LuaProfileEntry>> m_profileEntries; HashMap<tuple<String, unsigned>, shared_ptr<LuaProfileEntry>> m_profileEntries;
}; };

View File

@ -15,16 +15,16 @@ template <typename T>
struct LuaConverter<LuaNullTermWrapper<T>> : LuaConverter<T> { struct LuaConverter<LuaNullTermWrapper<T>> : LuaConverter<T> {
static LuaValue from(LuaEngine& engine, LuaNullTermWrapper<T>&& v) { static LuaValue from(LuaEngine& engine, LuaNullTermWrapper<T>&& v) {
auto enforcer = engine.nullTerminate(); 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) { static LuaValue from(LuaEngine& engine, LuaNullTermWrapper<T> const& v) {
auto enforcer = engine.nullTerminate(); auto enforcer = engine.nullTerminate();
return LuaConverter<T>::from(v); return LuaConverter<T>::from(engine, v);
} }
static LuaNullTermWrapper<T> to(LuaEngine& engine, LuaValue const& v) { static LuaNullTermWrapper<T> to(LuaEngine& engine, LuaValue const& v) {
return LuaConverter<T>::to(v); return LuaConverter<T>::to(engine, v);
} }
}; };

View File

@ -30,7 +30,9 @@ CommandProcessor::CommandProcessor(UniverseServer* universe)
m_scriptComponent.addCallbacks("universe", LuaBindings::makeUniverseServerCallbacks(m_universe)); m_scriptComponent.addCallbacks("universe", LuaBindings::makeUniverseServerCallbacks(m_universe));
m_scriptComponent.addCallbacks("CommandProcessor", makeCommandCallbacks()); m_scriptComponent.addCallbacks("CommandProcessor", makeCommandCallbacks());
m_scriptComponent.setScripts(jsonToStringList(assets->json("/universe_server.config:commandProcessorScripts"))); 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(); m_scriptComponent.init();
} }

View File

@ -1238,6 +1238,7 @@ void WorldServer::init(bool firstTime) {
m_damageManager = make_shared<DamageManager>(this, ServerConnectionId); m_damageManager = make_shared<DamageManager>(this, ServerConnectionId);
m_wireProcessor = make_shared<WireProcessor>(m_worldStorage); m_wireProcessor = make_shared<WireProcessor>(m_worldStorage);
m_luaRoot = make_shared<LuaRoot>(); m_luaRoot = make_shared<LuaRoot>();
m_luaRoot->luaEngine().setNullTerminated(false);
m_luaRoot->tuneAutoGarbageCollection(m_serverConfig.getFloat("luaGcPause"), m_serverConfig.getFloat("luaGcStepMultiplier")); m_luaRoot->tuneAutoGarbageCollection(m_serverConfig.getFloat("luaGcPause"), m_serverConfig.getFloat("luaGcStepMultiplier"));
m_sky = make_shared<Sky>(m_worldTemplate->skyParameters(), false); m_sky = make_shared<Sky>(m_worldTemplate->skyParameters(), false);

View File

@ -491,10 +491,10 @@ namespace LuaBindings {
callbacks.registerCallbackWithSignature<Maybe<String>, EntityId>("entityGender", bind(WorldEntityCallbacks::entityGender, world, _1)); 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>("entityName", bind(WorldEntityCallbacks::entityName, world, _1));
callbacks.registerCallbackWithSignature<Maybe<String>, EntityId, Maybe<String>>("entityDescription", bind(WorldEntityCallbacks::entityDescription, world, _1, _2)); 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<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<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>>("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<Json, EntityId, String, Maybe<Json>>("getNpcScriptParameter", bind(WorldEntityCallbacks::getNpcScriptParameter, world, _1, _2, _3));
callbacks.registerCallbackWithSignature<List<Vec2I>, EntityId>("objectSpaces", bind(WorldEntityCallbacks::objectSpaces, world, _1)); callbacks.registerCallbackWithSignature<List<Vec2I>, EntityId>("objectSpaces", bind(WorldEntityCallbacks::objectSpaces, world, _1));
@ -1421,14 +1421,9 @@ namespace LuaBindings {
return {}; return {};
} }
LuaNullTermWrapper<Maybe<JsonArray>> WorldEntityCallbacks::entityPortrait(World* world, EntityId entityId, String const& portraitMode) { LuaNullTermWrapper<Maybe<List<Drawable>>> WorldEntityCallbacks::entityPortrait(World* world, EntityId entityId, String const& portraitMode) {
auto entity = world->entity(entityId); if (auto portraitEntity = as<PortraitEntity>(world->entity(entityId)))
return portraitEntity->portrait(PortraitModeNames.getLeft(portraitMode));
if (auto portraitEntity = as<PortraitEntity>(entity)) {
PortraitMode mode = PortraitModeNames.getLeft(portraitMode);
auto drawables = portraitEntity->portrait(mode);
return drawables.transformed(mem_fn(&Drawable::toJson));
}
return {}; return {};
} }

View File

@ -5,6 +5,7 @@
#include "StarRect.hpp" #include "StarRect.hpp"
#include "StarPoly.hpp" #include "StarPoly.hpp"
#include "StarColor.hpp" #include "StarColor.hpp"
#include "StarDrawable.hpp"
#include "StarGameTypes.hpp" #include "StarGameTypes.hpp"
#include "StarCollisionBlock.hpp" #include "StarCollisionBlock.hpp"
#include "StarLua.hpp" #include "StarLua.hpp"
@ -122,7 +123,7 @@ namespace LuaBindings {
Maybe<String> entityGender(World* world, EntityId entityId); Maybe<String> entityGender(World* world, EntityId entityId);
Maybe<String> entityName(World* world, EntityId entityId); Maybe<String> entityName(World* world, EntityId entityId);
Maybe<String> entityDescription(World* world, EntityId entityId, Maybe<String> const& species); 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); Maybe<String> entityHandItem(World* world, EntityId entityId, String const& handName);
Json entityHandItemDescriptor(World* world, EntityId entityId, String const& handName); Json entityHandItemDescriptor(World* world, EntityId entityId, String const& handName);
LuaNullTermWrapper<Maybe<String>> entityUniqueId(World* world, EntityId entityId); LuaNullTermWrapper<Maybe<String>> entityUniqueId(World* world, EntityId entityId);