Search in sources :

Example 1 with ModuleEnvironment

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

the class HeadlessEnvironment method setupStorageManager.

@Override
protected void setupStorageManager() throws IOException {
    ModuleManager moduleManager = context.get(ModuleManager.class);
    EngineEntityManager engineEntityManager = context.get(EngineEntityManager.class);
    BlockManager blockManager = context.get(BlockManager.class);
    RecordAndReplaySerializer recordAndReplaySerializer = context.get(RecordAndReplaySerializer.class);
    Path savePath = PathManager.getInstance().getSavePath("world1");
    RecordAndReplayUtils recordAndReplayUtils = new RecordAndReplayUtils();
    RecordAndReplayCurrentStatus recordAndReplayCurrentStatus = context.get(RecordAndReplayCurrentStatus.class);
    ModuleEnvironment environment = context.get(ModuleManager.class).getEnvironment();
    context.put(BlockFamilyLibrary.class, new BlockFamilyLibrary(environment, context));
    ExtraBlockDataManager extraDataManager = context.get(ExtraBlockDataManager.class);
    context.put(StorageManager.class, new ReadWriteStorageManager(savePath, moduleManager.getEnvironment(), engineEntityManager, blockManager, extraDataManager, recordAndReplaySerializer, recordAndReplayUtils, recordAndReplayCurrentStatus));
}
Also used : EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) Path(java.nio.file.Path) BlockManager(org.terasology.engine.world.block.BlockManager) RecordAndReplayUtils(org.terasology.engine.recording.RecordAndReplayUtils) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) BlockFamilyLibrary(org.terasology.engine.world.block.family.BlockFamilyLibrary) RecordAndReplayCurrentStatus(org.terasology.engine.recording.RecordAndReplayCurrentStatus) ReadWriteStorageManager(org.terasology.engine.persistence.internal.ReadWriteStorageManager) ModuleManager(org.terasology.engine.core.module.ModuleManager) ExtraBlockDataManager(org.terasology.engine.world.chunks.blockdata.ExtraBlockDataManager) RecordAndReplaySerializer(org.terasology.engine.recording.RecordAndReplaySerializer)

Example 2 with ModuleEnvironment

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

the class CompleteApiScraper method getApi.

/**
 * @return Project's Packages, Interfaces, Classes and Methods
 * @throws Exception if the module environment cannot be loaded
 */
static StringBuffer getApi() throws Exception {
    ModuleManager moduleManager = ModuleManagerFactory.create();
    ModuleEnvironment environment = moduleManager.getEnvironment();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Multimap<String, String> api = Multimaps.newMultimap(new HashMap<String, Collection<String>>(), ArrayList::new);
    for (Class<?> apiClass : environment.getTypesAnnotatedWith(API.class)) {
        boolean isPackage = apiClass.isSynthetic();
        URL location;
        String category;
        String apiPackage = "";
        if (isPackage) {
            apiPackage = apiClass.getPackage().getName();
            location = classLoader.getResource(apiPackage.replace('.', '/'));
        } else {
            location = apiClass.getResource('/' + apiClass.getName().replace('.', '/') + ".class");
        }
        if (location == null) {
            logger.error("Failed to get a class/package location, skipping " + apiClass);
            continue;
        }
        switch(location.getProtocol()) {
            case "jar":
                // Find out what jar it came from and consider that the category
                String categoryFragment = location.getPath();
                int bang = categoryFragment.lastIndexOf("!");
                int hyphen = categoryFragment.lastIndexOf("-", bang);
                int slash = categoryFragment.lastIndexOf("/", hyphen);
                category = categoryFragment.substring(slash + 1, hyphen);
                if (isPackage) {
                    api.put(category, apiPackage + " (PACKAGE)");
                } else {
                    addToApi(category, apiClass, api);
                }
                break;
            case "file":
                // If file based we know it is local so organize it like that
                category = TERASOLOGY_API_CLASS_CATEGORY;
                if (isPackage) {
                    api.put(category, apiPackage + " (PACKAGE)");
                } else {
                    addToApi(category, apiClass, api);
                }
                break;
            default:
                logger.error("Unknown protocol for: " + apiClass + ", came from " + location);
        }
    }
    api.putAll(EXTERNAL, ExternalApiWhitelist.CLASSES.stream().map(clazz -> clazz.getName() + " (CLASS)").collect(Collectors.toSet()));
    api.putAll(EXTERNAL, ExternalApiWhitelist.PACKAGES.stream().map(packagee -> packagee + " (PACKAGE)").collect(Collectors.toSet()));
    // Puts the information in the StringBuffer
    StringBuffer stringApi = new StringBuffer();
    stringApi.append("# Modding API:\n");
    for (String key : api.keySet()) {
        stringApi.append("## ");
        stringApi.append(key);
        stringApi.append("\n");
        for (String value : api.get(key)) {
            stringApi.append("* ");
            stringApi.append(value);
            stringApi.append("\n");
        }
        stringApi.append("\n");
    }
    return stringApi;
}
Also used : ArrayList(java.util.ArrayList) ModuleManager(org.terasology.engine.core.module.ModuleManager) URL(java.net.URL) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) Collection(java.util.Collection)

