Search in sources :

Example 11 with ModuleManager

use of org.terasology.engine.core.module.ModuleManager in project Terasology by MovingBlocks.

the class JoinServer method step.

@Override
public boolean step() {
    if (applyModuleThread != null) {
        if (!applyModuleThread.isAlive()) {
            if (oldEnvironment != null) {
                oldEnvironment.close();
            }
            return true;
        }
        return false;
    } else if (joinStatus.getStatus() == JoinStatus.Status.COMPLETE) {
        Server server = networkSystem.getServer();
        ServerInfoMessage serverInfo = networkSystem.getServer().getInfo();
        // If no GameName, use Server IP Address
        if (serverInfo.getGameName().length() > 0) {
            gameManifest.setTitle(serverInfo.getGameName());
        } else {
            gameManifest.setTitle(server.getRemoteAddress());
        }
        for (WorldInfo worldInfo : serverInfo.getWorldInfoList()) {
            gameManifest.addWorld(worldInfo);
        }
        Map<String, Short> blockMap = Maps.newHashMap();
        for (Entry<Integer, String> entry : serverInfo.getBlockIds().entrySet()) {
            String name = entry.getValue();
            short id = entry.getKey().shortValue();
            Short oldId = blockMap.put(name, id);
            if (oldId != null && oldId != id) {
                logger.warn("Overwriting Id {} for {} with Id {}", oldId, name, id);
            }
        }
        gameManifest.setRegisteredBlockFamilies(serverInfo.getRegisterBlockFamilyList());
        gameManifest.setBlockIdMap(blockMap);
        gameManifest.setTime(networkSystem.getServer().getInfo().getTime());
        ModuleManager moduleManager = context.get(ModuleManager.class);
        Set<Module> moduleSet = Sets.newLinkedHashSet();
        for (NameVersion moduleInfo : networkSystem.getServer().getInfo().getModuleList()) {
            Module module = moduleManager.getRegistry().getModule(moduleInfo.getName(), moduleInfo.getVersion());
            if (module == null) {
                StateMainMenu mainMenu = new StateMainMenu("Missing required module: " + moduleInfo);
                context.get(GameEngine.class).changeState(mainMenu);
                return false;
            } else {
                logger.info("Activating module: {}:{}", moduleInfo.getName(), moduleInfo.getVersion());
                gameManifest.addModule(module.getId(), module.getVersion());
                moduleSet.add(module);
            }
        }
        oldEnvironment = moduleManager.getEnvironment();
        moduleManager.loadEnvironment(moduleSet, true);
        context.get(Game.class).load(gameManifest);
        EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
        applyModuleThread = new Thread(() -> environmentSwitchHandler.handleSwitchToGameEnvironment(context));
        applyModuleThread.start();
        return false;
    } else if (joinStatus.getStatus() == JoinStatus.Status.FAILED) {
        StateMainMenu mainMenu = new StateMainMenu("Failed to connect to server: " + joinStatus.getErrorMessage());
        context.get(GameEngine.class).changeState(mainMenu);
        networkSystem.shutdown();
    }
    return false;
}
Also used : Set(java.util.Set) Server(org.terasology.engine.network.Server) NameVersion(org.terasology.gestalt.naming.NameVersion) GameEngine(org.terasology.engine.core.GameEngine) ModuleManager(org.terasology.engine.core.module.ModuleManager) Entry(java.util.Map.Entry) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) WorldInfo(org.terasology.engine.world.internal.WorldInfo) ServerInfoMessage(org.terasology.engine.network.ServerInfoMessage) Module(org.terasology.gestalt.module.Module) Map(java.util.Map) EnvironmentSwitchHandler(org.terasology.engine.core.bootstrap.EnvironmentSwitchHandler)

Example 12 with ModuleManager

use of org.terasology.engine.core.module.ModuleManager 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 13 with ModuleManager

use of org.terasology.engine.core.module.ModuleManager in project Terasology by MovingBlocks.

the class EntitySystemSetupUtil method addEntityManagementRelatedClasses.

