Search in sources :

Example 1 with IRailcraftModule

use of mods.railcraft.api.core.IRailcraftModule in project Railcraft by Railcraft.

the class RailcraftModuleManager method processStage.

private static void processStage(Stage s) {
    setStage(s);
    Game.log(Level.TRACE, "Performing {0} on Modules.", stage.name());
    for (Class<? extends IRailcraftModule> moduleClass : loadOrder) {
        IRailcraftModule module = classToInstanceMapping.get(moduleClass);
        boolean enabled = enabledModules.contains(moduleClass);
        try {
            if (Game.DEVELOPMENT_ENVIRONMENT)
                Game.log(Level.INFO, "Module performing stage {0}: {1} {2}", stage.name(), getModuleName(module), enabled ? "+" : "-");
            stage.passToModule(module.getModuleEventHandler(enabled));
        } catch (Throwable th) {
            Game.logThrowable(Level.ERROR, 3, th, "Module failed during {0}: {1} {2}", stage.name(), getModuleName(module), enabled ? "+" : "-");
            throw th;
        }
    }
}
Also used : IRailcraftModule(mods.railcraft.api.core.IRailcraftModule)

Example 2 with IRailcraftModule

use of mods.railcraft.api.core.IRailcraftModule in project Railcraft by Railcraft.

the class RailcraftModuleManager method isConfigured.

private static boolean isConfigured(Configuration config, IRailcraftModule m) {
    RailcraftModule annotation = m.getClass().getAnnotation(RailcraftModule.class);
    String moduleName = annotation.value().toLowerCase(Locale.ENGLISH);
    // oops, remove this later
    config.renameProperty(CATEGORY_MODULES, moduleName.replaceAll("[_|]", "."), moduleName);
    Property prop = config.get(CATEGORY_MODULES, moduleName, true, annotation.description());
    return prop.getBoolean(true);
}
Also used : RailcraftModule(mods.railcraft.api.core.RailcraftModule) IRailcraftModule(mods.railcraft.api.core.IRailcraftModule) Property(net.minecraftforge.common.config.Property)

Example 3 with IRailcraftModule

use of mods.railcraft.api.core.IRailcraftModule in project Railcraft by Railcraft.

the class RailcraftModuleManager method getSoftDependencies.

private static Set<Class<? extends IRailcraftModule>> getSoftDependencies(Class<? extends IRailcraftModule> moduleClass) {
    RailcraftModule annotation = moduleClass.getAnnotation(RailcraftModule.class);
    String[] dependencies = annotation.softDependencies();
    Set<Class<? extends IRailcraftModule>> dependencyClasses = Sets.newHashSet();
    dependencyClasses.addAll(Arrays.asList(annotation.softDependencyClasses()));
    for (String dependency : dependencies) {
        dependencyClasses.add(nameToClassMapping.get(dependency));
    }
    return dependencyClasses;
}
Also used : RailcraftModule(mods.railcraft.api.core.RailcraftModule) IRailcraftModule(mods.railcraft.api.core.IRailcraftModule) IRailcraftModule(mods.railcraft.api.core.IRailcraftModule)

Example 4 with IRailcraftModule

use of mods.railcraft.api.core.IRailcraftModule in project Railcraft by Railcraft.

the class RailcraftModuleManager method getDependencies.

private static Set<Class<? extends IRailcraftModule>> getDependencies(Class<? extends IRailcraftModule> moduleClass) {
    RailcraftModule annotation = moduleClass.getAnnotation(RailcraftModule.class);
    String[] dependencies = annotation.dependencies();
    Set<Class<? extends IRailcraftModule>> dependencyClasses = Sets.newHashSet();
    dependencyClasses.addAll(Arrays.asList(annotation.dependencyClasses()));
    for (String dependency : dependencies) {
        dependencyClasses.add(nameToClassMapping.get(dependency));
    }
    return dependencyClasses;
}
Also used : RailcraftModule(mods.railcraft.api.core.RailcraftModule) IRailcraftModule(mods.railcraft.api.core.IRailcraftModule) IRailcraftModule(mods.railcraft.api.core.IRailcraftModule)

Example 5 with IRailcraftModule

use of mods.railcraft.api.core.IRailcraftModule in project Railcraft by Railcraft.

the class RailcraftModuleManager method preInit.

