Utilize the optional packet framework

master
D4VID 2 months ago
parent 410fcbf1cd
commit 1f35f3db60

@ -3,6 +3,7 @@ using CriticalPathAnalyzer.Client.Network;
using CriticalPathAnalyzer.Client.Tool;
using CriticalPathAnalyzer.Shared.Packets.S2C;
using EccsLogicWorldAPI.Client.Injectors;
using EccsLogicWorldAPI.Client.PacketIndexOrdering;
using FancyInput;
using LogicAPI.Client;
using LogicLog;
@ -33,10 +34,12 @@ namespace CriticalPathAnalyzer.Client {
}
);
// Inject packet handlers
RawPacketHandlerInjector.addPacketHandler(new AnnounceModPacketHandler(Manifest.Version));
// Register a handler for the server response packet
RawPacketHandlerInjector.addPacketHandler(new AnalyzePathResponseHandler());
// Mark it as optional so that the game works even if the mod is not present on one end
PacketIndexOrdering.markModAsOptional(GetType().Assembly);
Logger.Info("CriticalPathAnalyzer mod loaded");
}

@ -1,21 +0,0 @@
using System;
using CriticalPathAnalyzer.Shared.Packets.S2C;
using LogicWorld.SharedCode.Networking;
namespace CriticalPathAnalyzer.Client.Network {
public class AnnounceModPacketHandler : PacketHandler<AnnounceModPresence> {
private readonly Version _version;
public AnnounceModPacketHandler(Version version) {
_version = version;
}
public override void Handle(AnnounceModPresence packet, HandlerContext context) {
if (packet.Version == _version) {
CriticalPathAnalyzerClient.LoggerInstance.Info($"Mod is supported on the server: version={packet.Version}");
} else {
CriticalPathAnalyzerClient.LoggerInstance.Error($"Mod version mismatch: client={_version}, server={packet.Version}");
}
}
}
}

@ -1,6 +1,7 @@
using System;
using CriticalPathAnalyzer.Shared.Packets.C2S;
using CriticalPathAnalyzer.Shared.Packets.S2C;
using EccsLogicWorldAPI.Client.PacketIndexOrdering;
using JimmysUnityUtilities;
using LogicAPI.Data;
using LogicLog;
@ -137,6 +138,11 @@ namespace CriticalPathAnalyzer.Client.Tool {
return;
}
if (!PacketIndexOrdering.doesServerSupportPacket(typeof(AnalyzePathRequest))) {
_logger.Error("Server doesn't support this mod");
return;
}
_currentRequestGuid = Guid.NewGuid();
Instances.SendData.Send(new AnalyzePathRequest {
RequestGuid = _currentRequestGuid,

@ -1,26 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CriticalPathAnalyzer.Server.Network;
using CriticalPathAnalyzer.Server.Tool;
using CriticalPathAnalyzer.Shared.Packets.S2C;
using EccsLogicWorldAPI.Server;
using EccsLogicWorldAPI.Server.Injectors;
using EccsLogicWorldAPI.Shared.PacketWrapper;
using EccsLogicWorldAPI.Server.PacketIndexOrdering;
using LogicAPI.Data;
using LogicAPI.Networking;
using LogicAPI.Networking.Packets.Initialization;
using LogicAPI.Server;
using LogicAPI.Server.Networking;
using LogicAPI.Server.Networking.ClientVerification;
using LogicLog;
using LogicWorld.SharedCode.Networking;
namespace CriticalPathAnalyzer.Server {
// ReSharper disable once ClassNeverInstantiated.Global
public class CriticalPathAnalyzerServer : ServerMod, IClientVerifier {
public class CriticalPathAnalyzerServer : ServerMod {
private NetworkServer _networkServer;
private readonly Dictionary<string, bool> _playerHasWireTracer = new Dictionary<string, bool>();
public static ILogicLogger LoggerInstance;
@ -33,35 +27,11 @@ namespace CriticalPathAnalyzer.Server {
throw new Exception("Could not get Service 'NetworkServer'.");
}
// Inject client verifier:
RawJoinVerifierInjector.addVerifier(this);
PacketHandlerManager.getCustomPacketHandler<ClientLoadedWorldPacket>()
.addHandlerToEnd(new ClientJoinedPacketHandler(this));
// Register a handler for the client request packet
RawPacketHandlerInjector.addPacketHandler(new CriticalPathAnalyzerRequestHandler(this));
}
/// <summary>
/// Gets called before a player (client) joins the server.
/// Used to check if the player has this mod installed
/// </summary>
public void Verify(VerificationContext ctx) {
bool hasMod = ctx.ApprovalPacket.ClientMods.Contains((Manifest.ID, Manifest.Version));
string playerName = ctx.PlayerID.Name;
_playerHasWireTracer[playerName] = hasMod;
LoggerInstance.Info($"Verifying player {playerName}: hasMod={hasMod}");
}
/// <summary>
/// Gets called after verifying a connecting player.
/// If the player has this mod installed, send a packet informing about the mod's presence on the server.
/// </summary>
public void PlayerJoined(PlayerData playerData, HandlerContext context) {
_playerHasWireTracer.TryGetValue(playerData.Name, out bool hasWireTracer);
if (hasWireTracer) {
_networkServer.Send(context.Sender, new AnnounceModPresence() {
Version = Manifest.Version
});
}
// Mark it as optional so that the game works even if the mod is not present on one end
PacketIndexOrdering.markModAsOptional(GetType().Assembly);
}
/// <summary>

@ -1,16 +0,0 @@
using EccsLogicWorldAPI.Shared.PacketWrapper;
using LogicAPI.Networking.Packets.Initialization;
using LogicWorld.SharedCode.Networking;
namespace CriticalPathAnalyzer.Server.Network {
public class ClientJoinedPacketHandler : CustomPacketHandler<ClientLoadedWorldPacket> {
private readonly CriticalPathAnalyzerServer _criticalPathAnalyzerServer;
public ClientJoinedPacketHandler(CriticalPathAnalyzerServer criticalPathAnalyzerServer) {
_criticalPathAnalyzerServer = criticalPathAnalyzerServer;
}
public override void handle(ref bool isCancelled, ref ClientLoadedWorldPacket packet, HandlerContext context) {
_criticalPathAnalyzerServer.PlayerJoined(packet.PlayerData, context);
}
}
}

@ -1,10 +0,0 @@
using System;
using LogicAPI.Networking.Packets;
using MessagePack;
namespace CriticalPathAnalyzer.Shared.Packets.S2C {
[MessagePackObject]
public class AnnounceModPresence : Packet {
[Key(0)] public Version Version;
}
}
Loading…
Cancel
Save