Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions Source/Client/Factions/FactionCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Multiplayer.API;
using Multiplayer.Client.Util;
using Multiplayer.Common;
using Multiplayer.Common.Networking.Packet;
using RimWorld;
using RimWorld.Planet;
using UnityEngine;
Expand Down Expand Up @@ -75,11 +76,7 @@ public static void CreateFaction(int playerId, FactionCreationData creationData)
InitLocalVisuals(scenario, newMap);

// todo setting faction of self
Multiplayer.Client.Send(
Packets.Client_SetFaction,
Multiplayer.session.playerId,
newFaction.loadID
);
Multiplayer.Client.Send(new ClientSetFactionPacket(Multiplayer.session.playerId, newFaction.loadID));
}
}, "GeneratingMap", doAsynchronously: true, GameAndMapInitExceptionHandlers.ErrorWhileGeneratingMap);
}
Expand All @@ -106,7 +103,7 @@ private static Map GenerateNewMap(PlanetTile tile, Scenario scenario, bool setup
Find.GameInitData.playerFaction = null;
Find.GameInitData.PrepForMapGen();

// ScenPart_PlayerFaction --> PreMapGenerate
// ScenPart_PlayerFaction --> PreMapGenerate

Settlement settlement = (Settlement)WorldObjectMaker.MakeWorldObject(WorldObjectDefOf.Settlement);
settlement.Tile = tile;
Expand Down Expand Up @@ -203,7 +200,7 @@ private static void PrepareGameInitData(int sessionId, Scenario scenario, bool s
pawnStore.Remove(sessionId);
}
}

private static Faction NewFactionWithIdeo(string name, Color color, FactionDef def, IdeologyData chooseIdeoInfo)
{
var faction = new Faction
Expand All @@ -212,7 +209,7 @@ private static Faction NewFactionWithIdeo(string name, Color color, FactionDef d
def = def,
Name = name,
color = color,
hidden = true,
hidden = true,
};

faction.ideos = new FactionIdeosTracker(faction);
Expand Down
8 changes: 3 additions & 5 deletions Source/Client/Factions/FactionSidebar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Text;
using Multiplayer.Client.Factions;
using Multiplayer.Client.Util;
using Multiplayer.Common;
using Multiplayer.Common.Networking.Packet;
using RimWorld;
using RimWorld.Planet;
using UnityEngine;
Expand Down Expand Up @@ -105,11 +105,9 @@ private static void DrawFactionChooser(Rect inRect)
Current.Game.CurrentMap = factionHome;
Find.World.renderer.wantedMode = WorldRenderMode.None;
}

Multiplayer.Client.Send(
Packets.Client_SetFaction,
Multiplayer.session.playerId,
playerFaction.loadID
);
new ClientSetFactionPacket(Multiplayer.session.playerId, playerFaction.loadID));
}

Rect locateButton = new Rect(entryRect.xMax - 110, entryRect.y+7, 24, 24);
Expand Down
6 changes: 3 additions & 3 deletions Source/Client/Factions/FactionsWindow.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Linq;
using Multiplayer.Client.Util;
using Multiplayer.Common;
using Multiplayer.Common.Networking.Packet;
using RimWorld;
using UnityEngine;
using Verse;
Expand Down Expand Up @@ -32,7 +32,7 @@ void DrawFactionInLastRect(Faction faction)
{
DragAndDropWidget.DropArea(group, Layouter.LastRect(), playerId =>
{
Multiplayer.Client.Send(Packets.Client_SetFaction, (int)playerId, faction.loadID);
Multiplayer.Client.Send(new ClientSetFactionPacket((int)playerId, faction.loadID));
}, null);

Layouter.BeginVerticalInLastRect(spacing: 1f);
Expand All @@ -43,7 +43,7 @@ void DrawFactionInLastRect(Faction faction)
Layouter.Rect(0f, 5f);

if (Layouter.Button(">", 20f, 20f))
Multiplayer.Client.Send(Packets.Client_SetFaction, Multiplayer.session.playerId, faction.loadID);
Multiplayer.Client.Send(new ClientSetFactionPacket(Multiplayer.session.playerId, faction.loadID));

TooltipHandler.TipRegion(Layouter.LastRect(), "Switch faction");

Expand Down
25 changes: 9 additions & 16 deletions Source/Client/Networking/State/ClientBaseState.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
using Multiplayer.Common;
using Multiplayer.Common.Networking.Packet;

namespace Multiplayer.Client;

public abstract class ClientBaseState(ConnectionBase connection) : MpConnectionState(connection)
{
protected MultiplayerSession Session => Multiplayer.session;

protected void HandleKeepAlive(ByteReader data)
protected void HandleKeepAlive(ServerKeepAlivePacket packet)
{
int id = data.ReadInt32();
int ticksBehind = TickPatch.tickUntil - TickPatch.Timer;

connection.Send(
Packets.Client_KeepAlive,
ByteWriter.GetBytes(id, ticksBehind, TickPatch.Simulating, TickPatch.workTicks),
false
);
connection.Send(new ClientKeepAlivePacket(packet.id, ticksBehind, TickPatch.Simulating, TickPatch.workTicks),
false);
}

protected void HandleTimeControl(ByteReader data)
protected void HandleTimeControl(ServerTimeControlPacket packet)
{
int tickUntil = data.ReadInt32();
int sentCmds = data.ReadInt32();
float stpt = data.ReadFloat();
if (Multiplayer.session.remoteTickUntil >= packet.tickUntil) return;

if (Multiplayer.session.remoteTickUntil >= tickUntil) return;

TickPatch.serverTimePerTick = stpt;
Multiplayer.session.remoteTickUntil = tickUntil;
Multiplayer.session.remoteSentCmds = sentCmds;
TickPatch.serverTimePerTick = packet.serverTimePerTick;
Multiplayer.session.remoteTickUntil = packet.tickUntil;
Multiplayer.session.remoteSentCmds = packet.sentCmds;
Multiplayer.session.ProcessTimeControl();
}
}
12 changes: 6 additions & 6 deletions Source/Client/Networking/State/ClientLoadingState.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Ionic.Zlib;
using Multiplayer.Client.Saving;
using Multiplayer.Common;
using Multiplayer.Common.Networking.Packet;
using Verse;

namespace Multiplayer.Client;
Expand Down Expand Up @@ -140,9 +140,9 @@ public void HandleWorldData(ByteReader data)
connection.ChangeState(ConnectionStateEnum.ClientPlaying);
}

