-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExtraAdminRESTAPI.cs
991 lines (853 loc) · 36.8 KB
/
ExtraAdminRESTAPI.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/*
*
* Welcome to this template plugin.
* Level: All Levels
* Purpose: To get a working model for new plugins to be built off. This plugin will
* compile immediately, all you have to do is rename TemplatePlugin to reflect
* the purpose of the plugin.
*
* You may need to delete the references to TerrariaServer and TShockAPI. They
* could be pointing to my current folder. Just remove them and then right-click the
* references folder, go to browse go to the dll folder, and select both.
*
*/
using System;
using Terraria;
using TShockAPI;
using TerrariaApi.Server;
using System.Reflection;
using HttpServer;
using System.Collections;
using System.Collections.Generic;
using TShockAPI.DB;
using Rests;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Text;
using System.Text.RegularExpressions;
using Mono.Data.Sqlite;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Data;
using TShockAPI.ServerSideCharacters;
namespace extraAdminREST
{
[ApiVersion(1, 16)]
public class ExtraAdminREST : TerrariaPlugin
{
public static string SavePath = "tshock";
public static IDbConnection DB;
public override Version Version
{
get { return Assembly.GetExecutingAssembly().GetName().Version; }
}
public override string Author
{
get { return "Grandpa-G"; }
}
public override string Name
{
get { return "ExtraAdminREST"; }
}
public override string Description
{
get { return "Extra Admin Rest Api commands"; }
}
public static ConfigFile Config;
public static ServerSideConfig ServerSideCharacterConfig;
public override void Initialize()
{
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/version", getVersion, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/ConsoleInfo", ConsoleInfo, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/Broadcast", Broadcast, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/getPlayerData", getPlayerData, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/GroupList", GroupList, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/PlayerList", PlayerList, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/BanList", BanList, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/WorldInfo", WorldInfo, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/GroupInfo", GroupInfo, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/serverInfo", serverInfo, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/getLog", getLog, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/updateMOTD", updateMOTD, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/getConfig", getConfig, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/getConfigDescription", getConfigDescription, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/updateConfig", updateConfig, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/searchusers", searchUsers, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/updateInventory", updateInventory, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/userlist", UserList, "AdminRest.allow"));
TShock.RestApi.Register(new SecureRestCommand("/AdminREST/getInventory", getInventory, "AdminRest.allow"));
FileTools.SetupConfig();
}
private object Broadcast(RestRequestArgs args)
{
if (string.IsNullOrWhiteSpace(args.Parameters["msg"]))
return RestMissingParam("msg");
String message = args.Parameters["msg"];
TShock.Utils.Broadcast(
"(Server Broadcast) " + message,
Convert.ToByte(TShock.Config.BroadcastRGB[0]), Convert.ToByte(TShock.Config.BroadcastRGB[1]),
Convert.ToByte(TShock.Config.BroadcastRGB[2]));
return RestResponse("Message delievered.");
// return RestResponse("Message delieveredóú.");
}
private object ConsoleInfo(RestRequestArgs args)
{
if (string.IsNullOrWhiteSpace(args.Parameters["msg"]))
return RestMissingParam("msg");
String message = args.Parameters["msg"];
TShockAPI.Log.ConsoleInfo(message);
return RestResponse("Message delievered.");
}
private object PlayerList(RestRequestArgs args)
{
var playerList = new ArrayList();
foreach (TSPlayer tsPlayer in TShock.Players.Where(p => null != p))
{
var p = PlayerFilter(tsPlayer, args.Parameters);
if (null != p)
playerList.Add(p);
}
return new RestObject() { { "players", playerList } };
}
private object WorldInfo(RestRequestArgs args)
{
var msg = new TShockAPI.Net.WorldInfoMsg
{
Time = (int)Main.time,
DayTime = Main.dayTime,
MoonPhase = (byte)Main.moonPhase,
BloodMoon = Main.bloodMoon,
Eclipse = Main.eclipse,
MaxTilesX = (short)Main.maxTilesX,
MaxTilesY = (short)Main.maxTilesY,
SpawnX = (short)Main.spawnTileX,
SpawnY = (short)Main.spawnTileY,
WorldSurface = (short)Main.worldSurface,
RockLayer = (short)Main.rockLayer,
//Sending a fake world id causes the client to not be able to find a stored spawnx/y.
//This fixes the bed spawn point bug. With a fake world id it wont be able to find the bed spawn.
WorldID = Main.worldID,
MoonType = (byte)Main.moonType,
TreeX0 = Main.treeX[0],
TreeX1 = Main.treeX[1],
TreeX2 = Main.treeX[2],
TreeStyle0 = (byte)Main.treeStyle[0],
TreeStyle1 = (byte)Main.treeStyle[1],
TreeStyle2 = (byte)Main.treeStyle[2],
TreeStyle3 = (byte)Main.treeStyle[3],
CaveBackX0 = Main.caveBackX[0],
CaveBackX1 = Main.caveBackX[1],
CaveBackX2 = Main.caveBackX[2],
CaveBackStyle0 = (byte)Main.caveBackStyle[0],
CaveBackStyle1 = (byte)Main.caveBackStyle[1],
CaveBackStyle2 = (byte)Main.caveBackStyle[2],
CaveBackStyle3 = (byte)Main.caveBackStyle[3],
SetBG0 = (byte)WorldGen.treeBG,
SetBG1 = (byte)WorldGen.corruptBG,
SetBG2 = (byte)WorldGen.jungleBG,
SetBG3 = (byte)WorldGen.snowBG,
SetBG4 = (byte)WorldGen.hallowBG,
SetBG5 = (byte)WorldGen.crimsonBG,
SetBG6 = (byte)WorldGen.desertBG,
SetBG7 = (byte)WorldGen.oceanBG,
IceBackStyle = (byte)Main.iceBackStyle,
JungleBackStyle = (byte)Main.jungleBackStyle,
HellBackStyle = (byte)Main.hellBackStyle,
WindSpeed = Main.windSpeed,
NumberOfClouds = (byte)Main.numClouds,
BossFlags = (WorldGen.shadowOrbSmashed ? TShockAPI.Net.BossFlags.OrbSmashed : TShockAPI.Net.BossFlags.None) |
(NPC.downedBoss1 ? TShockAPI.Net.BossFlags.DownedBoss1 : TShockAPI.Net.BossFlags.None) |
(NPC.downedBoss2 ? TShockAPI.Net.BossFlags.DownedBoss2 : TShockAPI.Net.BossFlags.None) |
(NPC.downedBoss3 ? TShockAPI.Net.BossFlags.DownedBoss3 : TShockAPI.Net.BossFlags.None) |
(Main.hardMode ? TShockAPI.Net.BossFlags.HardMode : TShockAPI.Net.BossFlags.None) |
(NPC.downedClown ? TShockAPI.Net.BossFlags.DownedClown : TShockAPI.Net.BossFlags.None) |
(Main.ServerSideCharacter ? TShockAPI.Net.BossFlags.ServerSideCharacter : TShockAPI.Net.BossFlags.None) |
(NPC.downedPlantBoss ? TShockAPI.Net.BossFlags.DownedPlantBoss : TShockAPI.Net.BossFlags.None),
BossFlags2 = (NPC.downedMechBoss1 ? TShockAPI.Net.BossFlags2.DownedMechBoss1 : TShockAPI.Net.BossFlags2.None) |
(NPC.downedMechBoss2 ? TShockAPI.Net.BossFlags2.DownedMechBoss2 : TShockAPI.Net.BossFlags2.None) |
(NPC.downedMechBoss3 ? TShockAPI.Net.BossFlags2.DownedMechBoss3 : TShockAPI.Net.BossFlags2.None) |
(NPC.downedMechBossAny ? TShockAPI.Net.BossFlags2.DownedMechBossAny : TShockAPI.Net.BossFlags2.None) |
(Main.cloudBGActive == 1f ? TShockAPI.Net.BossFlags2.CloudBg : TShockAPI.Net.BossFlags2.None) |
(WorldGen.crimson ? TShockAPI.Net.BossFlags2.Crimson : TShockAPI.Net.BossFlags2.None) |
(Main.pumpkinMoon ? TShockAPI.Net.BossFlags2.PumpkinMoon : TShockAPI.Net.BossFlags2.None),
Rain = Main.maxRaining,
WorldName = TShock.Config.UseServerName ? TShock.Config.ServerName : Main.worldName
};
Console.Write(msg.BloodMoon);
Console.Write(msg.DayTime);
return new RestObject() { { "bloodmoon", msg.BloodMoon } };
}
private object BanList(RestRequestArgs args)
{
var banList = new ArrayList();
foreach (var ban in TShock.Bans.GetBans())
{
int userId = -1;
try
{
TShockAPI.DB.User user = TShock.Users.GetUserByName(ban.Name);
userId = user.ID;
}
catch (NullReferenceException e) {
userId = -1;
}
banList.Add(
new Dictionary<string, string>
{
{"name", null == ban.Name ? "" : ban.Name},
{"userid", String.Format("{0}", userId)},
{"ip", null == ban.IP ? "" : ban.IP},
{"reason", null == ban.Reason ? "" : ban.Reason},
{"banninguser", null == ban.BanningUser? "" : ban.BanningUser},
{"date", null == ban.Date ? "" : ban.Date},
{"expiration", null == ban.Expiration ? "" : ban.Expiration},
}
);
}
return new RestObject() { { "bans", banList } };
}
private object GroupList(RestRequestArgs args)
{
var groups = new ArrayList();
foreach (TShockAPI.Group group in TShock.Groups)
{
groups.Add(new Dictionary<string, object> { { "name", group.Name }, { "parent", group.ParentName }, { "chatcolor", group.ChatColor }, {"prefix", group.Prefix}, {"suffix", group.Suffix} });
}
return new RestObject() { { "groups", groups } };
}
private object GroupInfo(RestRequestArgs args)
{
var ret = GroupFind(args.Parameters);
if (ret is RestObject)
return ret;
TShockAPI.Group group = (TShockAPI.Group)ret;
return new RestObject() {
{"name", group.Name},
{"parent", group.ParentName},
{"chatcolor", string.Format("{0},{1},{2}", group.R, group.G, group.B)},
{"permissions", group.permissions},
{"prefix", group.Prefix},
{"suffix", group.Suffix},
{"negatedpermissions", group.negatedpermissions},
{"totalpermissions", group.TotalPermissions}
};
}
private object getLog(RestRequestArgs args)
{
if (string.IsNullOrWhiteSpace(args.Parameters["count"]))
return RestMissingParam("count");
String lineCount = args.Parameters["count"];
var directory = new DirectoryInfo(TShock.Config.LogPath);
String searchPattern = @"(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01]).*.log";
var log = Directory.GetFiles(TShock.Config.LogPath).Where(path => Regex.Match(path, searchPattern).Success).Last();
String logFile = Path.GetFullPath(log);
FileStream logFileStream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader logFileReader = new StreamReader(logFileStream);
int limit = System.Convert.ToInt32(lineCount);
var buffor = new Queue<string>(limit);
while (!logFileReader.EndOfStream)
{
string line = logFileReader.ReadLine();
if (buffor.Count >= limit)
buffor.Dequeue();
buffor.Enqueue(line);
}
string[] LogLinesEnd = buffor.ToArray();
// Clean up
logFileReader.Close();
logFileStream.Close();
return new RestObject() { { "file", logFile }, { "log", LogLinesEnd } };
}
#region GiveAll+
public static void ShowColor(CommandArgs args)
{
if (args.Parameters.Count < 1)
{
args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /giveall <item name/id> [item amount] [prefix id/name]");
return;
}
int amountParamIndex = -1;
string itemNameOrId;
if (amountParamIndex == -1)
itemNameOrId = string.Join(" ", args.Parameters);
else
itemNameOrId = string.Join(" ", args.Parameters.Take(amountParamIndex));
foreach (TSPlayer plr in TShock.Players)
{
if (plr != null)
{
TSPlayer.All.SendSuccessMessage("{0} hair {1} skin {2}.", args.Player.Name, plr.TPlayer.hairColor, plr.TPlayer.skinColor);
}
}
}
#endregion
private object getPlayerData(RestRequestArgs args)
{
var ret = PlayerFind(args.Parameters);
if (ret is RestObject)
return ret;
TSPlayer player = (TSPlayer)ret;
var inventory = player.TPlayer.inventory.Where(p => p.active).ToList();
var equipment = player.TPlayer.armor.Where(p => p.active).ToList();
var dyes = player.TPlayer.dye.Where(p => p.active).ToList();
return new RestObject()
{
{"index", player.Index},
{"nickname", player.Name},
{"username", null == player.UserAccountName ? "" : player.UserAccountName},
{"group", player.Group.Name},
{"inventory", string.Join(", ", inventory.Select(p => (p.name + ":" + p.stack)))},
{"armor", string.Join(", ", equipment.Select(p => (p.netID + ":" + p.prefix)))},
{"haircolor", player.TPlayer.hairColor},
{"skincolor", player.TPlayer.skinColor},
{"pantsColor", player.TPlayer.pantsColor},
{"shirtColor", player.TPlayer.shirtColor},
{"underShirtColor", player.TPlayer.underShirtColor},
{"eyeColor", player.TPlayer.eyeColor},
{"shoeColor", player.TPlayer.shoeColor},
{"dyes", string.Join(", ", dyes.Select(p => (p.name)))},
{"buffs", string.Join(", ", player.TPlayer.buffType)}
};
}
public static RestObject serverInfo(RestRequestArgs args)
{
return new RestObject() { { "worldID", Main.worldID } };
}
public static RestObject getConfigDescription(RestRequestArgs args)
{
if (string.IsNullOrWhiteSpace(args.Parameters["configFile"]))
return new RestObject("400") { Response = "No config type given" };
String configFile = args.Parameters["configFile"];
if (configFile == null)
{
return new RestObject("400") { Response = "No config type given" };
}
DumpDescriptions((configFile.Equals("config.json")));
return new RestObject() { { "description", ConfigDescription } };
}
public static RestObject getConfig(RestRequestArgs args)
{
if (string.IsNullOrWhiteSpace(args.Parameters["config"]))
return new RestObject("400") { Response = "No config given" };
String config = args.Parameters["config"];
if (config == null)
{
// Console.WriteLine(parameters["config"]);
return new RestObject("400") { Response = "No config given" };
}
String configFilePath = Path.Combine(TShock.SavePath, config);
if (!File.Exists(configFilePath))
{
return new RestObject("400") { Response = "Invalid file path" };
}
using (var sr = new StreamReader(configFilePath))
{
if (config.Equals("config.json"))
{
var cf = JsonConvert.DeserializeObject<ConfigFile>(sr.ReadToEnd());
return new RestObject() { { "config", cf } };
}
else
{
var cf = JsonConvert.DeserializeObject<ServerSideConfig>(sr.ReadToEnd());
return new RestObject() { { "config", cf } };
}
}
}
public static RestObject updateConfig(RestRequestArgs args)
{
if (string.IsNullOrWhiteSpace(args.Parameters["config"]))
return new RestObject("400") { Response = "No config given" };
String config = args.Parameters["config"];
if (config == null)
{
return new RestObject("400") { Response = "No config given" };
}
if (string.IsNullOrWhiteSpace(args.Parameters["configFile"]))
return RestMissingParam("configFile");
String configFile = args.Parameters["configFile"];
if (configFile == null)
{
// Console.WriteLine(parameters["config"]);
return new RestObject("400") { Response = "No config file given" };
}
JObject json = JObject.Parse(config);
Dictionary<string, dynamic> configList = new Dictionary<string, dynamic> { };
String configFilePath = Path.Combine(TShock.SavePath, configFile);
if (File.Exists(configFilePath))
{
using (var sw = new StreamWriter(configFilePath))
{
string j = JsonConvert.SerializeObject(json, Formatting.Indented);
sw.Write(j);
sw.Flush();
sw.Close();
}
// File.WriteAllText(configFilePath, config);
}
else
{
Console.WriteLine(configFilePath);
return new RestObject("400") { Response = "Invalid file path" };
}
return new RestObject() { Response = "config.json saved." };
}
public static RestObject updateMOTD(RestRequestArgs args)
{
if (string.IsNullOrWhiteSpace(args.Parameters["motd"]))
return new RestObject("400") { Response = "No text given" };
String motd = Convert.ToString(args.Parameters["motd"]);
if (motd == null)
{
return new RestObject("400") { Response = "No text given" };
}
String f = Path.Combine(TShock.SavePath, "motd.txt");
// Console.WriteLine(f);
if (File.Exists(f))
{
File.WriteAllText(f, motd);
}
return new RestObject()
{
{ "motd", motd }
};
}
private object getInventory(RestRequestArgs args)
{
if (string.IsNullOrWhiteSpace(args.Parameters["account"]))
return new RestObject("400") { Response = "Found no matches." };
int account = Convert.ToInt32(args.Parameters["account"]);
if (account <= 0)
{
return new RestObject("400") { Response = "Found no matches." };
}
List<SSCInventory> inventory = GetSSCInventory(account);
return new RestObject { { "inventory", inventory } };
}
public static RestObject updateInventory(RestRequestArgs args)
{
if (string.IsNullOrWhiteSpace(args.Parameters["account"]))
return new RestObject("400") { Response = "Invalid account." };
int userId = Convert.ToInt32(args.Parameters["account"]);
if (userId <= 0)
{
return new RestObject("400") { Response = "Invalid account." };
}
if (string.IsNullOrWhiteSpace(args.Parameters["inventory"]))
return RestMissingParam("inventory");
string inventory = Convert.ToString(args.Parameters["inventory"]);
if (inventory == null)
{
return new RestObject("400") { Response = "Invalid inventory." };
}
TShockAPI.DB.User user = TShock.Users.GetUserByID(userId);
foreach (var player in TShock.Players.Where(p => null != p && p.UserAccountName == user.Name))
{
return new RestObject("400") { Response = "Player active, inventory may not be changed." };
}
bool ok = ModifySSCInventory(userId, inventory);
if (ok)
return new RestObject { { "update", inventory } };
else
return new RestObject("400") { Response = "Update failure." };
}
public static RestObject getVersion(RestRequestArgs args)
{
String dbType;
if (TShock.DB.GetSqlType() == SqlType.Sqlite)
dbType = "SQLite";
else
dbType = "MySQL";
return new RestObject()
{
{ "version", Assembly.GetExecutingAssembly().GetName().Version.ToString() },
{ "db", dbType }
};
}
public static RestObject searchUsers(RestRequestArgs args)
{
string searchString = args.Parameters["where"];
if (searchString == null)
return RestError("Missing or empty search string - /AdminREST/searchusers where=<where clause>~");
List<UserList> userList = FindUsers(searchString, true);
return new RestObject() { { "Users", userList } };
}
public static RestObject UserList(RestRequestArgs args)
{
var ret = UserFind(args.Parameters);
if (ret is RestObject)
return (RestObject)ret;
User user = (User)ret;
return new RestObject()
{
{"id", user.ID},
{"username", user.Name},
{"ip", user.KnownIps},
{"uuid", user.UUID},
{"registered", user.Registered},
{"lastaccessed", user.LastAccessed},
{"group", user.Group}
};
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
}
base.Dispose(disposing);
}
public ExtraAdminREST(Main game)
: base(game)
{
Order = 1;
}
#region Utility Methods
private static RestObject RestError(string message, string status = "400")
{
return new RestObject(status) { Error = message };
}
private RestObject RestResponse(string message, string status = "200")
{
return new RestObject(status) { Response = message };
}
private static RestObject RestMissingParam(string var)
{
return RestError("Missing or empty " + var + " parameter");
}
private static RestObject RestMissingParam(params string[] vars)
{
return RestMissingParam(string.Join(", ", vars));
}
private RestObject RestInvalidParam(string var)
{
return RestError("Missing or invalid " + var + " parameter");
}
private bool GetBool(string val, bool def)
{
bool ret;
return bool.TryParse(val, out ret) ? ret : def;
}
private static object PlayerFind(IParameterCollection parameters)
{
string name = parameters["player"];
if (string.IsNullOrWhiteSpace(name))
return RestMissingParam("player");
var found = TShock.Utils.FindPlayer(name);
switch (found.Count)
{
case 1:
return found[0];
case 0:
return RestError("Player " + name + " was not found");
default:
return RestError("Player " + name + " matches " + found.Count + " players");
}
}
private static object UserFind(IParameterCollection parameters)
{
string name = parameters["user"];
if (string.IsNullOrWhiteSpace(name))
return RestMissingParam("user");
User user;
string type = parameters["type"];
try
{
switch (type)
{
case null:
case "name":
type = "name";
user = TShock.Users.GetUserByName(name);
break;
case "id":
user = TShock.Users.GetUserByID(Convert.ToInt32(name));
break;
default:
return RestError("Invalid Type: '" + type + "'");
}
}
catch (Exception e)
{
return RestError(e.Message);
}
if (null == user)
return RestError(String.Format("User {0} '{1}' doesn't exist", type, name));
return user;
}
private object BanFind(IParameterCollection parameters)
{
string name = parameters["ban"];
if (string.IsNullOrWhiteSpace(name))
return RestMissingParam("ban");
string type = parameters["type"];
if (string.IsNullOrWhiteSpace(type))
return RestMissingParam("type");
Ban ban;
switch (type)
{
case "ip":
ban = TShock.Bans.GetBanByIp(name);
break;
case "name":
ban = TShock.Bans.GetBanByName(name, GetBool(parameters["caseinsensitive"], true));
break;
default:
return RestError("Invalid Type: '" + type + "'");
}
if (null == ban)
return RestError("Ban " + type + " '" + name + "' doesn't exist");
return ban;
}
private object GroupFind(IParameterCollection parameters)
{
var name = parameters["group"];
if (string.IsNullOrWhiteSpace(name))
return RestMissingParam("group");
var group = TShock.Groups.GetGroupByName(name);
if (null == group)
return RestError("Group '" + name + "' doesn't exist");
return group;
}
private Dictionary<string, object> PlayerFilter(TSPlayer tsPlayer, IParameterCollection parameters, bool viewips = false)
{
var player = new Dictionary<string, object>
{
{"nickname", tsPlayer.Name},
{"index", tsPlayer.Index},
{"username", tsPlayer.UserAccountName ?? ""},
{"account", tsPlayer.UserID},
{"group", tsPlayer.Group.Name},
{"active", tsPlayer.Active},
{"state", tsPlayer.State},
{"team", tsPlayer.Team},
};
if (viewips)
{
player.Add("ip", tsPlayer.IP);
}
foreach (IParameter filter in parameters)
{
if (player.ContainsKey(filter.Name) && !player[filter.Name].Equals(filter.Value))
return null;
}
return player;
}
#endregion
/// <summary>
/// Gets a list of Users from db.
/// </summary>
private static List<UserList> FindUsers(string search, bool casesensitive = true)
{
UserList rec;
String sql;
List<UserList> UserList = new List<UserList>();
try
{
sql = "SELECT * FROM Users " + search + " order by UserName";
using (var reader = TShock.DB.QueryReader(sql))
{
while (reader.Read())
{
rec = new UserList(reader.Get<Int32>("Id"), reader.Get<string>("UserName"), reader.Get<string>("UserGroup"), reader.Get<string>("Registered"), reader.Get<string>("LastAccessed"), reader.Get<string>("KnownIPs"));
UserList.Add(rec);
}
return UserList;
}
}
catch (Exception ex)
{
Log.Error(ex.ToString());
Console.WriteLine(ex.StackTrace);
}
return null;
}
/// <summary>
/// Gets a list of SSCInventoryLog entries.
/// </summary>
public List<SSCInventory> GetSSCInventory(int account)
{
SSCInventory rec;
String sql;
List<SSCInventory> SSCInventorylist = new List<SSCInventory>();
try
{
using (var reader = TShock.DB.QueryReader("SELECT * FROM tsCharacter where account =@0", account))
{
if (reader.Read())
{
rec = new SSCInventory(reader.Get<Int32>("Account"), reader.Get<string>("Inventory"),
reader.Get<Int32>("hair"), reader.Get<Int32>("hairdye"), (Color)TShock.Utils.DecodeColor(reader.Get<Int32>("haircolor")),
(Color)TShock.Utils.DecodeColor(reader.Get<Int32>("pantscolor")), (Color)TShock.Utils.DecodeColor(reader.Get<Int32>("shirtcolor")), (Color)TShock.Utils.DecodeColor(reader.Get<Int32>("undershirtcolor")), (Color)TShock.Utils.DecodeColor(reader.Get<Int32>("shoecolor")),
(Color)TShock.Utils.DecodeColor(reader.Get<Int32>("skincolor")), (Color)TShock.Utils.DecodeColor(reader.Get<Int32>("eyecolor"))
);
SSCInventorylist.Add(rec);
return SSCInventorylist;
}
}
}
catch (Exception ex)
{
Log.Error(ex.ToString());
Console.WriteLine(ex.StackTrace);
}
return null;
}
/// <summary>
/// Gets a list of SSCInventoryLog entries.
/// </summary>
public static bool ModifySSCInventory(int account, string inventory)
{
try
{
using (var reader = TShock.DB.QueryReader("UPDATE tsCharacter set Inventory = @1 where account =@0", account, inventory))
{
try
{
// using (TShock.DB.QueryReader("COMMIT"))
{
}
}
catch (Exception ex)
{
Log.Error(ex.ToString());
Console.WriteLine(ex.StackTrace);
return false;
}
}
}
catch (Exception ex)
{
Log.Error(ex.ToString());
Console.WriteLine(ex.StackTrace);
return false;
}
return true;
}
/// <summary>
/// Dumps all configuration options to a text file in Markdown format
/// </summary>
// List<String[]> ConfigDescription = new List<String[]>();
public static Dictionary<string, object> ConfigDescription = new Dictionary<string, object> { };
public static void DumpDescriptions(bool SSCconfigFile)
{
ConfigDescription.Clear();
if (SSCconfigFile)
{
var defaults = new ConfigFile();
foreach (var field in defaults.GetType().GetFields().OrderBy(f => f.Name))
{
if (field.IsStatic)
continue;
var name = field.Name;
var type = field.FieldType.Name;
var descattr =
field.GetCustomAttributes(false).FirstOrDefault(o => o is DescriptionAttribute) as DescriptionAttribute;
string desc = descattr != null && !string.IsNullOrWhiteSpace(descattr.Description) ? descattr.Description : "None";
var def = field.GetValue(defaults);
Dictionary<string, string> definition = new Dictionary<string, string> { };
definition.Add("type", type);
// desc = desc.Replace("\"", "");
definition.Add("definition", desc);
definition.Add("default", def.ToString());
ConfigDescription.Add(name, definition);
}
}
else
{
var defaults = new ServerSideConfig();
foreach (var field in defaults.GetType().GetFields().OrderBy(f => f.Name))
{
if (field.IsStatic)
continue;
var name = field.Name;
var type = field.FieldType.Name;
var descattr =
field.GetCustomAttributes(false).FirstOrDefault(o => o is DescriptionAttribute) as DescriptionAttribute;
string desc = descattr != null && !string.IsNullOrWhiteSpace(descattr.Description) ? descattr.Description : "None";
var def = field.GetValue(defaults);
Dictionary<string, string> definition = new Dictionary<string, string> { };
definition.Add("type", type);
// desc = desc.Replace("\"", "");
definition.Add("definition", desc);
definition.Add("default", def.ToString());
ConfigDescription.Add(name, definition);
}
}
}
}
public class SSCInventory
{
public Int32 Id { get; set; }
public string Inventory { get; set; }
public Int32 Hair { get; set; }
public Int32 HairDye { get; set; }
public Color HairColor { get; set; }
public Color PantsColor { get; set; }
public Color ShirtColor { get; set; }
public Color UnderShirtColor { get; set; }
public Color ShoeColor { get; set; }
public Color SkinColor { get; set; }
public Color EyeColor { get; set; }
public SSCInventory(Int32 id, string inventory, Int32 hair, Int32 hairdye, Color haircolor,
Color pantscolor, Color shirtcolor, Color undershirtcolor, Color shoecolor, Color skincolor, Color eyecolor)
{
Id = id;
Inventory = inventory;
Hair = hair;
HairDye = hairdye;
HairColor = haircolor;
PantsColor = pantscolor;
ShirtColor = shirtcolor;
UnderShirtColor = undershirtcolor;
ShoeColor = haircolor;
SkinColor = skincolor;
EyeColor = eyecolor;
}
public SSCInventory()
{
Id = 0;
Inventory = string.Empty;
Hair = 0;
HairDye = 0;
HairColor = new Color(0,0,0);
PantsColor = new Color(0, 0, 0);
ShirtColor = new Color(0, 0, 0);
UnderShirtColor = new Color(0, 0, 0);
ShoeColor = new Color(0, 0, 0);
SkinColor = new Color(0, 0, 0);
EyeColor = new Color(0, 0, 0);
}
}
public class UserList
{
public Int32 Id { get; set; }
public string UserName { get; set; }
public string UserGroup { get; set; }
public string Registered { get; set; }
public string LastAccessed { get; set; }
public string KnownIPs { get; set; }
public UserList(Int32 id, string username, string usergroup, string registered, string lastaccessed, string knownIPs)
{
Id = id;
Console.Write(UserName + ":" + username);
UserName = username;
UserGroup = usergroup;
Registered = registered;
LastAccessed = lastaccessed;
KnownIPs = knownIPs;
}
public UserList()
{
Id = 0;
UserName = string.Empty;
UserGroup = string.Empty;
Registered = string.Empty;
LastAccessed = string.Empty;
KnownIPs = string.Empty;
}
}
}