public void applyBinds(InputSystem inputSystem, ModuleManager moduleManager) { inputSystem.clearBinds(); ModuleEnvironment env = moduleManager.getEnvironment(); registerButtonBinds(inputSystem, env, env.getTypesAnnotatedWith(RegisterBindButton.class)); registerAxisBinds(inputSystem, env, env.getTypesAnnotatedWith(RegisterBindAxis.class)); registerRealAxisBinds(inputSystem, env, env.getTypesAnnotatedWith(RegisterRealBindAxis.class)); }
private void registerButtonBinds( InputSystem inputSystem, ModuleEnvironment environment, Iterable<Class<?>> classes) { 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 = inputSystem.registerBindButton( bindUri, info.description(), (BindButtonEvent) registerBindClass.newInstance()); bindButton.setMode(info.mode()); bindButton.setRepeating(info.repeating()); for (Input input : getBinds(bindUri)) { if (input != null) { inputSystem.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); } } }
/** * Updates a config with any binds that it may be missing, through reflection over * RegisterBindButton annotations */ public void updateForChangedMods(Context context) { ModuleManager moduleManager = context.get(ModuleManager.class); DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry()); for (Name moduleId : moduleManager.getRegistry().getModuleIds()) { if (moduleManager.getRegistry().getLatestModuleVersion(moduleId).isCodeModule()) { ResolutionResult result = resolver.resolve(moduleId); if (result.isSuccess()) { try (ModuleEnvironment environment = moduleManager.loadEnvironment(result.getModules(), false)) { updateInputsFor( moduleId, environment.getTypesAnnotatedWith( RegisterBindButton.class, new FromModule(environment, moduleId))); } } } } }
/** * @return A new BindsConfig, with inputs set from the DefaultBinding annotations on bind classes */ public static BindsConfig createDefault(Context context) { ModuleManager moduleManager = context.get(ModuleManager.class); BindsConfig config = new BindsConfig(); DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry()); for (Name moduleId : moduleManager.getRegistry().getModuleIds()) { if (moduleManager.getRegistry().getLatestModuleVersion(moduleId).isCodeModule()) { ResolutionResult result = resolver.resolve(moduleId); if (result.isSuccess()) { try (ModuleEnvironment environment = moduleManager.loadEnvironment(result.getModules(), false)) { config.addDefaultsFor( moduleId, environment.getTypesAnnotatedWith( RegisterBindButton.class, new FromModule(environment, moduleId))); } } } } return config; }
/** * Updates a config with any binds that it may be missing, through reflection over * RegisterBindButton annotations */ public void updateForChangedMods(Context context) { ModuleManager moduleManager = context.get(ModuleManager.class); DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry()); for (Name moduleId : moduleManager.getRegistry().getModuleIds()) { if (moduleManager.getRegistry().getLatestModuleVersion(moduleId).isCodeModule()) { 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); updateButtonInputsFor(moduleId, buttons); updateAxisInputsFor(moduleId, axes); } } } } }
@Override public ModuleEnvironment loadEnvironment(Set<Module> modules, boolean asPrimary) { Set<Module> finalModules = Sets.newLinkedHashSet(modules); for (Module module : registry) { if (module.isOnClasspath()) { finalModules.add(module); } } ModuleEnvironment newEnvironment = new ModuleEnvironment( finalModules, moduleSecurityManager, Collections.<BytecodeInjector>emptyList()); if (asPrimary) { if (environment != null) { environment.close(); } environment = newEnvironment; } return newEnvironment; }
private void registerAxisBinds( InputSystem inputSystem, ModuleEnvironment environment, Iterable<Class<?>> classes) { for (Class<?> registerBindClass : classes) { RegisterBindAxis info = registerBindClass.getAnnotation(RegisterBindAxis.class); Name moduleId = environment.getModuleProviding(registerBindClass); SimpleUri id = new SimpleUri(moduleId, info.id()); if (BindAxisEvent.class.isAssignableFrom(registerBindClass)) { BindableButton positiveButton = inputSystem.getBindButton(new SimpleUri(info.positiveButton())); BindableButton negativeButton = inputSystem.getBindButton(new SimpleUri(info.negativeButton())); if (positiveButton == null) { logger.warn( "Failed to register axis \"{}\", missing positive button \"{}\"", id, info.positiveButton()); continue; } if (negativeButton == null) { logger.warn( "Failed to register axis \"{}\", missing negative button \"{}\"", id, info.negativeButton()); continue; } try { BindableAxis bindAxis = inputSystem.registerBindAxis( id.toString(), (BindAxisEvent) registerBindClass.newInstance(), positiveButton, negativeButton); bindAxis.setSendEventMode(info.eventMode()); logger.debug("Registered axis bind: {}", id); } catch (InstantiationException | IllegalAccessException e) { logger.error("Failed to register axis bind \"{}\"", id, e); } } else { logger.error("Failed to register axis bind \"{}\", does not extend BindAxisEvent", id); } } }
private void registerRealAxisBinds( InputSystem inputSystem, ModuleEnvironment environment, Iterable<Class<?>> classes) { for (Class<?> registerBindClass : classes) { RegisterRealBindAxis info = registerBindClass.getAnnotation(RegisterRealBindAxis.class); Name moduleId = environment.getModuleProviding(registerBindClass); SimpleUri id = new SimpleUri(moduleId, info.id()); if (BindAxisEvent.class.isAssignableFrom(registerBindClass)) { try { BindAxisEvent instance = (BindAxisEvent) registerBindClass.newInstance(); BindableAxis bindAxis = inputSystem.registerRealBindAxis(id.toString(), instance); bindAxis.setSendEventMode(info.eventMode()); for (Input input : getBinds(id)) { inputSystem.linkAxisToInput(input, id); } logger.debug("Registered axis bind: {}", id); } catch (InstantiationException | IllegalAccessException e) { logger.error("Failed to register axis bind \"{}\"", id, e); } } else { logger.error("Failed to register axis bind \"{}\", does not extend BindAxisEvent", id); } } }