[PacketHandler(Packets.Server_KeepAlive)]
public new void HandleKeepAlive(ByteReader data) => base.HandleKeepAlive(data);
[TypedPacketHandler]
public new void HandleKeepAlive(ServerKeepAlivePacket packet) => base.HandleKeepAlive(packet);

[PacketHandler(Packets.Server_TimeControl)]
public new void HandleTimeControl(ByteReader data) => base.HandleTimeControl(data);
[TypedPacketHandler]
public new void HandleTimeControl(ServerTimeControlPacket packet) => base.HandleTimeControl(packet);
}
40 changes: 16 additions & 24 deletions Source/Client/Networking/State/ClientPlayingState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace Multiplayer.Client
{
public class ClientPlayingState(ConnectionBase connection) : ClientBaseState(connection)
{
[PacketHandler(Packets.Server_KeepAlive)]
public new void HandleKeepAlive(ByteReader data) => base.HandleKeepAlive(data);
[TypedPacketHandler]
public new void HandleKeepAlive(ServerKeepAlivePacket packet) => base.HandleKeepAlive(packet);

[PacketHandler(Packets.Server_TimeControl)]
public new void HandleTimeControl(ByteReader data) => base.HandleTimeControl(data);
[TypedPacketHandler]
public new void HandleTimeControl(ServerTimeControlPacket packet) => base.HandleTimeControl(packet);

[TypedPacketHandler]
public void HandleCommand(ServerCommandPacket packet)
Expand Down Expand Up @@ -93,12 +93,8 @@ public void HandlePlayerList(ServerPlayerListPacket packet)
}
}

[PacketHandler(Packets.Server_Chat)]
public void HandleChat(ByteReader data)
{
string msg = data.ReadString();
Multiplayer.session.AddMsg(msg);
}
[TypedPacketHandler]
public void HandleChat(ServerChatPacket packet) => Multiplayer.session.AddMsg(packet.msg);

[TypedPacketHandler]
public void HandleCursor(ServerCursorPacket packet)
Expand Down Expand Up @@ -180,14 +176,11 @@ public void HandleNotification(ByteReader data)
public void HandleDesyncCheck(ServerSyncInfoPacket packet) =>
Multiplayer.game?.sync.AddClientOpinionAndCheckDesync(ClientSyncOpinion.FromNet(packet.SyncOpinion));

[PacketHandler(Packets.Server_Freeze)]
public void HandleFreze(ByteReader data)
[TypedPacketHandler]
public void HandleFreeze(ServerFreezePacket packet)
{
bool frozen = data.ReadBool();
int frozenAt = data.ReadInt32();

TickPatch.serverFrozen = frozen;
TickPatch.frozenAt = frozenAt;
TickPatch.serverFrozen = packet.frozen;
TickPatch.frozenAt = packet.gameTimer;
}

[PacketHandler(Packets.Server_Traces, allowFragmented: true)]
Expand Down Expand Up @@ -219,15 +212,14 @@ public void HandleDebug(ByteReader data)
Rejoiner.DoRejoin();
}

