Skip to content

Commit

Permalink
Fix fluid veins being lost when switching OS (#1929)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumquat-ir authored Jul 13, 2023
1 parent 68f67e4 commit 5278f85
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
24 changes: 21 additions & 3 deletions src/main/java/gregtech/api/util/FileUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 /
* <br>
* 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
* <br>
* 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/gregtech/api/worldgen/config/WorldGenRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand All @@ -441,7 +441,7 @@ private static <T extends IWorldgenDefinition> 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());
Expand Down

0 comments on commit 5278f85

Please sign in to comment.