forked from bdfzchen2015/ServerSideCharacter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
237 lines (228 loc) · 5.79 KB
/
Utils.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using ServerSideCharacter.Config;
using ServerSideCharacter.Extensions;
using ServerSideCharacter.Region;
using Terraria;
using Terraria.ModLoader;
namespace ServerSideCharacter
{
public static class Utils
{
public static string[] ParseArgs(string[] args) // Parse commands args. Example: ' /chest addfriend "player name" ' is parsed into {"addfriend", "player name"} instead of {"addfriend", "player", "name"}
{
List<string> parsedArgs = new List<string>();
string argsString = string.Join(" ", args).Trim();
bool quotes = false;
bool forceAppend = false;
StringBuilder arg = new StringBuilder();
for (int i = 0; i < argsString.Length; i++)
{
char c = argsString[i];
switch (c)
{
case '\\':
if (forceAppend)
{
arg.Append(c);
forceAppend = false;
}
else
forceAppend = true;
continue;
case '"':
if (forceAppend)
{
arg.Append(c);
forceAppend = false;
continue;
}
if (quotes && !forceAppend)
{
quotes = false;
parsedArgs.Add(arg.ToString());
arg.Clear();
}
else if (forceAppend)
{
arg.Append(c);
forceAppend = false;
}
else
quotes = true;
continue;
case ' ':
if (quotes)
{
arg.Append(c);
continue;
}
parsedArgs.Add(arg.ToString());
arg.Clear();
break;
default:
arg.Append(c);
break;
}
}
if (arg.Length > 0)
parsedArgs.Add(arg.ToString());
return parsedArgs.Where(a => a != "").ToArray();
}
public static Player TryGetPlayer(string name)
{
try
{
return Main.player.ToList().Find(player => player != null && player.active && player.name.Contains(name, StringComparison.OrdinalIgnoreCase));
}
catch (Exception)
{
return null;
}
}
public static int TryGetPlayerId(object obj)
{
if (!(obj is int) && !(obj is string) && !(obj is byte))
return -1;
string text = obj.ToString();
if (!int.TryParse(text, out var who))
{
Player player = TryGetPlayer(text);
if (player == null || !player.active)
{
return -1;
}
return player.whoAmI;
}
else
{
if (who < 0 || who > Main.maxPlayers)
return -1;
Player player = Main.player[who];
if (player == null || !player.active)
return -1;
return who;
}
}
private static Dictionary<int, NPC> _npcs = new Dictionary<int, NPC>();
private static readonly int[] IgnoredNpcs = { 8, 9, 11, 12, 14, 15, 36, 40, 41, 89, 90, 91, 92, 96, 97, 99, 100, 118, 119, 128, 129, 130, 131, 135, 136, 246, 247, 248, 261, 263, 264, 265, 394, 396, 397, 403, 404, 413, 414, 455, 456, 457, 458, 459, 492, 511, 512, 514, 515 };
private static void SetupNpcs()
{
for (int i = 0; i < Main.npc.Length; i++)
{
NPC npc = new NPC();
npc.SetDefaults(i);
_npcs.Add(i, npc);
}
}
public static NPC TryGetNpc(string name)
{
try
{
if (_npcs.Count == 0)
SetupNpcs();
NPC[] npcArray = _npcs.ToList().FindAll(v => v.Value.TypeName.Contains(name, StringComparison.OrdinalIgnoreCase) || v.Value.TypeName.Contains(name, StringComparison.OrdinalIgnoreCase)).Select(v => v.Value).ToArray();
NPC best = null;
if (npcArray.Length == 1)
return npcArray[0];
if (npcArray.Length > 1)
foreach (var npc in npcArray)
{
if (IgnoredNpcs.Contains(npc.type))
continue;
if (npc.TypeName.Contains(name, StringComparison.OrdinalIgnoreCase))
best = npc;
if (npc.TypeName.Equals(name, StringComparison.OrdinalIgnoreCase))
{
best = npc;
break;
}
}
else
return null;
return best == null ? npcArray[0] : best;
}
catch (Exception)
{
return null;
}
}
public static NetItem ToNetItem(Item item)
{
NetItem toRet = new NetItem();
if (item.ModItem != null)
{
string nameSpace = item.ModItem.GetType().Namespace;
string itemName = item.ModItem.GetType().Name;
toRet.ItemId = -1;
toRet.IsModItem = true;
toRet.Prefix = item.prefix;
toRet.ModName = nameSpace;
toRet.ItemName = itemName;
toRet.IsFavorite = item.favorited;
return toRet;
}
toRet.ItemId = item.netID;
toRet.IsModItem = false;
toRet.Prefix = item.prefix;
toRet.ModName = "";
toRet.ItemName = "";
toRet.IsFavorite = item.favorited;
return toRet;
}
public static NetItem ToNetItem(int type)
{
Item item = new Item();
item.netDefaults(type);
return ToNetItem(item);
}
public static Item GetItemFromNet(NetItem netItem)
{
if (netItem.IsModItem)
{
var targetMod = ModLoader.Mods.Where(mod => mod.Name == netItem.ModName);
if (targetMod.Count() == 0)
{
return new Item();
}
Item item = new Item();
targetMod.First().TryFind(netItem.ItemName, out ModItem itemType);
item.netDefaults(itemType.Type);
item.prefix = (byte)netItem.Prefix;
item.favorited = netItem.IsFavorite;
return item;
}
else
{
Item item = new Item();
item.netDefaults(netItem.ItemId);
item.prefix = (byte)netItem.Prefix;
item.favorited = netItem.IsFavorite;
return item;
}
}
public static ChestManager LoadChestInfo()
{
if (!File.Exists("SSC/chest.json"))
{
return new ChestManager().Initialize();
}
ChestManager manager;
using (StreamReader sr = new StreamReader("SSC/chest.json"))
{
string data = sr.ReadToEnd();
manager = JsonConvert.DeserializeObject<ChestManager>(data);
}
return manager;
}
public static void SaveChestInfo()
{
string data = JsonConvert.SerializeObject(ServerSideCharacter.ChestManager, Formatting.None);
File.WriteAllText("SSC/chest.json", data);
}
}
}