Skip to content

Commit 49e28e5

Browse files
committed
feat(API): PluginManager
1 parent e701660 commit 49e28e5

File tree

6 files changed

+202
-10
lines changed

6 files changed

+202
-10
lines changed

managed/src/SwiftlyS2.Core/Modules/Plugins/PluginStatus.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using SwiftlyS2.Shared;
2+
using SwiftlyS2.Shared.Plugins;
3+
4+
namespace SwiftlyS2.Core.Plugins;
5+
6+
internal class ScriptingPluginManager : IPluginManager
7+
{
8+
private readonly PluginManager manager;
9+
10+
public ScriptingPluginManager( PluginManager manager )
11+
{
12+
this.manager = manager;
13+
}
14+
15+
public Dictionary<string, PluginMetadata> GetAllPluginMetadata()
16+
{
17+
var plugins = manager.GetPlugins();
18+
var result = new Dictionary<string, PluginMetadata>();
19+
foreach (var plugin in plugins)
20+
{
21+
if (plugin.Metadata != null && plugin.Metadata.Id != null)
22+
{
23+
result[plugin.Metadata.Id] = plugin.Metadata;
24+
}
25+
}
26+
return result;
27+
}
28+
29+
public IEnumerable<string> GetAllPlugins()
30+
{
31+
var plugins = manager.GetPlugins();
32+
foreach (var plugin in plugins)
33+
{
34+
if (plugin.Metadata != null && plugin.Metadata.Id != null)
35+
{
36+
yield return plugin.Metadata.Id;
37+
}
38+
}
39+
}
40+
41+
public Dictionary<string, PluginStatus> GetAllPluginStatuses()
42+
{
43+
var plugins = manager.GetPlugins();
44+
var result = new Dictionary<string, PluginStatus>();
45+
foreach (var plugin in plugins)
46+
{
47+
if (plugin.Metadata != null && plugin.Metadata.Id != null && plugin.Status != null)
48+
{
49+
result[plugin.Metadata.Id] = plugin.Status.Value;
50+
}
51+
}
52+
return result;
53+
}
54+
55+
public PluginMetadata? GetPluginMetadata( string pluginId )
56+
{
57+
var plugins = manager.GetPlugins();
58+
foreach (var plugin in plugins)
59+
{
60+
if (plugin.Metadata != null && plugin.Metadata.Id == pluginId)
61+
{
62+
return plugin.Metadata;
63+
}
64+
}
65+
return null;
66+
}
67+
68+
public string? GetPluginPath( string pluginId )
69+
{
70+
var plugins = manager.GetPlugins();
71+
foreach (var plugin in plugins)
72+
{
73+
if (plugin.Metadata != null && plugin.Metadata.Id == pluginId)
74+
{
75+
return plugin.PluginDirectory;
76+
}
77+
}
78+
return null;
79+
}
80+
81+
public Dictionary<string, string> GetPluginPaths()
82+
{
83+
var plugins = manager.GetPlugins();
84+
var result = new Dictionary<string, string>();
85+
foreach (var plugin in plugins)
86+
{
87+
if (plugin.Metadata != null && plugin.Metadata.Id != null && plugin.PluginDirectory != null)
88+
{
89+
result[plugin.Metadata.Id] = plugin.PluginDirectory;
90+
}
91+
}
92+
return result;
93+
}
94+
95+
public PluginStatus? GetPluginStatus( string pluginId )
96+
{
97+
var plugins = manager.GetPlugins();
98+
foreach (var plugin in plugins)
99+
{
100+
if (plugin.Metadata != null && plugin.Metadata.Id == pluginId)
101+
{
102+
return plugin.Status;
103+
}
104+
}
105+
return null;
106+
}
107+
108+
public bool LoadPlugin( string pluginId, bool silent )
109+
{
110+
return manager.LoadPluginById(pluginId, silent);
111+
}
112+
113+
public bool ReloadPlugin( string pluginId, bool silent )
114+
{
115+
return manager.ReloadPluginById(pluginId, silent);
116+
}
117+
118+
public bool UnloadPlugin( string pluginId, bool silent )
119+
{
120+
return manager.UnloadPluginById(pluginId, silent);
121+
}
122+
}

