From 5278f85fc5ede756bbbdb9f7e7207e24a2031d08 Mon Sep 17 00:00:00 2001
From: froot <66188216+kumquat-ir@users.noreply.github.com>
Date: Wed, 12 Jul 2023 22:45:07 -0400
Subject: [PATCH] Fix fluid veins being lost when switching OS (#1929)
---
.../java/gregtech/api/util/FileUtility.java | 24 ++++++++++++++++---
.../BedrockFluidVeinHandler.java | 6 +++--
.../config/BedrockFluidDepositDefinition.java | 5 +++-
.../worldgen/config/IWorldgenDefinition.java | 4 ++++
.../worldgen/config/OreDepositDefinition.java | 5 +++-
.../api/worldgen/config/WorldGenRegistry.java | 12 +++++-----
6 files changed, 43 insertions(+), 13 deletions(-)
diff --git a/src/main/java/gregtech/api/util/FileUtility.java b/src/main/java/gregtech/api/util/FileUtility.java
index c33e7135f18..7f6e82d2ede 100644
--- a/src/main/java/gregtech/api/util/FileUtility.java
+++ b/src/main/java/gregtech/api/util/FileUtility.java
@@ -146,11 +146,11 @@ public static void extractJarFiles(String resource, File targetPath, boolean rep
* @return A String of the File name at the end of the file path
*/
public static String trimFileName(String name) {
- FileSystem fs = FileSystems.getDefault();
- String separator = fs.getSeparator();
+ // this method is passed deposit names, which need to be converted first
+ name = slashToNativeSep(name);
//Remove the leading "folderName\"
- String[] tempName = name.split(Matcher.quoteReplacement(separator));
+ String[] tempName = name.split(Matcher.quoteReplacement(File.separator));
//Take the last entry in case of nested folders
String newName = tempName[tempName.length - 1];
//Remove the ".json"
@@ -164,4 +164,22 @@ public static String trimFileName(String name) {
return newName;
}
+
+ /**
+ * Converts a path string from using the filesystem's native path separator to /
+ *
+ * Useful for converting paths to consistent strings across operating systems
+ */
+ public static String nativeSepToSlash(String path) {
+ return path.replace(File.separatorChar, '/');
+ }
+
+ /**
+ * Converts a path string from using / to the filesystem's native path separator
+ *
+ * Useful for allowing paths converted with {@link FileUtility#nativeSepToSlash(String)} to be used for file i/o
+ */
+ public static String slashToNativeSep(String path) {
+ return path.replace('/', File.separatorChar);
+ }
}
diff --git a/src/main/java/gregtech/api/worldgen/bedrockFluids/BedrockFluidVeinHandler.java b/src/main/java/gregtech/api/worldgen/bedrockFluids/BedrockFluidVeinHandler.java
index 65028b0fe25..1de810721b0 100644
--- a/src/main/java/gregtech/api/worldgen/bedrockFluids/BedrockFluidVeinHandler.java
+++ b/src/main/java/gregtech/api/worldgen/bedrockFluids/BedrockFluidVeinHandler.java
@@ -2,10 +2,11 @@
import gregtech.api.GTValues;
import gregtech.api.GregTechAPI;
-import gregtech.core.network.packets.PacketFluidVeinList;
+import gregtech.api.util.FileUtility;
import gregtech.api.util.GTLog;
import gregtech.api.util.XSTR;
import gregtech.api.worldgen.config.BedrockFluidDepositDefinition;
+import gregtech.core.network.packets.PacketFluidVeinList;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
@@ -286,7 +287,8 @@ public static FluidVeinWorldEntry readFromNBT(@Nonnull NBTTagCompound tag) {
if (tag.hasKey("vein")) {
String s = tag.getString("vein");
for (BedrockFluidDepositDefinition definition : veinList.keySet()) {
- if (s.equalsIgnoreCase(definition.getDepositName()))
+ // old save data can have deposit names with native separators, get rid of those
+ if (FileUtility.nativeSepToSlash(s).equalsIgnoreCase(definition.getDepositName()))
info.vein = definition;
}
}
diff --git a/src/main/java/gregtech/api/worldgen/config/BedrockFluidDepositDefinition.java b/src/main/java/gregtech/api/worldgen/config/BedrockFluidDepositDefinition.java
index 4f351f0d7e8..cc8406b326d 100644
--- a/src/main/java/gregtech/api/worldgen/config/BedrockFluidDepositDefinition.java
+++ b/src/main/java/gregtech/api/worldgen/config/BedrockFluidDepositDefinition.java
@@ -77,7 +77,10 @@ public boolean initializeFromConfig(@Nonnull JsonObject configRoot) {
return true;
}
- //This is the file name
+ /**
+ * Must be converted using {@link gregtech.api.util.FileUtility#slashToNativeSep(String)}
+ * before it can be used as a file path
+ */
@Override
public String getDepositName() {
return depositName;
diff --git a/src/main/java/gregtech/api/worldgen/config/IWorldgenDefinition.java b/src/main/java/gregtech/api/worldgen/config/IWorldgenDefinition.java
index 3f7a53c6441..4a7765ec4fc 100644
--- a/src/main/java/gregtech/api/worldgen/config/IWorldgenDefinition.java
+++ b/src/main/java/gregtech/api/worldgen/config/IWorldgenDefinition.java
@@ -7,6 +7,10 @@
public interface IWorldgenDefinition {
//This is the file name
+ /**
+ * Must be converted using {@link gregtech.api.util.FileUtility#slashToNativeSep(String)}
+ * before it can be used as a file path
+ */
String getDepositName();
boolean initializeFromConfig(@Nonnull JsonObject configRoot);
diff --git a/src/main/java/gregtech/api/worldgen/config/OreDepositDefinition.java b/src/main/java/gregtech/api/worldgen/config/OreDepositDefinition.java
index d2b6c2a9558..73a65c0f86c 100644
--- a/src/main/java/gregtech/api/worldgen/config/OreDepositDefinition.java
+++ b/src/main/java/gregtech/api/worldgen/config/OreDepositDefinition.java
@@ -85,7 +85,10 @@ public boolean initializeFromConfig(@Nonnull JsonObject configRoot) {
return true;
}
- //This is the file name
+ /**
+ * Must be converted using {@link gregtech.api.util.FileUtility#slashToNativeSep(String)}
+ * before it can be used as a file path
+ */
@Override
public String getDepositName() {
return depositName;
diff --git a/src/main/java/gregtech/api/worldgen/config/WorldGenRegistry.java b/src/main/java/gregtech/api/worldgen/config/WorldGenRegistry.java
index ab2b8342588..ae548cf1239 100644
--- a/src/main/java/gregtech/api/worldgen/config/WorldGenRegistry.java
+++ b/src/main/java/gregtech/api/worldgen/config/WorldGenRegistry.java
@@ -248,8 +248,8 @@ public void reinitializeRegisteredVeins() throws IOException {
break;
}
- // Finds the file name to create the Definition with
- String depositName = veinPath.relativize(worldgenDefinition).toString();
+ // Finds the file name to create the Definition with, using a consistent separator character
+ String depositName = FileUtility.nativeSepToSlash(veinPath.relativize(worldgenDefinition).toString());
try {
// Creates the deposit definition and initializes various components based on the json entries in the file
@@ -279,8 +279,8 @@ public void reinitializeRegisteredVeins() throws IOException {
break;
}
- // Finds the file name to create the Definition with
- String depositName = bedrockVeinPath.relativize(worldgenDefinition).toString();
+ // Finds the file name to create the Definition with, using a consistent separator character
+ String depositName = FileUtility.nativeSepToSlash(bedrockVeinPath.relativize(worldgenDefinition).toString());
try {
// Creates the deposit definition and initializes various components based on the json entries in the file
@@ -423,7 +423,7 @@ else if (targetPath.compareTo(extractLockPath) == 0) {
private static void removeExistingFiles(Path root, @Nonnull List extends IWorldgenDefinition> definitions) {
for (IWorldgenDefinition definition : definitions) {
- Path filePath = root.resolve(Paths.get(definition.getDepositName()));
+ Path filePath = root.resolve(Paths.get(FileUtility.slashToNativeSep(definition.getDepositName())));
try {
if (Files.exists(filePath)) {
@@ -441,7 +441,7 @@ private static void addAddonFiles(Path root, @No
while (it.hasNext()) {
T definition = it.next();
- JsonObject element = FileUtility.tryExtractFromFile(root.resolve(definition.getDepositName()));
+ JsonObject element = FileUtility.tryExtractFromFile(root.resolve(FileUtility.slashToNativeSep(definition.getDepositName())));
if (element == null) {
GTLog.logger.error("Addon mod tried to register bad ore definition at {}", definition.getDepositName());