protected byte[] loadChunkZip(Vector3i chunkPos) { byte[] chunkData = null; Vector3i chunkZipPos = storagePathProvider.getChunkZipPosition(chunkPos); Path chunkPath = storagePathProvider.getChunkZipPath(chunkZipPos); if (Files.isRegularFile(chunkPath)) { try (FileSystem chunkZip = FileSystems.newFileSystem(chunkPath, null)) { Path targetChunk = chunkZip.getPath(storagePathProvider.getChunkFilename(chunkPos)); if (Files.isRegularFile(targetChunk)) { chunkData = Files.readAllBytes(targetChunk); } } catch (IOException e) { logger.error("Failed to load chunk zip {}", chunkPath, e); } } return chunkData; }
public Path getChunkZipTempPath(Vector3i chunkZipPos) { return getWorldTempPath().resolve(getChunkZipFilename(chunkZipPos)); }
private void saveGameManifest() { try { Path path = storagePathProvider.getGameManifestTempPath(); GameManifest.save(path, gameManifest); } catch (IOException e) { logger.error("Failed to save world manifest", e); } }
public Path getChunkTempPath(Vector3i chunkPos) { return getWorldTempPath().resolve(getChunkFilename(chunkPos)); }
private void writeChunkStores() throws IOException { Path chunksPath = storagePathProvider.getWorldTempPath(); Files.createDirectories(chunksPath); if (storeChunksInZips) { for (Map.Entry<Vector3i, CompressedChunkBuilder> entry : allChunks.entrySet()) { Vector3i chunkPos = entry.getKey(); Vector3i chunkZipPos = storagePathProvider.getChunkZipPosition(chunkPos); FileSystem zip = newChunkZips.get(chunkZipPos); if (zip == null) { Path targetPath = storagePathProvider.getChunkZipTempPath(chunkZipPos); Files.deleteIfExists(targetPath); zip = FileSystems.newFileSystem(URI.create("jar:" + targetPath.toUri()), CREATE_ZIP_OPTIONS); newChunkZips.put(chunkZipPos, zip); Path chunkPath = zip.getPath(storagePathProvider.getChunkFilename(chunkPos)); CompressedChunkBuilder compressedChunkBuilder = entry.getValue(); byte[] compressedChunk = compressedChunkBuilder.buildEncodedChunk(); Path oldChunkZipPath = storagePathProvider.getChunkZipPath(chunkZipPos); final FileSystem zip = chunkZipEntry.getValue(); if (Files.isRegularFile(oldChunkZipPath)) { CompressedChunkBuilder compressedChunkBuilder = entry.getValue(); byte[] compressedChunk = compressedChunkBuilder.buildEncodedChunk(); Path chunkPath = storagePathProvider.getChunkTempPath(chunkPos); try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(chunkPath))) { out.write(compressedChunk);
/** * Gets a path to next game preview image. * * @param savePath a path to saves directory * @return a path to preview image */ public static Path getNextGamePreviewImagePath(final Path savePath) { final StoragePathProvider storagePathProvider = new StoragePathProvider(savePath); final Path previewsDirPath = storagePathProvider.getPreviewsPath(); final List<Path> previewsPaths = getAllPathsToFilesInFolder(previewsDirPath); final String fileName = getNextPreviewImageName(previewsPaths); return previewsDirPath.resolve(fileName); }
public Path getChunkPath(Vector3i chunkPos) { return worldPath.resolve(getChunkFilename(chunkPos)); }
public Path getChunkZipPath(Vector3i chunkZipPos) { return worldPath.resolve(getChunkZipFilename(chunkZipPos)); }
protected byte[] loadCompressedChunk(Vector3i chunkPos) { if (isStoreChunksInZips()) { return loadChunkZip(chunkPos); } else { Path chunkPath = storagePathProvider.getChunkPath(chunkPos); if (Files.isRegularFile(chunkPath)) { try { return Files.readAllBytes(chunkPath); } catch (IOException e) { logger.error("Failed to load chunk {}", chunkPos, e); } } } return null; }
@Override public void loadGlobalStore() throws IOException { Path globalDataFile = storagePathProvider.getGlobalEntityStorePath(); if (Files.isRegularFile(globalDataFile)) { try (InputStream in = new BufferedInputStream(Files.newInputStream(globalDataFile))) { EntityData.GlobalStore store = EntityData.GlobalStore.parseFrom(in); GlobalStoreLoader loader = new GlobalStoreLoader(environment, entityManager, prefabSerializer); loader.load(store); } } }
public AbstractStorageManager(Path savePath, ModuleEnvironment environment, EngineEntityManager entityManager, BlockManager blockManager, BiomeManager biomeManager, ExtraBlockDataManager extraDataManager, boolean storeChunksInZips) { this.entityManager = entityManager; this.environment = environment; this.storeChunksInZips = storeChunksInZips; this.prefabSerializer = new PrefabSerializer(entityManager.getComponentLibrary(), entityManager.getTypeSerializerLibrary()); this.blockManager = blockManager; this.biomeManager = biomeManager; this.extraDataManager = extraDataManager; this.storagePathProvider = new StoragePathProvider(savePath); this.helper = new OwnershipHelper(entityManager.getComponentLibrary()); }
/** * Gets all images for saved game. * * @param savePath a path to saves directory * @return all preview images */ public static List<BufferedImage> getAllPreviewImages(final Path savePath) { final List<BufferedImage> result = new ArrayList<>(); final StoragePathProvider storagePathProvider = new StoragePathProvider(savePath); final Path previewsDirPath = storagePathProvider.getPreviewsPath(); for (Path previewPath : getAllPathsToFilesInFolder(previewsDirPath)) { try (InputStream in = new BufferedInputStream(Files.newInputStream(previewPath))) { result.add(ImageIO.read(in)); } catch (IOException ex) { logger.warn("Can't load an image", ex); } } result.removeIf(Objects::isNull); return result; }