Search in sources :

Example 6 with ModuleEnvironment

use of org.terasology.gestalt.module.ModuleEnvironment in project Terasology by MovingBlocks.

the class RegisterMods method step.

@Override
public boolean step() {
    if (applyModulesThread != null) {
        if (!applyModulesThread.isAlive()) {
            if (oldEnvironment != null) {
                oldEnvironment.close();
            }
            return true;
        }
        return false;
    } else {
        ModuleManager moduleManager = context.get(ModuleManager.class);
        List<Name> moduleIds = gameManifest.getModules().stream().map(NameVersion::getName).collect(Collectors.toCollection(ArrayList::new));
        DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
        ResolutionResult result = resolver.resolve(moduleIds);
        if (result.isSuccess()) {
            oldEnvironment = moduleManager.getEnvironment();
            ModuleEnvironment env = moduleManager.loadEnvironment(result.getModules(), true);
            for (Module moduleInfo : env.getModulesOrderedByDependencies()) {
                logger.info("Activating module: {}:{}", moduleInfo.getId(), moduleInfo.getVersion());
            }
            EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
            applyModulesThread = new Thread(() -> environmentSwitchHandler.handleSwitchToGameEnvironment(context));
            applyModulesThread.start();
            return false;
        } else {
            logger.warn("Missing at least one required module (or dependency) from the following list: {}", moduleIds);
            context.get(GameEngine.class).changeState(new StateMainMenu("Missing required module or dependency"));
            return true;
        }
    }
}
Also used : ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) ResolutionResult(org.terasology.gestalt.module.dependencyresolution.ResolutionResult) GameEngine(org.terasology.engine.core.GameEngine) ModuleManager(org.terasology.engine.core.module.ModuleManager) Module(org.terasology.gestalt.module.Module) Name(org.terasology.gestalt.naming.Name) DependencyResolver(org.terasology.gestalt.module.dependencyresolution.DependencyResolver) EnvironmentSwitchHandler(org.terasology.engine.core.bootstrap.EnvironmentSwitchHandler)

Example 7 with ModuleEnvironment

use of org.terasology.gestalt.module.ModuleEnvironment in project Terasology by MovingBlocks.

the class ModuleManager method loadEnvironment.

public ModuleEnvironment loadEnvironment(Set<Module> modules, boolean asPrimary) {
    Set<Module> finalModules = Sets.newLinkedHashSet(modules);
    finalModules.add(engineModule);
    ModuleEnvironment newEnvironment;
    boolean permissiveSecurityEnabled = Boolean.parseBoolean(System.getProperty(SystemConfig.PERMISSIVE_SECURITY_ENABLED_PROPERTY));
    if (permissiveSecurityEnabled) {
        newEnvironment = new ModuleEnvironment(finalModules, wrappingPermissionProviderFactory);
    } else {
        newEnvironment = new ModuleEnvironment(finalModules, permissionProviderFactory);
    }
    if (asPrimary) {
        environment = newEnvironment;
    }
    return newEnvironment;
}
Also used : ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) Module(org.terasology.gestalt.module.Module)

Example 8 with ModuleEnvironment

use of org.terasology.gestalt.module.ModuleEnvironment in project Terasology by MovingBlocks.

the class ExtraBlockDataManager method getFieldsFromAnnotations.

