From 84537e2f99b52596c282d596316cd5d9961b4d60 Mon Sep 17 00:00:00 2001 From: dfsek Date: Thu, 18 Nov 2021 21:38:33 -0700 Subject: [PATCH] fix dependency sorting --- common/addons/api-addon-loader | 2 +- common/addons/manifest-addon-loader | 2 +- .../dfsek/terra/addon/DependencySorter.java | 39 ++++++++++--------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/common/addons/api-addon-loader b/common/addons/api-addon-loader index bcea245c2..d9d89e15c 160000 --- a/common/addons/api-addon-loader +++ b/common/addons/api-addon-loader @@ -1 +1 @@ -Subproject commit bcea245c217dead342bb927b81cc4cb6c8e27e5d +Subproject commit d9d89e15cbd5d09c8275ae1e58236606c52bc9c1 diff --git a/common/addons/manifest-addon-loader b/common/addons/manifest-addon-loader index 7ef78d5d7..46518ce4a 160000 --- a/common/addons/manifest-addon-loader +++ b/common/addons/manifest-addon-loader @@ -1 +1 @@ -Subproject commit 7ef78d5d79ea03c86d6fa47730ad3473447389a1 +Subproject commit 46518ce4a19218c8ed04f33789c3142f25ed4ab1 diff --git a/common/implementation/src/main/java/com/dfsek/terra/addon/DependencySorter.java b/common/implementation/src/main/java/com/dfsek/terra/addon/DependencySorter.java index b343e41f8..fd8646804 100644 --- a/common/implementation/src/main/java/com/dfsek/terra/addon/DependencySorter.java +++ b/common/implementation/src/main/java/com/dfsek/terra/addon/DependencySorter.java @@ -13,39 +13,41 @@ import com.dfsek.terra.api.addon.BaseAddon; public class DependencySorter { private final Map addons = new HashMap<>(); - private final Map visited = new HashMap<>(); + private final Map visited = new HashMap<>(); private final List addonList = new ArrayList<>(); public void add(BaseAddon addon) { addons.put(addon.getID(), addon); - visited.put(addon, false); + visited.put(addon.getID(), false); addonList.add(addon); } private void sortDependencies(BaseAddon addon, List sort) { addon.getDependencies().forEach((id, range) -> { if(!addons.containsKey(id)) { - throw new DependencyException("Addon " + addon.getID() + " specifies dependency on " + id + ", versions " + range + ", but no such addon is installed."); + throw new DependencyException("Addon " + addon.getID() + " specifies dependency on " + id + ", versions " + range + + ", but no such addon is installed."); } BaseAddon dependency = addons.get(id); if(!range.isSatisfiedBy(dependency.getVersion())) { - throw new DependencyVersionException("Addon " + addon.getID() + " specifies dependency on " + id + ", versions " + range + ", but non-matching version " + dependency.getVersion() + " is installed.."); + throw new DependencyVersionException("Addon " + addon.getID() + " specifies dependency on " + id + ", versions " + range + + ", but non-matching version " + dependency.getVersion() + " is installed.."); } - if(!visited.get(dependency)) { // if we've not visited it yet - visited.put(dependency, true); // we've visited it now + if(!visited.get(dependency.getID())) { // if we've not visited it yet + visited.put(dependency.getID(), true); // we've visited it now - if(!dependency.getDependencies().isEmpty()) { // if this addon has dependencies... - sortDependencies(dependency, sort); // sort them first. - } + sortDependencies(dependency, sort); if(sort.contains(dependency)) { - throw new CircularDependencyException("Addon " + addon.getID() + " has circular dependency beginning with " + dependency.getID()); + throw new CircularDependencyException( + "Addon " + addon.getID() + " has circular dependency beginning with " + dependency.getID()); } + sort.add(dependency); // add it to the list. } }); @@ -57,15 +59,16 @@ public class DependencySorter { for(int i = addonList.size() - 1; i >= 0; i--) { BaseAddon addon = addonList.get(i); addonList.remove(i); - - if(!visited.get(addon)) { // if we've not visited it yet - visited.put(addon, true); // we've visited it now - if(!addon.getDependencies().isEmpty()) { // if this addon has dependencies... - sortDependencies(addon, sorted); - } + + System.out.println(addon.getID() + ": " + visited.get(addon.getID())); + if(!visited.get(addon.getID())) { + sortDependencies(addon, sorted); + } + + if(!visited.get(addon.getID())) { + sorted.add(addon); + visited.put(addon.getID(), true); } - - sorted.add(addon); } return sorted;