-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMainPlugin.cs
More file actions
293 lines (243 loc) · 10.1 KB
/
MainPlugin.cs
File metadata and controls
293 lines (243 loc) · 10.1 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
namespace ScriptedEvents
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Exiled.API.Enums;
using Exiled.API.Features;
using MEC;
using RemoteAdmin;
using ScriptedEvents.API.Constants;
using ScriptedEvents.API.Enums;
using ScriptedEvents.API.Features;
using ScriptedEvents.API.Modules;
using ScriptedEvents.DemoScripts;
using ScriptedEvents.Structures;
public class MainPlugin : Plugin<Config, Translations>
{
/// <summary>
/// Whether or not this build of the plugin is experimental.
/// </summary>
public const bool IsExperimental = false;
/// <summary>
/// Gets a link to the Scripted Events Discord server.
/// </summary>
public const string DiscordUrl = "https://discord.gg/3j54zBnbbD";
/// <summary>
/// Gets a link to the plugin's GitHub page.
/// </summary>
public const string GitHub = "https://github.com/Thundermaker300/ScriptedEvents";
private static List<SEModule> modules = new();
/// <summary>
/// Gets or sets the plugin singleton.
/// </summary>
public static MainPlugin Singleton { get; set; }
/// <summary>
/// Gets the plugin Config singleton.
/// </summary>
public static Config Configs => Singleton?.Config;
/// <summary>
/// Gets the plugin Translations singleton.
/// </summary>
public static Translations Translations => Singleton?.Translation;
/// <summary>
/// Gets a list of demo scripts.
/// </summary>
public static IDemoScript[] DemoScripts { get; } =
{
new About(),
new DemoScript(),
new ConditionSamples(),
new DogHideAndSeek(),
new HitMarker(),
new PeanutRun(),
new JoinBroadcast(),
new ScpLeftServerInfo()
};
public static DateTime Epoch => new(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
public static List<Commands.CustomCommand> CustomCommands { get; } = new();
/// <summary>
/// The base path to SE files.
/// </summary>
public static readonly string BaseFilePath = Path.Combine(Paths.Configs, "ScriptedEvents");
/// <inheritdoc/>
public override string Name => "ScriptedEvents";
/// <inheritdoc/>
public override string Author => "Elektryk_Andrzej and Thunder";
/// <inheritdoc/>
public override Version Version => new(3, 5, 0);
/// <inheritdoc/>
public override Version RequiredExiledVersion => new(9, 10, 0);
/// <inheritdoc/>
public override PluginPriority Priority => PluginPriority.High;
public static IEnumerable<SEModule> Modules => modules.Where(mod => mod.IsActive);
public static ScriptModule ScriptModule => GetModule<ScriptModule>();
public static EventHandlingModule Handlers => GetModule<EventHandlingModule>();
public bool EnabledRanks => Configs.EnableCredits;
/// <summary>
/// Equivalent to <see cref="Logger.Info(string,Script)"/>, but checks the EnableLogs ScriptedEvents config first.
/// </summary>
/// <param name="message">The message to Logger.</param>
public static void Info(string message)
{
if (Singleton.Config.EnableLogs)
Logger.Info(message);
}
public static T GetModule<T>()
where T : SEModule => (T)Modules.FirstOrDefault(m => m.GetType() == typeof(T));
/// <inheritdoc/>
public override void OnEnabled()
{
try
{
Log.Info("Initalizing plugin...");
DoEverything();
}
catch (Exception ex)
{
Log.Error($"{ex.Message}\n{ex.StackTrace}\n{ex.Data}");
}
return;
void DoEverything()
{
Singleton = this;
foreach (Type type in Assembly.GetTypes())
{
if (type.BaseType == typeof(SEModule) && type.IsClass && type.GetConstructors().Length > 0)
{
SEModule module = (SEModule)Activator.CreateInstance(type);
if (module.ShouldGenerateFiles)
module.GenerateFiles();
module.Init();
modules.Add(module);
}
}
Timing.CallDelayed(6f, () =>
{
if (IsExperimental)
{
#if ADEBUG
Logger.Warn($"You are using a pre-release version of {Name} by Elektryk_Andrzej. Please report any issues encountered, thank you.");
#else
Logger.Warn($"You are using a pre-release version of {Name}. Please report any issues encountered, thank you.");
#endif
}
if (DateTime.Now.Month == 1 && DateTime.Now.Day == 25)
{
Logger.Info(Constants.ItsMyBirthday);
}
bool isUpdated = API.Features.ScriptHelpGenerator.Generator.CheckUpdated(out string docMessage);
if (docMessage == "SKIP") return;
if (isUpdated)
Logger.Info("[DOCUMENTATION GENERATOR]: " + docMessage);
else
Logger.Warn("[DOCUMENTATION GENERATOR]: " + docMessage);
});
// Delete help file on startup
string helpPath = Path.Combine(BaseFilePath, "HelpCommandResponse.txt");
if (File.Exists(helpPath))
{
try
{
File.Delete(helpPath);
}
catch (Exception ex)
{
Logger.Warn($"The removal of the '{helpPath}' file has failed. Reason: {ex}");
}
}
// Delete leftover doc config on startup
if (File.Exists(API.Features.ScriptHelpGenerator.Generator.ConfigPath))
{
try
{
File.Delete(API.Features.ScriptHelpGenerator.Generator.ConfigPath);
}
catch (Exception ex)
{
Logger.Warn($"The removal of the '{API.Features.ScriptHelpGenerator.Generator.ConfigPath}' file has failed. Reason: {ex}");
}
}
#if ADEBUG
ScriptedEventsIntegration.RegisterCustomActions();
#endif
base.OnEnabled();
}
}
/// <inheritdoc/>
public override void OnDisabled()
{
Handlers.OnRestarting();
base.OnDisabled();
foreach (SEModule module in Modules)
{
module.Kill();
}
Singleton = null;
#if ADEBUG
ScriptedEventsIntegration.UnregisterCustomActions();
#endif
}
public override void OnRegisteringCommands()
{
foreach (CustomCommand custom in Config.Commands)
{
if (!custom.Enabled) continue;
if (custom.Name == string.Empty)
{
Logger.Warn(ErrorGen.Get(ErrorCode.CustomCommand_NoName));
continue;
}
if (custom.Run is null || custom.Run.Count == 0)
{
Logger.Warn(ErrorGen.Get(ErrorCode.CustomCommand_NoScripts, custom.Name, custom.Type));
continue;
}
if (custom.Cooldown != -1 && custom.PlayerCooldown != -1)
{
Logger.Warn(ErrorGen.Get(ErrorCode.CustomCommand_MultCooldowns, custom.Name, custom.Type));
continue;
}
CommandCooldownMode cooldownMode = CommandCooldownMode.None;
int cooldown = -1;
if (custom.Cooldown != -1)
{
cooldownMode = CommandCooldownMode.Global;
cooldown = custom.Cooldown;
}
else if (custom.PlayerCooldown != -1)
{
cooldownMode = CommandCooldownMode.Player;
cooldown = custom.PlayerCooldown;
}
Commands.CustomCommand command = new()
{
Command = custom.Name,
Description = custom.Description,
Aliases = new string[0],
Type = custom.Type,
CooldownMode = cooldownMode,
Cooldown = cooldown,
DoResponse = custom.DefaultResponse,
Permission = custom.Permission == string.Empty ? string.Empty : "script.command." + custom.Permission,
Scripts = custom.Run.ToArray(),
};
CustomCommands.Add(command);
switch (command.Type)
{
case CommandType.PlayerConsole:
QueryProcessor.DotCommandHandler.RegisterCommand(command);
break;
case CommandType.ServerConsole:
GameCore.Console.ConsoleCommandHandler.RegisterCommand(command);
break;
case CommandType.RemoteAdmin:
CommandProcessor.RemoteAdminCommandHandler.RegisterCommand(command);
break;
}
}
base.OnRegisteringCommands();
}
}
}