// Find requests for extensions and which blocks they apply to.
private Map<Integer, Map<String, Set<Block>>> getFieldsFromAnnotations(Context context) {
    ModuleEnvironment environment = context.get(ModuleManager.class).getEnvironment();
    Collection<Block> blocks = context.get(BlockManager.class).listRegisteredBlocks();
    Map<Integer, Map<String, Set<Block>>> fieldss = new HashMap<>();
    TERA_ARRAY_FACTORIES.forEach((size, fac) -> fieldss.put(size, new HashMap<>()));
    for (Class<?> type : environment.getTypesAnnotatedWith(ExtraDataSystem.class)) {
        for (Method method : type.getMethods()) {
            RegisterExtraData registerAnnotation = method.getAnnotation(RegisterExtraData.class);
            if (registerAnnotation != null) {
                String errorType = validRegistrationMethod(method, registerAnnotation);
                if (errorType != null) {
                    logger.error("Unable to register extra block data: " + errorType + " for {}.{}: should be \"public static boolean {}(Block block)\", and bitSize should be 4, 8 or 16.", type.getName(), method.getName(), method.getName());
                    continue;
                }
                method.setAccessible(true);
                Set<Block> includedBlocks = new HashSet<>();
                for (Block block : blocks) {
                    try {
                        if ((boolean) method.invoke(null, block)) {
                            includedBlocks.add(block);
                        }
                    } catch (IllegalAccessException e) {
                        // This should not get to this point.
                        throw new RuntimeException("Incorrect access modifier on register extra data method", e);
                    } catch (InvocationTargetException e) {
                        throw new RuntimeException(e);
                    }
                }
                fieldss.get(registerAnnotation.bitSize()).put(registerAnnotation.name(), includedBlocks);
            }
        }
    }
    return fieldss;
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) ModuleManager(org.terasology.engine.core.module.ModuleManager) InvocationTargetException(java.lang.reflect.InvocationTargetException) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) BlockManager(org.terasology.engine.world.block.BlockManager) Block(org.terasology.engine.world.block.Block) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 9 with ModuleEnvironment

use of org.terasology.gestalt.module.ModuleEnvironment in project Terasology by MovingBlocks.

the class EnvironmentSwitchHandler method handleSwitchToGameEnvironment.

public void handleSwitchToGameEnvironment(Context context) {
    ModuleManager moduleManager = context.get(ModuleManager.class);
    ModuleEnvironment environment = moduleManager.getEnvironment();
    ModuleTypeRegistry typeRegistry = context.get(ModuleTypeRegistry.class);
    typeRegistry.reload(environment);
    CopyStrategyLibrary copyStrategyLibrary = context.get(CopyStrategyLibrary.class);
    copyStrategyLibrary.clear();
    for (CopyStrategyEntry<?> entry : typesWithCopyConstructors) {
        entry.registerWith(copyStrategyLibrary);
    }
    // TODO: find a permanent fix over just creating a new typehandler
    // https://github.com/Terasology/JoshariasSurvival/issues/31
    // TypeHandlerLibrary typeHandlerLibrary = context.get(TypeHandlerLibrary.class);
    // typeHandlerLibrary.addTypeHandler(CollisionGroup.class, new CollisionGroupTypeHandler(context.get(CollisionGroupManager.class)));
    TypeHandlerLibrary typeHandlerLibrary = TypeHandlerLibraryImpl.forModuleEnvironment(moduleManager, typeRegistry);
    typeHandlerLibrary.addTypeHandler(CollisionGroup.class, new CollisionGroupTypeHandler(context.get(CollisionGroupManager.class)));
    context.put(TypeHandlerLibrary.class, typeHandlerLibrary);
    // Entity System Library
    EntitySystemLibrary library = new EntitySystemLibrary(context, typeHandlerLibrary);
    context.put(EntitySystemLibrary.class, library);
    ComponentLibrary componentLibrary = library.getComponentLibrary();
    context.put(ComponentLibrary.class, componentLibrary);
    context.put(EventLibrary.class, library.getEventLibrary());
    context.put(ClassMetaLibrary.class, new ClassMetaLibraryImpl(context));
    registerComponents(componentLibrary, environment);
    registerTypeHandlers(context, typeHandlerLibrary, environment);
    // Load configs for the new environment
    AutoConfigManager autoConfigManager = context.get(AutoConfigManager.class);
    autoConfigManager.loadConfigsIn(context);
    ModuleAwareAssetTypeManager assetTypeManager = context.get(ModuleAwareAssetTypeManager.class);
    /*
         * The registering of the prefab formats is done in this method, because it needs to be done before
         * the environment of the asset manager gets changed.
         *
         * It can't be done before this method gets called because the ComponentLibrary isn't
         * existing then yet.
         */
    unregisterPrefabFormats(assetTypeManager);
    registeredPrefabFormat = new PrefabFormat(componentLibrary, typeHandlerLibrary);
    assetTypeManager.getAssetFileDataProducer(assetTypeManager.getAssetType(Prefab.class).orElseThrow(() -> new RuntimeException("Cannot get Prefab Asset typee"))).addAssetFormat(registeredPrefabFormat);
    registeredPrefabDeltaFormat = new PrefabDeltaFormat(componentLibrary, typeHandlerLibrary);
    assetTypeManager.getAssetFileDataProducer(assetTypeManager.getAssetType(Prefab.class).orElseThrow(() -> new RuntimeException("Cannot get Prefab Asset type"))).addDeltaFormat(registeredPrefabDeltaFormat);
    assetTypeManager.switchEnvironment(environment);
    assetTypeManager.reloadAssets();
}
Also used : ModuleTypeRegistry(org.terasology.reflection.ModuleTypeRegistry) ModuleAwareAssetTypeManager(org.terasology.gestalt.assets.module.ModuleAwareAssetTypeManager) PrefabFormat(org.terasology.engine.entitySystem.prefab.internal.PrefabFormat) CopyStrategyLibrary(org.terasology.reflection.copy.CopyStrategyLibrary) PrefabDeltaFormat(org.terasology.engine.entitySystem.prefab.internal.PrefabDeltaFormat) ModuleManager(org.terasology.engine.core.module.ModuleManager) CollisionGroupTypeHandler(org.terasology.engine.persistence.typeHandling.extensionTypes.CollisionGroupTypeHandler) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) TypeHandlerLibrary(org.terasology.persistence.typeHandling.TypeHandlerLibrary) EntitySystemLibrary(org.terasology.engine.entitySystem.metadata.EntitySystemLibrary) ComponentLibrary(org.terasology.engine.entitySystem.metadata.ComponentLibrary) AutoConfigManager(org.terasology.engine.config.flexible.AutoConfigManager) Prefab(org.terasology.engine.entitySystem.prefab.Prefab)

