use of org.terasology.gestalt.module.ModuleEnvironment in project Terasology by MovingBlocks.
the class RegisterMods method step.
@Override
public boolean step() {
if (applyModulesThread != null) {
if (!applyModulesThread.isAlive()) {
if (oldEnvironment != null) {
oldEnvironment.close();
}
return true;
}
return false;
} else {
ModuleManager moduleManager = context.get(ModuleManager.class);
List<Name> moduleIds = gameManifest.getModules().stream().map(NameVersion::getName).collect(Collectors.toCollection(ArrayList::new));
DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
ResolutionResult result = resolver.resolve(moduleIds);
if (result.isSuccess()) {
oldEnvironment = moduleManager.getEnvironment();
ModuleEnvironment env = moduleManager.loadEnvironment(result.getModules(), true);
for (Module moduleInfo : env.getModulesOrderedByDependencies()) {
logger.info("Activating module: {}:{}", moduleInfo.getId(), moduleInfo.getVersion());
}
EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
applyModulesThread = new Thread(() -> environmentSwitchHandler.handleSwitchToGameEnvironment(context));
applyModulesThread.start();
return false;
} else {
logger.warn("Missing at least one required module (or dependency) from the following list: {}", moduleIds);
context.get(GameEngine.class).changeState(new StateMainMenu("Missing required module or dependency"));
return true;
}
}
}
use of org.terasology.gestalt.module.ModuleEnvironment in project Terasology by MovingBlocks.
the class ModuleManager method loadEnvironment.
public ModuleEnvironment loadEnvironment(Set<Module> modules, boolean asPrimary) {
Set<Module> finalModules = Sets.newLinkedHashSet(modules);
finalModules.add(engineModule);
ModuleEnvironment newEnvironment;
boolean permissiveSecurityEnabled = Boolean.parseBoolean(System.getProperty(SystemConfig.PERMISSIVE_SECURITY_ENABLED_PROPERTY));
if (permissiveSecurityEnabled) {
newEnvironment = new ModuleEnvironment(finalModules, wrappingPermissionProviderFactory);
} else {
newEnvironment = new ModuleEnvironment(finalModules, permissionProviderFactory);
}
if (asPrimary) {
environment = newEnvironment;
}
return newEnvironment;
}
use of org.terasology.gestalt.module.ModuleEnvironment in project Terasology by MovingBlocks.
the class ExtraBlockDataManager method getFieldsFromAnnotations.
// Find requests for extensions and which blocks they apply to.
private Map<Integer, Map<String, Set<Block>>> getFieldsFromAnnotations(Context context) {
ModuleEnvironment environment = context.get(ModuleManager.class).getEnvironment();
Collection<Block> blocks = context.get(BlockManager.class).listRegisteredBlocks();
Map<Integer, Map<String, Set<Block>>> fieldss = new HashMap<>();
TERA_ARRAY_FACTORIES.forEach((size, fac) -> fieldss.put(size, new HashMap<>()));
for (Class<?> type : environment.getTypesAnnotatedWith(ExtraDataSystem.class)) {
for (Method method : type.getMethods()) {
RegisterExtraData registerAnnotation = method.getAnnotation(RegisterExtraData.class);
if (registerAnnotation != null) {
String errorType = validRegistrationMethod(method, registerAnnotation);
if (errorType != null) {
logger.error("Unable to register extra block data: " + errorType + " for {}.{}: should be \"public static boolean {}(Block block)\", and bitSize should be 4, 8 or 16.", type.getName(), method.getName(), method.getName());
continue;
}
method.setAccessible(true);
Set<Block> includedBlocks = new HashSet<>();
for (Block block : blocks) {
try {
if ((boolean) method.invoke(null, block)) {
includedBlocks.add(block);
}
} catch (IllegalAccessException e) {
// This should not get to this point.
throw new RuntimeException("Incorrect access modifier on register extra data method", e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
fieldss.get(registerAnnotation.bitSize()).put(registerAnnotation.name(), includedBlocks);
}
}
}
return fieldss;
}
use of org.terasology.gestalt.module.ModuleEnvironment 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();
}
use of org.terasology.gestalt.module.ModuleEnvironment in project Terasology by MovingBlocks.
the class EnvironmentSwitchHandler method handleSwitchBackFromPreviewEnvironment.
public void handleSwitchBackFromPreviewEnvironment(Context context) {
// The newly created ComponentLibrary instance cannot be invalidated in context
ModuleEnvironment environment = context.get(ModuleManager.class).getEnvironment();
cheapAssetManagerUpdate(context, environment);
}
Aggregations