Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 8ff2cd4

Browse files
committed
walk directories and preserve structure
1 parent eddf441 commit 8ff2cd4

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/main/java/com/dfsek/converter/ScriptConverter.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package com.dfsek.converter;
22

33
import com.dfsek.converter.dummy.DummyBlock;
4-
import com.dfsek.converter.dummy.DummySpawn;
54
import com.dfsek.converter.dummy.DummyStructure;
65

76
import java.io.BufferedWriter;
87
import java.io.File;
98
import java.io.FileInputStream;
10-
import java.io.FileOutputStream;
119
import java.io.FileWriter;
1210
import java.io.IOException;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
1313
import java.nio.file.Paths;
1414
import java.util.HashMap;
1515
import java.util.Map;
16-
import java.util.Objects;
1716

1817
public class ScriptConverter {
1918
public static void main(String[] args) throws IOException {
@@ -29,18 +28,13 @@ public static void main(String[] args) throws IOException {
2928
converter.put("[[Lcom.dfsek.terra.structure.StructureContainedBlock;", "[[Lcom.dfsek.converter.dummy.DummyBlock;");
3029
converter.put("[Lcom.dfsek.terra.structure.StructureContainedBlock;", "[Lcom.dfsek.converter.dummy.DummyBlock;");
3130

32-
File working = Paths.get(".").toFile();
31+
Path working = Paths.get(".");
3332

34-
System.out.println("Working directory: " + working.getName());
33+
System.out.println("Working directory: " + working.toFile().getAbsolutePath());
3534

36-
File[] files = working.listFiles(file -> file.getName().endsWith(".tstructure"));
37-
38-
if(files == null || files.length == 0) {
39-
System.out.println("No tstructure files discovered");
40-
return;
41-
}
42-
43-
for(File file : files) {
35+
Files.walk(working)
36+
.filter(file -> file.toFile().getName().endsWith(".tstructure")).forEach(path -> {
37+
File file = path.toFile();
4438
System.out.println("Converting " + file.getName());
4539

4640
try(MovedObjectInputStream inputStream = new MovedObjectInputStream(new FileInputStream(file), converter)) {
@@ -51,21 +45,26 @@ public static void main(String[] args) throws IOException {
5145
System.out.println("Converting structure to TerraScript...");
5246

5347
String script = buildStructure(structure);
54-
String oldName = file.getName();
48+
49+
String oldName = working.toUri().relativize(file.toURI()).getPath(); // get relative path (to preserve structure)
50+
5551
String newName = oldName.substring(0, oldName.length() - 10) + "tesf";
56-
File output = new File(working, newName);
57-
if(!output.exists()) output.createNewFile();
52+
File output = new File(working.toFile(), "out" + File.separator + newName);
53+
if(!output.exists()) {
54+
output.getParentFile().mkdirs();
55+
output.createNewFile();
56+
}
5857

5958
try(BufferedWriter writer = new BufferedWriter(new FileWriter(output))) {
6059
writer.write(script);
6160
}
62-
System.out.println("Converted to TerraScript. Saving structure to " + output.getName());
63-
} catch(ClassNotFoundException e) {
61+
System.out.println("Converted to TerraScript. Saving structure to " + output.getAbsolutePath());
62+
} catch(ClassNotFoundException | IOException e) {
6463
System.err.println("Failed to convert structure!");
6564
e.printStackTrace();
6665
System.err.println("Please report this issue.");
6766
}
68-
}
67+
});
6968
}
7069

7170
public static String buildStructure(DummyStructure structure) {

0 commit comments

Comments
 (0)