-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMessageChecker.cs
353 lines (338 loc) · 11.3 KB
/
MessageChecker.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
using Microsoft.Xna.Framework;
using ServerSideCharacter.GroupManage;
using ServerSideCharacter.Region;
using ServerSideCharacter.ServerCommand;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Terraria;
using Terraria.Chat;
using Terraria.ID;
using Terraria.Localization;
namespace ServerSideCharacter
{
public delegate bool MessagePatchDelegate(ref BinaryReader reader, int playerNumber);
public class MessageChecker
{
private Dictionary<int, MessagePatchDelegate> _method;
public bool CheckMessage(ref byte messageType, ref BinaryReader reader, int playerNumber)
{
try
{
MessagePatchDelegate mpd;
if (_method.TryGetValue(messageType, out mpd))
{
return mpd(ref reader, playerNumber);
}
}
catch (Exception ex)
{
CommandBoardcast.ConsoleError(ex);
}
return false;
}
public MessageChecker()
{
_method = new Dictionary<int, MessagePatchDelegate>
{
{ MessageID.SpawnPlayer, PlayerSpawn },
{ MessageID.ChatText, ChatText },
{ MessageID.NetModules, HandleNetModules },
{ MessageID.TileChange, TileChange },
{ MessageID.PlayerControls, PlayerControls },
{ MessageID.RequestChestOpen, RequestChestOpen }
};
}
private bool HandleNetModules(ref BinaryReader reader, int playernumber)
{
var moduleId = reader.ReadUInt16();
//LoadNetModule is now used for sending chat text.
//Read the module ID to determine if this is in fact the text module
if (Main.netMode == 2)
{
if (moduleId == Terraria.Net.NetManager.Instance.GetId<Terraria.GameContent.NetModules.NetTextModule>())
{
//Then deserialize the message from the reader
var msg = ChatMessage.Deserialize(reader);
return msg.Text.StartsWith("/", StringComparison.Ordinal);
}
}
return false;
}
private bool RequestChestOpen(ref BinaryReader reader, int playerNumber)
{
if (!ServerSideCharacter.Config.EnableChestProtection)
return false;
if (Main.netMode == 2)
{
int x = reader.ReadInt16();
int y = reader.ReadInt16();
int id = Chest.FindChest(x, y);
Player player = Main.player[playerNumber];
ServerPlayer sPlayer = player.GetServerPlayer();
ChestManager.Pending pending = ServerSideCharacter.ChestManager.GetPendings(sPlayer);
switch (pending)
{
case ChestManager.Pending.AddFriend:
if (ServerSideCharacter.ChestManager.IsOwner(id, sPlayer))
{
ServerPlayer friend = ServerSideCharacter.ChestManager.GetFriendP(sPlayer);
ServerSideCharacter.ChestManager.AddFriend(id, friend);
sPlayer.SendSuccessInfo($"{friend.Name} can open this chest now");
}
else
sPlayer.SendErrorInfo("You are not the owner of this chest");
break;
case ChestManager.Pending.RemoveFriend:
if (ServerSideCharacter.ChestManager.IsOwner(id, sPlayer))
{
ServerPlayer friend = ServerSideCharacter.ChestManager.GetFriendP(sPlayer);
ServerSideCharacter.ChestManager.RemoveFriend(id, friend);
sPlayer.SendSuccessInfo($"{friend.Name} can't open this chest now");
}
else
sPlayer.SendErrorInfo("You are not the owner of this chest");
break;
case ChestManager.Pending.Public:
if (ServerSideCharacter.ChestManager.IsOwner(id, sPlayer))
{
if (!ServerSideCharacter.ChestManager.IsPublic(id))
{
ServerSideCharacter.ChestManager.SetOwner(id, sPlayer.UUID, true);
sPlayer.SendSuccessInfo("This chest is now Public");
}
else
sPlayer.SendErrorInfo("This chest is already public");
}
else
sPlayer.SendErrorInfo("You are not the owner of this chest");
break;
case ChestManager.Pending.UnPublic:
if (ServerSideCharacter.ChestManager.IsOwner(id, sPlayer))
{
if (ServerSideCharacter.ChestManager.IsPublic(id))
{
ServerSideCharacter.ChestManager.SetOwner(id, sPlayer.UUID, false);
sPlayer.SendSuccessInfo("This chest is not Public anymore");
}
else
sPlayer.SendErrorInfo("This chest is not public");
}
else
sPlayer.SendErrorInfo("You are not the owner of this chest");
break;
case ChestManager.Pending.Protect:
if (ServerSideCharacter.ChestManager.IsNull(id))
{
ServerSideCharacter.ChestManager.SetOwner(id, sPlayer.UUID, false);
sPlayer.SendSuccessInfo("You now own this chest");
}
else if (ServerSideCharacter.ChestManager.IsOwner(id, sPlayer))
sPlayer.SendErrorInfo("You already protected this chest");
else
sPlayer.SendErrorInfo("This chest as already been protected by other player");
break;
case ChestManager.Pending.DeProtect:
if (ServerSideCharacter.ChestManager.IsOwner(id, sPlayer))
{
ServerSideCharacter.ChestManager.SetOwner(id, -1, false);
sPlayer.SendSuccessInfo("This chest is no longer yours");
}
else if (ServerSideCharacter.ChestManager.IsNull(id))
sPlayer.SendErrorInfo("This chest don't have a owner");
else
sPlayer.SendErrorInfo("You are not the owner of this chest");
break;
case ChestManager.Pending.Info:
if (ServerSideCharacter.ChestManager.IsOwner(id, sPlayer))
{
ChestInfo chest = ServerSideCharacter.ChestManager.ChestInfo[id];
StringBuilder info = new StringBuilder();
if (sPlayer.PermissionGroup.HasPermission("chest"))
info.AppendLine($"Owner: {ServerPlayer.FindPlayer(chest.OwnerID).Name}"); //For Admins
info.AppendLine($"Public Chest: {chest.IsPublic.ToString().ToUpper()}");
info.AppendLine($"Friends ({chest.Friends.Count.ToString()}): {string.Join(", ", chest.Friends.ToArray().Take(10).Select(uuid => ServerPlayer.FindPlayer(uuid).Name))}");
sPlayer.SendInfo(info.ToString());
}
else if (ServerSideCharacter.ChestManager.IsNull(id))
sPlayer.SendErrorInfo("This chest don't have a owner");
else
sPlayer.SendErrorInfo("You are not the owner of this chest");
break;
default:
if (ServerSideCharacter.ChestManager.IsNull(id))
{
if (ServerSideCharacter.Config.AutoProtectChests)
{
ServerSideCharacter.ChestManager.SetOwner(id, sPlayer.UUID, false);
sPlayer.SendSuccessInfo("You now own this chest");
}
else
sPlayer.SendErrorInfo("Use '/chest protect' to become the owner of this chest");
return false;
}
else if (ServerSideCharacter.ChestManager.CanOpen(id, sPlayer))
{
return false;
}
else
{
sPlayer.SendErrorInfo("You cannot open this chest");
}
break;
}
ServerSideCharacter.ChestManager.RemovePending(sPlayer, pending);
}
return true;
}
private bool PlayerControls(ref BinaryReader reader, int playerNumber)
{
if (Main.netMode == 2)
{
byte plr = reader.ReadByte();
BitsByte control = reader.ReadByte();
BitsByte pulley = reader.ReadByte();
byte item = reader.ReadByte();
var pos = reader.ReadVector2();
Player player = Main.player[playerNumber];
ServerPlayer sPlayer = player.GetServerPlayer();
if (pulley[2])
{
var vel = reader.ReadVector2();
}
if (ServerSideCharacter.Config.IsItemBanned(sPlayer.PrototypePlayer.inventory[item], sPlayer))
{
sPlayer.ApplyLockBuffs();
sPlayer.SendErrorInfo("You used a banned item: " + player.inventory[item].Name);
}
}
return false;
}
private bool TileChange(ref BinaryReader reader, int playerNumber)
{
if (Main.netMode == 2)
{
try
{
Player p = Main.player[playerNumber];
ServerPlayer player = p.GetServerPlayer();
int action = reader.ReadByte();
short X = reader.ReadInt16();
short Y = reader.ReadInt16();
short type = reader.ReadInt16();
int style = reader.ReadByte();
if (ServerSideCharacter.CheckSpawn(X, Y) && player.PermissionGroup.GroupName != "spadmin")
{
player.SendErrorInfo("Warning: Spawn is protected from change");
NetMessage.SendTileSquare(-1, X, Y, 4);
return true;
}
else if (ServerSideCharacter.RegionManager.CheckRegion(X, Y, player))
{
player.SendErrorInfo("Warning: You don't have permission to change this tile");
NetMessage.SendTileSquare(-1, X, Y, 4);
return true;
}
else if (player.PermissionGroup.GroupName == "criminal")
{
player.SendErrorInfo("Warning: Criminals cannot change tiles");
NetMessage.SendTileSquare(-1, X, Y, 4);
return true;
}
}
catch (Exception ex)
{
CommandBoardcast.ConsoleError(ex);
}
}
return false;
}
private bool ChatText(ref BinaryReader reader, int playerNumber)
{
int playerID = reader.ReadByte();
if (Main.netMode == 2)
{
playerID = playerNumber;
}
Color c = reader.ReadRGB();
if (Main.netMode == 2)
{
c = new Color(255, 255, 255);
}
string text = reader.ReadString();
if (Main.netMode == 1)
{
string text2 = text.Substring(text.IndexOf('>') + 1);
if (playerID < 255)
{
Main.player[playerID].chatOverhead.NewMessage(text2, Main.chatLength / 2);
}
Main.NewTextMultiline(text, false, c, -1);
}
else
{
Player p = Main.player[playerID];
ServerPlayer player = p.GetServerPlayer();
Group group = player.PermissionGroup;
string prefix = "[" + group.ChatPrefix + "] ";
c = group.ChatColor;
NetMessage.SendData(25, -1, -1, NetworkText.FromLiteral(prefix + "<" + p.name + "> " + text), playerID, (float)c.R, (float)c.G, (float)c.B, 0, 0, 0);
if (Main.dedServ)
{
Console.WriteLine("{0}<" + Main.player[playerID].name + "> " + text, prefix);
}
}
return true;
}
private bool PlayerSpawn(ref BinaryReader reader, int playerNumber)
{
int id = reader.ReadByte();
if (Main.netMode == 2)
{
id = playerNumber;
}
Player player = Main.player[id];
player.SpawnX = reader.ReadInt16();
player.SpawnY = reader.ReadInt16();
player.Spawn();
if (id == Main.myPlayer && Main.netMode != 2)
{
Main.ActivePlayerFileData.StartPlayTimer();
Player.Hooks.EnterWorld(Main.myPlayer);
}
if (Main.netMode != 2 || Netplay.Clients[playerNumber].State < 3)
{
return true;
}
//如果数据中没有玩家的信息
if (!ServerSideCharacter.XmlData.Data.ContainsKey(Main.player[playerNumber].name))
{
try
{
//创建新的玩家数据
ServerPlayer serverPlayer = ServerPlayer.CreateNewPlayer(Main.player[playerNumber]);
serverPlayer.PrototypePlayer = Main.player[playerNumber];
ServerSideCharacter.XmlData.Data.Add(Main.player[playerNumber].name, serverPlayer);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
if (Netplay.Clients[playerNumber].State == 3)
{
Netplay.Clients[playerNumber].State = 10;
NetMessage.greetPlayer(playerNumber);
NetMessage.buffer[playerNumber].broadcast = true;
ServerSideCharacter.SyncConnectedPlayer(playerNumber);
NetMessage.SendData(MessageID.SpawnPlayer, -1, playerNumber, NetworkText.Empty, playerNumber, 0f, 0f, 0f, 0, 0, 0);
NetMessage.SendData(MessageID.AnglerQuest, playerNumber, -1, NetworkText.FromLiteral(Main.player[playerNumber].name), Main.anglerQuest, 0f, 0f, 0f, 0, 0, 0);
return true;
}
NetMessage.SendData(MessageID.SpawnPlayer, -1, playerNumber, NetworkText.Empty, playerNumber, 0f, 0f, 0f, 0, 0, 0);
return true;
}
}
}