Search in sources :

Example 1 with DirectedGraph

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);
    }
}
Also used : DataNode(org.spongepowered.common.util.graph.DirectedGraph.DataNode) ValueFactory(org.spongepowered.api.data.value.ValueFactory) BlockStatistic(org.spongepowered.api.statistic.BlockStatistic) TextSerializerFactory(org.spongepowered.api.text.serializer.TextSerializerFactory) Level(org.apache.logging.log4j.Level) FurnaceRecipes(net.minecraft.item.crafting.FurnaceRecipes) CyclicGraphException(org.spongepowered.common.util.graph.CyclicGraphException) BookView(org.spongepowered.api.text.BookView) ExtentBufferFactory(org.spongepowered.api.world.extent.ExtentBufferFactory) Block(net.minecraft.block.Block) TextColor(org.spongepowered.api.text.format.TextColor) RegistryModuleAlreadyRegisteredException(org.spongepowered.api.registry.RegistryModuleAlreadyRegisteredException) SpongeSelectorFactory(org.spongepowered.common.text.selector.SpongeSelectorFactory) Map(java.util.Map) Path(java.nio.file.Path) SpongeTextSerializerFactory(org.spongepowered.common.text.serializer.SpongeTextSerializerFactory) ItemStatistic(org.spongepowered.api.statistic.ItemStatistic) EntityList(net.minecraft.entity.EntityList) DisplaySlotRegistryModule(org.spongepowered.common.registry.type.scoreboard.DisplaySlotRegistryModule) AITaskTypeModule(org.spongepowered.common.registry.type.entity.AITaskTypeModule) RegistryModuleLoader(org.spongepowered.common.registry.util.RegistryModuleLoader) Set(java.util.Set) DisplaySlot(org.spongepowered.api.scoreboard.displayslot.DisplaySlot) DirectedGraph(org.spongepowered.common.util.graph.DirectedGraph) AITaskType(org.spongepowered.api.entity.ai.task.AITaskType) Favicon(org.spongepowered.api.network.status.Favicon) Singleton(com.google.inject.Singleton) DataRegistration(org.spongepowered.api.data.DataRegistration) Agent(org.spongepowered.api.entity.living.Agent) PrettyPrinter(org.spongepowered.asm.util.PrettyPrinter) Supplier(java.util.function.Supplier) SpongeDataManager(org.spongepowered.common.data.SpongeDataManager) ArrayList(java.util.ArrayList) AdditionalCatalogRegistryModule(org.spongepowered.api.registry.AdditionalCatalogRegistryModule) Rotation(org.spongepowered.api.util.rotation.Rotation) Text(org.spongepowered.api.text.Text) SmeltingRecipeRegistry(org.spongepowered.api.item.recipe.smelting.SmeltingRecipeRegistry) TopologicalOrder(org.spongepowered.common.util.graph.TopologicalOrder) Nullable(javax.annotation.Nullable) StatList(net.minecraft.stats.StatList) CraftingRecipeRegistry(org.spongepowered.api.item.recipe.crafting.CraftingRecipeRegistry) IOException(java.io.IOException) Field(java.lang.reflect.Field) EntityStatistic(org.spongepowered.api.statistic.EntityStatistic) SpongeTranslation(org.spongepowered.common.text.translation.SpongeTranslation) AbstractAITask(org.spongepowered.api.entity.ai.task.AbstractAITask) ResettableBuilder(org.spongepowered.api.util.ResettableBuilder) TypeSerializers(ninja.leaping.configurate.objectmapping.serialize.TypeSerializers) ResourceLocation(net.minecraft.util.ResourceLocation) RegistryModule(org.spongepowered.api.registry.RegistryModule) PluginProvidedRegistryModule(org.spongepowered.api.registry.util.PluginProvidedRegistryModule) SpongePropertyRegistry(org.spongepowered.common.data.property.SpongePropertyRegistry) Item(net.minecraft.item.Item) ExtraClassCatalogRegistryModule(org.spongepowered.api.registry.ExtraClassCatalogRegistryModule) URL(java.net.URL) Inject(com.google.inject.Inject) DefaultGameRules(org.spongepowered.api.world.gamerule.DefaultGameRules) LocaleCache(org.spongepowered.common.util.LocaleCache) GameRegistry(org.spongepowered.api.GameRegistry) CatalogRegistryModule(org.spongepowered.api.registry.CatalogRegistryModule) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) RotationRegistryModule(org.spongepowered.common.registry.type.block.RotationRegistryModule) Locale(java.util.Locale) RegistrationDependency(org.spongepowered.api.registry.util.RegistrationDependency) StatisticTypes(org.spongepowered.api.statistic.StatisticTypes) BookViewDataBuilder(org.spongepowered.api.text.serializer.BookViewDataBuilder) IdentityHashMap(java.util.IdentityHashMap) BufferedImage(java.awt.image.BufferedImage) Collection(java.util.Collection) RegistrationPhase(org.spongepowered.api.registry.RegistrationPhase) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) ResourcePack(org.spongepowered.api.resourcepack.ResourcePack) Tuple(org.spongepowered.api.util.Tuple) SpongeFavicon(org.spongepowered.common.network.status.SpongeFavicon) SetSerializer(org.spongepowered.common.util.SetSerializer) SpongeGameRegistryRegisterEvent(org.spongepowered.common.event.registry.SpongeGameRegistryRegisterEvent) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) BlockType(org.spongepowered.api.block.BlockType) Optional(java.util.Optional) EventContext(org.spongepowered.api.event.cause.EventContext) SpongeImpl(org.spongepowered.common.SpongeImpl) CatalogTypeTypeSerializer(org.spongepowered.common.config.CatalogTypeTypeSerializer) TextTemplate(org.spongepowered.api.text.TextTemplate) SpongeCraftingRecipeRegistry(org.spongepowered.common.item.recipe.crafting.SpongeCraftingRecipeRegistry) TypeToken(com.google.common.reflect.TypeToken) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) TextConfigSerializer(org.spongepowered.api.text.serializer.TextConfigSerializer) VillagerRegistry(org.spongepowered.api.item.merchant.VillagerRegistry) DataRegistrar(org.spongepowered.common.data.DataRegistrar) Translation(org.spongepowered.api.text.translation.Translation) TextTemplateConfigSerializer(org.spongepowered.api.text.serializer.TextTemplateConfigSerializer) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SpongeExtentBufferFactory(org.spongepowered.common.world.extent.SpongeExtentBufferFactory) StatisticType(org.spongepowered.api.statistic.StatisticType) SpongeValueFactory(org.spongepowered.common.data.value.SpongeValueFactory) EntityType(org.spongepowered.api.entity.EntityType) Comparator(java.util.Comparator) ItemType(org.spongepowered.api.item.ItemType) Collections(java.util.Collections) InputStream(java.io.InputStream) DisplaySlotRegistryModule(org.spongepowered.common.registry.type.scoreboard.DisplaySlotRegistryModule) AdditionalCatalogRegistryModule(org.spongepowered.api.registry.AdditionalCatalogRegistryModule) RegistryModule(org.spongepowered.api.registry.RegistryModule) PluginProvidedRegistryModule(org.spongepowered.api.registry.util.PluginProvidedRegistryModule) ExtraClassCatalogRegistryModule(org.spongepowered.api.registry.ExtraClassCatalogRegistryModule) CatalogRegistryModule(org.spongepowered.api.registry.CatalogRegistryModule) RotationRegistryModule(org.spongepowered.common.registry.type.block.RotationRegistryModule) ArrayList(java.util.ArrayList) CyclicGraphException(org.spongepowered.common.util.graph.CyclicGraphException) CatalogType(org.spongepowered.api.CatalogType) PrettyPrinter(org.spongepowered.asm.util.PrettyPrinter) AdditionalCatalogRegistryModule(org.spongepowered.api.registry.AdditionalCatalogRegistryModule) ExtraClassCatalogRegistryModule(org.spongepowered.api.registry.ExtraClassCatalogRegistryModule) CatalogRegistryModule(org.spongepowered.api.registry.CatalogRegistryModule) DirectedGraph(org.spongepowered.common.util.graph.DirectedGraph) DataNode(org.spongepowered.common.util.graph.DirectedGraph.DataNode) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) Tuple(org.spongepowered.api.util.Tuple)

