Skip to content

Commit 730722a

Browse files
committed
Setup two-step plugin loading sequence (Closes #1244) (#1251)
1 parent 8b90f13 commit 730722a

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

Daybreak.Shared/Services/Plugins/IPluginsService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ public interface IPluginsService
88

99
IEnumerable<AvailablePlugin> GetCurrentlyLoadedPlugins();
1010

11-
void LoadPlugins();
11+
void UpdateLoadedPlugins(IReadOnlyList<AvailablePlugin> loadedPlugins);
12+
13+
IReadOnlyList<AvailablePlugin> LoadPlugins();
1214

1315
IEnumerable<AvailablePlugin> GetAvailablePlugins();
1416

Daybreak/Launch/Launcher.Bootstrap.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ private static ServiceProvider SetupBootstrap()
2828

2929
private static void BootstrapPluginsService(IServiceCollection services)
3030
{
31+
services.AddDaybreakOptions<PluginsServiceOptions>();
3132
services.AddSingleton<IPluginsService, PluginsService>();
3233
}
3334

Daybreak/Launch/Launcher.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ private static void LaunchSequence(string[] args, IServiceProvider bootstrap)
3535
scopedLogger.LogDebug("Starting Daybreak Launcher...");
3636
var builder = CreateMainBuilder(args);
3737
var pluginsService = bootstrap.GetRequiredService<IPluginsService>();
38-
pluginsService.LoadPlugins();
39-
40-
var loadedPlugins = pluginsService.GetCurrentlyLoadedPlugins();
38+
var loadedPlugins = pluginsService.LoadPlugins();
4139
var configurations = loadedPlugins.Select<AvailablePlugin, (string, PluginConfigurationBase?, AvailablePlugin?)>(p => (p.Name, p.Configuration, (AvailablePlugin?)p))
4240
.Prepend(("Daybreak Core", new ProjectConfiguration(), default)).ToList();
4341

@@ -54,8 +52,6 @@ private static void LaunchSequence(string[] args, IServiceProvider bootstrap)
5452
}
5553

5654
builder.Services.AddSingleton<IReadOnlyDictionary<string, MenuCategory>>(menuEntryProducer.categories.AsReadOnly());
57-
builder.Services.AddSingleton(pluginsService);
58-
5955
var mainApp = CreateMainApp(builder);
6056
foreach(var (pluginName, configuration, plugin) in configurations)
6157
{
@@ -68,6 +64,10 @@ private static void LaunchSequence(string[] args, IServiceProvider bootstrap)
6864
RegisterThemes(bootstrap, configuration, scopedLogger);
6965
}
7066

67+
scopedLogger.LogInformation("Registering loaded plugins");
68+
var finalPluginService = mainApp.Services.GetRequiredService<IPluginsService>();
69+
finalPluginService.UpdateLoadedPlugins(loadedPlugins);
70+
7171
var theme = mainApp.Services.GetRequiredService<GameScreenshotsTheme>();
7272
Theme.Themes.Add(theme);
7373
var keyboardHook = mainApp.Services.GetRequiredService<IKeyboardHookService>();

Daybreak/Services/Plugins/PluginsService.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ public void SaveEnabledPlugins(IEnumerable<AvailablePlugin> availablePlugins)
7878
this.optionsProvider.SaveOption(options);
7979
}
8080

81-
public void LoadPlugins()
81+
public void UpdateLoadedPlugins(IReadOnlyList<AvailablePlugin> loadedPlugins)
82+
{
83+
this.loadedPlugins.ClearAnd().AddRange(loadedPlugins);
84+
}
85+
86+
public IReadOnlyList<AvailablePlugin> LoadPlugins()
8287
{
8388
this.pluginsSemaphore.Wait();
8489
var scopedLogger = this.logger.CreateScopedLogger();
@@ -105,9 +110,11 @@ public void LoadPlugins()
105110
catch (Exception e)
106111
{
107112
scopedLogger.LogError(e, "Caught exception while loading plugins");
113+
this.pluginsSemaphore.Release();
108114
throw;
109115
}
110116

117+
var loadedPlugins = new List<AvailablePlugin>();
111118
foreach (var result in results)
112119
{
113120
var pluginScopedLogger = this.logger.CreateScopedLogger(flowIdentifier: result.PluginEntry?.Name ?? string.Empty);
@@ -119,35 +126,33 @@ public void LoadPlugins()
119126
if (assembly is null)
120127
{
121128
// Exclude the plugin from the enabled list if it failed to load
122-
this.DisablePlugin(result);
123129
continue;
124130
}
125131

126132
var entryPoint = assembly.GetTypes().FirstOrDefault(t => t.IsSubclassOf(typeof(PluginConfigurationBase)));
127133
if (entryPoint is null)
128134
{
129135
pluginScopedLogger.LogError($"Assembly loaded but unable to find entry point. The plugin will not start");
130-
this.DisablePlugin(result);
131136
continue;
132137
}
133138

134139
var pluginConfig = Activator.CreateInstance(entryPoint)?.As<PluginConfigurationBase>();
135140
if (pluginConfig is null)
136141
{
137142
pluginScopedLogger.LogError($"Assembly loaded but unable to create entry point. The plugin will not start");
138-
this.DisablePlugin(result);
139143
continue;
140144
}
141145

142-
this.loadedPlugins.Add(new AvailablePlugin { Name = result.PluginEntry?.Name ?? string.Empty, Path = result.PluginEntry?.Path ?? string.Empty, Enabled = true, Configuration = pluginConfig });
146+
loadedPlugins.Add(new AvailablePlugin { Name = result.PluginEntry?.Name ?? string.Empty, Path = result.PluginEntry?.Path ?? string.Empty, Enabled = true, Configuration = pluginConfig });
143147
}
144-
catch(Exception e)
148+
catch (Exception e)
145149
{
146150
pluginScopedLogger.LogError(e, $"Encountered exception while loading plugin");
147151
}
148152
}
149153

150154
this.pluginsSemaphore.Release();
155+
return loadedPlugins;
151156
}
152157

153158
public async Task<bool> AddPlugin(string pathToPlugin)

0 commit comments

Comments
 (0)