[PacketHandler(Packets.Server_SetFaction)]
public void HandleSetFaction(ByteReader data)
[TypedPacketHandler]
public void HandleSetFaction(ServerSetFactionPacket packet)
{
int player = data.ReadInt32();
int factionId = data.ReadInt32();

Session.GetPlayerInfo(player).factionId = factionId;
var playerId = packet.playerId;
var factionId = packet.factionId;
Session.GetPlayerInfo(playerId).factionId = factionId;

if (Session.playerId == player)
if (Session.playerId == playerId)
{
Multiplayer.game.ChangeRealPlayerFaction(factionId);
Session.myFactionId = factionId;
Expand Down
11 changes: 6 additions & 5 deletions Source/Client/Patches/LongEvents.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using HarmonyLib;
using Multiplayer.Common;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using Multiplayer.Common;
using Multiplayer.Common.Networking.Packet;
using Verse;

namespace Multiplayer.Client.Patches
Expand Down Expand Up @@ -51,7 +52,7 @@ static void Postfix(bool __state)
if (Multiplayer.Client == null) return;

if (__state && MarkLongEvents.IsTickMarked(LongEventHandler.currentEvent?.eventAction))
Multiplayer.Client.Send(Packets.Client_Freeze, new object[] { true });
Multiplayer.Client.Send(ClientFreezePacket.Freeze());
}
}

Expand All @@ -61,7 +62,7 @@ static class LongEventEnd
static void Postfix()
{
if (Multiplayer.Client != null && NewLongEvent.currentEventWasMarked)
Multiplayer.Client.Send(Packets.Client_Freeze, new object[] { false });
Multiplayer.Client.Send(ClientFreezePacket.Unfreeze());
}
}

Expand Down
8 changes: 3 additions & 5 deletions Source/Client/Persistent/GravshipTravelSession.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Linq;
using Multiplayer.API;
using Multiplayer.Client.Patches;
using Multiplayer.Common.Networking.Packet;
using RimWorld;
using RimWorld.Planet;
using System.Linq;
using Verse;

namespace Multiplayer.Client.Persistent;
Expand Down Expand Up @@ -90,10 +91,7 @@ public static void StopFreeze()
SetFreeze(false);
}

private static void SetFreeze(bool value)
{
Multiplayer.Client.Send(Common.Packets.Client_Freeze, [value]);
}
private static void SetFreeze(bool value) => Multiplayer.Client.Send(new ClientFreezePacket(value));
private static string GravshipDialogPrefix => "ConfirmGravEngineLaunch".Translate().RawText;

// TODO: Try to find a better solution for that
Expand Down
11 changes: 6 additions & 5 deletions Source/Client/Windows/ChatWindow.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using LiteNetLib;
using Multiplayer.Common;
using RimWorld;
using Steamworks;
using System;
using System.Collections.Generic;
using System.Text;
using LiteNetLib;
using Multiplayer.Client.Factions;
using Multiplayer.Client.Networking;
using Multiplayer.Client.Util;
using Multiplayer.Common;
using Multiplayer.Common.Networking.Packet;
using RimWorld;
using Steamworks;
using UnityEngine;
using Verse;

Expand Down Expand Up @@ -354,7 +355,7 @@ public void SendMsg()
else if (Multiplayer.Client == null)
Multiplayer.session.AddMsg(Multiplayer.username + ": " + currentMsg);
else
Multiplayer.Client.Send(Packets.Client_Chat, currentMsg);
Multiplayer.Client.Send(ClientChatPacket.Create(currentMsg));

currentMsg = "";
}
Expand Down
3 changes: 2 additions & 1 deletion Source/Common/FreezeManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using Multiplayer.Common.Networking.Packet;
using Verse;

namespace Multiplayer.Common
Expand All @@ -13,7 +14,7 @@ public bool Frozen
private set
{
frozen = value;
Server.SendToPlaying(Packets.Server_Freeze, new object[] { frozen, Server.gameTimer });
Server.SendToPlaying(new ServerFreezePacket(frozen, Server.gameTimer));
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Common/MultiplayerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private void TickNet()

// Send to simulating players as well to update the simulation window for them and actually update further
// during the same simulation.
SendToPlaying(Packets.Server_TimeControl, ByteWriter.GetBytes(gameTimer, sentCmdsSnapshot, serverTimePerTick), false);
SendToPlaying(new ServerTimeControlPacket(gameTimer, sentCmdsSnapshot, serverTimePerTick), false);

serverTimePerTick = PlayingIngamePlayers.MaxOrZero(p => p.frameTime);

Expand Down Expand Up @@ -256,7 +256,7 @@ public void SendToPlaying(Packets id, byte[] data, bool reliable = true, ServerP
public void SendChat(string msg)
{
ServerLog.Detail($"[Chat] {msg}");
SendToPlaying(Packets.Server_Chat, new object[] { msg });
SendToPlaying(ServerChatPacket.Create(msg));
}

public void SendNotification(string key, params string[] args)
Expand Down
27 changes: 27 additions & 0 deletions Source/Common/Networking/Packet/ChatPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Multiplayer.Common.Networking.Packet;

[PacketDefinition(Packets.Server_Chat)]
public record struct ServerChatPacket : IPacket
{
public string msg;

public static ServerChatPacket Create(string msg) => new() { msg = msg.Trim() };

public void Bind(PacketBuffer buf)
{
buf.Bind(ref msg);
}
}

[PacketDefinition(Packets.Client_Chat)]
public record struct ClientChatPacket : IPacket
{
public string msg;

public static ClientChatPacket Create(string msg) => new() { msg = msg.Trim() };

public void Bind(PacketBuffer buf)
{
buf.Bind(ref msg);
}
}
Loading
Loading