/**
 * Objects for the following classes must be available in the context:
 * <ul>
 * <li>{@link ModuleEnvironment}</li>
 * <li>{@link NetworkSystem}</li>
 * <li>{@link ReflectFactory}</li>
 * <li>{@link CopyStrategyLibrary}</li>
 * <li>{@link TypeHandlerLibrary}</li>
 * </ul>
 * <p>
 * The method will make objects for the following classes available in the context:
 * <ul>
 * <li>{@link EngineEntityManager}</li>
 * <li>{@link ComponentLibrary}</li>
 * <li>{@link EventLibrary}</li>
 * <li>{@link PrefabManager}</li>
 * <li>{@link EventSystem}</li>
 * </ul>
 */
public static void addEntityManagementRelatedClasses(Context context) {
    ModuleManager moduleManager = context.get(ModuleManager.class);
    ModuleEnvironment environment = moduleManager.getEnvironment();
    NetworkSystem networkSystem = context.get(NetworkSystem.class);
    // Entity Manager
    PojoEntityManager entityManager = new PojoEntityManager();
    context.put(EntityManager.class, entityManager);
    context.put(EngineEntityManager.class, entityManager);
    // Standard serialization library
    TypeHandlerLibrary typeHandlerLibrary = context.get(TypeHandlerLibrary.class);
    typeHandlerLibrary.addTypeHandler(EntityRef.class, new EntityRefTypeHandler(entityManager));
    entityManager.setTypeSerializerLibrary(typeHandlerLibrary);
    // Prefab Manager
    PrefabManager prefabManager = new PojoPrefabManager(context);
    entityManager.setPrefabManager(prefabManager);
    context.put(PrefabManager.class, prefabManager);
    EntitySystemLibrary library = context.get(EntitySystemLibrary.class);
    entityManager.setComponentLibrary(library.getComponentLibrary());
    // Record and Replay
    RecordAndReplayCurrentStatus recordAndReplayCurrentStatus = context.get(RecordAndReplayCurrentStatus.class);
    RecordAndReplayUtils recordAndReplayUtils = context.get(RecordAndReplayUtils.class);
    CharacterStateEventPositionMap characterStateEventPositionMap = context.get(CharacterStateEventPositionMap.class);
    DirectionAndOriginPosRecorderList directionAndOriginPosRecorderList = context.get(DirectionAndOriginPosRecorderList.class);
    RecordedEventStore recordedEventStore = new RecordedEventStore();
    RecordAndReplaySerializer recordAndReplaySerializer = new RecordAndReplaySerializer(entityManager, recordedEventStore, recordAndReplayUtils, characterStateEventPositionMap, directionAndOriginPosRecorderList, moduleManager, context.get(TypeRegistry.class));
    context.put(RecordAndReplaySerializer.class, recordAndReplaySerializer);
    // Event System
    EventSystem eventSystem = createEventSystem(networkSystem, entityManager, library, recordedEventStore, recordAndReplaySerializer, recordAndReplayUtils, recordAndReplayCurrentStatus);
    entityManager.setEventSystem(eventSystem);
    context.put(EventSystem.class, eventSystem);
    // TODO: Review - NodeClassLibrary related to the UI for behaviours. Should not be here and probably not even in the CoreRegistry
    context.put(OneOfProviderFactory.class, new OneOfProviderFactory());
    registerComponents(library.getComponentLibrary(), environment);
    registerEvents(entityManager.getEventSystem(), environment);
}
Also used : PojoPrefabManager(org.terasology.engine.entitySystem.prefab.internal.PojoPrefabManager) PrefabManager(org.terasology.engine.entitySystem.prefab.PrefabManager) PojoPrefabManager(org.terasology.engine.entitySystem.prefab.internal.PojoPrefabManager) NetworkSystem(org.terasology.engine.network.NetworkSystem) RecordedEventStore(org.terasology.engine.recording.RecordedEventStore) ModuleManager(org.terasology.engine.core.module.ModuleManager) TypeRegistry(org.terasology.reflection.TypeRegistry) RecordAndReplaySerializer(org.terasology.engine.recording.RecordAndReplaySerializer) DirectionAndOriginPosRecorderList(org.terasology.engine.recording.DirectionAndOriginPosRecorderList) OneOfProviderFactory(org.terasology.nui.properties.OneOfProviderFactory) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) TypeHandlerLibrary(org.terasology.persistence.typeHandling.TypeHandlerLibrary) EntitySystemLibrary(org.terasology.engine.entitySystem.metadata.EntitySystemLibrary) RecordAndReplayUtils(org.terasology.engine.recording.RecordAndReplayUtils) PojoEntityManager(org.terasology.engine.entitySystem.entity.internal.PojoEntityManager) EntityRefTypeHandler(org.terasology.engine.persistence.typeHandling.extensionTypes.EntityRefTypeHandler) EventSystem(org.terasology.engine.entitySystem.event.internal.EventSystem) RecordAndReplayCurrentStatus(org.terasology.engine.recording.RecordAndReplayCurrentStatus) CharacterStateEventPositionMap(org.terasology.engine.recording.CharacterStateEventPositionMap)