managed/src/SwiftlyS2.Core/Modules/Plugins/SwiftlyCore.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
using SwiftlyS2.Core.Natives;
4242
using SwiftlyS2.Core.FileSystem;
4343
using SwiftlyS2.Shared.FileSystem;
44+
using SwiftlyS2.Core.Plugins;
45+
using SwiftlyS2.Shared.Plugins;
4446

4547
namespace SwiftlyS2.Core.Services;
4648

@@ -79,6 +81,7 @@ internal class SwiftlyCore : ISwiftlyCore, IDisposable
7981
public string ContextBasePath { get; init; }
8082
public string PluginDataDirectory { get; init; }
8183
public GameFileSystem GameFileSystem { get; init; }
84+
public ScriptingPluginManager PluginManager { get; init; }
8285
public SwiftlyCore( string contextId, string contextBaseDirectory, PluginMetadata? pluginManifest, Type contextType, IServiceProvider coreProvider, string pluginDataDirectory )
8386
{
8487

@@ -91,6 +94,7 @@ public SwiftlyCore( string contextId, string contextBaseDirectory, PluginMetadat
9194
.AddSingleton(this)
9295
.AddSingleton<ISwiftlyCore>(this)
9396
.AddSingleton(coreProvider.GetRequiredService<ProfileService>())
97+
.AddSingleton(coreProvider.GetRequiredService<PluginManager>())
9498
.AddSingleton(coreProvider.GetRequiredService<ConfigurationService>())
9599
.AddSingleton(coreProvider.GetRequiredService<HookManager>())
96100
.AddSingleton(coreProvider.GetRequiredService<PlayerManagerService>())
@@ -120,6 +124,7 @@ public SwiftlyCore( string contextId, string contextBaseDirectory, PluginMetadat
120124
.AddSingleton<RegistratorService>()
121125
// .AddSingleton<MenuManager>()
122126
.AddSingleton<CommandLineService>()
127+
.AddSingleton<ScriptingPluginManager>()
123128
.AddSingleton<HelpersService>()
124129
.AddSingleton<GameService>()
125130
.AddSingleton<IPermissionManager>(provider => provider.GetRequiredService<PermissionManager>())
@@ -137,6 +142,7 @@ public SwiftlyCore( string contextId, string contextBaseDirectory, PluginMetadat
137142
.AddSingleton<IContextedProfilerService>(provider => provider.GetRequiredService<ContextedProfilerService>())
138143
.AddSingleton<ISchedulerService>(provider => provider.GetRequiredService<SchedulerService>())
139144
.AddSingleton<IEngineService>(provider => provider.GetRequiredService<EngineService>())
145+
.AddSingleton<IPluginManager>(provider => provider.GetRequiredService<ScriptingPluginManager>())
140146
.AddSingleton<ITraceManager>(provider => provider.GetRequiredService<TraceManager>())
141147
.AddSingleton<IDatabaseService>(provider => provider.GetRequiredService<DatabaseService>())
142148
.AddSingleton<ITranslationService>(provider => provider.GetRequiredService<TranslationService>())
@@ -180,6 +186,7 @@ public SwiftlyCore( string contextId, string contextBaseDirectory, PluginMetadat
180186
GameService = serviceProvider.GetRequiredService<GameService>();
181187
Logger = LoggerFactory.CreateLogger(contextType);
182188
GameFileSystem = serviceProvider.GetRequiredService<GameFileSystem>();
189+
PluginManager = serviceProvider.GetRequiredService<ScriptingPluginManager>();
183190
}
184191

185192
public void InitializeType( Type type )
@@ -226,6 +233,7 @@ public void Dispose()
226233
IHelpers ISwiftlyCore.Helpers => Helpers;
227234
IGameService ISwiftlyCore.Game => GameService;
228235
IGameFileSystem ISwiftlyCore.GameFileSystem => GameFileSystem;
236+
IPluginManager ISwiftlyCore.PluginManager => PluginManager;
229237
string ISwiftlyCore.PluginPath => ContextBasePath;
230238
string ISwiftlyCore.PluginDataDirectory => PluginDataDirectory;
231239
string ISwiftlyCore.CSGODirectory => NativeEngineHelpers.GetCSGODirectoryPath();

managed/src/SwiftlyS2.Core/Services/CoreCommandService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using SwiftlyS2.Core.Plugins;
88
using SwiftlyS2.Core.Natives;
99
using SwiftlyS2.Shared.Commands;
10+
using SwiftlyS2.Shared.Plugins;
1011

1112
namespace SwiftlyS2.Core.Services;
1213

managed/src/SwiftlyS2.Shared/ISwiftlyCore.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using SwiftlyS2.Shared.NetMessages;
1515
using SwiftlyS2.Shared.Permissions;
1616
using SwiftlyS2.Shared.Players;
17+
using SwiftlyS2.Shared.Plugins;
1718
using SwiftlyS2.Shared.Profiler;
1819
using SwiftlyS2.Shared.Scheduler;
1920
using SwiftlyS2.Shared.Services;
@@ -166,6 +167,10 @@ public interface ISwiftlyCore
166167
/// Game file system interface.
167168
/// </summary>
168169
public IGameFileSystem GameFileSystem { get; }
170+
/// <summary>
171+
/// Plugin manager.
172+
/// </summary>
173+
public IPluginManager PluginManager { get; }
169174

170175
/// <summary>
171176
/// Gets the file path to the plugin directory.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
namespace SwiftlyS2.Shared.Plugins;
2+
3+
public enum PluginStatus
4+
{
5+
Loaded,
6+
Unloaded,
7+
Loading,
8+
Error,
9+
Indeterminate
10+
}
11+
12+
public interface IPluginManager
13+
{
14+
/// <summary>
15+
/// Loads the specified plugin.
16+
/// </summary>
17+
/// <param name="pluginId">The ID of the plugin.</param>
18+
/// <param name="silent">If true, suppresses any error messages.</param>
19+
/// <returns>True if the plugin was loaded successfully, false otherwise.</returns>
20+
public bool LoadPlugin( string pluginId, bool silent = false );
21+
/// <summary>
22+
/// Unloads the specified plugin.
23+
/// </summary>
24+
/// <param name="pluginId">The ID of the plugin.</param>
25+
/// <param name="silent">If true, suppresses any error messages.</param>
26+
/// <returns>True if the plugin was unloaded successfully, false otherwise.</returns>
27+
public bool UnloadPlugin( string pluginId, bool silent = false );
28+
/// <summary>
29+
/// Reloads the specified plugin.
30+
/// </summary>
31+
/// <param name="pluginId">The ID of the plugin.</param>
32+
/// <param name="silent">If true, suppresses any error messages.</param>
33+
/// <returns>True if the plugin was reloaded successfully, false otherwise.</returns>
34+
public bool ReloadPlugin( string pluginId, bool silent = false );
35+
/// <summary>
36+
/// Gets the status of the specified plugin.
37+
/// </summary>
38+
/// <param name="pluginId">The ID of the plugin.</param>
39+
public PluginStatus? GetPluginStatus( string pluginId );
40+
/// <summary>
41+
/// Gets the metadata of the specified plugin.
42+
/// </summary>
43+
/// <param name="pluginId">The ID of the plugin.</param>
44+
public PluginMetadata? GetPluginMetadata( string pluginId );
45+
/// <summary>
46+
/// Gets the path of the specified plugin.
47+
/// </summary>
48+
/// <param name="pluginId">The ID of the plugin.</param>
49+
public string? GetPluginPath( string pluginId );
50+
/// <summary>
51+
/// Gets a dictionary of all plugin paths, keyed by plugin ID.
52+
/// </summary>
53+
public Dictionary<string, string> GetPluginPaths();
54+
/// <summary>
55+
/// Gets a dictionary of all plugin statuses, keyed by plugin ID.
56+
/// </summary>
57+
public Dictionary<string, PluginStatus> GetAllPluginStatuses();
58+
/// <summary>
59+
/// Gets a dictionary of all plugin metadata, keyed by plugin ID.
60+
/// </summary>
61+
public Dictionary<string, PluginMetadata> GetAllPluginMetadata();
62+
/// <summary>
63+
/// Gets a list of all plugin IDs.
64+
/// </summary>
65+
public IEnumerable<string> GetAllPlugins();
66+
}

0 commit comments

Comments
 (0)