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("");
}
}
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);
}
}
}
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);
}
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);
}
}
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);
}
}
}
}
Aggregations