forked from MadYeling/Fargowiltas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FargoNet.cs
241 lines (209 loc) · 8.04 KB
/
FargoNet.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
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
namespace Fargowiltas
{
public static class FargoNet
{
public const byte SummonNPCFromClient = 0;
private const bool Debug = true;
public static void SendData(int dataType, int dataA, int dataB, string text, int playerID, float dataC, float dataD, float dataE, int clientType)
{
NetMessage.SendData(dataType, dataA, dataB, NetworkText.FromLiteral(text), playerID, dataC, dataD, dataE, clientType);
}
public static ModPacket WriteToPacket(ModPacket packet, byte msg, params object[] param)
{
packet.Write(msg);
for (int m = 0; m < param.Length; m++)
{
object obj = param[m];
switch (obj)
{
case byte[] _:
{
byte[] array = (byte[])obj;
foreach (byte b in array)
{
packet.Write(b);
}
break;
}
case bool _:
packet.Write((bool)obj);
break;
case byte _:
packet.Write((byte)obj);
break;
case short _:
packet.Write((short)obj);
break;
case int _:
packet.Write((int)obj);
break;
case float _:
packet.Write((float)obj);
break;
}
}
return packet;
}
public static void SyncAI(Entity codable, float[] ai, int aitype)
{
int entType = codable is NPC ? 0 : codable is Projectile ? 1 : -1;
if (entType == -1)
{
return;
}
int id = codable is NPC ? ((NPC)codable).whoAmI : ((Projectile)codable).identity;
SyncAI(entType, id, ai, aitype);
}
/*
* Used to sync custom ai float arrays. (the npc or projectile requires a method called 'public void SetAI(float[] ai, int type)' that sets the ai for this to work)
*/
public static void SyncAI(int entType, int id, float[] ai, int aitype)
{
object[] ai2 = new object[ai.Length + 4];
ai2[0] = (byte)entType;
ai2[1] = (short)id;
ai2[2] = (byte)aitype;
ai2[3] = (byte)ai.Length;
for (int m = 4; m < ai2.Length; m++)
{
ai2[m] = ai[m - 4];
}
SendFargoNetMessage(1, ai2);
}
/*
* Writes a vector2 array to an obj[] array that can be sent via netmessaging.
*/
public static object[] WriteVector2Array(Vector2[] array)
{
List<object> list = new List<object>
{
array.Length,
};
foreach (Vector2 vec in array)
{
list.Add(vec.X);
list.Add(vec.Y);
}
return list.ToArray();
}
/*
* Writes a vector2 array to a binary writer.
*/
public static void WriteVector2Array(Vector2[] array, BinaryWriter writer)
{
writer.Write(array.Length);
foreach (Vector2 vec in array)
{
writer.Write(vec.X);
writer.Write(vec.Y);
}
}
/*
* Reads a vector2 array from a binary reader.
*/
public static Vector2[] ReadVector2Array(BinaryReader reader)
{
int arrayLength = reader.ReadInt32();
Vector2[] array = new Vector2[arrayLength];
for (int m = 0; m < arrayLength; m++)
{
array[m] = new Vector2(reader.ReadSingle(), reader.ReadSingle());
}
return array;
}
public static void SendFargoNetMessage(int msg, params object[] param)
{
// Nothing to sync in SP
if (Main.netMode == NetmodeID.SinglePlayer)
{
return;
}
WriteToPacket(ModContent.GetInstance<Fargowiltas>().GetPacket(), (byte)msg, param).Send();
}
public static void HandlePacket(BinaryReader bb)
{
byte msg = bb.ReadByte();
if (Debug)
{
ModContent.GetInstance<Fargowiltas>().Logger.Error((Main.netMode == NetmodeID.Server ? "--SERVER-- " : "--CLIENT-- ") + "HANDING MESSAGE: " + msg);
}
try
{
if (msg == SummonNPCFromClient)
{
if (Main.netMode == NetmodeID.Server)
{
int playerID = bb.ReadByte();
int bossType = bb.ReadInt16();
bool spawnMessage = bb.ReadBoolean();
int npcCenterX = bb.ReadInt32();
int npcCenterY = bb.ReadInt32();
string overrideDisplayName = bb.ReadString();
bool namePlural = bb.ReadBoolean();
Fargowiltas.SpawnBoss(Main.player[playerID], bossType, spawnMessage, new Vector2(npcCenterX, npcCenterY), overrideDisplayName, namePlural);
}
}
}
catch (Exception e)
{
ModContent.GetInstance<Fargowiltas>().Logger.Error((Main.netMode == NetmodeID.Server ? "--SERVER-- " : "--CLIENT-- ") + "ERROR HANDLING MSG: " + msg.ToString() + ": " + e.Message);
ModContent.GetInstance<Fargowiltas>().Logger.Error(e.StackTrace);
ModContent.GetInstance<Fargowiltas>().Logger.Error("-------");
}
}
public static void SyncPlayer(int toWho, int fromWho, bool newPlayer)
{
if (Debug)
{
ModContent.GetInstance<Fargowiltas>().Logger.Error((Main.netMode == NetmodeID.Server ? "--SERVER-- " : "--CLIENT-- ") + "SYNC PLAYER CALLED! NEWPLAYER: " + newPlayer + ". TOWHO: " + toWho + ". FROMWHO:" + fromWho);
}
if (Main.netMode == NetmodeID.Server && (toWho > -1 || fromWho > -1))
{
PlayerConnected();
}
}
public static void PlayerConnected()
{
if (Debug)
{
ModContent.GetInstance<Fargowiltas>().Logger.Info("--SERVER-- PLAYER JOINED!");
}
}
public static void SendNetMessage(int msg, params object[] param)
{
SendNetMessageClient(msg, -1, param);
}
public static void SendNetMessageClient(int msg, int client, params object[] param)
{
try
{
if (Main.netMode == NetmodeID.SinglePlayer)
{
return;
}
WriteToPacket(ModContent.GetInstance<Fargowiltas>().GetPacket(), (byte)msg, param).Send(client);
}
catch (Exception e)
{
ModContent.GetInstance<Fargowiltas>().Logger.Error((Main.netMode == NetmodeID.Server ? "--SERVER-- " : "--CLIENT-- ") + "ERROR SENDING MSG: " + msg.ToString() + ": " + e.Message);
ModContent.GetInstance<Fargowiltas>().Logger.Error(e.StackTrace);
ModContent.GetInstance<Fargowiltas>().Logger.Error("-------");
string param2 = string.Empty;
for (int m = 0; m < param.Length; m++)
{
param2 += param[m];
}
ModContent.GetInstance<Fargowiltas>().Logger.Error("PARAMS: " + param2);
ModContent.GetInstance<Fargowiltas>().Logger.Error("-------");
}
}
}
}