Merge pull request #43 from ErodeesFleurs/sendPacket

Add the Lua function for sending packet.
This commit is contained in:
Kae 2024-03-19 18:24:23 +11:00 committed by GitHub
commit e068172a09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 32 additions and 0 deletions

View File

@ -167,6 +167,22 @@ PacketPtr createPacket(PacketType type) {
}
}
PacketPtr createPacket(PacketType type, JsonArray const& args) {
switch (type) {
case PacketType::Pause: return make_shared<PausePacket>(args[0].toBool());
case PacketType::ServerInfo: return make_shared<ServerInfoPacket>(args[0].toUInt(), args[1].toUInt());
case PacketType::GiveItem: return make_shared<GiveItemPacket>(ItemDescriptor(args[0]));
case PacketType::UpdateTileProtection: return make_shared<UpdateTileProtectionPacket>(args[0].toUInt(), args[1].toBool());
case PacketType::SetDungeonGravity: return make_shared<SetDungeonGravityPacket>(args[0].toUInt(), args[0].toFloat());
case PacketType::SetDungeonBreathable: return make_shared<SetDungeonBreathablePacket>(args[0].toUInt(), args[0].toBool());
case PacketType::SetPlayerStart: return make_shared<SetPlayerStartPacket>(Vec2F{args[0].toArray()[0].toFloat(), args[0].toArray()[0].toFloat()}, args[1].toBool());
case PacketType::EntityMessage: return make_shared<EntityMessagePacket>(EntityId(args[0].toUInt()), args[1].toString(), args[2].toArray(), Uuid(args[3].toString()));
case PacketType::UpdateWorldProperties: return make_shared<UpdateWorldPropertiesPacket>(args[0].toObject());
default:
throw StarPacketException(strf("Unrecognized packet type {}", (unsigned int)type));
}
}
ProtocolRequestPacket::ProtocolRequestPacket()
: requestProtocolVersion(0) {}

View File

@ -139,6 +139,7 @@ struct Packet {
};
PacketPtr createPacket(PacketType type);
PacketPtr createPacket(PacketType type, JsonArray const& args);
template <PacketType PacketT>
struct PacketBase : public Packet {

View File

@ -473,6 +473,11 @@ bool UniverseServer::updatePlanetType(CelestialCoordinate const& coordinate, Str
return false;
}
void UniverseServer::sendPacket(ConnectionId clientId, PacketPtr packet) {
RecursiveMutexLocker locker(m_mainLock);
m_connectionServer->sendPackets(clientId, {packet});
}
void UniverseServer::run() {
Logger::info("UniverseServer: Starting UniverseServer with UUID: {}", m_universeSettings->uuid().hex());

View File

@ -104,6 +104,8 @@ public:
bool updatePlanetType(CelestialCoordinate const& coordinate, String const& newType, String const& weatherBiome);
void sendPacket(ConnectionId clientId, PacketPtr packet);
protected:
virtual void run();

View File

@ -21,6 +21,7 @@ LuaCallbacks LuaBindings::makeUniverseServerCallbacks(UniverseServer* universe)
callbacks.registerCallbackWithSignature<bool, String>("isWorldActive", bind(UniverseServerCallbacks::isWorldActive, universe, _1));
callbacks.registerCallbackWithSignature<StringList>("activeWorlds", bind(UniverseServerCallbacks::activeWorlds, universe));
callbacks.registerCallbackWithSignature<RpcThreadPromise<Json>, String, String, LuaVariadic<Json>>("sendWorldMessage", bind(UniverseServerCallbacks::sendWorldMessage, universe, _1, _2, _3));
callbacks.registerCallbackWithSignature<void, ConnectionId, String, Json>("sendPacket", bind(UniverseServerCallbacks::sendPacket, universe, _1, _2, _3));
return callbacks;
}
@ -126,4 +127,10 @@ RpcThreadPromise<Json> LuaBindings::UniverseServerCallbacks::sendWorldMessage(Un
return universe->sendWorldMessage(parseWorldId(worldId), message, JsonArray::from(std::move(args)));
}
void LuaBindings::UniverseServerCallbacks::sendPacket(UniverseServer* universe, ConnectionId clientId, String const& packetTypeName, Json const& args) {
auto packetType = PacketTypeNames.getLeft(packetTypeName);
auto packet = createPacket(packetType, args.toArray());
universe->sendPacket(clientId, packet);
}
}

View File

@ -25,6 +25,7 @@ namespace LuaBindings {
bool isWorldActive(UniverseServer* universe, String const& worldId);
StringList activeWorlds(UniverseServer* universe);
RpcThreadPromise<Json> sendWorldMessage(UniverseServer* universe, String const& worldId, String const& message, LuaVariadic<Json> args);
void sendPacket(UniverseServer* universe, ConnectionId clientId, String const& packetTypeName, Json const& args);
}
}
}