-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpawnHooksExtend.cs
358 lines (282 loc) · 12 KB
/
SpawnHooksExtend.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
using System;
using System.Collections.Generic;
using System.Linq;
using Oxide.Core;
using Oxide.Core.Plugins;
using UnityEngine;
using Newtonsoft.Json;
namespace Oxide.Plugins
{
[Info("Spawn Hooks", "NickRimmer", "1.0.0")]
[Description("Provided additional spawn hooks")]
public class SpawnHooksExtend : RustPlugin
{
private const string DefaultAddedHookName = "OnEntitySpawned";
private const string DefaultRemovedHookName = "OnEntityRemoved";
private const string DefaultGroupRemovedHookName = "OnGroupEntityRemoved";
private readonly List<EntityTracker> _trackers = new List<EntityTracker>();
private PluginConfiguration _config;
private bool _initialized;
private bool _debug;
#region init
private void Init()
{
_config = Config.ReadObject<PluginConfiguration>();
}
private void OnServerInitialized(bool serverInitialized)
{
if (_config.CheckSpawnsOnInit) NextTick(SpawnHooksReload);
else _initialized = true;
}
[HookMethod("SpawnHooksReload")]
private void SpawnHooksReload()
{
Puts("Spawns looking...");
Reset();
LoadSpawns();
_initialized = true;
}
private void Reset()
{
foreach (var tracker in _trackers.ToList())
tracker.Stop();
// not necessary, but to be sure (;
_trackers.Clear();
}
private void LoadSpawns()
{
var entities = UnityEngine.Object
.FindObjectsOfType<BaseEntity>()
.Where(IsEntitySpawned);
foreach (var entity in entities)
{
var prop = FindProp(entity);
if(prop != null) AddToTracking(entity, prop);
}
}
#endregion
private void OnEntitySpawned(BaseEntity entity)
{
if (!_initialized) return;
if (!IsEntitySpawned(entity)) return;
if(_debug) LogEntity("spawned", entity);
var prop = FindProp(entity);
if(prop != null) AddToTracking(entity, prop);
}
private void AddToTracking(BaseEntity entity, EntityProperty prop)
{
LogEntity("added", entity);
var tag = CallAddedHook(entity, prop) as string ?? prop.Name;
var tracker = new EntityTracker(
entity,
prop.UpdateIntervalSeconds,
timer,
tag,
(t) => RemoveFromTracking(prop, t));
_trackers.Add(tracker);
}
private void RemoveFromTracking(EntityProperty prop, EntityTracker tracker)
{
if (_trackers.Remove(tracker))
{
CallRemovedHook(prop, tracker);
LogEntity("removed", tracker.Entity);
}
}
#region Call added/removed hooks
private Dictionary<string, uint> GetCurrentTags()
{
return _trackers
.Select(x => x.Tag)
.GroupBy(x => x)
.ToDictionary(x => x.Key, x => (uint) x.Count());
}
private string CallAddedHook(BaseEntity entity, EntityProperty prop)
{
var hookName = string.IsNullOrEmpty(prop.CustomAddedHookName)
? DefaultAddedHookName
: prop.CustomAddedHookName;
var tags = GetCurrentTags();
return Interface.uMod.CallHook(hookName, prop.Name, entity, tags) as string;
}
private void CallRemovedHook(EntityProperty prop, EntityTracker tracker)
{
var tags = GetCurrentTags();
var theLastOne = !tags.ContainsKey(tracker.Tag) || tags[tracker.Tag] <= 0;
if (theLastOne)
{
var groupHookName = string.IsNullOrEmpty(prop.CustomGroupRemovedHookName)
? DefaultGroupRemovedHookName
: prop.CustomGroupRemovedHookName;
Interface.uMod.CallHook(groupHookName, prop.Name, tracker.Tag);
}
var hookName = string.IsNullOrEmpty(prop.CustomRemovedHookName)
? DefaultRemovedHookName
: prop.CustomRemovedHookName;
Interface.uMod.CallHook(hookName, prop.Name, tracker.Tag, tags);
}
#endregion
#region Console commands
[ConsoleCommand("spawns")]
private void CmdHelp(ConsoleSystem.Arg arg)
{
if (arg.Player()?.IsAdmin == false) return;
var commands = new[]
{
"spawns.find [name] - search spawned entity",
$"spawns.debug - toggle debug mode ({(_debug ? "Enabled" : "Disabled")})"
};
arg.ReplyWith(string.Join("\n", commands));
}
[ConsoleCommand("spawns.find")]
private void CmdFind(ConsoleSystem.Arg arg)
{
if (arg.Player()?.IsAdmin == false) return;
if (!arg.Args.Any())
{
CmdHelp(arg);
return;
}
Func<BaseEntity, string> BuildEntityString = (entity) =>
{
var info = new List<string>
{
entity.GetType().Name
};
info.Add($"Id: {entity.GetInstanceID()}");
info.Add($"Prefab: {entity.PrefabName}");
info.Add($"Position: {entity.ServerPosition}");
info.Add($"Traits: {string.Join(", ", entity.Traits)}");
info.Add($"Flags: {string.Join(", ", entity.flags)}");
info.Add($"Tag: {entity.tag}");
info.Add(string.Empty);
return string.Join("\n ", info);
};
var s = arg.Args[0].ToLower();
var entities = UnityEngine.GameObject
.FindObjectsOfType<BaseEntity>();
var result = new List<BaseEntity>();
result.AddRange(entities
.Where(x=>x.GetType().Name.ToLower().StartsWith(s)));
result.AddRange(entities
.Where(x=>x.PrefabName.ToLower().StartsWith(s)));
result.AddRange(entities
.Where(x=>x.GetType().Name.ToLower().Contains(s)));
result.AddRange(entities
.Where(x=>x.PrefabName.ToLower().Contains(s)));
result = result
.GroupBy(x => x.ServerPosition)
.Select(x => x.First())
.ToList();
arg.ReplyWith(string.Join("\n", result.Select(x => BuildEntityString(x))));
foreach (var entity in result)
LogEntity("find", entity);
}
[ConsoleCommand("spawns.debug")]
private void CmdDebug(ConsoleSystem.Arg arg)
{
if (arg.Player()?.IsAdmin == false) return;
_debug = !_debug;
arg.ReplyWith(_debug ? "Debug enabled" : "Debug disabled");
}
#endregion
#region Boring things
protected override void LoadDefaultConfig() => Config.WriteObject(new PluginConfiguration(), true);
private void LogEntity(string operation, BaseEntity entity)
{
var message = $"{operation} | {entity.GetType().Name} / {entity.PrefabName} {(IsEntitySpawned(entity) ? entity.ServerPosition.ToString() : string.Empty)}";
var messageWithTimestamp = $"{DateTime.Now.ToString("HH:mm:ss")} | {message}";
if (_config.LogToConsole) Puts(message);
if (_config.LogToBroadcast) Server.Broadcast(messageWithTimestamp);
if (_config.LogToFile) LogToFile("common", messageWithTimestamp, this);
}
private EntityProperty FindProp(BaseEntity entity)
{
Func<EntityProperty, bool> EqualByClass = (prop) =>
prop.Name.Equals(entity.GetType().Name, StringComparison.InvariantCultureIgnoreCase);
Func<EntityProperty, bool> EqualByPrefab = (prop) =>
prop.Name.EndsWith(".prefab") && prop.Name.Equals(entity.PrefabName, StringComparison.InvariantCultureIgnoreCase);
return _config
.EntityProps
.FirstOrDefault(prop => EqualByClass(prop) || EqualByPrefab(prop));
}
#endregion
public static bool IsEntitySpawned(BaseEntity entity) =>
entity.IsValid() &&
entity.gameObject.activeInHierarchy &&
!entity.IsDestroyed;
#region SpawnHooksExtend.Models
private class EntityProperty
{
[JsonProperty("Entity name")]
public string Name { get; set; }
[JsonProperty("Update interval")]
public byte UpdateIntervalSeconds { get; set; }
[JsonProperty("Custom added hook")]
public string CustomAddedHookName { get; set; }
[JsonProperty("Custom removed hook")]
public string CustomRemovedHookName { get; set; }
[JsonProperty("Custom group removed hook")]
public string CustomGroupRemovedHookName { get; set; }
}
private class EntityTracker
{
public BaseEntity Entity { get; }
public string Tag { get; }
private readonly PluginTimers _timerPlugin;
private readonly Action<EntityTracker> _onDeactivate;
private readonly byte _updateIntervalSeconds;
private Timer _innerTimer;
public EntityTracker(BaseEntity entity, byte updateIntervalSeconds, PluginTimers timerPlugin, string tag, Action<EntityTracker> onDeactivate)
{
if (entity == null) throw new ArgumentNullException(nameof(entity));
if (onDeactivate == null) throw new ArgumentNullException(nameof(onDeactivate));
if (timerPlugin == null) throw new ArgumentNullException(nameof(timerPlugin));
if (updateIntervalSeconds == 0) throw new ArgumentNullException(nameof(updateIntervalSeconds));
if (tag == null) throw new ArgumentNullException(nameof(tag));
Entity = entity;
Tag = tag;
_updateIntervalSeconds = updateIntervalSeconds;
_timerPlugin = timerPlugin;
_onDeactivate = onDeactivate;
CheckActive();
}
private void CheckActive()
{
if (!Plugins.SpawnHooksExtend.IsEntitySpawned(Entity))
{
_onDeactivate.Invoke(this);
return;
}
_innerTimer = _timerPlugin.Once(_updateIntervalSeconds, CheckActive);
}
public void Stop()
{
_onDeactivate.Invoke(this);
_innerTimer?.Destroy();
_innerTimer = null;
}
}
private class PluginConfiguration
{
[JsonProperty("Log to file")]
public bool LogToFile { get; set; }
[JsonProperty("Log to global chat")]
public bool LogToBroadcast { get; set; }
[JsonProperty("Log to console")]
public bool LogToConsole { get; set; }
[JsonProperty("Check spawns after initialized")]
public bool CheckSpawnsOnInit { get; set; } = true;
[JsonProperty("List of entities")]
public EntityProperty[] EntityProps { get; set; } = new[]
{
new EntityProperty {Name = nameof(BradleyAPC), UpdateIntervalSeconds = 15},
new EntityProperty {Name = nameof(CargoPlane), UpdateIntervalSeconds = 30},
new EntityProperty {Name = nameof(CargoShip), UpdateIntervalSeconds = 60},
new EntityProperty {Name = nameof(CH47Helicopter), UpdateIntervalSeconds = 30},
new EntityProperty {Name = nameof(BaseHelicopter), UpdateIntervalSeconds = 30},
};
}
#endregion
}
}