Skip to content

Commit

Permalink
Make HandlePlugin and InvokeX in PluginManager async
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides committed Mar 18, 2024
1 parent d0cd748 commit a611fa3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
33 changes: 6 additions & 27 deletions Obsidian/Plugins/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public async Task LoadPluginsAsync()
//Add dependency to plugin
canLoad.LoadContext.AddDependency(pluginContainer.LoadContext);

this.HandlePlugin(canLoad);
await this.HandlePluginAsync(canLoad);

waitingForDepend.Remove(canLoad);
}
Expand All @@ -137,7 +137,7 @@ public async Task LoadPluginsAsync()
{
var plugin = await packedPluginProvider.GetPluginAsync(path).ConfigureAwait(false);

return plugin is null ? null : HandlePlugin(plugin);
return plugin is null ? null : await HandlePluginAsync(plugin);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -194,7 +194,7 @@ public async Task UnloadPluginAsync(PluginContainer pluginContainer)
loadContext.Unload();
}

public void ServerReady()
public async ValueTask OnServerReadyAsync()
{
PluginServiceProvider ??= this.pluginServiceDescriptors.BuildServiceProvider(true);
foreach (var pluginContainer in this.plugins)
Expand All @@ -206,7 +206,7 @@ public void ServerReady()

pluginContainer.InjectServices(this.logger);

InvokeOnServerReady(pluginContainer);
await pluginContainer.Plugin.OnServerReadyAsync(this.server);
}

//THis only needs to be called once 😭😭
Expand All @@ -231,7 +231,7 @@ private void ConfigureInitialServices(IServerEnvironment env)
this.pluginServiceDescriptors.AddSingleton<IServerConfiguration>(x => env.Configuration);
}

private PluginContainer HandlePlugin(PluginContainer pluginContainer)
private async ValueTask<PluginContainer> HandlePluginAsync(PluginContainer pluginContainer)
{
//The plugin still hasn't fully loaded. Probably due to it having a hard dependency
if (pluginContainer.Plugin is null)
Expand All @@ -252,7 +252,7 @@ private PluginContainer HandlePlugin(PluginContainer pluginContainer)

pluginContainer.Loaded = true;

InvokeOnLoad(pluginContainer);
await pluginContainer.Plugin.OnLoadedAsync(this.server);
}
else
{
Expand Down Expand Up @@ -290,27 +290,6 @@ private async void OnPluginSourceDeleted(string path)
if (deletedPlugin != null)
await UnloadPluginAsync(deletedPlugin);
}

private void InvokeOnLoad(PluginContainer plugin)
{
var task = plugin.Plugin.OnLoadedAsync(this.server).AsTask();
if (task.Status == TaskStatus.Created)
task.RunSynchronously();

if (task.Status == TaskStatus.Faulted)
logger?.LogError(task.Exception?.InnerException, "Invoking {pluginName}.OnLoadedAsync faulted.", plugin.Info.Name);

}

private void InvokeOnServerReady(PluginContainer plugin)
{
var task = plugin.Plugin.OnServerReadyAsync(this.server).AsTask();
if (task.Status == TaskStatus.Created)
task.RunSynchronously();

if (task.Status == TaskStatus.Faulted)
logger?.LogError(task.Exception?.InnerException, "Invoking {pluginName}.OnServerReadyAsync faulted.", plugin.Info.Name);
}
}

// thank you Roxxel && DorrianD3V for the invasion <3
Expand Down
2 changes: 1 addition & 1 deletion Obsidian/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public async Task RunAsync()
while (!this.WorldManager.ReadyToJoin && !this._cancelTokenSource.IsCancellationRequested)
continue;

this.PluginManager.ServerReady();
await this.PluginManager.OnServerReadyAsync();

_logger.LogInformation("Listening for new clients...");

Expand Down

0 comments on commit a611fa3

Please sign in to comment.