-
Notifications
You must be signed in to change notification settings - Fork 2
/
NetworkSync.cs
564 lines (497 loc) · 18.4 KB
/
NetworkSync.cs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
using UnityEngine;
using Lidgren.Network;
using System.Collections.Generic;
using System.IO;
using System.Linq;
public static class NetworkSync
{
// For use by clients/local host
public static NetworkStatusWrapper NetworkStatus;
public static TrackNetworksWrapper TrackNetworks;
public static void GetNetworkStatus(int networkindex, Player player)
{
if (networkindex == -1 || FreightCartManager.instance == null || FreightCartManager.instance.Networks == null)
return;
int count = FreightCartManager.instance.Networks.Count;
if (networkindex >= count && count > 0)
networkindex = 0;
else if (networkindex >= count)
return;
string networkid = FreightCartManager.instance.Networks[networkindex];
NetworkStatusWrapper wrapper = new NetworkStatusWrapper(FreightCartManager.instance.GetNetworkRegistries(networkid), networkid);
// Server is local player making request
if (player == WorldScript.mLocalPlayer)
{
NetworkStatus = wrapper;
return;
}
ModManager.ModSendServerCommToClient("steveman0.NetworkStatus", player, wrapper);
}
public static void GetTrackNetworks(int tracknetwork, int station, int cart, Player player)
{
if (FreightCartManager.instance == null || FreightCartManager.instance.GlobalTrackNetworks == null)
return;
TrackNetworksWrapper wrapper = new TrackNetworksWrapper(tracknetwork, station, cart);
// Server is local player making request
if (player == WorldScript.mLocalPlayer)
{
TrackNetworks = wrapper;
return;
}
ModManager.ModSendServerCommToClient("steveman0.TrackNetworks", player, wrapper);
}
public static void SendNetworkStatus(BinaryWriter writer, Player player, object data)
{
(data as NetworkStatusWrapper).Write(writer);
}
public static void ReadNetworkStatus(NetIncomingMessage message)
{
NetworkStatusWrapper wrapper = new NetworkStatusWrapper();
wrapper.Read(message);
NetworkStatus = wrapper;
SystemMonitorWindow.networkredraw = true;
}
public static void SendTrackNetworks(BinaryWriter writer, Player player, object data)
{
(data as TrackNetworksWrapper).Write(writer);
}
public static void ReadTrackNetworks(NetIncomingMessage message)
{
TrackNetworksWrapper wrapper = new TrackNetworksWrapper();
wrapper.Read(message);
TrackNetworks = wrapper;
SystemMonitorWindow.networkredraw = true;
}
}
public class NetworkStatusWrapper
{
public string NetworkID;
public List<FreightRegistry> Registries = new List<FreightRegistry>();
public NetworkStatusWrapper(List<FreightRegistry> reg, string networkid)
{
this.NetworkID = networkid;
this.Registries = reg;
}
public NetworkStatusWrapper() { }
public void Write(BinaryWriter writer)
{
if (string.IsNullOrEmpty(NetworkID))
{
Debug.LogWarning("FreightCarts NetworkSync tried to write a NetworkStatusWrapper with null/empty networkid");
writer.Write(string.Empty);
}
else
writer.Write(this.NetworkID);
int count = this.Registries.Count;
writer.Write(count);
for (int n = 0; n < count; n++)
{
FreightRegistry reg = Registries[n];
if (reg.FreightItem.mType == ItemType.ItemCubeStack)
{
writer.Write(-1);
writer.Write((reg.FreightItem as ItemCubeStack).mCubeType);
writer.Write((reg.FreightItem as ItemCubeStack).mCubeValue);
}
else
writer.Write(reg.FreightItem.mnItemID);
writer.Write(reg.Deficit);
writer.Write(reg.Surplus);
writer.Write(reg.Inventory);
writer.Write(reg.Stock);
}
}
public void Read(NetIncomingMessage message)
{
this.Registries = new List<FreightRegistry>();
this.NetworkID = message.ReadString();
int count = message.ReadInt32();
for (int n = 0; n < count; n++)
{
int itemid = message.ReadInt32();
ushort type;
ushort val;
ItemBase item;
if (itemid == -1)
{
type = message.ReadUInt16();
val = message.ReadUInt16();
item = ItemManager.SpawnCubeStack(type, val, 1);
}
else
item = ItemManager.SpawnItem(itemid);
FreightRegistry reg = new FreightRegistry(this.NetworkID, null, item, 0, 0, FreightRegistry.RegistryType.Registry);
reg.Deficit = message.ReadInt32();
reg.Surplus = message.ReadInt32();
reg.Inventory = message.ReadInt32();
reg.Stock = message.ReadInt32();
this.Registries.Add(reg);
}
}
}
public class TrackNetworksWrapper
{
// Track Networks
public int NetworkCount;
public List<TrackNetworkStats> NetworkStats;
// Freight Cart Stations
public int StationCount;
public List<StationStats> Stations;
// Station details - these need to be item names and number only for serialization... change object to a struct?
public List<FreightRegistry> StationDeficits;
public List<FreightRegistry> StationSurplus;
//Carts
public int CartCount;
public List<CartStats> Carts;
//Displayed Cart
public List<ItemBase> CartInventory;
public TrackNetworksWrapper(int tracknetwork, int station, int cart)
{
// This shouldn't happen but just in case...
if (FreightCartManager.instance == null)
return;
// Track Networks
this.NetworkCount = FreightCartManager.instance.GlobalTrackNetworks.Count;
this.NetworkStats = new List<TrackNetworkStats>(this.NetworkCount);
for (int n = 0; n < this.NetworkCount; n++)
this.NetworkStats.Add(new TrackNetworkStats(n));
// Freight Cart Stations
if (tracknetwork != -1 && tracknetwork < this.NetworkCount)
{
List<FreightCartStation> stations = FreightCartManager.instance.GlobalTrackNetworks[tracknetwork].GetNetworkStations();
stations = stations.OrderBy(x => x.StationName).ToList();
this.StationCount = stations.Count;
this.Stations = new List<StationStats>(this.StationCount);
foreach (FreightCartStation sta in stations)
this.Stations.Add(new StationStats(sta));
if (station != -1 && station < this.StationCount)
{
FreightCartStation Station = stations[station];
// Station details
this.GetStationGoods(Station);
// Carts
this.CartCount = Station.CartList.Count;
this.Carts = new List<CartStats>(this.CartCount);
foreach (FreightCartMob mob in Station.CartList)
this.Carts.Add(new CartStats(mob));
// Displayed Cart
if (cart != -1 && cart < this.CartCount)
{
FreightCartMob Cart = Station.CartList[cart];
if (!string.IsNullOrEmpty(Station.NetworkID) && Cart.LocalInventory.ContainsKey(Station.NetworkID))
CartInventory = Cart.LocalInventory[Station.NetworkID].Inventory;
}
}
else
this.CartCount = 0;
}
else
{
this.StationCount = 0;
this.CartCount = 0;
}
}
public TrackNetworksWrapper() { }
public void Write(BinaryWriter writer)
{
// Track Networks
writer.Write(NetworkCount);
if (NetworkCount > 0)
{
foreach (TrackNetworkStats stats in this.NetworkStats)
stats.Write(writer);
}
//Stations
writer.Write(StationCount);
if (StationCount > 0)
{
foreach (StationStats stats in this.Stations)
stats.Write(writer);
}
WriteRegistries(StationDeficits, writer, true);
WriteRegistries(StationSurplus, writer, false);
// Carts
writer.Write(CartCount);
if (CartCount > 0)
{
foreach (CartStats stats in Carts)
stats.Write(writer);
}
// Displayed Cart
if (CartInventory != null)
{
int count = CartInventory.Count;
writer.Write(count);
for (int n = 0; n < count; n++)
ItemWriter(CartInventory[n], writer);
}
else
writer.Write(0);
}
public void Read(NetIncomingMessage message)
{
// Track Networks
this.NetworkCount = message.ReadInt32();
this.NetworkStats = new List<TrackNetworkStats>(this.NetworkCount);
for (int n = 0; n < NetworkCount; n++)
{
TrackNetworkStats stats = new TrackNetworkStats();
stats.Read(message);
this.NetworkStats.Add(stats);
}
// Stations
this.StationCount = message.ReadInt32();
if (this.StationCount > 0)
{
this.Stations = new List<StationStats>(this.StationCount);
for (int n = 0; n < StationCount; n++)
{
StationStats stats = new StationStats();
stats.Read(message);
this.Stations.Add(stats);
}
}
ReadRegistries(message, true);
ReadRegistries(message, false);
// Carts
this.CartCount = message.ReadInt32();
if (this.CartCount > 0)
{
this.Carts = new List<CartStats>(this.CartCount);
for (int n = 0; n < CartCount; n++)
{
CartStats stats = new CartStats();
stats.Read(message);
this.Carts.Add(stats);
}
}
// Displayed Cart
int count = message.ReadInt32();
CartInventory = new List<ItemBase>(count);
for (int n = 0; n < count; n++)
{
ItemBase item;
item = ItemReader(message);
if (item != null)
CartInventory.Add(item);
}
}
private void ItemWriter(ItemBase item, BinaryWriter writer)
{
if (item == null)
{
Debug.LogWarning("Freight Cart NetworkSync tried to write a null cart inventory item!");
writer.Write(-2);
}
writer.Write(item.mnItemID);
if (item.mnItemID == -1)
{
// Cube!
ItemCubeStack cube = (item as ItemCubeStack);
writer.Write(cube.mCubeType);
writer.Write(cube.mCubeValue);
writer.Write(cube.mnAmount);
}
else
{
if (item.mType == ItemType.ItemStack)
writer.Write((item as ItemStack).mnAmount);
}
}
private ItemBase ItemReader(NetIncomingMessage message)
{
ItemBase item;
int itemid = message.ReadInt32();
if (itemid == -2)
{
Debug.LogWarning("Freight Cart NetworkSync read in a cart null inventory item!");
return null;
}
else if (itemid == -1)
return ItemManager.SpawnCubeStack(message.ReadUInt16(), message.ReadUInt16(), message.ReadInt32());
else
{
item = ItemManager.SpawnItem(itemid);
if (item.mType == ItemType.ItemStack)
(item as ItemStack).mnAmount = message.ReadInt32();
return item;
}
}
private void GetStationGoods(FreightCartStation station)
{
List<FreightRegistry> LocalDeficits = new List<FreightRegistry>();
List<FreightRegistry> LocalSurplus = new List<FreightRegistry>();
if (station.massStorageCrate != null)
{
LocalDeficits = FreightCartManager.instance.GetLocalDeficit(station.NetworkID, station.massStorageCrate);
LocalSurplus = FreightCartManager.instance.GetLocalSurplus(station.NetworkID, station.massStorageCrate);
}
else if (station.HopperInterface != null)
{
LocalDeficits = this.FreightListingConversion(station.HopperInterface.FreightRequests.OrderByDescending(x => x.Quantity).Take(3).ToList());
LocalSurplus = this.FreightListingConversion(station.HopperInterface.FreightOfferings.OrderByDescending(x => x.Quantity).Take(3).ToList());
}
else if (station.AttachedInterface != null)
{
LocalDeficits = this.FreightListingConversion(station.AttachedInterface.FreightRequests.OrderByDescending(x => x.Quantity).Take(3).ToList());
LocalSurplus = this.FreightListingConversion(station.AttachedInterface.FreightOfferings.OrderByDescending(x => x.Quantity).Take(3).ToList());
}
this.StationDeficits = LocalDeficits;
this.StationSurplus = LocalSurplus;
}
private List<FreightRegistry> FreightListingConversion(List<FreightListing> items)
{
List<FreightRegistry> regs = new List<FreightRegistry>();
foreach (FreightListing item in items)
{
FreightRegistry reg = new FreightRegistry(null, null, item.Item, 0, 0, FreightRegistry.RegistryType.Registry);
reg.Deficit = item.Quantity;
reg.Surplus = item.Quantity;
regs.Add(reg);
}
return regs;
}
private void WriteRegistries(List<FreightRegistry> regs, BinaryWriter writer, bool IsDef)
{
if (regs == null)
{
writer.Write(0);
return;
}
int count = regs.Count;
writer.Write(count);
for (int n = 0; n < count; n++)
{
ItemWriter(regs[n].FreightItem, writer);
if (IsDef)
writer.Write(regs[n].Deficit);
else
writer.Write(regs[n].Surplus);
}
}
private void ReadRegistries(NetIncomingMessage message, bool IsDef)
{
int count = message.ReadInt32();
List<FreightRegistry> regs = new List<FreightRegistry>(count);
FreightRegistry reg;
for (int n = 0; n < count; n++)
{
reg = new FreightRegistry(null, null, ItemReader(message), 0, 0, FreightRegistry.RegistryType.Registry);
if (IsDef)
reg.Deficit = message.ReadInt32();
else
reg.Surplus = message.ReadInt32();
regs.Add(reg);
}
if (IsDef)
this.StationDeficits = regs;
else
this.StationSurplus = regs;
}
}
public class TrackNetworkStats
{
public int JunctionCount;
public int NetworkID;
public bool NetworkClosed;
public int AssignedCarts;
public int AvailableCarts;
public TrackNetworkStats(int junccount, int netid, bool netclosed, int assigned, int avail)
{
JunctionCount = junccount;
NetworkID = netid;
NetworkClosed = netclosed;
AssignedCarts = assigned;
AvailableCarts = avail;
}
public TrackNetworkStats(int networkindex)
{
FreightTrackNetwork network = FreightCartManager.instance.GlobalTrackNetworks[networkindex];
JunctionCount = network.TrackJunctions.Count;
NetworkID = network.NetworkID;
network.GetNetworkStats(out AssignedCarts, out AvailableCarts, out NetworkClosed);
}
public TrackNetworkStats() { }
public void Write(BinaryWriter writer)
{
writer.Write(JunctionCount);
writer.Write(NetworkID);
writer.Write(NetworkClosed);
writer.Write(AssignedCarts);
writer.Write(AvailableCarts);
}
public void Read(NetIncomingMessage message)
{
JunctionCount = message.ReadInt32();
NetworkID = message.ReadInt32();
NetworkClosed = message.ReadByte() == 1;
AssignedCarts = message.ReadInt32();
AvailableCarts = message.ReadInt32();
}
}
public class StationStats
{
public string StationName;
public string NetworkID;
public float StationFull;
public int AssignedCarts;
public int AvailableCarts;
public StationStats(string name, string netid, float full, int assigned, int avail)
{
StationName = name;
NetworkID = netid;
StationFull = full;
AssignedCarts = assigned;
AvailableCarts = avail;
}
public StationStats(FreightCartStation station)
{
StationName = station.StationName;
NetworkID = station.NetworkID;
StationFull = station.StationFull;
AssignedCarts = station.AssignedCarts;
AvailableCarts = station.AvailableCarts;
}
public StationStats() { }
public void Write(BinaryWriter writer)
{
writer.Write(StationName);
writer.Write(NetworkID);
writer.Write(StationFull);
writer.Write(AssignedCarts);
writer.Write(AvailableCarts);
}
public void Read(NetIncomingMessage message)
{
StationName = message.ReadString();
NetworkID = message.ReadString();
StationFull = message.ReadFloat();
AssignedCarts = message.ReadInt32();
AvailableCarts = message.ReadInt32();
}
}
public class CartStats
{
public FreightCartMob.eMinecartType CartType;
public int UsedStorage;
public int MaxStorage;
public CartStats(FreightCartMob mob)
{
CartType = mob.meType;
UsedStorage = mob.mnUsedStorage;
MaxStorage = mob.mnMaxStorage;
}
public CartStats() { }
public void Write(BinaryWriter writer)
{
writer.Write((byte)CartType);
writer.Write(UsedStorage);
writer.Write(MaxStorage);
}
public void Read(NetIncomingMessage message)
{
CartType = (FreightCartMob.eMinecartType)message.ReadByte();
UsedStorage = message.ReadInt32();
MaxStorage = message.ReadInt32();
}
}