Make configs generate paths with forwardslashes whenever possible

This commit is contained in:
Lukas Rieger (Blue) 2023-06-19 15:44:57 +02:00
parent 31ae055ae5
commit b4b3e72d51
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2

View File

@ -34,10 +34,7 @@
import de.bluecolored.bluemap.core.util.Tristate; import de.bluecolored.bluemap.core.util.Tristate;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -396,12 +393,28 @@ private ConfigTemplate createEndMapTemplate(String name, Path worldFolder, int i
} }
private String formatPath(Path path) { private String formatPath(Path path) {
return Path.of("") // normalize path
path = Path.of("")
.toAbsolutePath() .toAbsolutePath()
.relativize(path.toAbsolutePath()) .relativize(path.toAbsolutePath())
.normalize() .normalize();
.toString() String pathString = path.toString();
.replace("\\", "\\\\");
String formatted = pathString;
String separator = FileSystems.getDefault().getSeparator();
// try to replace separator with standardized forward slash
if (!separator.equals("/"))
formatted = pathString.replace(separator, "/");
// sanity check forward slash compatibility
if (!Path.of(formatted).equals(path))
formatted = pathString;
// escape all backslashes
formatted = formatted.replace("\\", "\\\\");
return formatted;
} }
} }