|
| 1 | +package cc.woverflow.crashpatch.hooks; |
| 2 | + |
| 3 | +import cc.woverflow.onecore.tweaker.OneCoreTweaker; |
| 4 | +import com.google.common.collect.Lists; |
| 5 | +import com.google.gson.JsonObject; |
| 6 | +import com.google.gson.JsonParser; |
| 7 | +import com.google.gson.stream.MalformedJsonException; |
| 8 | +import net.minecraft.launchwrapper.Launch; |
| 9 | +import net.minecraft.launchwrapper.LaunchClassLoader; |
| 10 | +import net.minecraftforge.fml.common.versioning.DefaultArtifactVersion; |
| 11 | +import org.apache.commons.lang3.StringUtils; |
| 12 | + |
| 13 | +import javax.swing.*; |
| 14 | +import java.awt.*; |
| 15 | +import java.io.File; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.lang.reflect.InvocationTargetException; |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.List; |
| 21 | +import java.util.*; |
| 22 | +import java.util.stream.Collectors; |
| 23 | +import java.util.zip.ZipEntry; |
| 24 | +import java.util.zip.ZipFile; |
| 25 | + |
| 26 | +public class ModsCheckerPlugin extends OneCoreTweaker { |
| 27 | + private static final JsonParser PARSER = new JsonParser(); |
| 28 | + public static final HashMap<String, Triple<File, String, String>> modsMap = new HashMap<>(); //modid : file, version, name |
| 29 | + |
| 30 | + @Override |
| 31 | + public void injectIntoClassLoader(LaunchClassLoader classLoader) { |
| 32 | + File modsFolder = new File(Launch.minecraftHome, "mods"); |
| 33 | + File[] modFolder = modsFolder.listFiles((dir, name) -> name.endsWith(".jar")); |
| 34 | + HashMap<String, ArrayList<Triple<File, String, String>>> dupeMap = new HashMap<>(); |
| 35 | + if (modFolder != null) { |
| 36 | + for (File file : modFolder) { |
| 37 | + try { |
| 38 | + try (ZipFile mod = new ZipFile(file)) { |
| 39 | + ZipEntry entry = mod.getEntry("mcmod.info"); |
| 40 | + if (entry != null) { |
| 41 | + try (InputStream inputStream = mod.getInputStream(entry)) { |
| 42 | + byte[] availableBytes = new byte[inputStream.available()]; |
| 43 | + inputStream.read(availableBytes, 0, inputStream.available()); |
| 44 | + JsonObject modInfo = PARSER.parse(new String(availableBytes)).getAsJsonArray().get(0).getAsJsonObject(); |
| 45 | + if (!modInfo.has("modid") || !modInfo.has("version")) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + String modid = modInfo.get("modid").getAsString(); |
| 50 | + if (modsMap.containsKey(modid)) { |
| 51 | + if (dupeMap.containsKey(modid)) { |
| 52 | + dupeMap.get(modid).add(new Triple<>(file, modInfo.get("version").getAsString(), modInfo.has("name") ? modInfo.get("name").getAsString() : modid)); |
| 53 | + } else { |
| 54 | + dupeMap.put(modid, Lists.newArrayList(modsMap.get(modid), new Triple<>(file, modInfo.get("version").getAsString(), modInfo.has("name") ? modInfo.get("name").getAsString() : modid))); |
| 55 | + } |
| 56 | + } else { |
| 57 | + modsMap.put(modid, new Triple<>(file, modInfo.get("version").getAsString(), modInfo.has("name") ? modInfo.get("name").getAsString() : modid)); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } catch (MalformedJsonException | IllegalStateException ignored) { |
| 63 | + } catch (Exception e) { |
| 64 | + e.printStackTrace(); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + Iterator<ArrayList<Triple<File, String, String>>> iterator = dupeMap.values().iterator(); |
| 70 | + |
| 71 | + while (iterator.hasNext()) { |
| 72 | + try { |
| 73 | + ArrayList<Triple<File, String, String>> next = iterator.next(); |
| 74 | + List<Triple<File, String, String>> blank = next.stream().sorted((a, b) -> { |
| 75 | + if (a != null && b != null) { |
| 76 | + try { |
| 77 | + int value = new DefaultArtifactVersion(substringBeforeAny(a.second, "-beta", "-alpha", "-pre", "+beta", "+alpha", "+pre")).compareTo(new DefaultArtifactVersion(substringBeforeAny(b.second, "-beta", "-alpha", "-pre", "+beta", "+alpha", "+pre"))); |
| 78 | + return -value; |
| 79 | + } catch (Exception e) { |
| 80 | + e.printStackTrace(); |
| 81 | + try { |
| 82 | + String[] array = {a.second, b.second}; |
| 83 | + Arrays.sort(array); |
| 84 | + return array[0].equals(a.second) ? -1 : Objects.equals(a.second, b.second) ? 0 : 1; |
| 85 | + } catch (Exception ex) { |
| 86 | + ex.printStackTrace(); |
| 87 | + return 0; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return 0; |
| 92 | + }).collect(Collectors.toList()); |
| 93 | + next.clear(); |
| 94 | + next.addAll(blank); |
| 95 | + ListIterator<Triple<File, String, String>> otherIterator = next.listIterator(); |
| 96 | + int index = 0; |
| 97 | + while (otherIterator.hasNext()) { |
| 98 | + Triple<File, String, String> remove = otherIterator.next(); |
| 99 | + ++index; |
| 100 | + if (index != 1) { |
| 101 | + tryDeleting(remove.first); |
| 102 | + otherIterator.remove(); |
| 103 | + } |
| 104 | + } |
| 105 | + if (next.size() <= 1) { |
| 106 | + iterator.remove(); |
| 107 | + } |
| 108 | + } catch (Exception e) { |
| 109 | + e.printStackTrace(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (modsMap.containsKey("itlt")) { |
| 114 | + tryDeleting(modsMap.get("itlt").first); |
| 115 | + } |
| 116 | + if (modsMap.containsKey("custommainmenu")) { |
| 117 | + tryDeleting(modsMap.get("custommainmenu").first); |
| 118 | + } |
| 119 | + if (!dupeMap.isEmpty()) { |
| 120 | + try { |
| 121 | + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
| 122 | + } catch (Exception e) { |
| 123 | + e.printStackTrace(); |
| 124 | + } |
| 125 | + |
| 126 | + DesktopManager.open(modsFolder); |
| 127 | + JOptionPane.showMessageDialog(null, "Duplicate mods have been detected! These mods are...\n" + |
| 128 | + getStringOf(dupeMap.values()) + "\nPlease removes these mods from your mod folder, which is opened. Go to https://inv.wtf/skyclient for more info.", "Duplicate Mods Detected!", JOptionPane.ERROR_MESSAGE); |
| 129 | + try { |
| 130 | + Class<?> exitClass = Class.forName("java.lang.Shutdown"); |
| 131 | + Method exit = exitClass.getDeclaredMethod("exit", int.class); |
| 132 | + exit.setAccessible(true); |
| 133 | + exit.invoke(null, 0); |
| 134 | + } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 135 | + e.printStackTrace(); |
| 136 | + } |
| 137 | + } |
| 138 | + super.injectIntoClassLoader(classLoader); |
| 139 | + } |
| 140 | + |
| 141 | + private String getStringOf(Collection<ArrayList<Triple<File, String, String>>> dupes) { |
| 142 | + StringBuilder builder = new StringBuilder(); |
| 143 | + for (ArrayList<Triple<File, String, String>> list : dupes) { |
| 144 | + builder.append("\n"); |
| 145 | + for (Triple<File, String, String> triple : list) { |
| 146 | + builder.append(" ").append(triple.first.getAbsolutePath()); |
| 147 | + } |
| 148 | + } |
| 149 | + return builder.toString().trim(); |
| 150 | + } |
| 151 | + |
| 152 | + private String substringBeforeAny(String string, String... values) { |
| 153 | + String returnString = string; |
| 154 | + for (String value : values) { |
| 155 | + if (returnString.contains(value)) { |
| 156 | + returnString = StringUtils.substringBefore(returnString, value); |
| 157 | + } |
| 158 | + } |
| 159 | + return returnString; |
| 160 | + } |
| 161 | + |
| 162 | + private void tryDeleting(File file) { |
| 163 | + if (!file.delete()) { |
| 164 | + if (!file.delete()) { |
| 165 | + if (!file.delete()) { |
| 166 | + file.deleteOnExit(); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + public static class Triple<A, B, C> { |
| 173 | + public A first; |
| 174 | + public B second; |
| 175 | + public C third; |
| 176 | + |
| 177 | + public Triple(A a, B b, C c) { |
| 178 | + first = a; |
| 179 | + second = b; |
| 180 | + third = c; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public String toString() { |
| 185 | + return "Triple{" + |
| 186 | + "first=" + first + |
| 187 | + ", second=" + second + |
| 188 | + ", third=" + third + |
| 189 | + '}'; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Taken from UniversalCraft under LGPLv3 |
| 195 | + * https://github.com/EssentialGG/UniversalCraft/blob/master/LICENSE |
| 196 | + */ |
| 197 | + private static class DesktopManager { |
| 198 | + private static final boolean isLinux; |
| 199 | + private static final boolean isXdg; |
| 200 | + private static boolean isKde; |
| 201 | + private static boolean isGnome; |
| 202 | + private static final boolean isMac; |
| 203 | + private static final boolean isWindows; |
| 204 | + |
| 205 | + static { |
| 206 | + String osName; |
| 207 | + try { |
| 208 | + osName = System.getProperty("os.name"); |
| 209 | + } catch (SecurityException ignored) { |
| 210 | + osName = null; |
| 211 | + } |
| 212 | + isLinux = osName != null && (osName.startsWith("Linux") || osName.startsWith("LINUX")); |
| 213 | + isMac = osName != null && osName.startsWith("Mac"); |
| 214 | + isWindows = osName != null && osName.startsWith("Windows"); |
| 215 | + if (isLinux) { |
| 216 | + String xdg = System.getenv("XDG_SESSION_ID"); |
| 217 | + isXdg = xdg != null && !xdg.isEmpty(); |
| 218 | + String gdm = System.getenv("GDMSESSION"); |
| 219 | + if (gdm != null) { |
| 220 | + String lowercaseGDM = gdm.toLowerCase(Locale.ENGLISH); |
| 221 | + isGnome = lowercaseGDM.contains("gnome"); |
| 222 | + isKde = lowercaseGDM.contains("kde"); |
| 223 | + } |
| 224 | + } else { |
| 225 | + isXdg = false; |
| 226 | + isKde = false; |
| 227 | + isGnome = false; |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + |
| 232 | + public static void open(File file) { |
| 233 | + if (!openDesktop(file)) { |
| 234 | + openSystemSpecific(file.getPath()); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + private static boolean openSystemSpecific(String file) { |
| 239 | + return isLinux ? (isXdg ? runCommand("xdg-open \"" + file + '"') : (isKde ? runCommand("kde-open \"" + file + '"') : (isGnome ? runCommand("gnome-open \"" + file + '"') : runCommand("kde-open \"" + file + '"') || runCommand("gnome-open \"" + file + '"')))) : (isMac ? runCommand("open \"" + file + '"') : (isWindows && runCommand("explorer \"" + file + '"'))); |
| 240 | + } |
| 241 | + |
| 242 | + private static boolean openDesktop(File file) { |
| 243 | + boolean worked; |
| 244 | + if (!Desktop.isDesktopSupported()) { |
| 245 | + worked = false; |
| 246 | + } else { |
| 247 | + boolean worked2; |
| 248 | + try { |
| 249 | + if (!Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) { |
| 250 | + return false; |
| 251 | + } |
| 252 | + |
| 253 | + Desktop.getDesktop().open(file); |
| 254 | + worked2 = true; |
| 255 | + } catch (Throwable var4) { |
| 256 | + worked2 = false; |
| 257 | + } |
| 258 | + |
| 259 | + worked = worked2; |
| 260 | + } |
| 261 | + |
| 262 | + return worked; |
| 263 | + } |
| 264 | + |
| 265 | + private static boolean runCommand(String command) { |
| 266 | + try { |
| 267 | + Process process = Runtime.getRuntime().exec(command); |
| 268 | + return process != null && process.isAlive(); |
| 269 | + } catch (IOException var5) { |
| 270 | + return false; |
| 271 | + } |
| 272 | + } |
| 273 | + } |
| 274 | +} |
0 commit comments