From 0a5e92ef389a67956b1075cf7e4f0c3b2461c190 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Thu, 17 Oct 2024 19:02:24 +1100 Subject: [PATCH] unify UniverseClient & WorldClient LuaRoots & let universeClient scripts intercept packets can be used for intercepting chat packets, for example! --- source/game/StarUniverseClient.cpp | 36 ++++++++++++++----- source/game/StarUniverseClient.hpp | 2 -- source/game/StarWorldClient.cpp | 18 +++++----- source/game/StarWorldClient.hpp | 5 +-- .../game/scripting/StarWorldLuaBindings.cpp | 2 ++ 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/source/game/StarUniverseClient.cpp b/source/game/StarUniverseClient.cpp index 19b78cd..d739f5a 100644 --- a/source/game/StarUniverseClient.cpp +++ b/source/game/StarUniverseClient.cpp @@ -28,11 +28,17 @@ namespace Star { UniverseClient::UniverseClient(PlayerStoragePtr playerStorage, StatisticsPtr statistics) { + auto& root = Root::singleton(); + auto assets = root.assets(); + m_storageTriggerDeadline = 0; m_playerStorage = std::move(playerStorage); m_statistics = std::move(statistics); m_pause = false; m_luaRoot = make_shared(); + + auto clientConfig = assets->json("/client.config"); + m_luaRoot->tuneAutoGarbageCollection(clientConfig.getFloat("luaGcPause"), clientConfig.getFloat("luaGcStepMultiplier")); reset(); } @@ -151,11 +157,9 @@ Maybe UniverseClient::connect(UniverseConnection connection, bool allowA m_teamClient = make_shared(m_mainPlayer, m_clientContext); m_mainPlayer->setClientContext(m_clientContext); m_mainPlayer->setStatistics(m_statistics); - m_worldClient = make_shared(m_mainPlayer); + m_worldClient = make_shared(m_mainPlayer, m_luaRoot); m_worldClient->clientState().setNetCompatibilityRules(compatibilityRules); m_worldClient->setAsyncLighting(true); - for (auto& pair : m_luaCallbacks) - m_worldClient->setLuaCallbacks(pair.first, pair.second); m_connection = std::move(connection); m_celestialDatabase = make_shared(std::move(success->celestialInformation)); @@ -489,13 +493,13 @@ uint16_t UniverseClient::maxPlayers() { } void UniverseClient::setLuaCallbacks(String const& groupName, LuaCallbacks const& callbacks) { - m_luaCallbacks[groupName] = callbacks; - if (m_worldClient) - m_worldClient->setLuaCallbacks(groupName, callbacks); + m_luaRoot->addCallbacks(groupName, callbacks); } void UniverseClient::startLua() { + m_luaRoot->restart(); setLuaCallbacks("celestial", LuaBindings::makeCelestialCallbacks(this)); + setLuaCallbacks("world", LuaBindings::makeWorldCallbacks(m_worldClient.get())); auto assets = Root::singleton().assets(); for (auto& p : assets->json("/client.config:universeScriptContexts").toObject()) { @@ -503,9 +507,6 @@ void UniverseClient::startLua() { scriptComponent->setLuaRoot(m_luaRoot); scriptComponent->setScripts(jsonToStringList(p.second.toArray())); - for (auto& pair : m_luaCallbacks) - scriptComponent->addCallbacks(pair.first, pair.second); - m_scriptContexts.set(p.first, scriptComponent); scriptComponent->init(); } @@ -662,6 +663,23 @@ void UniverseClient::setPause(bool pause) { void UniverseClient::handlePackets(List const& packets) { for (auto const& packet : packets) { try { + bool skip = false; + Maybe packetJson; + auto functionName = strf("on{}Packet", PacketTypeNames.getRight(packet->type())); + for (auto& context : m_scriptContexts) { + auto& luaContext = *context.second->context(); + auto method = luaContext.get(functionName); + if (method != LuaNil) { + if (!packetJson) + packetJson = packet->writeJson(); + if (skip = luaContext.luaTo(std::move(method)).invoke(*packetJson).maybe().value()) { + break; + } + } + } + if (skip) + continue; + if (auto clientContextUpdate = as(packet)) { m_clientContext->readUpdate(clientContextUpdate->updateData, m_clientContext->netCompatibilityRules()); m_playerStorage->applyShipUpdates(m_clientContext->playerUuid(), m_clientContext->newShipUpdates()); diff --git a/source/game/StarUniverseClient.hpp b/source/game/StarUniverseClient.hpp index 6a85cdb..3a3860d 100644 --- a/source/game/StarUniverseClient.hpp +++ b/source/game/StarUniverseClient.hpp @@ -132,8 +132,6 @@ private: SystemWorldClientPtr m_systemWorldClient; Maybe m_connection; Maybe m_serverInfo; - - StringMap m_luaCallbacks; CelestialSlaveDatabasePtr m_celestialDatabase; ClientContextPtr m_clientContext; diff --git a/source/game/StarWorldClient.cpp b/source/game/StarWorldClient.cpp index d5c6bd7..d3e5c0c 100644 --- a/source/game/StarWorldClient.cpp +++ b/source/game/StarWorldClient.cpp @@ -28,7 +28,7 @@ const std::string SECRET_BROADCAST_PUBLIC_KEY = "SecretBroadcastPublicKey"; const std::string SECRET_BROADCAST_PREFIX = "\0Broadcast\0"s; const float WorldClient::DropDist = 6.0f; -WorldClient::WorldClient(PlayerPtr mainPlayer) { +WorldClient::WorldClient(PlayerPtr mainPlayer, LuaRootPtr luaRoot) { auto& root = Root::singleton(); auto assets = root.assets(); @@ -48,7 +48,7 @@ WorldClient::WorldClient(PlayerPtr mainPlayer) { m_collisionDebug = false; m_inWorld = false; - m_luaRoot = make_shared(); + m_luaRoot = luaRoot; m_mainPlayer = mainPlayer; @@ -1076,10 +1076,6 @@ List WorldClient::getOutgoingPackets() { return std::move(m_outgoingPackets); } -void WorldClient::setLuaCallbacks(String const& groupName, LuaCallbacks const& callbacks) { - m_luaRoot->addCallbacks(groupName, callbacks); -} - void WorldClient::update(float dt) { if (!inWorld()) return; @@ -1765,8 +1761,6 @@ void WorldClient::initWorld(WorldStartPacket const& startPacket) { return m_tileArray->tile(pos); }; m_damageManager = make_shared(this, startPacket.clientId); - m_luaRoot->restart(); - m_luaRoot->tuneAutoGarbageCollection(m_clientConfig.getFloat("luaGcPause"), m_clientConfig.getFloat("luaGcStepMultiplier")); m_playerStart = startPacket.playerRespawn; m_respawnInWorld = startPacket.respawnInWorld; m_worldProperties = startPacket.worldProperties.optObject().value(); @@ -1841,8 +1835,6 @@ void WorldClient::clearWorld() { m_damageManager.reset(); - m_luaRoot->shutdown(); - m_particles.reset(); m_sky.reset(); @@ -2244,6 +2236,9 @@ LuaRootPtr WorldClient::luaRoot() { } RpcPromise WorldClient::findUniqueEntity(String const& uniqueId) { + if (!inWorld()) + return RpcPromise::createFailed("Not currently in a world"); + if (auto entity = m_entityMap->uniqueEntity(uniqueId)) return RpcPromise::createFulfilled(entity->position()); @@ -2257,6 +2252,9 @@ RpcPromise WorldClient::findUniqueEntity(String const& uniqueId) { } RpcPromise WorldClient::sendEntityMessage(Variant const& entityId, String const& message, JsonArray const& args) { + if (!inWorld()) + return RpcPromise::createFailed("Not currently in a world"); + EntityPtr entity; if (entityId.is()) entity = m_entityMap->entity(entityId.get()); diff --git a/source/game/StarWorldClient.hpp b/source/game/StarWorldClient.hpp index 2957661..502a928 100644 --- a/source/game/StarWorldClient.hpp +++ b/source/game/StarWorldClient.hpp @@ -38,7 +38,7 @@ STAR_EXCEPTION(WorldClientException, StarException); class WorldClient : public World { public: - WorldClient(PlayerPtr mainPlayer); + WorldClient(PlayerPtr mainPlayer, LuaRootPtr luaRoot); ~WorldClient(); ConnectionId connection() const override; @@ -134,9 +134,6 @@ public: void handleIncomingPackets(List const& packets); List getOutgoingPackets(); - - // Sets default callbacks in the LuaRoot. - void setLuaCallbacks(String const& groupName, LuaCallbacks const& callbacks); // Set the rendering window for this client. void setClientWindow(RectI window); diff --git a/source/game/scripting/StarWorldLuaBindings.cpp b/source/game/scripting/StarWorldLuaBindings.cpp index 08a7a43..624660f 100644 --- a/source/game/scripting/StarWorldLuaBindings.cpp +++ b/source/game/scripting/StarWorldLuaBindings.cpp @@ -356,6 +356,8 @@ namespace LuaBindings { }); if (auto clientWorld = as(world)) { + callbacks.registerCallback("inWorld", [clientWorld]() { return clientWorld->inWorld(); }); + callbacks.registerCallback("mainPlayer", [clientWorld]() { return clientWorld->clientState().playerId(); }); callbacks.registerCallback("isClient", []() { return true; }); callbacks.registerCallback("isServer", []() { return false; }); callbacks.registerCallbackWithSignature("clientWindow", bind(ClientWorldCallbacks::clientWindow, clientWorld));