Example 2 with DirectedGraph

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.");
    }
}
Also used : DisplaySlotRegistryModule(org.spongepowered.common.registry.type.scoreboard.DisplaySlotRegistryModule) AdditionalCatalogRegistryModule(org.spongepowered.api.registry.AdditionalCatalogRegistryModule) RegistryModule(org.spongepowered.api.registry.RegistryModule) PluginProvidedRegistryModule(org.spongepowered.api.registry.util.PluginProvidedRegistryModule) ExtraClassCatalogRegistryModule(org.spongepowered.api.registry.ExtraClassCatalogRegistryModule) CatalogRegistryModule(org.spongepowered.api.registry.CatalogRegistryModule) RotationRegistryModule(org.spongepowered.common.registry.type.block.RotationRegistryModule) DirectedGraph(org.spongepowered.common.util.graph.DirectedGraph) DataNode(org.spongepowered.common.util.graph.DirectedGraph.DataNode) CyclicGraphException(org.spongepowered.common.util.graph.CyclicGraphException)

Aggregations

Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableList (com.google.common.collect.ImmutableList)1 TypeToken (com.google.common.reflect.TypeToken)1 Inject (com.google.inject.Inject)1 Singleton (com.google.inject.Singleton)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 IdentityHashMap (java.util.IdentityHashMap)1 List (java.util.List)1 Locale (java.util.Locale)1