-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandsModule.cs
More file actions
182 lines (155 loc) · 6.66 KB
/
Copy pathCommandsModule.cs
File metadata and controls
182 lines (155 loc) · 6.66 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
using SFDGameScriptInterface;
namespace SFD.Scripting.CommandsPlusPlus;
public partial class GameScript : GameScriptInterfaceExtended
{
/// <summary>
/// Base class for a Commands++ module. Each module owns its
/// <see cref="CommandHandler.Command"/> instances in a plain list and
/// exposes them via <see cref="CommandHandler.ActiveCommands"/>.
/// <see cref="Restricted"/> persists the restricted state in
/// <see cref="IGame.LocalStorage"/> and defaults to
/// <see cref="DefaultRestricted"/>; the runtime
/// <see cref="IsRestricted"/> flag guards <see cref="OnRestricted"/> so it
/// never fires redundantly. Restricting a module limits all of its commands
/// to the host instead of withdrawing them.
/// </summary>
public abstract class CommandsModule
{
/// <summary>
/// Unique module name, compared case-insensitively. Used for
/// <c>/commands [module]</c>, <c>/toggle_module</c> and storage keys.
/// </summary>
public abstract string Name { get; }
/// <summary>Human-readable module description for <c>/modules</c>.</summary>
public abstract string Description { get; }
/// <summary>Storage key prefix for all module flags.</summary>
public const string StorageKeyPrefix = "CommandsPlusPlus.Module.";
private string StorageKey => StorageKeyPrefix + Name + ".Restricted";
/// <summary>
/// Restricted state for fresh installs with no stored value.
/// Override to restrict a module by default.
/// </summary>
public virtual bool DefaultRestricted => false;
/// <summary>
/// Whether a restricted state was ever persisted for this module.
/// </summary>
public bool HasPersistedState => Game.LocalStorage.ContainsKey(StorageKey);
/// <summary>
/// Persisted restricted state. Reads <see cref="DefaultRestricted"/>
/// when no value is stored.
/// </summary>
public bool Restricted
{
get => HasPersistedState && Game.LocalStorage.TryGetItemBool(StorageKey, out bool result) && result;
set => Game.LocalStorage.SetItem(StorageKey, value);
}
private bool _isRestricted;
/// <summary>
/// Runtime state. Setting it fires <see cref="OnRestricted"/> exactly
/// once per transition — assigning the current value is a no-op.
/// </summary>
public bool IsRestricted
{
get => _isRestricted;
private set
{
if (_isRestricted == value) return;
_isRestricted = value;
OnRestricted(value);
}
}
/// <summary>
/// Starts allowed. Restriction (<see cref="Restrict"/>) is driven by
/// <see cref="ModuleRegistry.RegisterAll"/> from the persisted
/// <see cref="Restricted"/> value, so construction never fires virtuals
/// and never ignores stored state.
/// </summary>
protected CommandsModule() => _isRestricted = false;
/// <summary>
/// Commands owned by this module. A plain list on purpose: unlike
/// <see cref="CommandHandler.CommandCollection"/> it has no
/// auto-subscribe side effects — exposure happens explicitly in
/// <see cref="Register"/> and nowhere else.
/// </summary>
public readonly List<CommandHandler.Command> Commands = [];
/// <summary>
/// Each owned command's permissions before <see cref="Restrict"/>
/// overrode them. Captured on first restriction, restored on
/// <see cref="Allow"/>.
/// </summary>
private readonly Dictionary<CommandHandler.Command, (bool hostOnly, bool moderatorOnly)> _originalPermissions = [];
/// <summary>
/// Called once per allow/restrict transition. React to the restriction
/// change here.
/// </summary>
public virtual void OnRestricted(bool restricted) { }
/// <summary>
/// Exposes owned commands to the handler. Skips commands that are
/// already exposed, so repeated calls never create duplicates.
/// </summary>
public void Register()
{
foreach (CommandHandler.Command command in Commands)
{
if (!CommandHandler.ActiveCommands.Contains(command))
CommandHandler.ActiveCommands.Add(command);
}
}
/// <summary>
/// Persists the restricted state and limits every owned command to the
/// host, remembering original permissions for <see cref="Allow"/>.
/// Fires <see cref="OnRestricted"/> once (guarded by <see cref="IsRestricted"/>).
/// </summary>
public void Restrict()
{
Restricted = true;
foreach (CommandHandler.Command command in Commands)
{
if (!_originalPermissions.ContainsKey(command))
_originalPermissions[command] = (command.HostOnly, command.ModeratorOnly);
command.HostOnly = true;
}
IsRestricted = true;
}
/// <summary>
/// Persists the allowed state and restores every owned command's
/// original permissions. Fires <see cref="OnRestricted"/> once (guarded
/// by <see cref="IsRestricted"/>).
/// </summary>
public void Allow()
{
Restricted = false;
foreach (var entry in _originalPermissions)
{
entry.Key.HostOnly = entry.Value.hostOnly;
entry.Key.ModeratorOnly = entry.Value.moderatorOnly;
}
_originalPermissions.Clear();
IsRestricted = false;
}
/// <summary>Flips the state via <see cref="Restrict"/>/<see cref="Allow"/>. Returns the new restricted state.</summary>
public bool Toggle()
{
if (IsRestricted)
Allow();
else
Restrict();
return IsRestricted;
}
/// <summary>
/// Helper to create, configure and track a command owned by this module.
/// </summary>
protected CommandHandler.Command AddCommand(string name, Action<UserMessageCallbackArgs> onCommand,
string description = null, bool hostOnly = false, bool moderatorOnly = false)
{
CommandHandler.Command command = new(name, onCommand)
{
Description = description,
HostOnly = hostOnly,
ModeratorOnly = moderatorOnly
};
Commands.Add(command);
return command;
}
}
}