Repository navigation
Expand file tree
/
Copy pathIModuleManager.cs
More file actions
216 lines (188 loc) · 9.65 KB
/
Copy pathIModuleManager.cs
File metadata and controls
216 lines (188 loc) · 9.65 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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
namespace SS.Core.ComponentInterfaces
{
/// <summary>
/// Interface for a service that manages server modules.
/// </summary>
public interface IModuleManager : IComponentInterface
{
#region Arena Attach/Detach
/// <summary>
/// Attaches a module to an arena.
/// </summary>
/// <param name="moduleTypeName">
/// The <see cref="Type"/> name of module.
/// It can be the <see cref="Type.FullName"/> or for built-in modules, the <see cref="Type.AssemblyQualifiedName"/>.
/// </param>
/// <param name="arena">The arena to attach the module to.</param>
/// <returns>True on success. False on failure.</returns>
Task<bool> AttachModuleAsync(string moduleTypeName, Arena arena);
/// <summary>
/// Attaches a module to an arena.
/// </summary>
/// <param name="type">The type of the module to attach.</param>
/// <param name="arena">The arena to attach the module to.</param>
/// <returns>True on success. False on failure.</returns>
Task<bool> AttachModuleAsync(Type type, Arena arena);
/// <summary>
/// Detaches a module from an arena.
/// </summary>
/// <param name="moduleTypeName">
/// The <see cref="Type"/> name of module.
/// It can be the <see cref="Type.FullName"/> or for built-in modules, the <see cref="Type.AssemblyQualifiedName"/>.
/// </param>
/// <param name="arena">The arena to detach the module from.</param>
/// <returns>True on success. False on failure.</returns>
Task<bool> DetachModuleAsync(string moduleTypeName, Arena arena);
/// <summary>
/// Detaches a module from an arena.
/// </summary>
/// <param name="type">The type of the module to detach.</param>
/// <param name="arena">The arena to detach the module from.</param>
/// <returns>True on success. False on failure.</returns>
Task<bool> DetachModuleAsync(Type type, Arena arena);
/// <summary>
/// Detaches all modules that are attached to an arena.
/// </summary>
/// <param name="arena">The arena to detach modules from.</param>
/// <returns>True on success. False on failure.</returns>
Task<bool> DetachAllFromArenaAsync(Arena arena);
#endregion
#region Load
/// <summary>
/// Locates a module type by name, creates an instance of it, and loads it.
/// </summary>
/// <remarks>
/// This method does NOT support loading modules from plug-in assemblies.
/// For that, use <see cref="LoadModuleAsync(string, string?)"/> with a path instead.
/// </remarks>
/// <param name="moduleTypeName">
/// The <see cref="Type"/> name of module.
/// It can be the <see cref="Type.FullName"/> or the <see cref="Type.AssemblyQualifiedName"/>.
/// </param>
/// <returns><see langword="true"/> if the module was loaded; otherwise, <see langword="false"/>.</returns>
Task<bool> LoadModuleAsync(string moduleTypeName);
/// <summary>
/// Locates a module type by name and path, creates an instance of it, and loads it.
/// </summary>
/// <remarks>
/// This method supports loading modules from plug-in assemblies.
/// </remarks>
/// <param name="moduleTypeName">
/// The <see cref="Type"/> name of module.
/// It can be the <see cref="Type.FullName"/>, or for built-in modules the <see cref="Type.AssemblyQualifiedName"/>.
/// </param>
/// <param name="path">The path of the assembly that contains the module. <see langword="null"/> for built-in modules.</param>
/// <returns><see langword="true"/> if the module was loaded; otherwise, <see langword="false"/>.</returns>
Task<bool> LoadModuleAsync(string moduleTypeName, string? path);
/// <summary>
/// Creates an instance of a module and loads it.
/// </summary>
/// <typeparam name="TModule">The type of the module to instantiate and load.</typeparam>
/// <returns><see langword="true"/> if the module was loaded; otherwise, <see langword="false"/>.</returns>
Task<bool> LoadModuleAsync<TModule>() where TModule : class;
/// <summary>
/// Creates an instance of a module and loads it.
/// </summary>
/// <param name="moduleType">The type of the module to instantiate and load.</param>
/// <returns><see langword="true"/> if the module was loaded; otherwise, <see langword="false"/>.</returns>
Task<bool> LoadModuleAsync(Type moduleType);
/// <summary>
/// Loads a module that's already been constructed.
/// </summary>
/// <param name="module">The module instance to load.</param>
/// <returns><see langword="true"/> if the module was loaded; otherwise, <see langword="false"/>.</returns>
Task<bool> LoadModuleAsync(IModule module);
/// <summary>
/// Loads a module that's already been constructed.
/// </summary>
/// <param name="module">The module instance to load.</param>
/// <returns><see langword="true"/> if the module was loaded; otherwise, <see langword="false"/>.</returns>
Task<bool> LoadModuleAsync(IAsyncModule module);
/// <summary>
/// Loads a module that's already been constructed.
/// </summary>
/// <typeparam name="TModule">Type of the module to load.</typeparam>
/// <param name="module">The module instance to load.</param>
/// <returns><see langword="true"/> if the module was loaded; otherwise, <see langword="false"/>.</returns>
Task<bool> LoadModuleAsync<TModule>(TModule module) where TModule : class;
#endregion
#region Unload
/// <summary>
/// Unloads a module by name.
/// </summary>
/// <param name="moduleTypeName">
/// The <see cref="Type"/> name of module.
/// It can be the <see cref="Type.FullName"/> or for built-in modules, the <see cref="Type.AssemblyQualifiedName"/>.
/// </param>
/// <returns>
/// The # of modules unloaded.
/// This usually is only one, but can be more than one if:
/// more than one assembly declared the same type in the same namespace OR
/// an assembly is loaded more than once (e.g. from 2 file locations which may or may not differ in version).
/// </returns>
Task<int> UnloadModuleAsync(string moduleTypeName);
/// <summary>
/// Unloads a module by name.
/// </summary>
/// <param name="moduleTypeName"><inheritdoc cref="UnloadModuleAsync(string)" path="/param[@name='moduleTypeName']"/></param>
/// <param name="gcPlugin">Whether to force a garbage collection when a plug-in assembly is unloaded.</param>
/// <returns><inheritdoc cref="UnloadModuleAsync(string)" path="/returns"/></returns>
Task<int> UnloadModuleAsync(string moduleTypeName, bool gcPlugin);
/// <summary>
/// Unloads a module by type.
/// </summary>
/// <param name="type">The type of the module.</param>
/// <returns>Whether the module was unloaded.</returns>
Task<bool> UnloadModuleAsync(Type type);
/// <summary>
/// Attempts to unloads all modules.
/// </summary>
Task UnloadAllModulesAsync();
#endregion
#region Utility
/// <summary>
/// Gets the types and descriptions of the loaded modules.
/// </summary>
/// <param name="arena">The arena to filter by attached modules, or <see langword="null"/> to not filter by arena.</param>
/// <returns>An enumerable that provides the types and descriptions of the loaded modules.</returns>
IEnumerable<(Type Type, string? Description)> GetModuleTypesAndDescriptions(Arena? arena);
/// <summary>
/// Gets info about loaded modules.
/// </summary>
/// <param name="moduleTypeName">
/// The <see cref="Type"/> name of module.
/// It can be the <see cref="Type.FullName"/> or for built-in modules, the <see cref="Type.AssemblyQualifiedName"/>.
/// </param>
/// <returns>
/// A collection containing info about modules that matched the criteria.
/// Note: There can be multiple modules with the same name.
/// <list type="bullet">
/// <item>There can be multiple copies of the same assembly loaded. In fact, they can be different versions.</item>
/// <item>Two modules could have used the same namespace and type name. (unlikely)</item>
/// </list>
/// </returns>
IEnumerable<ModuleInfo> GetModuleInfo(string moduleTypeName);
/// <summary>
/// Gets info about a loaded module.
/// </summary>
/// <param name="type">The type of module to get info about.</param>
/// <param name="moduleInfo">Info about the module.</param>
/// <returns><see langword="true"/> if the module was found; otherwise, <see langword="false"/>.</returns>
bool TryGetModuleInfo(Type type, [MaybeNullWhen(false)] out ModuleInfo moduleInfo);
#endregion
}
/// <summary>
/// Information about a module.
/// </summary>
public readonly record struct ModuleInfo
{
public required Type Type { get; init; }
public required bool IsPlugin { get; init; }
public required string? Description { get; init; }
public required IReadOnlySet<Arena> AttachedArenas { get; init; }
}
}