Example 14 with ModuleManager

use of org.terasology.engine.core.module.ModuleManager in project Terasology by MovingBlocks.

the class TerasologyEngine method initManagers.

private void initManagers() {
    changeStatus(TerasologyEngineStatus.INITIALIZING_MODULE_MANAGER);
    TypeRegistry.WHITELISTED_CLASSES = ExternalApiWhitelist.CLASSES.stream().map(Class::getName).collect(Collectors.toSet());
    TypeRegistry.WHITELISTED_PACKAGES = ExternalApiWhitelist.PACKAGES;
    ModuleManager moduleManager = new ModuleManager(rootContext.get(Config.class), classesOnClasspathsToAddToEngine);
    ModuleTypeRegistry typeRegistry = new ModuleTypeRegistry(moduleManager.getEnvironment());
    rootContext.put(ModuleTypeRegistry.class, typeRegistry);
    rootContext.put(TypeRegistry.class, typeRegistry);
    rootContext.put(ModuleManager.class, moduleManager);
    changeStatus(TerasologyEngineStatus.INITIALIZING_LOWLEVEL_OBJECT_MANIPULATION);
    ReflectFactory reflectFactory = new ReflectionReflectFactory();
    rootContext.put(ReflectFactory.class, reflectFactory);
    CopyStrategyLibrary copyStrategyLibrary = new CopyStrategyLibrary(reflectFactory);
    rootContext.put(CopyStrategyLibrary.class, copyStrategyLibrary);
    rootContext.put(TypeHandlerLibrary.class, TypeHandlerLibraryImpl.forModuleEnvironment(moduleManager, typeRegistry));
    changeStatus(TerasologyEngineStatus.INITIALIZING_ASSET_TYPES);
    assetTypeManager = new AutoReloadAssetTypeManager();
    rootContext.put(ModuleAwareAssetTypeManager.class, assetTypeManager);
    rootContext.put(AssetManager.class, assetTypeManager.getAssetManager());
}
Also used : ReflectionReflectFactory(org.terasology.reflection.reflect.ReflectionReflectFactory) ReflectionReflectFactory(org.terasology.reflection.reflect.ReflectionReflectFactory) ReflectFactory(org.terasology.reflection.reflect.ReflectFactory) ModuleTypeRegistry(org.terasology.reflection.ModuleTypeRegistry) Config(org.terasology.engine.config.Config) CopyStrategyLibrary(org.terasology.reflection.copy.CopyStrategyLibrary) ModuleManager(org.terasology.engine.core.module.ModuleManager) AutoReloadAssetTypeManager(org.terasology.gestalt.assets.module.autoreload.AutoReloadAssetTypeManager)

Example 15 with ModuleManager

use of org.terasology.engine.core.module.ModuleManager in project Terasology by MovingBlocks.

the class BindingScraper method main.

/**
 * @param args (ignored)
 * @throws Exception if the module environment cannot be loaded
 */
