forked from rwmt/Multiplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientLoadingState.cs
More file actions
105 lines (84 loc) · 3.38 KB
/
ClientLoadingState.cs
File metadata and controls
105 lines (84 loc) · 3.38 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
using System.Collections.Generic;
using System.Linq;
using Ionic.Zlib;
using Multiplayer.Client.Saving;
using Multiplayer.Common;
using Verse;
namespace Multiplayer.Client;
public enum LoadingState
{
Waiting,
Downloading
}
public class ClientLoadingState : ClientBaseState
{
public LoadingState subState = LoadingState.Waiting;
public ClientLoadingState(ConnectionBase connection) : base(connection)
{
}
[PacketHandler(Packets.Server_WorldDataStart)]
public void HandleWorldDataStart(ByteReader data)
{
subState = LoadingState.Downloading;
connection.Lenient = false; // Lenient is set while rejoining
}
[PacketHandler(Packets.Server_WorldData, allowFragmented: true)]
public void HandleWorldData(ByteReader data)
{
Log.Message("Game data size: " + data.Length);
int factionId = data.ReadInt32();
Multiplayer.session.myFactionId = factionId;
int tickUntil = data.ReadInt32();
int remoteSentCmds = data.ReadInt32();
bool serverFrozen = data.ReadBool();
byte[] worldData = GZipStream.UncompressBuffer(data.ReadPrefixedBytes());
byte[] sessionData = GZipStream.UncompressBuffer(data.ReadPrefixedBytes());
var mapCmdsDict = new Dictionary<int, List<ScheduledCommand>>();
var mapDataDict = new Dictionary<int, byte[]>();
List<int> mapsToLoad = new List<int>();
int mapCmdsCount = data.ReadInt32();
for (int i = 0; i < mapCmdsCount; i++)
{
int mapId = data.ReadInt32();
int mapCmdsLen = data.ReadInt32();
List<ScheduledCommand> mapCmds = new List<ScheduledCommand>(mapCmdsLen);
for (int j = 0; j < mapCmdsLen; j++)
mapCmds.Add(ScheduledCommand.Deserialize(new ByteReader(data.ReadPrefixedBytes())));
mapCmdsDict[mapId] = mapCmds;
}
int mapDataCount = data.ReadInt32();
for (int i = 0; i < mapDataCount; i++)
{
int mapId = data.ReadInt32();
byte[] rawMapData = data.ReadPrefixedBytes();
byte[] mapData = GZipStream.UncompressBuffer(rawMapData);
mapDataDict[mapId] = mapData;
mapsToLoad.Add(mapId);
}
Session.dataSnapshot = new GameDataSnapshot(
0,
worldData,
sessionData,
mapDataDict,
mapCmdsDict
);
TickPatch.tickUntil = tickUntil;
Multiplayer.session.receivedCmds = remoteSentCmds;
Multiplayer.session.remoteTickUntil = tickUntil;
TickPatch.serverFrozen = serverFrozen;
int syncInfos = data.ReadInt32();
for (int i = 0; i < syncInfos; i++)
Session.initialOpinions.Add(ClientSyncOpinion.Deserialize(new ByteReader(data.ReadPrefixedBytes())));
Log.Message(syncInfos > 0
? $"Initial sync opinions: {Session.initialOpinions.First().startTick}...{Session.initialOpinions.Last().startTick}"
: "No initial sync opinions");
TickPatch.SetSimulation(
toTickUntil: true,
onFinish: () => Multiplayer.Client.Send(Packets.Client_WorldReady),
cancelButtonKey: "Quit",
onCancel: GenScene.GoToMainMenu // Calls StopMultiplayer through a patch
);
Loader.ReloadGame(mapsToLoad, true, false);
connection.ChangeState(ConnectionStateEnum.ClientPlaying);
}
}