-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCleanupModule.cs
More file actions
360 lines (314 loc) · 13.3 KB
/
CleanupModule.cs
File metadata and controls
360 lines (314 loc) · 13.3 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;
using Sandbox.Game.Entities;
using Sandbox.Game.World;
using Torch.Commands;
using NLog;
using Sandbox.Game.EntityComponents;
using Sandbox.Game.Multiplayer;
using SpaceEngineers.Game.Entities.Blocks;
using Torch.Mod;
using Torch.Mod.Messages;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
using Vector3D = VRageMath.Vector3D;
namespace Essentials.Commands
{
[Category("cleanup")]
public class CleanupModule : CommandModule
{
private static readonly Logger Log = LogManager.GetLogger("Essentials");
[Command("scan", "Find grids matching the given conditions")]
public void Scan()
{
var count = ScanConditions(Context.Args).Count();
Context.Respond($"Found {count} grids matching the given conditions.");
}
[Command("list", "Lists grids matching the given conditions")]
public void List()
{
var grids = ScanConditions(Context.Args).OrderBy(g => g.DisplayName).ToList();
if (Context.SentBySelf)
{
Context.Respond(String.Join("\n", grids.Select((g, i) => $"{i + 1}. {grids[i].DisplayName} ({grids[i].BlocksCount} block(s))")));
Context.Respond($"Found {grids.Count} grids matching the given conditions.");
}
else
{
var m = new DialogMessage("Cleanup", null, $"Found {grids.Count} matching", String.Join("\n", grids.Select((g, i) => $"{i + 1}. {grids[i].DisplayName} ({grids[i].BlocksCount} block(s))")));
ModCommunication.SendMessageTo(m, Context.Player.SteamUserId);
}
}
[Command("delete", "Delete grids matching the given conditions")]
public void Delete()
{
var count = 0;
foreach (var grid in ScanConditions(Context.Args))
{
Log.Info($"Deleting grid: {grid.EntityId}: {grid.DisplayName}");
grid.Close();
count++;
}
Context.Respond($"Deleted {count} grids matching the given conditions.");
Log.Info($"Cleanup deleted {count} grids matching conditions {string.Join(", ", Context.Args)}");
}
[Command("help", "Lists all cleanup conditions.")]
public void Help()
{
var sb = new StringBuilder();
foreach (var c in _conditionLookup)
{
sb.AppendLine($"{c.Command}{(string.IsNullOrEmpty(c.InvertCommand) ? string.Empty : $" ({c.InvertCommand})")}:");
sb.AppendLine($" {c.HelpText}");
}
if(!Context.SentBySelf)
ModCommunication.SendMessageTo(new DialogMessage("Cleanup help", null, sb.ToString()), Context.Player.SteamUserId);
else
Context.Respond(sb.ToString());
}
public CleanupModule()
{
var methods = typeof(CleanupModule).GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
_conditionLookup = new List<Condition>(methods.Length);
foreach (var m in methods)
{
var a = m.GetCustomAttribute<ConditionAttribute>();
if (a == null)
continue;
var c = new Condition(m, a);
_conditionLookup.Add(c);
}
}
private List<Condition> _conditionLookup;
private IEnumerable<MyCubeGrid> ScanConditions(IReadOnlyList<string> args)
{
var conditions = new List<Func<MyCubeGrid, bool?>>();
for (var i = 0; i < args.Count; i ++)
{
string parameter;
if (i + 1 >= args.Count)
{
parameter = null;
}
else
{
parameter = args[i + 1];
}
var arg = args[i];
if (parameter != null)
{
//parameter is the name of a command. Assume this command requires no parameters
if (_conditionLookup.Any(c => parameter.Equals(c.Command, StringComparison.CurrentCultureIgnoreCase) || parameter.Equals(c.InvertCommand, StringComparison.CurrentCultureIgnoreCase)))
{
parameter = null;
}
//next string is a parameter, so pass it to the condition and skip it next loop
else
i++;
}
bool found = false;
foreach (var condition in _conditionLookup)
{
if (arg.Equals(condition.Command, StringComparison.CurrentCultureIgnoreCase))
{
conditions.Add(g => condition.Evaluate(g, parameter, false, this));
found = true;
break;
}
else if (arg.Equals(condition.InvertCommand, StringComparison.CurrentCultureIgnoreCase))
{
conditions.Add(g => condition.Evaluate(g, parameter, true, this));
found = true;
break;
}
}
if (!found)
{
Context.Respond($"Unknown argument '{arg}'");
yield break;
}
}
foreach (var group in MyCubeGridGroups.Static.Logical.Groups)
{
//if (group.Nodes.All(grid => conditions.TrueForAll(func => func(grid.NodeData))))
foreach (var node in group.Nodes)
{
foreach (var c in conditions)
{
bool? r = c.Invoke(node.NodeData);
if (r == false || r == null)
yield break;
}
}
foreach (var grid in group.Nodes)
yield return grid.NodeData;
}
}
[Condition("name", helpText: "Finds grids with a matching name. Accepts regex format.")]
public bool NameMatches(MyCubeGrid grid, string str)
{
if (string.IsNullOrEmpty(grid.DisplayName))
return false;
var regex = new Regex(str);
return regex.IsMatch(grid.DisplayName);
}
[Condition("blockslessthan", helpText: "Finds grids with less than the given number of blocks.")]
public bool BlocksLessThan(MyCubeGrid grid, int count)
{
return grid.BlocksCount < count;
}
[Condition("blocksgreaterthan", helpText: "Finds grids with more than the given number of blocks.")]
public bool BlocksGreaterThan(MyCubeGrid grid, int count)
{
return grid.BlocksCount > count;
}
[Condition("haspower", "nopower", "Finds grids with, or without power.")]
public bool HasPower(MyCubeGrid grid)
{
foreach (var b in grid.GetFatBlocks())
{
var c = b.Components?.Get<MyResourceSourceComponent>();
if (c == null)
continue;
if (c.HasCapacityRemainingByType(MyResourceDistributorComponent.ElectricityId) && c.ProductionEnabledByType(MyResourceDistributorComponent.ElectricityId))
return true;
}
return false;
}
[Condition("insideplanet", helpText: "Finds grids that are trapped inside planets.")]
public bool InsidePlanet(MyCubeGrid grid)
{
var s = grid.PositionComp.WorldVolume;
var voxels = new List<MyVoxelBase>();
MyGamePruningStructure.GetAllVoxelMapsInSphere(ref s, voxels);
if (!voxels.Any())
return false;
foreach (var v in voxels)
{
var planet = v as MyPlanet;
if (planet == null)
continue;
var dist2center = Vector3D.DistanceSquared(s.Center, planet.PositionComp.WorldVolume.Center);
if (dist2center <= (planet.MaximumRadius * planet.MaximumRadius) / 2)
return true;
}
return false;
}
[Condition("playerdistancelessthan", "playerdistancegreaterthan", "Finds grids that are further than the given distance from players.")]
public bool PlayerDistanceLessThan(MyCubeGrid grid, double dist)
{
dist *= dist;
foreach (var player in MySession.Static.Players.GetOnlinePlayers())
{
if (Vector3D.DistanceSquared(player.GetPosition(), grid.PositionComp.GetPosition()) < dist)
return true;
}
return false;
}
[Condition("ownedby", helpText: "Finds grids owned by the given player. Can specify player name, IdentityId, 'nobody', or 'pirates'.")]
public bool OwnedBy(MyCubeGrid grid, string str)
{
long identityId;
if (string.Compare(str, "nobody", StringComparison.InvariantCultureIgnoreCase) == 0)
{
return grid.BigOwners.Count == 0;
}
if (string.Compare(str, "pirates", StringComparison.InvariantCultureIgnoreCase) == 0)
{
identityId = MyPirateAntennas.GetPiratesId();
}
else
{
var player = Utilities.GetPlayerByNameOrId(str);
if (player == null)
return false;
identityId = player.IdentityId;
}
return grid.BigOwners.Contains(identityId);
}
[Condition("hastype", "notype", "Finds grids containing blocks of the given type.")]
public bool BlockType(MyCubeGrid grid, string str)
{
return grid.HasBlockType(str);
}
[Condition("hassubtype", "nosubtype", "Finds grids containing blocks of the given subtype.")]
public bool BlockSubType(MyCubeGrid grid, string str)
{
return grid.HasBlockSubtype(str);
}
private class Condition
{
public string Command;
public string InvertCommand;
public string HelpText;
private MethodInfo _method;
public readonly ParameterInfo Parameter;
public Condition(MethodInfo evalMethod, ConditionAttribute attribute)
{
Command = attribute.Command;
InvertCommand = attribute.InvertCommand;
HelpText = attribute.HelpText;
_method = evalMethod;
if(_method.ReturnType!= typeof(bool))
throw new TypeLoadException("Condition does not return a bool!");
var p = _method.GetParameters();
if (p.Length < 1 || p[0].ParameterType != typeof(MyCubeGrid))
throw new TypeLoadException("Condition does not accept MyCubeGrid as first parameter");
if (p.Length > 2)
throw new TypeLoadException("Condition can only have two parameters");
if (p.Length == 1)
Parameter = null;
else
Parameter = p[1];
}
public bool? Evaluate(MyCubeGrid grid, string arg, bool invert, CleanupModule module)
{
var context = module.Context;
bool result;
if (!string.IsNullOrEmpty(arg) && Parameter == null)
{
context.Respond($"Condition does not accept an argument. Cannot continue!");
return null;
}
if (string.IsNullOrEmpty(arg) && Parameter != null && !Parameter.HasDefaultValue)
{
context.Respond($"Condition requires an argument! {Parameter.ParameterType.Name}: {Parameter.Name} Not supplied, cannot continue!");
return null;
}
if (Parameter != null && !string.IsNullOrEmpty(arg))
{
if (!arg.TryConvert(Parameter.ParameterType, out object val))
{
context.Respond($"Could not parse argument!");
return null;
}
result = (bool)_method.Invoke(module, new[] {grid, val});
}
else
{
result = (bool)_method.Invoke(module, new object[] {grid});
}
return result != invert;
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
private sealed class ConditionAttribute : Attribute
{
public string Command;
public string InvertCommand;
public string HelpText;
public ConditionAttribute(string command, string invertCommand = null, string helpText = null)
{
Command = command;
InvertCommand = invertCommand;
HelpText = helpText;
}
}
}
}