unify UniverseClient & WorldClient LuaRoots & let universeClient scripts intercept packets
can be used for intercepting chat packets, for example!
This commit is contained in:
parent
9ba9eb2ac3
commit
0a5e92ef38
@ -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());
|
||||
|
@ -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;
|
||||
|
@ -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>());
|
||||
|
@ -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);
|
||||
|
@ -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));
|
||||
|
Loading…
x
Reference in New Issue
Block a user