private void addGameManifestToSaveTransaction(SaveTransactionBuilder saveTransactionBuilder) {
    BlockManager blockManager = CoreRegistry.get(BlockManager.class);
    BiomeManager biomeManager = CoreRegistry.get(BiomeManager.class);
    WorldProvider worldProvider = CoreRegistry.get(WorldProvider.class);
    Time time = CoreRegistry.get(Time.class);
    Game game = CoreRegistry.get(Game.class);

    GameManifest gameManifest =
        new GameManifest(game.getName(), game.getSeed(), time.getGameTimeInMs());
    for (Module module : CoreRegistry.get(ModuleManager.class).getEnvironment()) {
      gameManifest.addModule(module.getId(), module.getVersion());
    }

    List<String> registeredBlockFamilies = Lists.newArrayList();
    for (BlockFamily family : blockManager.listRegisteredBlockFamilies()) {
      registeredBlockFamilies.add(family.getURI().toString());
    }
    gameManifest.setRegisteredBlockFamilies(registeredBlockFamilies);
    gameManifest.setBlockIdMap(blockManager.getBlockIdMap());
    List<Biome> biomes = biomeManager.getBiomes();
    Map<String, Short> biomeIdMap = new HashMap<>(biomes.size());
    for (Biome biome : biomes) {
      short shortId = biomeManager.getBiomeShortId(biome);
      String id = biomeManager.getBiomeId(biome);
      biomeIdMap.put(id, shortId);
    }
    gameManifest.setBiomeIdMap(biomeIdMap);
    gameManifest.addWorld(worldProvider.getWorldInfo());
    saveTransactionBuilder.setGameManifest(gameManifest);
  }
  private void sendModules(List<NetData.ModuleRequest> moduleRequestList) {
    for (NetData.ModuleRequest request : moduleRequestList) {
      NetData.ModuleDataHeader.Builder result = NetData.ModuleDataHeader.newBuilder();
      result.setId(request.getModuleId());
      Module module = moduleManager.getEnvironment().get(new Name(request.getModuleId()));
      if (module.isOnClasspath()
          || module.getLocations().size() != 1
          || !Files.isReadable(module.getLocations().get(0))) {
        result.setError("Module not available for download");
      } else {
        Path location = module.getLocations().get(0);
        try {
          result.setVersion(module.getVersion().toString());
          result.setSize(Files.size(location));
          channelHandlerContext
              .getChannel()
              .write(NetData.NetMessage.newBuilder().setModuleDataHeader(result).build());
        } catch (IOException e) {
          logger.error("Error sending module data header", e);
          channelHandlerContext.getChannel().close();
          break;
        }

        try (InputStream stream = new BufferedInputStream(Files.newInputStream(location))) {

          long remainingData = Files.size(location);
          byte[] data = new byte[1024];
          while (remainingData > 0) {
            int nextBlock = (int) Math.min(remainingData, 1024);
            ByteStreams.read(stream, data, 0, nextBlock);
            channelHandlerContext
                .getChannel()
                .write(
                    NetData.NetMessage.newBuilder()
                        .setModuleData(
                            NetData.ModuleData.newBuilder()
                                .setModule(ByteString.copyFrom(data, 0, nextBlock)))
                        .build());
            remainingData -= nextBlock;
          }
        } catch (IOException e) {
          logger.error("Error sending module", e);
          channelHandlerContext.getChannel().close();
          break;
        }
      }
    }
  }
Пример #3
0
  public ModuleManagerImpl() {
    metadataReader = new ModuleMetadataReader();
    for (ModuleExtension ext : StandardModuleExtension.values()) {
      metadataReader.registerExtension(ext.getKey(), ext.getValueType());
    }
    Module engineModule;
    try (Reader reader =
        new InputStreamReader(
            getClass().getResourceAsStream("/engine-module.txt"), TerasologyConstants.CHARSET)) {
      ModuleMetadata metadata = metadataReader.read(reader);
      engineModule = ClasspathModule.create(metadata, getClass(), Module.class);
    } catch (IOException e) {
      throw new RuntimeException("Failed to read engine metadata", e);
    } catch (URISyntaxException e) {
      throw new RuntimeException("Failed to convert engine library location to path", e);
    }

    registry = new TableModuleRegistry();
    registry.add(engineModule);
    ModulePathScanner scanner = new ModulePathScanner(new ModuleLoader(metadataReader));
    scanner.getModuleLoader().setModuleInfoPath(TerasologyConstants.MODULE_INFO_FILENAME);
    scanner.scan(registry, PathManager.getInstance().getModulePaths());

    DependencyInfo engineDep = new DependencyInfo();
    engineDep.setId(engineModule.getId());
    engineDep.setMinVersion(engineModule.getVersion());
    engineDep.setMaxVersion(engineModule.getVersion().getNextPatchVersion());

    for (Module mod : registry) {
      if (mod != engineModule) {
        mod.getMetadata().getDependencies().add(engineDep);
      }
    }

    setupSandbox();
    loadEnvironment(Sets.newHashSet(engineModule), true);
  }