-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.cs
More file actions
145 lines (126 loc) · 5.29 KB
/
Copy pathPlugin.cs
File metadata and controls
145 lines (126 loc) · 5.29 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
using System;
using System.IO;
using LabApi.Features.Console;
using LabApi.Features.Wrappers;
using LabApi.Loader;
using LabApi.Loader.Features.Plugins;
using LabApi.Loader.Features.Plugins.Enums;
using PSCFaction.Commands;
using PSCFaction.Config;
using PSCFaction.Events;
using PSCFaction.Services;
namespace PSCFaction
{
public sealed class Plugin : Plugin<PluginConfig>
{
public static Plugin Instance { get; private set; }
private EventHandlers eventHandlers;
private PscCommand command;
public PscService Psc { get; private set; }
public Translation Translation { get; private set; } = new Translation();
public override string Name => "PSCFaction";
public override string Description => "为 SCP:SL 提供 PSC 情报探员、正常增援与应急派系玩法。";
public override string Author => "PSCFaction Contributors";
public override Version Version => new Version(1, 1, 0);
public override Version RequiredApiVersion => new Version(1, 1, 7);
public override LoadPriority Priority => LoadPriority.Low;
public override bool IsTransparent => false;
public override string ConfigFileName => "config.yml";
/// <summary>
/// 只读加载:LabAPI 的 LoadConfig/TryLoadConfig 会在成功读取后回写规范化文件,
/// 而本插件承诺不改写管理员的人工排版,因此已存在的文件只走 TryReadConfig。
/// </summary>
public override void LoadConfigs()
{
Config = LoadOrCreate("config.yml", () => new PluginConfig());
Translation = LoadOrCreate("translation.yml", () => new Translation());
}
public override void Enable()
{
Instance = this;
try
{
Config ??= new PluginConfig();
Translation ??= new Translation();
foreach (ValidationIssue issue in ConfigValidator.ValidateAndRepair(Config, Translation))
Logger.Warn($"[PSCFaction] {issue}");
if (!Config.IsEnabled)
{
Logger.Info("[PSCFaction] 配置已禁用插件玩法入口。插件保持加载,但不会注册事件和命令。");
return;
}
Psc = new PscService(Config, Translation);
Psc.Enable();
eventHandlers = new EventHandlers(Psc);
eventHandlers.Register();
command = new PscCommand(Psc, Translation);
Server.RemoteAdminCommandHandler.RegisterCommand(command);
Logger.Info($"[PSCFaction] 已启用 v{Version},LabAPI 最低版本 {RequiredApiVersion}。");
}
catch (Exception exception)
{
Logger.Error($"[PSCFaction] 启用失败,正在回滚已注册的入口和资源:{exception}");
CleanupLifecycle("Enable 回滚");
Instance = null;
throw;
}
}
public override void Disable()
{
try
{
CleanupLifecycle("Disable");
Logger.Info("[PSCFaction] 已禁用并完成资源清理。");
}
finally
{
Instance = null;
}
}
/// <summary>先关闭命令与事件入口,再清理插件拥有的游戏状态;每一步都独立隔离异常。</summary>
private void CleanupLifecycle(string context)
{
if (command != null)
{
TryCleanup(() => Server.RemoteAdminCommandHandler.UnregisterCommand(command), "注销 RemoteAdmin 命令", context);
command = null;
}
if (eventHandlers != null)
{
TryCleanup(eventHandlers.Unregister, "注销事件", context);
eventHandlers = null;
}
if (Psc != null)
{
TryCleanup(Psc.CleanupAll, "清理 PSC 状态", context);
Psc = null;
}
}
private T LoadOrCreate<T>(string fileName, Func<T> createDefault) where T : class, new()
{
string path = ConfigurationLoader.GetConfigPath(this, fileName, false);
if (!File.Exists(path))
{
T created = createDefault();
if (!ConfigurationLoader.TrySaveConfig(this, created, fileName, false))
Logger.Warn($"[PSCFaction] {fileName} 不存在且默认模板生成失败,继续使用内存默认值。");
return created;
}
if (ConfigurationLoader.TryReadConfig(this, fileName, out T loaded, false) && loaded != null)
return loaded;
Logger.Warn($"[PSCFaction] {fileName} 读取失败,使用内存默认值;不会改写已有文件。");
return createDefault();
}
private static void TryCleanup(Action cleanup, string operation, string context)
{
try
{
cleanup();
}
catch (Exception exception)
{
Logger.Error($"[PSCFaction] {context} 期间{operation}失败,将继续清理其他资源:{exception}");
}
}
}
}