public static void main(String[] args) throws Exception {
    ModuleManager moduleManager = ModuleManagerFactory.create();
    // Holds normal input mappings where there is only one key
    Multimap<InputCategory, String> categories = ArrayListMultimap.create();
    Multimap<String, Input> keys = ArrayListMultimap.create();
    Map<String, String> desc = new HashMap<>();
    for (Class<?> holdingType : moduleManager.getEnvironment().getTypesAnnotatedWith(InputCategory.class)) {
        InputCategory inputCategory = holdingType.getAnnotation(InputCategory.class);
        categories.put(inputCategory, null);
        for (String button : inputCategory.ordering()) {
            categories.put(inputCategory, button);
        }
    }
    for (Class<?> buttonEvent : moduleManager.getEnvironment().getTypesAnnotatedWith(RegisterBindButton.class)) {
        DefaultBinding defBinding = buttonEvent.getAnnotation(DefaultBinding.class);
        RegisterBindButton info = buttonEvent.getAnnotation(RegisterBindButton.class);
        String cat = info.category();
        String id = "engine:" + info.id();
        desc.put(id, info.description());
        if (cat.isEmpty()) {
            InputCategory inputCategory = findEntry(categories, id);
            if (inputCategory == null) {
                System.out.println("Invalid category for: " + info.id());
            }
        } else {
            InputCategory inputCategory = findCategory(categories, cat);
            if (inputCategory != null) {
                categories.put(inputCategory, id);
            } else {
                System.out.println("Invalid category for: " + info.id());
            }
        }
        if (defBinding != null) {
            // This handles bindings with just one key
            Input input = defBinding.type().getInput(defBinding.id());
            keys.put(id, input);
        } else {
            // See if there is a multi-mapping for this button
            DefaultBindings multiBinding = buttonEvent.getAnnotation(DefaultBindings.class);
            // Annotation math magic. We're expecting a DefaultBindings containing one DefaultBinding pair
            if (multiBinding != null && multiBinding.value().length == 2) {
                DefaultBinding[] bindings = multiBinding.value();
                Input primary = bindings[0].type().getInput(bindings[0].id());
                Input secondary = bindings[1].type().getInput(bindings[1].id());
                keys.put(id, primary);
                keys.put(id, secondary);
            }
        }
    }
    for (InputCategory row : categories.keySet()) {
        System.out.println("# " + row.displayName());
        categories.get(row).stream().filter(entry -> entry != null).forEach(entry -> System.out.println(desc.get(entry) + ": " + keys.get(entry)));
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) RegisterBindButton(org.terasology.engine.input.RegisterBindButton) DefaultBindings(org.terasology.engine.input.DefaultBindings) InputCategory(org.terasology.input.InputCategory) ModuleManager(org.terasology.engine.core.module.ModuleManager) DefaultBinding(org.terasology.engine.input.DefaultBinding) Map(java.util.Map) Input(org.terasology.input.Input) HashMap(java.util.HashMap) ModuleManagerFactory(org.terasology.engine.testUtil.ModuleManagerFactory) Multimap(com.google.common.collect.Multimap) DefaultBinding(org.terasology.engine.input.DefaultBinding) HashMap(java.util.HashMap) RegisterBindButton(org.terasology.engine.input.RegisterBindButton) ModuleManager(org.terasology.engine.core.module.ModuleManager) Input(org.terasology.input.Input) InputCategory(org.terasology.input.InputCategory) DefaultBindings(org.terasology.engine.input.DefaultBindings)

Aggregations

ModuleManager (org.terasology.engine.core.module.ModuleManager)35 ModuleEnvironment (org.terasology.gestalt.module.ModuleEnvironment)15 Module (org.terasology.gestalt.module.Module)10 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ContextImpl (org.terasology.engine.context.internal.ContextImpl)7 RecordAndReplayCurrentStatus (org.terasology.engine.recording.RecordAndReplayCurrentStatus)7 ModuleAwareAssetTypeManager (org.terasology.gestalt.assets.module.ModuleAwareAssetTypeManager)6 Name (org.terasology.gestalt.naming.Name)6 PojoPrefab (org.terasology.engine.entitySystem.prefab.internal.PojoPrefab)5 ModuleAwareAssetTypeManagerImpl (org.terasology.gestalt.assets.module.ModuleAwareAssetTypeManagerImpl)5 DependencyResolver (org.terasology.gestalt.module.dependencyresolution.DependencyResolver)5 ResolutionResult (org.terasology.gestalt.module.dependencyresolution.ResolutionResult)5 TypeHandlerLibrary (org.terasology.persistence.typeHandling.TypeHandlerLibrary)5 GameEngine (org.terasology.engine.core.GameEngine)4 EntitySystemLibrary (org.terasology.engine.entitySystem.metadata.EntitySystemLibrary)4 PojoPrefabManager (org.terasology.engine.entitySystem.prefab.internal.PojoPrefabManager)4 NetworkSystem (org.terasology.engine.network.NetworkSystem)4 TypeRegistry (org.terasology.reflection.TypeRegistry)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3