Example 10 with ModuleEnvironment

use of org.terasology.gestalt.module.ModuleEnvironment in project Terasology by MovingBlocks.

the class EnvironmentSwitchHandler method handleSwitchBackFromPreviewEnvironment.

public void handleSwitchBackFromPreviewEnvironment(Context context) {
    // The newly created ComponentLibrary instance cannot be invalidated in context
    ModuleEnvironment environment = context.get(ModuleManager.class).getEnvironment();
    cheapAssetManagerUpdate(context, environment);
}
Also used : ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) ModuleManager(org.terasology.engine.core.module.ModuleManager)

Aggregations

ModuleEnvironment (org.terasology.gestalt.module.ModuleEnvironment)31 ModuleManager (org.terasology.engine.core.module.ModuleManager)24 Module (org.terasology.gestalt.module.Module)6 Name (org.terasology.gestalt.naming.Name)6 DependencyResolver (org.terasology.gestalt.module.dependencyresolution.DependencyResolver)5 ResolutionResult (org.terasology.gestalt.module.dependencyresolution.ResolutionResult)5 SimpleUri (org.terasology.engine.core.SimpleUri)4 RegisterBindButton (org.terasology.engine.input.RegisterBindButton)4 RecordAndReplayCurrentStatus (org.terasology.engine.recording.RecordAndReplayCurrentStatus)4 RecordAndReplaySerializer (org.terasology.engine.recording.RecordAndReplaySerializer)4 RecordAndReplayUtils (org.terasology.engine.recording.RecordAndReplayUtils)4 EngineEntityManager (org.terasology.engine.entitySystem.entity.internal.EngineEntityManager)3 NetworkSystem (org.terasology.engine.network.NetworkSystem)3 DirectionAndOriginPosRecorderList (org.terasology.engine.recording.DirectionAndOriginPosRecorderList)3 BlockManager (org.terasology.engine.world.block.BlockManager)3 BlockFamilyLibrary (org.terasology.engine.world.block.family.BlockFamilyLibrary)3 TypeHandlerLibrary (org.terasology.persistence.typeHandling.TypeHandlerLibrary)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URL (java.net.URL)2 Path (java.nio.file.Path)2