diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2024-10-17 19:02:24 +1100 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2024-10-17 19:02:24 +1100 |
commit | 0a5e92ef389a67956b1075cf7e4f0c3b2461c190 (patch) | |
tree | af463a81a8c67650a33d150bd50b87f01e0f2d2f | |
parent | 9ba9eb2ac33a53a8fd50ce14d40be00d42a9f7af (diff) |
unify UniverseClient & WorldClient LuaRoots & let universeClient scripts intercept packets
can be used for intercepting chat packets, for example!
-rw-r--r-- | source/game/StarUniverseClient.cpp | 36 | ||||
-rw-r--r-- | source/game/StarUniverseClient.hpp | 2 | ||||
-rw-r--r-- | source/game/StarWorldClient.cpp | 18 | ||||
-rw-r--r-- | source/game/StarWorldClient.hpp | 5 | ||||
-rw-r--r-- | source/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<LuaRoot>(); + + auto clientConfig = assets->json("/client.config"); + m_luaRoot->tuneAutoGarbageCollection(clientConfig.getFloat("luaGcPause"), clientConfig.getFloat("luaGcStepMultiplier")); reset(); } @@ -151,11 +157,9 @@ Maybe<String> UniverseClient::connect(UniverseConnection connection, bool allowA m_teamClient = make_shared<TeamClient>(m_mainPlayer, m_clientContext); m_mainPlayer->setClientContext(m_clientContext); m_mainPlayer->setStatistics(m_statistics); - m_worldClient = make_shared<WorldClient>(m_mainPlayer); + m_worldClient = make_shared<WorldClient>(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<CelestialSlaveDatabase>(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<PacketPtr> const& packets) { for (auto const& packet : packets) { try { + bool skip = false; + Maybe<Json> 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<LuaFunction>(std::move(method)).invoke<LuaValue>(*packetJson).maybe<LuaBoolean>().value()) { + break; + } + } + } + if (skip) + continue; + if (auto clientContextUpdate = as<ClientContextUpdatePacket>(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<UniverseConnection> m_connection; Maybe<ServerInfo> m_serverInfo; - - StringMap<LuaCallbacks> 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<LuaRoot>(); + m_luaRoot = luaRoot; m_mainPlayer = mainPlayer; @@ -1076,10 +1076,6 @@ List<PacketPtr> 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<DamageManager>(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<Vec2F> WorldClient::findUniqueEntity(String const& uniqueId) { + if (!inWorld()) + return RpcPromise<Vec2F>::createFailed("Not currently in a world"); + if (auto entity = m_entityMap->uniqueEntity(uniqueId)) return RpcPromise<Vec2F>::createFulfilled(entity->position()); @@ -2257,6 +2252,9 @@ RpcPromise<Vec2F> WorldClient::findUniqueEntity(String const& uniqueId) { } RpcPromise<Json> WorldClient::sendEntityMessage(Variant<EntityId, String> const& entityId, String const& message, JsonArray const& args) { + if (!inWorld()) + return RpcPromise<Json>::createFailed("Not currently in a world"); + EntityPtr entity; if (entityId.is<EntityId>()) entity = m_entityMap->entity(entityId.get<EntityId>()); 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<PacketPtr> const& packets); List<PacketPtr> 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<WorldClient>(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<RectI>("clientWindow", bind(ClientWorldCallbacks::clientWindow, clientWorld)); |