Skip to content

Commit

Permalink
Add dependency order for unloading in reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
Tharylia committed Oct 13, 2023
1 parent 5ef961d commit e23bb0e
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions Blish HUD/GameServices/ModuleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,16 +320,9 @@ protected override void Load() {
}

private void Gw2Instance_Gw2Started(object sender, EventArgs e) {
var resolvedDependencyOrder = SortByDependency(_modules, module => {
var dependencyModules = _modules.Where(m => {
// Get all modules which have a dependency on the current module
return module.Manifest?.Dependencies?.Any(dm => !dm.IsBlishHud && dm.Namespace == m.Manifest.Namespace) ?? false;
});
return dependencyModules;
}).ToList();
var resolvedDependencyOrder = SortByDependencies(_modules).ToList();

Logger.Debug($"Resolved dependency order: {string.Join(", ", resolvedDependencyOrder.Select(x => x.Manifest.Namespace).ToArray())}");
Logger.Debug($"Resolved load dependency order: {string.Join(", ", resolvedDependencyOrder.Select(x => x.Manifest.Namespace).ToArray())}");

foreach (var module in resolvedDependencyOrder) {
if (module.State.Enabled) {
Expand All @@ -338,12 +331,19 @@ private void Gw2Instance_Gw2Started(object sender, EventArgs e) {
}
}

private static IEnumerable<ModuleManager> SortByDependency(IEnumerable<ModuleManager> source, Func<ModuleManager, IEnumerable<ModuleManager>> dependencies) {
private static IEnumerable<ModuleManager> SortByDependencies(IEnumerable<ModuleManager> modules) {
var sorted = new List<ModuleManager>();
var visited = new HashSet<ModuleManager>();

foreach (var item in source) {
VisitDependency(item, visited, sorted, dependencies);
foreach (var module in modules) {
VisitDependency(module, visited, sorted, m => {
var dependencyModules = modules.Where(m => {
// Get all modules which have a dependency on the current module
return m.Manifest?.Dependencies?.Any(md => !md.IsBlishHud && md.Namespace == m.Manifest.Namespace) ?? false;
});
return dependencyModules;
});
}

return sorted;
Expand Down Expand Up @@ -432,7 +432,12 @@ protected override void Update(GameTime gameTime) {
protected override void Unload() {
GameService.GameIntegration.Gw2Instance.Gw2Started -= Gw2Instance_Gw2Started;

foreach (var module in _modules) {
// We need to unload modules in reverse order as modules could need to execute context methods on unload.
var resolvedDependencyOrder = SortByDependencies(_modules).Reverse().ToList();

Logger.Debug($"Resolved unload dependency order: {string.Join(", ", resolvedDependencyOrder.Select(x => x.Manifest.Namespace).ToArray())}");

foreach (var module in resolvedDependencyOrder) {
if (module.Enabled) {
try {
Logger.Info("Unloading module {module}.", module.Manifest.GetDetailedName());
Expand Down

0 comments on commit e23bb0e

Please sign in to comment.