public static void preInit() {
    setStage(Stage.DEPENDENCY_CHECKING);
    Game.log(Level.TRACE, "Checking Module dependencies and config.");
    Locale locale = Locale.getDefault();
    Locale.setDefault(Locale.ENGLISH);
    config = new Configuration(new File(Railcraft.getMod().getConfigFolder(), MODULE_CONFIG_FILE_NAME));
    config.load();
    config.addCustomCategoryComment(CATEGORY_MODULES, "Disabling these Modules can greatly change how the mod functions.\n" + "For example, disabling the Train Module will prevent you from linking carts.\n" + "Disabling the Locomotive Module will remove the extra drag added to Trains.\n" + "Disabling the World Module will disable all world gen.\n" + "Railcraft will attempt to compensate for disabled Modules on a best effort basis.\n" + "It will define alternate recipes and crafting paths, but the system is far from flawless.\n" + "Unexpected behavior, bugs, or crashes may occur. Please report any issues so they can be fixed.\n");
    // Add enabled modules to list
    List<Class<? extends IRailcraftModule>> toEnable = Lists.newArrayList();
    TreeSet<Class<? extends IRailcraftModule>> toDisable = new TreeSet<>(Comparator.comparing(RailcraftModuleManager::getModuleName));
    for (Map.Entry<Class<? extends IRailcraftModule>, IRailcraftModule> entry : classToInstanceMapping.entrySet()) {
        if (ModuleCore.class.equals(entry.getKey()))
            continue;
        IRailcraftModule module = entry.getValue();
        String moduleName = getModuleName(module);
        if (!isConfigured(config, module)) {
            Game.log(Level.INFO, "Module disabled: {0}", module);
            continue;
        }
        try {
            module.checkPrerequisites();
        } catch (IRailcraftModule.MissingPrerequisiteException ex) {
            Game.logThrowable(Level.INFO, 0, ex, "Module failed prerequisite check, disabling: {0}", moduleName);
            toDisable.add(module.getClass());
            continue;
        }
        toEnable.add(module.getClass());
    }
    // Determine which modules are lacking dependencies
    TreeSet<Class<? extends IRailcraftModule>> toLoad = new TreeSet<>(Comparator.comparing(RailcraftModuleManager::getModuleName));
    toLoad.add(ModuleCore.class);
    boolean changed;
    do {
        changed = false;
        Iterator<Class<? extends IRailcraftModule>> it = toEnable.iterator();
        while (it.hasNext()) {
            Class<? extends IRailcraftModule> moduleClass = it.next();
            if (toLoad.containsAll(getDependencies(moduleClass))) {
                it.remove();
                toLoad.add(moduleClass);
                changed = true;
                break;
            }
        }
    } while (changed);
    // Tell the user which modules are missing dependencies
    for (Class<? extends IRailcraftModule> moduleClass : toEnable) {
        Game.log(Level.WARN, "Module is missing dependencies, disabling: {0} -> {1}", getDependencies(moduleClass), getModuleName(moduleClass));
    }
    // Add modules missing dependencies to the disabled list
    toDisable.addAll(toEnable);
    // Build and sort loadOrder
    toLoad.remove(ModuleCore.class);
    loadOrder.add(ModuleCore.class);
    do {
        changed = false;
        Iterator<Class<? extends IRailcraftModule>> it = toLoad.iterator();
        while (it.hasNext()) {
            Class<? extends IRailcraftModule> moduleClass = it.next();
            if (loadOrder.containsAll(getAllDependencies(moduleClass))) {
                it.remove();
                loadOrder.add(moduleClass);
                changed = true;
                break;
            }
        }
    } while (changed);
    // Add the valid modules to the enabled list in the load order
    enabledModules.addAll(loadOrder);
    // Add the disabled modules to the load order
    loadOrder.addAll(toDisable);
    if (config.hasChanged())
        config.save();
    Locale.setDefault(locale);
    processStage(Stage.CONSTRUCTION);
    processStage(Stage.PRE_INIT);
}
Also used : Configuration(net.minecraftforge.common.config.Configuration) IRailcraftModule(mods.railcraft.api.core.IRailcraftModule) File(java.io.File)

Aggregations

IRailcraftModule (mods.railcraft.api.core.IRailcraftModule)5 RailcraftModule (mods.railcraft.api.core.RailcraftModule)3 File (java.io.File)1 Configuration (net.minecraftforge.common.config.Configuration)1 Property (net.minecraftforge.common.config.Property)1