-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathPackCompiler.java
More file actions
240 lines (222 loc) · 11.6 KB
/
PackCompiler.java
File metadata and controls
240 lines (222 loc) · 11.6 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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 {
// Parse mode flags
boolean build116 = true; // Build Forge 1.16.5 jar via Gradle
boolean build112 = true; // Build legacy assets-only jar for 1.12.2
if (args != null && args.length > 0) {
String mode = String.join(" ", args).toLowerCase();
// If a specific target is requested, disable the other
if (mode.contains("116")) {
build116 = true;
build112 = false;
} else if (mode.contains("112")) {
build116 = false;
build112 = true;
}
}
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 only if we're about to build the 1.16.5 jar
//so that a subsequent 1.12.2-only run doesn't delete prior outputs.
File libsDir = new File(currentDir, "build/libs");
if (build116 && libsDir.exists()) {
for (File file : libsDir.listFiles()) {
file.delete();
}
}
//Make sure ForgePackLoader.java is proper to all included mods.
Set<String> packIDs = new HashSet<>();
File packAssetRootDir = new File(currentDir, "src/main/resources/assets");
for (File file : packAssetRootDir.listFiles()) {
if (file.isDirectory()) {
String packID = file.getName();
File packCompiledFileDir = new File(currentDir, "src/main/java/" + packID);
File packCompiledFile = new File(packCompiledFileDir, "ForgePackLoader.java");
if (packCompiledFile.exists()) {
packIDs.add(packID);
System.out.println("Found pack with ID: " + packID);
String data = BASE_COMPILED_FILE.replace("packID", packID);
BufferedWriter fileOutput = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(packCompiledFile)));
fileOutput.write(data);
fileOutput.close();
}
}
}
//Check for pngs in the texture folder and make any item JSON models as required.
for (String packID : packIDs) {
Set<String> requiredJSONs = new HashSet<>();
File packAssetItemJSONDir = new File(packAssetRootDir, "mts/models/item");
File packAssetItemPNGDir = new File(packAssetRootDir, packID + "/textures/item");
if (packAssetItemPNGDir.exists()) {
for (File pngFile : packAssetItemPNGDir.listFiles()) {
String rawFileName = pngFile.getName().substring(0, pngFile.getName().lastIndexOf("."));
File jsonFile = new File(packAssetItemJSONDir, packID + "." + rawFileName + ".json");
requiredJSONs.add(jsonFile.getName());
if (!jsonFile.exists()) {
jsonFile.getParentFile().mkdirs();
String createdJSON = "{\"parent\":\"mts:item/basic\",\"textures\":{\"layer0\": \"" + packID + ":item/" + rawFileName + "\"}}";
BufferedWriter fileOutput = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(jsonFile)));
fileOutput.write(createdJSON);
fileOutput.close();
System.out.println("Auto-created missing item JSON for " + packID + ":" + rawFileName);
}
}
for (File jsonFile : packAssetItemJSONDir.listFiles()) {
if (!requiredJSONs.contains(jsonFile.getName())) {
jsonFile.delete();
System.out.println("Removed un-needed JSON file " + packID + ":" + jsonFile.getName());
}
}
}
}
// Determine naming components
String packVersion = readGradleString(new File(currentDir, "build.gradle"), "version");
if (packVersion == null || packVersion.isEmpty()) {
packVersion = "unknown";
}
String baseName = readGradleString(new File(currentDir, "build.gradle"), "archivesBaseName");
if (baseName == null || baseName.isEmpty()) {
baseName = "MTS Official Pack";
}
if (build116) {
System.out.println("All files checked, compiling 1.16.5.");
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.
if (libsDir.exists()) {
File[] files = libsDir.listFiles();
if (files != null) {
File target = null;
for (File file : files) {
String name = file.getName().toLowerCase();
if (name.endsWith(".jar") && !name.contains("-sources")) {
target = file;
break;
}
}
if (target != null) {
File newName = new File(libsDir, baseName + "-1.16.5-" + packVersion + ".jar");
target.renameTo(newName);
System.out.println("Named 1.16.5 jar: " + newName.getName());
}
}
}
}
if (build112) {
System.out.println("Creating 1.12.2 assets pack jar.");
if (!libsDir.exists()) {
libsDir.mkdirs();
}
ZipOutputStream pack = new ZipOutputStream(new FileOutputStream(new File(libsDir, baseName + "-1.12.2-" + packVersion + ".jar")));
addToZip(packAssetRootDir, pack, packAssetRootDir.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 String readGradleString(File buildGradle, String key) {
try {
if (!buildGradle.exists()) return null;
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(buildGradle), "UTF-8"));
String line;
Pattern p = Pattern.compile("^" + Pattern.quote(key) + "\\s*=\\s*\"([^\"]*)\"");
while ((line = reader.readLine()) != null) {
Matcher m = p.matcher(line.trim());
if (m.find()) {
reader.close();
return m.group(1);
}
}
reader.close();
} catch (Exception ignored) {}
return null;
}
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;
}
}
}
}