use of org.spongepowered.common.util.graph.DirectedGraph in project SpongeCommon by SpongePowered.
the class SpongeGameRegistry method preRegistryInit.
public void preRegistryInit() {
CommonModuleRegistry.getInstance().registerDefaultModules();
final DirectedGraph<Class<? extends RegistryModule>> graph = new DirectedGraph<>();
for (RegistryModule module : this.registryModules) {
this.classMap.put(module.getClass(), module);
addToGraph(module, graph);
}
// Now we need ot do the catalog ones
for (CatalogRegistryModule<?> module : this.catalogRegistryMap.values()) {
this.classMap.put(module.getClass(), module);
addToGraph(module, graph);
}
try {
this.orderedModules.addAll(TopologicalOrder.createOrderedLoad(graph));
} catch (CyclicGraphException e) {
StringBuilder msg = new StringBuilder();
msg.append("Registry module dependencies are cyclical!\n");
msg.append("Dependency loops are:\n");
for (DataNode<?>[] cycle : e.getCycles()) {
msg.append("[");
for (DataNode<?> node : cycle) {
msg.append(node.getData().toString()).append(" ");
}
msg.append("]\n");
}
SpongeImpl.getLogger().fatal(msg.toString());
throw new RuntimeException("Registry modules dependencies error.");
}
registerModulePhase();
SpongeVillagerRegistry.registerVanillaTrades();
DataRegistrar.setupSerialization();
final List<Tuple<Class<? extends CatalogType>, CatalogRegistryModule<?>>> modules = new ArrayList<>();
for (Map.Entry<Class<? extends CatalogType>, CatalogRegistryModule<?>> entry : this.catalogRegistryMap.entrySet()) {
modules.add(new Tuple<>(entry.getKey(), entry.getValue()));
}
modules.sort(Comparator.comparing(tuple -> tuple.getFirst().getSimpleName()));
if (PRINT_CATALOG_TYPES) {
// Lol... this gets spammy really fast.... Probably at some point should be put to file.
final PrettyPrinter printer = new PrettyPrinter(100).add("Printing all Catalogs and their ID's").centre().hr().addWrapped("This is a test to print out all registered catalogs during initialization for their mapping, id's, and objects themselves.");
for (Tuple<Class<? extends CatalogType>, CatalogRegistryModule<?>> module : modules) {
printer.add(" %s : %s", "CatalogType", module.getFirst().getSimpleName());
final Collection<? extends CatalogType> all = module.getSecond().getAll();
final List<CatalogType> catalogTypes = new ArrayList<>(all);
catalogTypes.sort(Comparator.comparing(CatalogType::getId));
for (CatalogType catalogType : catalogTypes) {
printer.add(" -%s", catalogType.getId());
}
printer.hr();
}
printer.trace(System.err, SpongeImpl.getLogger(), Level.DEBUG);
}
}
use of org.spongepowered.common.util.graph.DirectedGraph in project SpongeCommon by SpongePowered.
the class SpongeGameRegistry method syncModules.
private void syncModules() {
final DirectedGraph<Class<? extends RegistryModule>> graph = new DirectedGraph<>();
for (RegistryModule aModule : this.registryModules) {
if (!this.classMap.containsKey(aModule.getClass())) {
this.classMap.put(aModule.getClass(), aModule);
}
addToGraph(aModule, graph);
}
// Now we need ot do the catalog ones
for (CatalogRegistryModule<?> aModule : this.catalogRegistryMap.values()) {
if (!this.classMap.containsKey(aModule.getClass())) {
this.classMap.put(aModule.getClass(), aModule);
}
addToGraph(aModule, graph);
}
this.orderedModules.clear();
try {
this.orderedModules.addAll(TopologicalOrder.createOrderedLoad(graph));
} catch (CyclicGraphException e) {
StringBuilder msg = new StringBuilder();
msg.append("Registry module dependencies are cyclical!\n");
msg.append("Dependency loops are:\n");
for (DataNode<?>[] cycle : e.getCycles()) {
msg.append("[");
for (DataNode<?> node : cycle) {
msg.append(node.getData().toString()).append(" ");
}
msg.append("]\n");
}
SpongeImpl.getLogger().fatal(msg.toString());
throw new RuntimeException("Registry modules dependencies error.");
}
}
Aggregations