diff --git a/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/FullDataFileHandler.java b/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/FullDataFileHandler.java index 81ec52f95..010314c36 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/FullDataFileHandler.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/FullDataFileHandler.java @@ -248,8 +248,9 @@ public class FullDataFileHandler implements IFullDataSourceProvider return null; } - // This is a CAS with expected null value. this.topDetailLevel.updateAndGet(oldDetailLevel -> Math.max(oldDetailLevel, pos.sectionDetailLevel)); + + // This is a CAS with expected null value. FullDataMetaFile metaFileCas = this.fileBySectionPos.putIfAbsent(pos, metaFile); return metaFileCas == null ? metaFile : metaFileCas; } @@ -266,6 +267,7 @@ public class FullDataFileHandler implements IFullDataSourceProvider byte sectionDetail = posAreaToGet.sectionDetailLevel; boolean allEmpty = true; + // get all existing files for this position outerLoop: while (--sectionDetail >= this.minDetailLevel) { @@ -439,28 +441,33 @@ public class FullDataFileHandler implements IFullDataSourceProvider HighDetailIncompleteFullDataSource.createEmpty(pos) : LowDetailIncompleteFullDataSource.createEmpty(pos); } - - protected CompletableFuture sampleFromFiles(IIncompleteFullDataSource source, ArrayList existingFiles) + + /** populates the given data source using the given array of files */ + protected CompletableFuture sampleFromFileArray(IIncompleteFullDataSource recipientFullDataSource, ArrayList existingFiles) { // read in the existing data final ArrayList> loadDataFutures = new ArrayList<>(existingFiles.size()); for (FullDataMetaFile existingFile : existingFiles) { loadDataFutures.add(existingFile.loadOrGetCachedDataSourceAsync() - .exceptionally((ex) -> /*Ignore file read errors*/null) - .thenAccept((fullDataSource) -> + .exceptionally((ex) -> /*Ignore file read errors*/null) + .thenAccept((existingFullDataSource) -> + { + if (existingFullDataSource == null) { - if (fullDataSource == null) return; - //this.checkIfSectionNeedsAdditionalGeneration(pos, fullDataSource); - //LOGGER.info("Merging data from {} into {}", data.getSectionPos(), pos); - source.sampleFrom(fullDataSource); - }) + return; + } + + //LOGGER.info("Merging data from {} into {}", data.getSectionPos(), pos); + recipientFullDataSource.sampleFrom(existingFullDataSource); + }) ); } - return CompletableFuture.allOf(loadDataFutures.toArray(new CompletableFuture[0])).thenApply(v -> source); + return CompletableFuture.allOf(loadDataFutures.toArray(new CompletableFuture[0])).thenApply(voidObj -> recipientFullDataSource); } - protected void makeFiles(ArrayList posList, ArrayList output) { + protected void makeFiles(ArrayList posList, ArrayList output) + { for (DhSectionPos missingPos : posList) { FullDataMetaFile newFile = this.getLoadOrMakeFile(missingPos, true); @@ -487,8 +494,8 @@ public class FullDataFileHandler implements IFullDataSourceProvider } else { - makeFiles(missing, existFiles); - return sampleFromFiles(source, existFiles).thenApply(IIncompleteFullDataSource::tryPromotingToCompleteDataSource) + this.makeFiles(missing, existFiles); + return this.sampleFromFileArray(source, existFiles).thenApply(IIncompleteFullDataSource::tryPromotingToCompleteDataSource) .exceptionally((e) -> { FullDataMetaFile newMetaFile = this.removeCorruptedFile(pos, file, e); diff --git a/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/GeneratedFullDataFileHandler.java b/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/GeneratedFullDataFileHandler.java index dd315783b..5e9c3d96e 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/GeneratedFullDataFileHandler.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/GeneratedFullDataFileHandler.java @@ -120,7 +120,7 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler ArrayList existingFiles = new ArrayList<>(); byte sectDetailLevel = (byte) (DhSectionPos.SECTION_MINIMUM_DETAIL_LEVEL + maxSectDataDetailLevel); pos.forEachChildAtLevel(sectDetailLevel, p -> existingFiles.add(getLoadOrMakeFile(p, true))); - return sampleFromFiles(dataSource, existingFiles).thenApply(this::tryPromoteDataSource) + return sampleFromFileArray(dataSource, existingFiles).thenApply(this::tryPromoteDataSource) .exceptionally((e) -> { FullDataMetaFile newMetaFile = removeCorruptedFile(pos, file, e); @@ -152,26 +152,28 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler // Try update the gen queue on this data source. If null, then nothing was done. @Nullable - private CompletableFuture updateDataGenStatus(FullDataMetaFile file, IIncompleteFullDataSource data) + private CompletableFuture updateFromExistingDataSources(FullDataMetaFile file, IIncompleteFullDataSource data) { DhSectionPos pos = file.pos; ArrayList existingFiles = new ArrayList<>(); ArrayList missingPositions = new ArrayList<>(); this.getDataFilesForPosition(pos, pos, existingFiles, missingPositions); - - if (missingPositions.size() == 1) { + + if (missingPositions.size() == 1) + { // Only missing myself. I.e. no child file data exists yet. - return tryStartGenTask(file, data); + return this.tryStartGenTask(file, data); } - else { - // Has stuff to sample. - makeFiles(missingPositions, existingFiles); - return sampleFromFiles(data, existingFiles).thenApply(this::tryPromoteDataSource) - .exceptionally((e) -> - { - FullDataMetaFile newMetaFile = removeCorruptedFile(pos, file, e); - return null; - }); + else + { + // There are other data source files to sample from. + this.makeFiles(missingPositions, existingFiles); + return this.sampleFromFileArray(data, existingFiles).thenApply(this::tryPromoteDataSource) + .exceptionally((e) -> + { + this.removeCorruptedFile(pos, file, e); + return null; + }); } } @@ -179,8 +181,8 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler public CompletableFuture onCreateDataFile(FullDataMetaFile file) { DhSectionPos pos = file.pos; - CompletableFuture future = updateDataGenStatus(file, data); IIncompleteFullDataSource data = makeEmptyDataSource(pos); + CompletableFuture future = updateFromExistingDataSources(file, data); // Cant start gen task, so return the data return future == null ? CompletableFuture.completedFuture(data) : future; } @@ -205,12 +207,16 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler } this.fireOnGenPosSuccessListeners(source.getSectionPos()); - if (source instanceof IIncompleteFullDataSource && !file.genQueueChecked) { + if (source instanceof IIncompleteFullDataSource && !file.genQueueChecked) + { WorldGenerationQueue worldGenQueue = this.worldGenQueueRef.get(); - if (worldGenQueue != null) { - CompletableFuture future = updateDataGenStatus(file, (IIncompleteFullDataSource) source); - if (future != null) { - return future.thenApply((newSource) -> { + if (worldGenQueue != null) + { + CompletableFuture future = this.updateFromExistingDataSources(file, (IIncompleteFullDataSource) source); + if (future != null) + { + return future.thenApply((newSource) -> + { onUpdated.accept(newSource); return newSource; });