Search in sources :

Example 16 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 17 with ModuleEnvironment

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

the class BindsSubsystem method registerButtonBinds.

private void registerButtonBinds(ModuleEnvironment environment) {
    Iterable<Class<?>> classes = environment.getTypesAnnotatedWith(RegisterBindButton.class);
    for (Class<?> registerBindClass : classes) {
        RegisterBindButton info = registerBindClass.getAnnotation(RegisterBindButton.class);
        SimpleUri bindUri = new SimpleUri(environment.getModuleProviding(registerBindClass), info.id());
        if (BindButtonEvent.class.isAssignableFrom(registerBindClass)) {
            try {
                BindableButton bindButton = registerBindButton(bindUri, info.description(), (BindButtonEvent) registerBindClass.newInstance());
                bindButton.setMode(info.mode());
                bindButton.setRepeating(info.repeating());
                bindsConfiguration.getBinds(bindUri).stream().filter(input -> input != null).forEach(input -> linkBindButtonToInput(input, bindUri));
                logger.debug("Registered button bind: {}", bindUri);
            } catch (InstantiationException | IllegalAccessException e) {
                logger.error("Failed to register button bind \"{}\"", e);
            }
        } else {
            logger.error("Failed to register button bind \"{}\", does not extend BindButtonEvent", bindUri);
        }
    }
}
Also used : RegisterBindButton(org.terasology.engine.input.RegisterBindButton) BindsConfiguration(org.terasology.engine.config.facade.BindsConfiguration) BindAxisEvent(org.terasology.engine.input.BindAxisEvent) ModuleManager(org.terasology.engine.core.module.ModuleManager) RegisterBindAxis(org.terasology.engine.input.RegisterBindAxis) ControllerInput(org.terasology.input.ControllerInput) LoggerFactory(org.slf4j.LoggerFactory) BindsConfig(org.terasology.engine.config.BindsConfig) FromModule(org.terasology.gestalt.module.predicates.FromModule) ResolutionResult(org.terasology.gestalt.module.dependencyresolution.ResolutionResult) Collections.unmodifiableCollection(java.util.Collections.unmodifiableCollection) Lists(com.google.common.collect.Lists) DefaultBinding(org.terasology.engine.input.DefaultBinding) Map(java.util.Map) Input(org.terasology.input.Input) RegisterRealBindAxis(org.terasology.engine.input.RegisterRealBindAxis) AbstractBindableAxis(org.terasology.engine.input.internal.AbstractBindableAxis) MouseInput(org.terasology.input.MouseInput) BindableButtonImpl(org.terasology.engine.input.internal.BindableButtonImpl) InputType(org.terasology.input.InputType) Logger(org.slf4j.Logger) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) Collection(java.util.Collection) AxisEvent(org.terasology.engine.input.events.AxisEvent) BindableAxisImpl(org.terasology.engine.input.internal.BindableAxisImpl) BindButtonEvent(org.terasology.engine.input.BindButtonEvent) Maps(com.google.common.collect.Maps) BindableButton(org.terasology.engine.input.BindableButton) SimpleUri(org.terasology.engine.core.SimpleUri) EngineSubsystem(org.terasology.engine.core.subsystem.EngineSubsystem) DependencyResolver(org.terasology.gestalt.module.dependencyresolution.DependencyResolver) List(java.util.List) Context(org.terasology.engine.context.Context) Config(org.terasology.engine.config.Config) Annotation(java.lang.annotation.Annotation) BindableAxis(org.terasology.engine.input.BindableAxis) BindableRealAxis(org.terasology.engine.input.internal.BindableRealAxis) ButtonEvent(org.terasology.engine.input.events.ButtonEvent) Name(org.terasology.gestalt.naming.Name) BindableButton(org.terasology.engine.input.BindableButton) RegisterBindButton(org.terasology.engine.input.RegisterBindButton) SimpleUri(org.terasology.engine.core.SimpleUri)

Example 18 with ModuleEnvironment

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

the class BindsSubsystem method registerBinds.

@Override
public void registerBinds() {
    ModuleManager moduleManager = context.get(ModuleManager.class);
    ModuleEnvironment environment = moduleManager.getEnvironment();
    clearBinds();
    registerButtonBinds(environment);
    registerAxisBinds(environment);
    registerRealAxisBinds(environment);
}
Also used : ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) ModuleManager(org.terasology.engine.core.module.ModuleManager)

Example 19 with ModuleEnvironment

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

the class NUIManagerInternal method refreshWidgetsLibrary.

public void refreshWidgetsLibrary() {
    widgetsLibrary = new WidgetLibrary(context.get(ModuleManager.class).getEnvironment(), context.get(ReflectFactory.class), context.get(CopyStrategyLibrary.class));
    ModuleEnvironment environment = context.get(ModuleManager.class).getEnvironment();
    for (Class<? extends UIWidget> type : environment.getSubtypesOf(UIWidget.class)) {
        Name module = verifyNotNull(environment.getModuleProviding(type), "No module provides %s", type);
        widgetsLibrary.register(new ResourceUrn(module.toString(), type.getSimpleName()), type);
    }
}
Also used : ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) WidgetLibrary(org.terasology.nui.reflection.WidgetLibrary) TypeWidgetLibrary(org.terasology.nui.widgets.types.TypeWidgetLibrary) ModuleManager(org.terasology.engine.core.module.ModuleManager) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Name(org.terasology.gestalt.naming.Name)

Example 20 with ModuleEnvironment

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

the class BindsSubsystem method updateDefaultBinds.

private void updateDefaultBinds(Context passedContext, BindsConfiguration config) {
    ModuleManager moduleManager = passedContext.get(ModuleManager.class);
    DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
    for (Name moduleId : moduleManager.getRegistry().getModuleIds()) {
        ResolutionResult result = resolver.resolve(moduleId);
        if (result.isSuccess()) {
            try (ModuleEnvironment environment = moduleManager.loadEnvironment(result.getModules(), false)) {
                FromModule filter = new FromModule(environment, moduleId);
                Iterable<Class<?>> buttons = environment.getTypesAnnotatedWith(RegisterBindButton.class, filter);
                Iterable<Class<?>> axes = environment.getTypesAnnotatedWith(RegisterRealBindAxis.class, filter);
                addButtonDefaultsFor(moduleId, buttons, config);
                addAxisDefaultsFor(moduleId, axes, config);
            }
        }
    }
}
Also used : ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) ResolutionResult(org.terasology.gestalt.module.dependencyresolution.ResolutionResult) ModuleManager(org.terasology.engine.core.module.ModuleManager) FromModule(org.terasology.gestalt.module.predicates.FromModule) DependencyResolver(org.terasology.gestalt.module.dependencyresolution.DependencyResolver) Name(org.terasology.gestalt.naming.Name)

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