-
Notifications
You must be signed in to change notification settings - Fork 4
/
VaultNetWork.cs
76 lines (68 loc) · 2.45 KB
/
VaultNetWork.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
using InnoVault.TileProcessors;
using System.Collections.Generic;
using System.IO;
using Terraria.ModLoader;
namespace InnoVault
{
internal class VaultNetWork : IVaultLoader
{
public enum MessageType : byte
{
PlaceInWorldSync,
TPNetWork,
ClientRequest_TPData_Send,
Handle_TPData_Receive,
}
internal static List<NetWorkEvent> NetWorkEvents = new List<NetWorkEvent>();
public static void HandlePacket(Mod mod, BinaryReader reader, int whoAmI) {
MessageType type = (MessageType)reader.ReadByte();
if (type == MessageType.PlaceInWorldSync) {
TileProcessorNetWork.PlaceInWorldNetReceive(mod, reader, whoAmI);
}
else if (type == MessageType.TPNetWork) {
TileProcessorNetWork.TileProcessorReceiveData(reader, whoAmI);
}
else if (type == MessageType.ClientRequest_TPData_Send) {
TileProcessorNetWork.ServerRecovery_TPData(reader, whoAmI);
}
else if (type == MessageType.Handle_TPData_Receive) {
TileProcessorNetWork.Handle_TPData_Receive(reader);
}
}
public static void UpdateNetWorkEvent() {
if (VaultUtils.isSinglePlayer) {
NetWorkEvents = new List<NetWorkEvent>();
return;
}
foreach (var netWorkEvent in NetWorkEvents) {
netWorkEvent.Update();
}
NetWorkEvents.RemoveAll(n => n.sendTime <= 0);
}
}
internal class NetWorkEvent
{
public int sendTime;
public virtual void SendEvent() { }
public void Update() {
if (--sendTime <= 0) {
SendEvent();
}
}
}
internal class TPNetWorkEvent : NetWorkEvent
{
private TileProcessor tpEntity;
public TPNetWorkEvent(int sendTime, TileProcessor tpEntity) {
this.sendTime = sendTime;
this.tpEntity = tpEntity;
}
public override void SendEvent() => TileProcessorNetWork.TileProcessorSendData(tpEntity);
public static void Add(int sendTime, TileProcessor tpEntity)
=> VaultNetWork.NetWorkEvents.Add(new TPNetWorkEvent(sendTime, tpEntity));
}
internal class VaultNetSystem : ModSystem
{
public override void PostUpdateEverything() => VaultNetWork.UpdateNetWorkEvent();
}
}