-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13b659b
commit 9b5a834
Showing
6 changed files
with
188 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
common/src/main/java/xyz/jpenilla/squaremap/common/config/DataFacilityType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package xyz.jpenilla.squaremap.common.config; | ||
|
||
public enum DataFacilityType { | ||
FLATFILE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
common/src/main/java/xyz/jpenilla/squaremap/common/data/facilities/DataFacility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package xyz.jpenilla.squaremap.common.data.facilities; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import xyz.jpenilla.squaremap.common.data.ChunkCoordinate; | ||
import xyz.jpenilla.squaremap.common.data.RegionCoordinate; | ||
|
||
public interface DataFacility { | ||
|
||
@Nullable Map<RegionCoordinate, Boolean> getRenderProgress(); | ||
|
||
void saveRenderProgress(Map<RegionCoordinate, Boolean> renderProgress); | ||
|
||
void deleteRenderProgress(); | ||
|
||
Set<ChunkCoordinate> getDirtyChunks(); | ||
|
||
void saveDirtyChunks(Set<ChunkCoordinate> dirtyChunks); | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
common/src/main/java/xyz/jpenilla/squaremap/common/data/facilities/DataFacilityFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package xyz.jpenilla.squaremap.common.data.facilities; | ||
|
||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.checkerframework.framework.qual.DefaultQualifier; | ||
import xyz.jpenilla.squaremap.api.WorldIdentifier; | ||
import xyz.jpenilla.squaremap.common.config.Config; | ||
|
||
@DefaultQualifier(NonNull.class) | ||
public final class DataFacilityFactory { | ||
|
||
public static DataFacility getDataFacility(WorldIdentifier identifier, String worldName) { | ||
// todo | ||
switch (Config.DATA_FACILITY_TYPE) { | ||
case FLATFILE -> new FlatfileDataFacility(identifier, worldName); | ||
} | ||
return null; | ||
} | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
common/src/main/java/xyz/jpenilla/squaremap/common/data/facilities/FlatfileDataFacility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package xyz.jpenilla.squaremap.common.data.facilities; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonIOException; | ||
import com.google.gson.JsonSyntaxException; | ||
import com.google.gson.reflect.TypeToken; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.checkerframework.framework.qual.DefaultQualifier; | ||
import xyz.jpenilla.squaremap.api.WorldIdentifier; | ||
import xyz.jpenilla.squaremap.common.Logging; | ||
import xyz.jpenilla.squaremap.common.SquaremapCommon; | ||
import xyz.jpenilla.squaremap.common.data.ChunkCoordinate; | ||
import xyz.jpenilla.squaremap.common.data.RegionCoordinate; | ||
import xyz.jpenilla.squaremap.common.util.RecordTypeAdapterFactory; | ||
|
||
@DefaultQualifier(NonNull.class) | ||
public class FlatfileDataFacility implements DataFacility { | ||
private static final String DIRTY_CHUNKS_FILE_NAME = "dirty_chunks.json"; | ||
private static final String RENDER_PROGRESS_FILE_NAME = "resume_render.json"; | ||
private static final Gson GSON = new GsonBuilder() | ||
.registerTypeAdapterFactory(new RecordTypeAdapterFactory()) | ||
.enableComplexMapKeySerialization() | ||
.create(); | ||
|
||
private final WorldIdentifier identifier; | ||
private final Path dataPath; | ||
|
||
public FlatfileDataFacility(WorldIdentifier identifier, String worldName) { | ||
this.identifier = identifier; | ||
this.dataPath = SquaremapCommon.instance().platform().dataDirectory().resolve("data").resolve(worldName); | ||
try { | ||
if (!Files.exists(this.dataPath)) { | ||
Files.createDirectories(this.dataPath); | ||
} | ||
} catch (final IOException e) { | ||
throw new IllegalStateException(String.format("Failed to create data directory for world '%s'", this.identifier), e); | ||
} | ||
} | ||
|
||
@Override | ||
public @Nullable Map<RegionCoordinate, Boolean> getRenderProgress() { | ||
try { | ||
final Path file = this.dataPath.resolve(RENDER_PROGRESS_FILE_NAME); | ||
if (Files.isRegularFile(file)) { | ||
final Type type = new TypeToken<LinkedHashMap<RegionCoordinate, Boolean>>() { | ||
}.getType(); | ||
try (final BufferedReader reader = Files.newBufferedReader(file)) { | ||
return GSON.fromJson(reader, type); | ||
} | ||
} | ||
} catch (JsonIOException | JsonSyntaxException | IOException e) { | ||
Logging.logger().warn("Failed to deserialize render progress for world '{}'", this.identifier.asString(), e); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public void saveRenderProgress(Map<RegionCoordinate, Boolean> renderProgress) { | ||
try { | ||
Files.writeString(this.dataPath.resolve(RENDER_PROGRESS_FILE_NAME), GSON.toJson(renderProgress)); | ||
} catch (IOException e) { | ||
Logging.logger().warn("Failed to serialize render progress for world '{}'", this.identifier.asString(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public void deleteRenderProgress() { | ||
try { | ||
Files.deleteIfExists(this.dataPath.resolve(RENDER_PROGRESS_FILE_NAME)); | ||
} catch (IOException e) { | ||
Logging.logger().warn("Failed to delete render progress data for world '{}'", this.identifier.asString(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public Set<ChunkCoordinate> getDirtyChunks() { | ||
Set<ChunkCoordinate> ret = ConcurrentHashMap.newKeySet(); | ||
try { | ||
final Path file = this.dataPath.resolve(DIRTY_CHUNKS_FILE_NAME); | ||
if (Files.isRegularFile(file)) { | ||
try (final BufferedReader reader = Files.newBufferedReader(file)) { | ||
ret.addAll( | ||
GSON.fromJson( | ||
reader, | ||
TypeToken.getParameterized(List.class, ChunkCoordinate.class).getType() | ||
) | ||
); | ||
} | ||
} | ||
} catch (JsonIOException | JsonSyntaxException | IOException e) { | ||
Logging.logger().warn("Failed to deserialize dirty chunks for world '{}'", this.identifier.asString(), e); | ||
} | ||
return ret; | ||
} | ||
|
||
@Override | ||
public void saveDirtyChunks(Set<ChunkCoordinate> dirtyChunks) { | ||
try { | ||
Files.writeString(this.dataPath.resolve(DIRTY_CHUNKS_FILE_NAME), GSON.toJson(dirtyChunks)); | ||
} catch (IOException e) { | ||
Logging.logger().warn("Failed to serialize dirty chunks for world '{}'", this.identifier.asString(), e); | ||
} | ||
} | ||
} |