Skip to content

Commit c2d87f8

Browse files
committed
NoIssue: Move modinfo population
This functionality should really be a part of the MinecraftModInformation class rather than SyncFile
1 parent 9d43d7b commit c2d87f8

File tree

2 files changed

+98
-87
lines changed

2 files changed

+98
-87
lines changed

src/main/java/com/superzanti/serversync/util/SyncFile.java

+2-84
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
package com.superzanti.serversync.util;
22

33
import java.io.File;
4-
import java.io.IOException;
5-
import java.io.InputStream;
6-
import java.io.InputStreamReader;
74
import java.io.Serializable;
85
import java.nio.file.Files;
96
import java.nio.file.Path;
107
import java.nio.file.Paths;
118
import java.util.ArrayList;
129
import java.util.List;
13-
import java.util.jar.JarEntry;
14-
import java.util.jar.JarFile;
1510

16-
import com.google.gson.JsonArray;
17-
import com.google.gson.JsonElement;
18-
import com.google.gson.JsonObject;
19-
import com.google.gson.JsonParseException;
20-
import com.google.gson.JsonStreamParser;
2111
import com.superzanti.serversync.util.errors.InvalidSyncFileException;
12+
import com.superzanti.serversync.util.minecraft.MinecraftModInformation;
2213

2314
/**
2415
* Holds all relevant information about a synchronizable file, also handles
@@ -144,80 +135,7 @@ private boolean isZipJar(String fileName) {
144135

145136
private void populateModInformation() {
146137
if (Files.exists(this.getFileAsPath()) && this.isZipJar(this.synchronizableFile.getName())) {
147-
InputStream is = null;
148-
InputStreamReader read = null;
149-
JsonStreamParser parser = null;
150-
JarFile packagedMod = null;
151-
152-
try {
153-
ArrayList<String> infoTests = new ArrayList<>(2);
154-
infoTests.add("mcmod.info");
155-
infoTests.add("neimod.info");
156-
packagedMod = new JarFile(this.synchronizableFile);
157-
JarEntry modInfo = null;
158-
159-
for (String test : infoTests) {
160-
modInfo = packagedMod.getJarEntry(test);
161-
162-
if (modInfo != null) {
163-
break;
164-
}
165-
}
166-
167-
if (modInfo != null) {
168-
is = packagedMod.getInputStream(modInfo);
169-
read = new InputStreamReader(is);
170-
parser = new JsonStreamParser(read);
171-
172-
while (parser.hasNext()) {
173-
JsonElement element = parser.next();
174-
if (element.isJsonArray()) {
175-
// This will be the opening document array
176-
JsonArray jArray = element.getAsJsonArray();
177-
178-
// Get each array of objects
179-
// array 1 {"foo":"bar"}, array 2 {"foo":"bar"}
180-
for (JsonElement jObject : jArray) {
181-
// This will contain all of the mod info
182-
JsonObject info = jObject.getAsJsonObject();
183-
184-
// Skip conditions /////////////////////////////
185-
if (info == null) {
186-
continue;
187-
}
188-
189-
if (!info.has("version") || !info.has("name")) {
190-
continue;
191-
}
192-
193-
this.minecraftInformation = new MinecraftModInformation(
194-
info.get("version").getAsString(), info.get("name").getAsString());
195-
}
196-
}
197-
}
198-
199-
read.close();
200-
is.close();
201-
packagedMod.close();
202-
}
203-
204-
} catch (JsonParseException e) {
205-
System.out
206-
.println("File: " + this.synchronizableFile.getName() + " failed to parse mcmod.info as JSON");
207-
} catch (IOException e) {
208-
e.printStackTrace();
209-
} finally {
210-
try {
211-
if (read != null)
212-
read.close();
213-
if (is != null)
214-
is.close();
215-
if (packagedMod != null)
216-
packagedMod.close();
217-
} catch (IOException e) {
218-
e.printStackTrace();
219-
}
220-
}
138+
MinecraftModInformation.fromFile(this.getFileAsPath());
221139
} else {
222140
System.out.println("File: " + this.synchronizableFile.getName()
223141
+ " not recognized as a minecraft mod (not a problem)");
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,107 @@
1-
package com.superzanti.serversync.util;
1+
package com.superzanti.serversync.util.minecraft;
22

3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.InputStreamReader;
36
import java.io.Serializable;
7+
import java.nio.file.Path;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.jar.JarEntry;
11+
import java.util.jar.JarFile;
12+
13+
import com.google.gson.JsonArray;
14+
import com.google.gson.JsonElement;
15+
import com.google.gson.JsonObject;
16+
import com.google.gson.JsonStreamParser;
17+
import com.superzanti.serversync.util.AutoClose;
18+
import com.superzanti.serversync.util.Logger;
419

520
public class MinecraftModInformation implements Serializable {
621
private static final long serialVersionUID = 8210520496949620158L;
722
public final String version;
823
public final String name;
9-
10-
public MinecraftModInformation(String version, String name) {
24+
25+
private MinecraftModInformation(String version, String name) {
1126
this.version = version;
1227
this.name = name;
1328
}
29+
30+
public static MinecraftModInformation fromFile(Path path) {
31+
JarFile packagedMod = null;
32+
try {
33+
packagedMod = new JarFile(path.toFile());
34+
} catch (IOException e) {
35+
e.printStackTrace();
36+
}
37+
38+
JarEntry modInfoEntry = null;
39+
40+
List<String> validModInfoFiles = Arrays.asList("mcmod.info", "neimod.info");
41+
for (String fileName : validModInfoFiles) {
42+
try {
43+
modInfoEntry = packagedMod.getJarEntry(fileName);
44+
} catch (IllegalStateException e) {
45+
e.printStackTrace();
46+
}
47+
48+
if (modInfoEntry != null) {
49+
break;
50+
}
51+
}
52+
53+
if (modInfoEntry != null) {
54+
InputStream is = null;
55+
try {
56+
is = packagedMod.getInputStream(modInfoEntry);
57+
} catch (IOException e) {
58+
e.printStackTrace();
59+
}
60+
61+
InputStreamReader read = new InputStreamReader(is);
62+
JsonStreamParser parser = new JsonStreamParser(read);
63+
MinecraftModInformation modInformation = null;
64+
List<String> desiredFields = Arrays.asList("version", "name");
65+
66+
while (parser.hasNext()) {
67+
JsonElement element = parser.next();
68+
if (element.isJsonArray()) {
69+
// This will be the opening document array
70+
JsonArray jArray = element.getAsJsonArray();
71+
72+
// Get each array of objects
73+
// array 1 {"foo":"bar"}, array 2 {"foo":"bar"}
74+
for (JsonElement jObject : jArray) {
75+
// This will contain all of the mod info
76+
JsonObject info = jObject.getAsJsonObject();
77+
78+
// Skip conditions /////////////////////////////
79+
if (info == null) {
80+
continue;
81+
}
82+
83+
// At the moment we only care about these two entries in the information file
84+
if (desiredFields.stream().allMatch(info::has)) {
85+
modInformation = new MinecraftModInformation(info.get("version").getAsString(),
86+
info.get("name").getAsString());
87+
break;
88+
}
89+
}
90+
}
91+
}
92+
93+
AutoClose.closeResource(read, is, packagedMod);
94+
95+
if (modInformation == null) {
96+
Logger.debug(String.format("Could not find the desired fields in the mod information file: %s",
97+
String.join(",", desiredFields)));
98+
return new MinecraftModInformation("", "");
99+
}
100+
return modInformation;
101+
} else {
102+
Logger.log(String.format("Could not find a mod information file that matches: %s",
103+
String.join(",", validModInfoFiles)));
104+
return null;
105+
}
106+
}
14107
}

0 commit comments

Comments
 (0)