forked from steveman0/FreightCartMod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkSync.cs
More file actions
120 lines (104 loc) · 3.95 KB
/
NetworkSync.cs
File metadata and controls
120 lines (104 loc) · 3.95 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
using UnityEngine;
using Lidgren.Network;
using System.Collections.Generic;
using System.IO;
public static class NetworkSync
{
// For use by clients/local host
public static NetworkStatusWrapper NetworkStatus;
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;
NetworkStatusWrapper wrapper = new NetworkStatusWrapper(FreightCartManager.instance.GetNetworkRegistries(networkindex), FreightCartManager.instance.Networks[networkindex]);
// Server is local player making request
if (player == WorldScript.mLocalPlayer)
{
NetworkStatus = wrapper;
return;
}
ModManager.ModSendServerCommToClient("steveman0.NetworkStatus", 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 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);
}
}
}