-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.cs
122 lines (109 loc) · 4.06 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework;
using Newtonsoft.Json;
using TShockAPI;
using System.Net.Sockets;
using System.IO;
namespace Chireiden.Stellaria
{
public static class Utils
{
public static void Chat(TcpClient ts, string text)
{
var data = new MemoryStream();
var wr = new BinaryWriter(data);
wr.Write((short)2 + 1 + 2 + 4 + 1 + text.Length);
wr.Write((byte)82);
wr.Write((short)1);
wr.Write("Say");
wr.Write(text);
ts.Client.Send(data.ToArray());
}
private static readonly MethodInfo _parseParameters =
typeof(Commands).GetMethod("ParseParameters", BindingFlags.NonPublic | BindingFlags.Static);
internal static readonly FieldInfo CacheIP =
typeof(TSPlayer).GetField("CacheIP", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly Random Rng = new Random();
public static List<string> ParseParameters(string text)
{
return (List<string>) _parseParameters?.Invoke(null, new object[] {text.Remove(0, 1)});
}
public static byte[] RandomKey(int length)
{
var ret = new byte[length];
Rng.NextBytes(ret);
return ret;
}
public static string ReadFromBinaryReader(byte[] buffer, int start)
{
return new BinaryReader(new MemoryStream(buffer, start, buffer[start] + 1)).ReadString();
}
public static void ReadConfig<TConfig>(string path, TConfig defaultConfig, out TConfig config)
{
if (!File.Exists(path))
{
config = defaultConfig;
File.WriteAllText(path, JsonConvert.SerializeObject(config, Formatting.Indented));
}
else
{
config = JsonConvert.DeserializeObject<TConfig>(File.ReadAllText(path));
File.WriteAllText(path, JsonConvert.SerializeObject(config, Formatting.Indented));
}
}
public static void Pli(TSPlayer player, string text, bool silent = false)
{
if (string.IsNullOrEmpty(text))
{
return;
}
var args = ParseParameters(text);
if (args == null || args.Count < 1)
{
return;
}
var cmdName = args[0].ToLower();
args.RemoveAt(0);
var cmds = Commands.ChatCommands.Where(c => c.HasAlias(cmdName)).ToList();
if (!cmds.Any())
{
if (player.AwaitingResponse.ContainsKey(cmdName))
{
Action<CommandArgs> call = player.AwaitingResponse[cmdName];
player.AwaitingResponse.Remove(cmdName);
call(new CommandArgs(text.Remove(0, 1), player, args));
return;
}
player.SendErrorMessage("Invalid command entered. Type /help for a list of valid commands.");
return;
}
foreach (var cmd in cmds)
{
if (!cmd.AllowServer && !player.RealPlayer)
{
player.SendErrorMessage("You must use this command in-game.");
}
else
{
if (cmd.DoLog && silent == false)
{
TShock.Utils.SendLogs($"{player.Name} executed: /{text.Remove(0, 1)}.", Color.Red);
}
try
{
cmd.CommandDelegate(new CommandArgs(text.Remove(0, 1), false, player, args));
}
catch (Exception e)
{
player.SendErrorMessage("Command failed, check logs for more details.");
TShock.Log.Error(e.ToString());
}
}
}
}
}
}