Example 3 with ModuleEnvironment

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

the class BindsSubsystemTest method setUpMockModuleEnvironment.

private void setUpMockModuleEnvironment() {
    ModuleManager moduleManager = mock(ModuleManager.class);
    ModuleRegistry moduleRegistry = new TableModuleRegistry();
    Module module = mock(Module.class);
    when(module.getId()).thenReturn(new Name(TEST_MODULE));
    when(module.getVersion()).thenReturn(new Version(0, 0, 1, true));
    when(module.getMetadata()).thenReturn(new ModuleMetadata());
    moduleRegistry.add(module);
    when(moduleManager.getRegistry()).thenReturn(moduleRegistry);
    ModuleEnvironment environment = mock(ModuleEnvironment.class);
    when(moduleManager.loadEnvironment(any(), anyBoolean())).thenReturn(environment);
    when(moduleManager.getEnvironment()).thenReturn(environment);
    registerBindButtonClasses = new ArrayList<>();
    when(environment.getTypesAnnotatedWith(eq(RegisterBindButton.class))).thenReturn(registerBindButtonClasses);
    when(environment.getTypesAnnotatedWith(eq(RegisterBindButton.class), any())).thenReturn(registerBindButtonClasses);
    registerRealBindAxisClasses = new ArrayList<>();
    when(environment.getTypesAnnotatedWith(eq(RegisterBindAxis.class))).thenReturn(registerRealBindAxisClasses);
    when(environment.getTypesAnnotatedWith(eq(RegisterBindAxis.class), any())).thenReturn(registerRealBindAxisClasses);
    when(environment.getModuleProviding(any())).thenReturn(new Name(TEST_MODULE));
    context.put(ModuleManager.class, moduleManager);
}
Also used : TableModuleRegistry(org.terasology.gestalt.module.TableModuleRegistry) Version(org.terasology.gestalt.naming.Version) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) RegisterBindButton(org.terasology.engine.input.RegisterBindButton) RegisterBindAxis(org.terasology.engine.input.RegisterBindAxis) ModuleRegistry(org.terasology.gestalt.module.ModuleRegistry) TableModuleRegistry(org.terasology.gestalt.module.TableModuleRegistry) ModuleMetadata(org.terasology.gestalt.module.ModuleMetadata) ModuleManager(org.terasology.engine.core.module.ModuleManager) Module(org.terasology.gestalt.module.Module) Name(org.terasology.gestalt.naming.Name)

Example 4 with ModuleEnvironment

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

the class ApiScraper 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();
    ModuleEnvironment environment = moduleManager.getEnvironment();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    SortedSetMultimap<String, String> sortedApi = Multimaps.newSortedSetMultimap(new HashMap<>(), TreeSet::new);
    for (Class<?> apiClass : environment.getTypesAnnotatedWith(API.class)) {
        // System.out.println("Processing: " + apiClass);
        boolean isPackage = apiClass.isSynthetic();
        URL location;
        String category;
        String apiPackage = "";
        if (isPackage) {
            apiPackage = apiClass.getPackage().getName();
            location = classLoader.getResource(apiPackage.replace('.', '/'));
        } else {
            location = apiClass.getResource('/' + apiClass.getName().replace('.', '/') + ".class");
        }
        if (location == null) {
            System.out.println("Failed to get a class/package location, skipping " + apiClass);
            continue;
        }
        switch(location.getProtocol()) {
            case "jar":
                // Find out what jar it came from and consider that the category
                String categoryFragment = location.getPath();
                // System.out.println("category fragment as path: " + categoryFragment);
                int bang = categoryFragment.lastIndexOf("!");
                int hyphen = categoryFragment.lastIndexOf("-", bang);
                int slash = categoryFragment.lastIndexOf("/", hyphen);
                category = categoryFragment.substring(slash + 1, hyphen);
                if (isPackage) {
                    // System.out.println("Jar-based Package: " + apiPackage + ", came from " + location);
                    sortedApi.put(category, apiPackage + " (PACKAGE)");
                } else {
                    // System.out.println("Jar-based Class: " + apiClass + ", came from " + location);
                    sortedApi.put(category, apiClass.getName() + (apiClass.isInterface() ? " (INTERFACE)" : " (CLASS)"));
                }
                break;
            case "file":
                // If file based we know it is local so organize it like that
                category = "terasology engine";
                if (isPackage) {
                    // System.out.println("Local Package: " + apiPackage + ", came from " + location);
                    sortedApi.put(category, apiPackage + " (PACKAGE)");
                } else {
                    // System.out.println("Local Class: " + apiClass + ", came from " + location);
                    sortedApi.put(category, apiClass.getName() + (apiClass.isInterface() ? " (INTERFACE)" : " (CLASS)"));
                }
                break;
            default:
                System.out.println("Unknown protocol for: " + apiClass + ", came from " + location);
        }
    }
    sortedApi.putAll("external", ExternalApiWhitelist.CLASSES.stream().map(clazz -> clazz.getName() + " (CLASS)").collect(Collectors.toSet()));
    sortedApi.putAll("external", ExternalApiWhitelist.PACKAGES.stream().map(packagee -> packagee + " (PACKAGE)").collect(Collectors.toSet()));
    System.out.println("# Modding API:\n");
    for (String key : sortedApi.keySet()) {
        System.out.println("## " + key + "\n");
        for (String value : sortedApi.get(key)) {
            System.out.println("* " + value);
        }
        System.out.println("");
    }
}
Also used : ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) TreeSet(java.util.TreeSet) ModuleManager(org.terasology.engine.core.module.ModuleManager) URL(java.net.URL)

