mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-05 07:16:10 +00:00
fix dependency sorting
This commit is contained in:
Submodule common/addons/api-addon-loader updated: bcea245c21...d9d89e15cb
Submodule common/addons/manifest-addon-loader updated: 7ef78d5d79...46518ce4a1
@@ -13,39 +13,41 @@ import com.dfsek.terra.api.addon.BaseAddon;
|
||||
|
||||
public class DependencySorter {
|
||||
private final Map<String, BaseAddon> addons = new HashMap<>();
|
||||
private final Map<BaseAddon, Boolean> visited = new HashMap<>();
|
||||
private final Map<String, Boolean> visited = new HashMap<>();
|
||||
|
||||
private final List<BaseAddon> 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<BaseAddon> 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;
|
||||
|
||||
Reference in New Issue
Block a user