|
1 |
| -package com.superzanti.serversync.util; |
| 1 | +package com.superzanti.serversync.util.minecraft; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.InputStreamReader; |
3 | 6 | 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; |
4 | 19 |
|
5 | 20 | public class MinecraftModInformation implements Serializable {
|
6 | 21 | private static final long serialVersionUID = 8210520496949620158L;
|
7 | 22 | public final String version;
|
8 | 23 | public final String name;
|
9 |
| - |
10 |
| - public MinecraftModInformation(String version, String name) { |
| 24 | + |
| 25 | + private MinecraftModInformation(String version, String name) { |
11 | 26 | this.version = version;
|
12 | 27 | this.name = name;
|
13 | 28 | }
|
| 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 | + } |
14 | 107 | }
|
0 commit comments