-
Notifications
You must be signed in to change notification settings - Fork 18
/
PackCompiler.java
141 lines (126 loc) · 6.57 KB
/
PackCompiler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class PackCompiler {
private static final String BASE_COMPILED_FILE = "package packID;\r\n" + "import net.minecraftforge.fml.common.Mod;\r\n" + "@Mod(\"packID\")\r\n" + "public class ForgePackLoader {public ForgePackLoader() {}}";
private static int killedDSStores = 0;
public static void main(String[] args) {
try {
File currentDir = new File(PackCompiler.class.getProtectionDomain().getCodeSource().getLocation().toURI());
//Enable for IDE where path isn't where file lies.
//currentDir = new File("D:\\MinecraftDev\\code_ocp");
boolean onWindows = System.getProperty("os.name").toLowerCase().startsWith("windows");
//Make sure the user has the java compiler installed.
String result = runCommand("javac -version");
if (!result.startsWith("javac")) {
System.out.println(result);
System.out.println("No Java compiler found. Please install a Java JDK compiler.");
return;
} else {
System.out.println("Found Java compipler, looking for packs.");
}
//Kill off any DS_Store files as they'll foul Forge.
killDSStores(new File(currentDir, "src/main"));
if (killedDSStores > 0) {
System.out.println("Compile-inator Packing Systems V2.0 found " + killedDSStores + " DS_Stores in your files. These have been sent to the shadow realm.");
}
//Remove any old packs.
File libsDir = new File(currentDir, "build/libs");
for (File file : libsDir.listFiles()) {
file.delete();
}
//Make sure ForgePackLoader.java is proper to all included mods.
List<File> packCompiledFiles = new ArrayList<>();
File packAssetDir = new File(currentDir, "src/main/resources/assets");
for (File file : packAssetDir.listFiles()) {
if (file.isDirectory()) {
String packID = file.getName();
File packCompiledFileDir = new File(currentDir, "src/main/java/" + packID);
System.out.println("Found pack with ID: " + packID);
File packCompiledFile = new File(packCompiledFileDir, "ForgePackLoader.java");
String data = BASE_COMPILED_FILE.replace("packID", packID);
BufferedWriter fileOutput = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(packCompiledFile)));
fileOutput.write(data);
fileOutput.close();
packCompiledFiles.add(packCompiledFile);
}
}
System.out.println("All files checked, compiling.");
result = runCommand(onWindows ? "gradlew.bat build" : "./gradlew build");
//Re-name compiled file, we know there will only be one, but not what it's called.
for (File file : libsDir.listFiles()) {
file.renameTo(new File(libsDir, "Pack-1.16.5.jar"));
}
//Now make a 1.12.2 compliant pack by zipping everything in the assets folder.
ZipOutputStream pack = new ZipOutputStream(new FileOutputStream(new File(libsDir, "Pack-1.12.2.jar")));
addToZip(packAssetDir, pack, packAssetDir.getAbsolutePath().length() - "assets".length());
pack.close();
System.out.println("Your pack is located in " + libsDir.getAbsolutePath().toString());
System.out.println("If you haven't already, please edit the mods.toml file, located in " + (new File(currentDir, "src/main/resources/META-INF").getAbsolutePath()));
System.out.println("This parser cannot edit or know all the various things in that file, so you must edit it before running this script.");
} catch (Exception e) {
System.out.println("Build failed!");
e.printStackTrace();
}
}
private static void killDSStores(File directory) {
for (File file : directory.listFiles()) {
if (file.isDirectory()) {
killDSStores(file);
} else if (file.getName().contains("DS_Store")) {
file.delete();
++killedDSStores;
}
}
}
private static String runCommand(String command) throws Exception {
try {
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"));
String result = "";
while (output.ready()) {
result += output.readLine() + "\n";
}
while (error.ready()) {
result += error.readLine() + "\n";
}
return result;
} catch (Exception e) {
System.out.println("Error running command: " + command);
throw e;
}
}
private static void addToZip(File directory, ZipOutputStream pack, int directoryLength) throws Exception {
for (File file : directory.listFiles()) {
try {
if (file.isDirectory()) {
addToZip(file, pack, directoryLength);
} else {
//Need to replace \ with / since parser expects URI format, not Windows.
ZipEntry entry = new ZipEntry(file.getAbsolutePath().substring(directoryLength).replace('\\', '/'));
pack.putNextEntry(entry);
FileInputStream stream = new FileInputStream(file);
byte[] bytes = new byte[1024];
int bytesRead;
while((bytesRead = stream.read(bytes)) > 0) {
pack.write(bytes, 0, bytesRead);
}
pack.closeEntry();
}
}catch (Exception e) {
System.out.println("Error zipping file: " + file);
throw e;
}
}
}
}