Example 5 with ModuleEnvironment

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

the class StorageManagerTest method setup.

@BeforeEach
public void setup(@TempDir Path tempHome) throws Exception {
    super.setup();
    PathManager.getInstance().useOverrideHomePath(tempHome);
    savePath = PathManager.getInstance().getSavePath("testSave");
    assert !Files.isRegularFile(tempHome.resolve("global.dat"));
    entityManager = context.get(EngineEntityManager.class);
    moduleEnvironment = mock(ModuleEnvironment.class);
    blockManager = context.get(BlockManager.class);
    extraDataManager = context.get(ExtraBlockDataManager.class);
    ModuleManager moduleManager = mock(ModuleManager.class);
    when(moduleManager.getEnvironment()).thenReturn(moduleEnvironment);
    RecordedEventStore recordedEventStore = new RecordedEventStore();
    recordAndReplayUtils = new RecordAndReplayUtils();
    CharacterStateEventPositionMap characterStateEventPositionMap = new CharacterStateEventPositionMap();
    DirectionAndOriginPosRecorderList directionAndOriginPosRecorderList = new DirectionAndOriginPosRecorderList();
    recordAndReplaySerializer = new RecordAndReplaySerializer(entityManager, recordedEventStore, recordAndReplayUtils, characterStateEventPositionMap, directionAndOriginPosRecorderList, moduleManager, mock(TypeRegistry.class));
    recordAndReplayCurrentStatus = context.get(RecordAndReplayCurrentStatus.class);
    esm = new ReadWriteStorageManager(savePath, moduleEnvironment, entityManager, blockManager, extraDataManager, false, recordAndReplaySerializer, recordAndReplayUtils, recordAndReplayCurrentStatus);
    context.put(StorageManager.class, esm);
    this.character = entityManager.create();
    Client client = createClientMock(PLAYER_ID, character);
    NetworkSystem networkSystem = mock(NetworkSystem.class);
    when(networkSystem.getMode()).thenReturn(NetworkMode.NONE);
    when(networkSystem.getPlayers()).thenReturn(Arrays.asList(client));
    context.put(NetworkSystem.class, networkSystem);
    AssetManager assetManager = context.get(AssetManager.class);
    BlockFamilyDefinitionData data = new BlockFamilyDefinitionData();
    data.setBlockFamily(SymmetricFamily.class);
    assetManager.loadAsset(new ResourceUrn("test:testblock"), data, BlockFamilyDefinition.class);
    assetManager.loadAsset(new ResourceUrn("test:testblock2"), data, BlockFamilyDefinition.class);
    testBlock = context.get(BlockManager.class).getBlock("test:testblock");
    testBlock2 = context.get(BlockManager.class).getBlock("test:testblock2");
    context.put(ChunkProvider.class, mock(ChunkProvider.class));
    WorldProvider worldProvider = mock(WorldProvider.class);
    when(worldProvider.getWorldInfo()).thenReturn(new WorldInfo());
    context.put(WorldProvider.class, worldProvider);
}
Also used : EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) AssetManager(org.terasology.gestalt.assets.management.AssetManager) NetworkSystem(org.terasology.engine.network.NetworkSystem) RecordedEventStore(org.terasology.engine.recording.RecordedEventStore) ExtraBlockDataManager(org.terasology.engine.world.chunks.blockdata.ExtraBlockDataManager) ModuleManager(org.terasology.engine.core.module.ModuleManager) RecordAndReplaySerializer(org.terasology.engine.recording.RecordAndReplaySerializer) DirectionAndOriginPosRecorderList(org.terasology.engine.recording.DirectionAndOriginPosRecorderList) BlockFamilyDefinitionData(org.terasology.engine.world.block.loader.BlockFamilyDefinitionData) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) BlockManager(org.terasology.engine.world.block.BlockManager) RecordAndReplayUtils(org.terasology.engine.recording.RecordAndReplayUtils) WorldProvider(org.terasology.engine.world.WorldProvider) WorldInfo(org.terasology.engine.world.internal.WorldInfo) RecordAndReplayCurrentStatus(org.terasology.engine.recording.RecordAndReplayCurrentStatus) Client(org.terasology.engine.network.Client) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) ChunkProvider(org.terasology.engine.world.chunks.ChunkProvider) CharacterStateEventPositionMap(org.terasology.engine.recording.CharacterStateEventPositionMap) BeforeEach(org.junit.jupiter.api.BeforeEach)

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