use of org.terasology.engine.module.ModuleManager in project Terasology by MovingBlocks.
the class ModulesMetric method updateModules.
private void updateModules() {
ModuleManager moduleManager = context.get(ModuleManager.class);
ModuleEnvironment moduleEnvironment = moduleManager.getEnvironment();
modules.clear();
Iterator<Module> iterator = moduleEnvironment.iterator();
while (iterator.hasNext()) {
Module next = iterator.next();
modules.add(next);
}
}
use of org.terasology.engine.module.ModuleManager in project Terasology by MovingBlocks.
the class WorldGeneratorManager method refresh.
public void refresh() {
ModuleManager moduleManager = context.get(ModuleManager.class);
List<WorldGeneratorInfo> infos = Lists.newArrayList();
for (Name moduleId : moduleManager.getRegistry().getModuleIds()) {
Module module = moduleManager.getRegistry().getLatestModuleVersion(moduleId);
if (module.isCodeModule()) {
DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
ResolutionResult resolutionResult = resolver.resolve(module.getId());
if (resolutionResult.isSuccess()) {
try (ModuleEnvironment tempEnvironment = moduleManager.loadEnvironment(resolutionResult.getModules(), false)) {
for (Class<?> generatorClass : tempEnvironment.getTypesAnnotatedWith(RegisterWorldGenerator.class)) {
if (tempEnvironment.getModuleProviding(generatorClass).equals(module.getId())) {
RegisterWorldGenerator annotation = generatorClass.getAnnotation(RegisterWorldGenerator.class);
if (isValidWorldGenerator(generatorClass)) {
SimpleUri uri = new SimpleUri(moduleId, annotation.id());
infos.add(new WorldGeneratorInfo(uri, annotation.displayName(), annotation.description()));
} else {
logger.error("{} marked to be registered as a World Generator, but is not a subclass of WorldGenerator or lacks the correct constructor", generatorClass);
}
}
}
} catch (Exception e) {
logger.error("Error loading world generator in module {}, skipping", module.getId(), e);
}
} else {
logger.warn("Could not resolve dependencies for module: {}", module);
}
}
}
Collections.sort(infos);
generatorInfo = ImmutableList.copyOf(infos);
}
use of org.terasology.engine.module.ModuleManager in project Terasology by MovingBlocks.
the class WorldGeneratorManager method createGenerator.
/**
* @param uri uri of the world generator to create.
* @param context objects from this context will be injected into the
* @return The instantiated world generator.
*/
public static WorldGenerator createGenerator(SimpleUri uri, Context context) throws UnresolvedWorldGeneratorException {
ModuleManager moduleManager = context.get(ModuleManager.class);
Module module = moduleManager.getEnvironment().get(uri.getModuleName());
if (module == null) {
DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
ResolutionResult result = resolver.resolve(uri.getModuleName());
if (!result.isSuccess()) {
if (moduleManager.getRegistry().getLatestModuleVersion(uri.getModuleName()) == null) {
throw new UnresolvedWorldGeneratorException("Unable to resolve world generator '" + uri + "' - not found");
} else {
throw new UnresolvedWorldGeneratorException("Unable to resolve world generator '" + uri + "' - unable to resolve module dependencies");
}
}
try (ModuleEnvironment environment = moduleManager.loadEnvironment(result.getModules(), false)) {
return createWorldGenerator(uri, context, environment);
}
} else {
return createWorldGenerator(uri, context, moduleManager.getEnvironment());
}
}
use of org.terasology.engine.module.ModuleManager in project Terasology by MovingBlocks.
the class JoinServer method step.
@Override
public boolean step() {
if (applyModuleThread != null) {
if (!applyModuleThread.isAlive()) {
if (oldEnvironment != null) {
oldEnvironment.close();
}
return true;
}
return false;
} else if (joinStatus.getStatus() == JoinStatus.Status.COMPLETE) {
Server server = networkSystem.getServer();
ServerInfoMessage serverInfo = networkSystem.getServer().getInfo();
// If no GameName, use Server IP Address
if (serverInfo.getGameName().length() > 0) {
gameManifest.setTitle(serverInfo.getGameName());
} else {
gameManifest.setTitle(server.getRemoteAddress());
}
for (WorldInfo worldInfo : serverInfo.getWorldInfoList()) {
gameManifest.addWorld(worldInfo);
}
Map<String, Short> blockMap = Maps.newHashMap();
for (Entry<Integer, String> entry : serverInfo.getBlockIds().entrySet()) {
String name = entry.getValue();
short id = entry.getKey().shortValue();
Short oldId = blockMap.put(name, id);
if (oldId != null && oldId != id) {
logger.warn("Overwriting Id {} for {} with Id {}", oldId, name, id);
}
}
Map<String, Short> biomeMap = Maps.newHashMap();
for (Entry<Short, String> entry : serverInfo.getBiomeIds().entrySet()) {
String name = entry.getValue();
short id = entry.getKey();
Short oldId = biomeMap.put(name, id);
if (oldId != null && oldId != id) {
logger.warn("Overwriting Biome Id {} for {} with Id {}", oldId, name, id);
}
}
gameManifest.setRegisteredBlockFamilies(serverInfo.getRegisterBlockFamilyList());
gameManifest.setBlockIdMap(blockMap);
gameManifest.setBiomeIdMap(biomeMap);
gameManifest.setTime(networkSystem.getServer().getInfo().getTime());
ModuleManager moduleManager = context.get(ModuleManager.class);
Set<Module> moduleSet = Sets.newLinkedHashSet();
for (NameVersion moduleInfo : networkSystem.getServer().getInfo().getModuleList()) {
Module module = moduleManager.getRegistry().getModule(moduleInfo.getName(), moduleInfo.getVersion());
if (module == null) {
StateMainMenu mainMenu = new StateMainMenu("Missing required module: " + moduleInfo);
context.get(GameEngine.class).changeState(mainMenu);
return false;
} else {
logger.info("Activating module: {}:{}", moduleInfo.getName(), moduleInfo.getVersion());
gameManifest.addModule(module.getId(), module.getVersion());
moduleSet.add(module);
}
}
oldEnvironment = moduleManager.getEnvironment();
moduleManager.loadEnvironment(moduleSet, true);
context.get(Game.class).load(gameManifest);
EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
applyModuleThread = new Thread(() -> environmentSwitchHandler.handleSwitchToGameEnvironment(context));
applyModuleThread.start();
return false;
} else if (joinStatus.getStatus() == JoinStatus.Status.FAILED) {
StateMainMenu mainMenu = new StateMainMenu("Failed to connect to server: " + joinStatus.getErrorMessage());
context.get(GameEngine.class).changeState(mainMenu);
networkSystem.shutdown();
}
return false;
}
use of org.terasology.engine.module.ModuleManager 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: {}", moduleIds);
context.get(GameEngine.class).changeState(new StateMainMenu("Missing required module or dependency"));
return true;
}
}
}
Aggregations