Beispiel #1
0
  @Override
  public void load() {
    super.load();

    TopologicalSort.DirectedGraph<Mod> modGraph = new TopologicalSort.DirectedGraph<>();

    mods.keySet().forEach(modGraph::addNode);

    // Create directed graph edges.
    mods.keySet()
        .forEach(
            mod -> {
              Map<String, String> depMap = dependencyToMap(mod.dependencies());
              depMap.forEach(
                  (id, version) -> {
                    Optional<Mod> dependent =
                        mods.keySet().stream().filter(m2 -> m2.id().equals(id)).findFirst();

                    // TODO: Compare version requirements.
                    if (dependent.isPresent()) {
                      modGraph.addEdge(dependent.get(), mod);
                    }
                  });

              // Priority check
              mods.keySet()
                  .forEach(
                      compareMod -> {
                        if (mod.priority() < compareMod.priority()) {
                          modGraph.addEdge(compareMod, mod);
                        }
                      });
            });

    orderedMods.clear();

    TopologicalSort.topologicalSort(modGraph)
        .stream()
        .map(mods::get)
        .filter(mod -> mod instanceof Loadable)
        .map(mod -> (Loadable) mod)
        .forEachOrdered(orderedMods::add);

    Game.logger().info("NOVA mods loaded: " + mods.size());
  }