forked from RedBigz/Citruslib-FixedUp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatCommands.cs
More file actions
228 lines (191 loc) · 7.51 KB
/
ChatCommands.cs
File metadata and controls
228 lines (191 loc) · 7.51 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using Landfall.Network;
using Newtonsoft.Json;
using UnityEngine;
namespace WobbleBridge
{
public static partial class Wobble
{
internal static PermList permList;
static SettingsFile<PermList> perms;
static Dictionary<string, Command> commands = new Dictionary<string, Command>();
public static bool disableMissingCommandParrot = false;
internal static void WriteNewPermList()
{
perms.settings = new List<PermList>() { permList };
perms.WriteSettings();
}
internal static void LoadPermSettings(string path)
{
bool absolute = true;
if (path == "")
{
absolute = false;
path = "PlayerPerms";
}
perms = new SettingsFile<PermList>(path, absolute);
PermList defaultPermList = new PermList();
defaultPermList.name = "players";
defaultPermList.description = "List of players with modified permission level. Default permission level is 0.";
PermList.PermPlayer dp = new PermList.PermPlayer();
dp.epic = "epic goes here";
dp.name = "player name (does not need to be exact!)";
dp.permlevel = 1;
defaultPermList.players.Add(dp);
defaultPermList.players.Add(dp);
perms.AddSetting(defaultPermList);
perms.ReadSettings();
if (perms.TryGetSetting("players", out permList))
{
log.Log("Player Perm List loaded!");
foreach (PermList.PermPlayer ply in permList.players)
log.Log($"{ply.name} ({ply.epic}), PermLevel: {ply.permlevel}");
}
else
{
log.LogError("Missing player permission list!");
}
}
/// <summary>
/// Adds a chat command that can be triggered by multiple names.
/// </summary>
public static void AddCommand(string[] names, Action<string[], TABGPlayerServer> function, string modName = "", string description = "", string paramDesc = "")
{
foreach (string name in names)
{
AddCommand(name, function, modName, description, paramDesc);
paramDesc = "";
description = "";
}
}
/// <summary>
/// Adds a chat command with a single name.
/// </summary>
public static void AddCommand(string name, Action<string[], TABGPlayerServer> function, string modName = "", string description = "", string paramDesc = "", int permLevel = 1)
{
if (commands.ContainsKey(name))
{
log.LogError($"Command \"{name}\" is already registered by another mod!");
return;
}
if (function == null)
{
log.LogError($"Refusing to register command \"{name}\" — it has no function!");
return;
}
commands.Add(name, new Command(name, function, modName, description, paramDesc, permLevel));
log.Log($"[Commands] Registered command: \"{name}\" (mod: {modName}, permLevel: {permLevel})");
}
internal static bool RunCommand(string name, string[] prms, TABGPlayerServer player)
{
if (commands.TryGetValue(name, out Command cmd))
{
return cmd.Run(prms, player);
}
else
{
log.LogWarning($"[Commands] Unknown command: \"{name}\" — registered commands: [{string.Join(", ", commands.Keys)}]");
if (!disableMissingCommandParrot)
SelfParrot(player, "unknown command: " + name);
return false;
}
}
static int CommandCompare(Command a, Command b) => a.permLevel.CompareTo(b.permLevel);
internal static void WriteAllCommands()
{
string f = "COMMANDS LIST:";
log.Log("Writing Command List to file!");
Dictionary<string, List<Command>> sort = new Dictionary<string, List<Command>>();
foreach (Command c in commands.Values)
{
string key = c.modName != "" ? c.modName : "Unnamed Commands";
if (!sort.ContainsKey(key))
sort.Add(key, new List<Command>());
sort[key].Add(c);
}
foreach (List<Command> lc in sort.Values)
{
f += "\n\n";
f += "======" + (lc[0].modName != "" ? lc[0].modName : "General\n") + "======";
lc.Sort(CommandCompare);
foreach (Command c in lc)
{
f += "\n\n";
f += c.ToString();
}
}
DirectoryInfo directoryInfo = new DirectoryInfo(Application.dataPath);
string text = Path.Combine(directoryInfo.Parent.FullName, "Commands_List.txt");
File.WriteAllText(text, f);
}
}
[Serializable]
internal class PermList : SettingObject
{
[JsonProperty(Order = 1)]
public List<PermPlayer> players = new List<PermPlayer>();
[Serializable]
public class PermPlayer
{
public string name;
public string epic;
public int permlevel;
}
}
class Command
{
string name;
public Action<string[], TABGPlayerServer> func;
string description;
string paramDesc;
public readonly string modName;
public readonly int permLevel;
public Command(string n, Action<string[], TABGPlayerServer> f, string mName = "", string desc = "", string pDesc = "", int plev = 1)
{
name = n;
func = f;
description = desc;
paramDesc = pDesc;
modName = mName;
permLevel = plev;
}
public override string ToString()
{
string ret = description;
if (ret != "") ret += "\n";
ret += $"Perm Level: {permLevel}\n";
ret += "/" + name + " " + paramDesc;
return ret;
}
public bool Run(string[] prms, TABGPlayerServer player)
{
if (player == null)
{
Wobble.log.LogWarning($"[Commands] Command \"{name}\" called with null player — aborting.");
return false;
}
int plev = 0;
if (Wobble.permList != null)
{
PermList.PermPlayer ply = Wobble.permList.players.Find(p => p.epic == player.EpicUserName);
if (ply != null) plev = ply.permlevel;
}
else
{
Wobble.log.LogWarning($"[Commands] permList is null — treating player {player.PlayerName} as perm level 0.");
}
if (plev < permLevel)
{
Wobble.log.Log($"[Commands] Player \"{player.PlayerName}\" lacks permission for \"{name}\" (has {plev}, needs {permLevel}).");
if (!Wobble.disableMissingCommandParrot)
Wobble.SelfParrot(player, $"Command \"{name}\" requires a higher permission level.");
return false;
}
Wobble.log.Log($"[Commands] Executing \"{name}\" for player \"{player.PlayerName}\".");
func(prms, player);
return true;
}
}
}