-
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathClientJoiningState.cs
More file actions
150 lines (129 loc) · 5.98 KB
/
ClientJoiningState.cs
File metadata and controls
150 lines (129 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using Multiplayer.Client.Networking;
using Multiplayer.Common;
using Multiplayer.Common.Networking.Packet;
using RimWorld;
using Verse;
namespace Multiplayer.Client
{
// We want to inherit the shared typed packet handlers from ClientBaseState (keepalive, time control, disconnect).
// Disabling inheritance can cause missing core handlers during joining and lead to early disconnects / broken UI.
[PacketHandlerClass(inheritHandlers: true)]
public class ClientJoiningState : ClientBaseState
{
public ClientJoiningState(ConnectionBase connection) : base(connection)
{
}
[TypedPacketHandler]
public void HandleBootstrap(ServerBootstrapPacket packet)
{
// Server informs us early that it's in bootstrap/configuration mode.
// Full UI/flow is handled on the client side; for now we just persist the flag
// so receiving the packet doesn't error during join (tests rely on this).
Multiplayer.session.serverIsInBootstrap = packet.bootstrap;
Multiplayer.session.serverBootstrapSettingsMissing = packet.settingsMissing;
}
public override void StartState()
{
connection.Send(ClientProtocolPacket.Current());
ConnectionStatusListeners.TryNotifyAll_Connected();
}
[TypedPacketHandler]
public void HandleProtocolOk(ServerProtocolOkPacket packet)
{
if (packet.hasPassword)
{
// Delay showing the window for better UX
OnMainThread.Schedule(() => Find.WindowStack.Add(new GamePasswordWindow
{
returnToServerBrowser = Find.WindowStack.WindowOfType<BaseConnectingWindow>().returnToServerBrowser
}), 0.3f);
}
else
{
connection.Send(new ClientUsernamePacket(Multiplayer.username));
}
}
[TypedPacketHandler]
public void HandleInitDataRequest(ServerInitDataRequestPacket packet) =>
connection.SendFragmented(PackInitData(packet.includeConfigs).ToNet().Serialize());
public static ServerInitData PackInitData(bool includeConfigs) => new(
JoinData.WriteServerData(includeConfigs),
VersionControl.CurrentVersionString,
Sync.handlers.Where(h => h.debugOnly).Select(h => h.syncId).ToHashSet(),
Sync.handlers.Where(h => h.hostOnly).Select(h => h.syncId).ToHashSet(),
(MultiplayerData.modCtorRoundMode, MultiplayerData.staticCtorRoundMode),
new Dictionary<string, DefInfo>(MultiplayerData.localDefInfos)
);
[PacketHandler(Packets.Server_UsernameOk)]
public void HandleUsernameOk(ByteReader data) =>
connection.SendFragmented(new ClientJoinDataPacket
{
modCtorRoundMode = MultiplayerData.modCtorRoundMode,
staticCtorRoundMode = MultiplayerData.staticCtorRoundMode,
defInfos = MultiplayerData.localDefInfos.Select(kv => new KeyedDefInfo
{ name = kv.Key, count = kv.Value.count, hash = kv.Value.hash }).ToArray()
}.Serialize());
[TypedPacketHandler]
public void HandleJoinData(ServerJoinDataPacket packet)
{
Multiplayer.session.gameName = packet.gameName;
Multiplayer.session.playerId = packet.playerId;
var remoteInfo = new RemoteData
{
remoteRwVersion = packet.rwVersion,
remoteMpVersion = packet.mpVersion,
remoteAddress = Multiplayer.session.address,
remotePort = Multiplayer.session.port,
remoteSteamHost = Multiplayer.session.steamHost
};
var defDiff = false;
var defStatusMap = new Dictionary<DefInfo, DefCheckStatus>();
var i = 0;
foreach (var local in MultiplayerData.localDefInfos)
{
var status = packet.defStatus[i++];
defStatusMap.Add(local.Value, status);
if (status != DefCheckStatus.Ok)
defDiff = true;
}
JoinData.ReadServerData(packet.rawServerInitData, remoteInfo);
// Delay showing the window for better UX
OnMainThread.Schedule(Complete, 0.3f);
void Complete()
{
if (JoinData.CompareToLocal(remoteInfo) && !defDiff)
{
StartDownloading();
return;
}
if (defDiff)
Multiplayer.StopMultiplayerAndClearAllWindows();
var defDiffStr = "\n\n" + MultiplayerData.localDefInfos
.Select(kv => (name: kv.Key, def: kv.Value, status: defStatusMap[kv.Value]))
.Where(kv => kv.status != DefCheckStatus.Ok)
.Take(10)
.Join(kv => $"{kv.name}: {kv.status}", "\n");
Find.WindowStack.Add(new JoinDataWindow(remoteInfo){
connectAnywayDisabled = defDiff ? "MpMismatchDefsDiff".Translate() + defDiffStr : null,
connectAnywayCallback = StartDownloading
});
void StartDownloading()
{
if (Multiplayer.session.serverIsInBootstrap)
{
// Server is in bootstrap/configuration mode: don't request world data.
// Instead, show a dedicated configuration UI.
connection.ChangeState(ConnectionStateEnum.ClientBootstrap);
Find.WindowStack.Add(new BootstrapConfiguratorWindow(connection));
return;
}
connection.Send(Packets.Client_WorldRequest);
connection.ChangeState(ConnectionStateEnum.ClientLoading);
}
}
}
}
}