-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkModule.cs
119 lines (104 loc) · 3.49 KB
/
NetworkModule.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
using Bolt;
using System;
using System.IO;
namespace TokyoDrift
{
public class NetworkConfig
{
public const ulong ModNetworkPacked = 999999422;
public readonly static NetworkId ModNetworkID = new NetworkId(ModNetworkPacked);
internal const string ModName = "TurtleShellMod Network Data";
}
//copy paste all of this below
public class NetworkCallbacksServerMod : CoopServerCallbacks
{
public override void OnEvent(ChatEvent evnt)
{
if (evnt.Sender == NetworkConfig.ModNetworkID)
{
NetworkManager.RecieveCommand(NetworkManager.StringToBytes(evnt.Message));
return;
}
base.OnEvent(evnt);
}
}
public class NetworkCallbacksClientMod : CoopPlayerCallbacks
{
public override void OnEvent(ChatEvent evnt)
{
if (evnt.Sender == NetworkConfig.ModNetworkID)
{
NetworkManager.RecieveCommand(NetworkManager.StringToBytes(evnt.Message));
return;
}
base.OnEvent(evnt);
}
}
public static class NetworkManager
{
public static byte[] StringToBytes(string cmd)
{
var a = cmd.ToCharArray();
var b = new byte[a.Length];
for (int i = 0; i < a.Length; i++)
{
b[i] = (byte)a[i];
}
return b;
}
public static string BytesToString(byte[] b)
{
string s = string.Empty;
for (int i = 0; i < b.Length; i++)
{
s += (char)b[i];
}
return s;
}
/// <summary>
/// Sends bytes to targets, calls RecieveCommand on them
/// </summary>
/// <param name="bytearray">Bytes to send</param>
/// <param name="target">Choose between possible recievers</param>
public static void SendCommand(byte[] bytearray, GlobalTargets target)
{
if (BoltNetwork.isRunning)
{
ChatEvent chatEvent = ChatEvent.Create(target);
chatEvent.Message = BytesToString(bytearray);
chatEvent.Sender = NetworkConfig.ModNetworkID;
chatEvent.Send();
}
}
public static void RecieveCommand(byte[] b)
{
using (MemoryStream stream = new MemoryStream(b))
{
using (BinaryReader r = new BinaryReader(stream))
{
int cmdIndex = r.ReadInt32();
try
{
switch (cmdIndex)
{
case 1:
ShellModOptionsGUI.Reset();
ModSettings.bounciness = r.ReadSingle();
ModSettings.acceleration = r.ReadSingle();
ModSettings.bouncetimer = r.ReadSingle();
ModSettings.friction= r.ReadSingle();
ModSettings.turnRate = r.ReadSingle();
break;
}
}
catch (Exception e)
{
ModAPI.Log.Write("Network error: " + cmdIndex + "\n\n" + e.ToString());
}
r.Close();
}
stream.Close();
}
}
}
}