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 668a03cbf..81ec52f95 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 @@ -30,7 +30,6 @@ import java.util.function.Consumer; import java.util.function.Function; import static com.seibel.distanthorizons.core.util.FileScanUtil.LOD_FILE_POSTFIX; -import static com.seibel.distanthorizons.core.util.FileScanUtil.RENDER_FILE_POSTFIX; public class FullDataFileHandler implements IFullDataSourceProvider { @@ -40,11 +39,13 @@ public class FullDataFileHandler implements IFullDataSourceProvider protected static ExecutorService fileHandlerThreadPool; protected static ConfigChangeListener configListener; - + private final ConcurrentHashMap unloadedFiles = new ConcurrentHashMap<>(); private final ConcurrentHashMap fileBySectionPos = new ConcurrentHashMap<>(); public void ForEachFile(Consumer consumer) { this.fileBySectionPos.values().forEach(consumer); } - + + private LinkedList> onUpdatedListeners = new LinkedList<>(); + protected final IDhLevel level; protected final File saveDir; /** @@ -59,6 +60,11 @@ public class FullDataFileHandler implements IFullDataSourceProvider protected final AtomicInteger topDetailLevel = new AtomicInteger(DhSectionPos.SECTION_REGION_DETAIL_LEVEL); protected final int minDetailLevel = CompleteFullDataSource.SECTION_SIZE_OFFSET; + + //=============// + // constructor // + //=============// + public FullDataFileHandler(IDhLevel level, AbstractSaveStructure saveStructure) { this.level = level; @@ -69,7 +75,9 @@ public class FullDataFileHandler implements IFullDataSourceProvider } FileScanUtil.scanFiles(saveStructure, level.getLevelWrapper(), this, null); } - + + // constructor helpers // + /** * Caller must ensure that this method is called only once, * and that the {@link FullDataFileHandler} is not used before this method is called. @@ -179,6 +187,9 @@ public class FullDataFileHandler implements IFullDataSourceProvider } } + + + protected FullDataMetaFile getLoadOrMakeFile(DhSectionPos pos, boolean allowCreateFile) { FullDataMetaFile metaFile = this.fileBySectionPos.get(pos); @@ -414,41 +425,19 @@ public class FullDataFileHandler implements IFullDataSourceProvider } return metaFile.flushAndSaveAsync(); } - - - private LinkedList> onUpdatedListeners = new LinkedList<>(); + + @Override public synchronized void addOnUpdatedListener(Consumer listener) { this.onUpdatedListeners.add(listener); } - -// @Override -// public long getCacheVersion(DhSectionPos sectionPos) -// { -// FullDataMetaFile file = this.files.get(sectionPos); -// if (file == null) -// { -// return 0; -// } -// return file.getCacheVersion(); -// } -// @Override -// public boolean isCacheVersionValid(DhSectionPos sectionPos, long cacheVersion) -// { -// FullDataMetaFile file = this.files.get(sectionPos); -// if (file == null) -// { -// return cacheVersion >= 0; -// } -// return file.isCacheVersionValid(cacheVersion); -// } - - protected IIncompleteFullDataSource makeDataSource(DhSectionPos pos) + protected IIncompleteFullDataSource makeEmptyDataSource(DhSectionPos pos) { return pos.sectionDetailLevel <= HighDetailIncompleteFullDataSource.MAX_SECTION_DETAIL ? - HighDetailIncompleteFullDataSource.createEmpty(pos) : LowDetailIncompleteFullDataSource.createEmpty(pos); + HighDetailIncompleteFullDataSource.createEmpty(pos) : + LowDetailIncompleteFullDataSource.createEmpty(pos); } protected CompletableFuture sampleFromFiles(IIncompleteFullDataSource source, ArrayList existingFiles) @@ -486,7 +475,7 @@ public class FullDataFileHandler implements IFullDataSourceProvider public CompletableFuture onCreateDataFile(FullDataMetaFile file) { DhSectionPos pos = file.pos; - IIncompleteFullDataSource source = this.makeDataSource(pos); + IIncompleteFullDataSource source = this.makeEmptyDataSource(pos); ArrayList existFiles = new ArrayList<>(); ArrayList missing = new ArrayList<>(); this.getDataFilesForPosition(pos, pos, existFiles, missing); diff --git a/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/FullDataMetaFile.java b/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/FullDataMetaFile.java index 8cd060da7..9ec036858 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/FullDataMetaFile.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/FullDataMetaFile.java @@ -254,9 +254,9 @@ public class FullDataMetaFile extends AbstractMetaDataContainerFile implements I }, executorService)); } - private void makeCreateCompletionStage(ExecutorService executorService, CompletableFuture completer) + private void makeCreateCompletionStage(CompletableFuture completer) { - makeUpdateCompletionStage(completer, this.fullDataSourceProvider.onCreateDataFile(this) + this.makeUpdateCompletionStage(completer, this.fullDataSourceProvider.onCreateDataFile(this) .thenApply((fullDataSource) -> { this.baseMetaData = this._makeBaseMetaData(fullDataSource); @@ -273,31 +273,39 @@ public class FullDataMetaFile extends AbstractMetaDataContainerFile implements I CacheQueryResult result = this.getCachedDataSourceAsync(); - if (result.needsLoad) { - LodUtil.assertTrue(!inCrit); - inCrit = true; + if (result.needsLoad) + { + LodUtil.assertTrue(!this.inCrit); + this.inCrit = true; CompletableFuture future = result.future; // don't continue if the provider has been shut down ExecutorService executorService = this.fullDataSourceProvider.getIOExecutor(); if (executorService.isTerminated()) { - inCrit = false; - dataSourceLoadFutureRef.set(null); + this.inCrit = false; + this.dataSourceLoadFutureRef.set(null); future.complete(null); return future; } // create a new Meta file - if (!doesFileExist) { - makeCreateCompletionStage(executorService, future); + if (!this.doesFileExist) + { + this.makeCreateCompletionStage(future); } - // Otherwise, load and update file - else { - if (this.baseMetaData == null) throw new IllegalStateException("Meta data not loaded!"); - makeLoadCompletionStage(executorService, future); + else + { + // Otherwise, load and update file + if (this.baseMetaData == null) + { + throw new IllegalStateException("Meta data not loaded!"); + } + + this.makeLoadCompletionStage(executorService, future); } } + return result.future; } 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 6f9631bbf..dd315783b 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 @@ -17,7 +17,6 @@ import com.seibel.distanthorizons.core.util.LodUtil; import org.apache.logging.log4j.Logger; import javax.annotation.Nullable; -import java.io.File; import java.lang.ref.WeakReference; import java.util.*; import java.util.concurrent.*; @@ -180,8 +179,8 @@ public class GeneratedFullDataFileHandler extends FullDataFileHandler public CompletableFuture onCreateDataFile(FullDataMetaFile file) { DhSectionPos pos = file.pos; - IIncompleteFullDataSource data = makeDataSource(pos); CompletableFuture future = updateDataGenStatus(file, data); + IIncompleteFullDataSource data = makeEmptyDataSource(pos); // Cant start gen task, so return the data return future == null ? CompletableFuture.completedFuture(data) : future; }