Created
March 25, 2018 22:16
-
-
Save Ricket/84ac1e076e50435957b8c15987a5d91f to your computer and use it in GitHub Desktop.
addition to MCUpdater's PathWalker, to create a path/filename given the mod id and original filename
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static Map<String, String> modPaths = new HashMap<>(); | |
private static List<String> badVersionStrings = new ArrayList<>(); | |
static { | |
// key is mod id | |
// value is mod filename (no `mods/`) | |
modPaths.put("betterbuilderswands", "BetterBuildersWands.jar"); | |
modPaths.put("bibliocraft", "BiblioCraft.jar"); | |
modPaths.put("biomesoplenty", "BiomesOPlenty.jar"); | |
modPaths.put("ChunkPregeneratorV", "Chunk Pregenerator.jar"); | |
modPaths.put("craftstudioapi", "CraftStudio.jar"); | |
modPaths.put("environmentaltech", "environmentaltech.jar"); | |
modPaths.put("extracells", "ExtraCells.jar"); | |
modPaths.put("immersiveengineeringcore", "ImmersiveEngineering-core.jar"); | |
modPaths.put("industrialforegoing", "industrialforegoing.jar"); | |
modPaths.put("InvTweaks", "InventoryTweaks.jar"); | |
modPaths.put("jeresources", "JustEnoughResources.jar"); | |
modPaths.put("magipsi", "MagicalPsi.jar"); | |
modPaths.put("mcjtylib_ng", "mcjtylib.jar"); | |
modPaths.put("harvestcraft", "PamsHarvestcraft.jar"); | |
modPaths.put("scalinghealth", "ScalingHealth.jar"); | |
modPaths.put("signals", "Signals.jar"); | |
modPaths.put("SilentLib", "SilentLib.jar"); | |
modPaths.put("tinkertoolleveling", "TinkerToolLeveling.jar"); | |
modPaths.put("waila", "Hwyla.jar"); // This one seems iffy | |
modPaths.put("wailaharvestability", "WailaHarvestability.jar"); | |
badVersionStrings.add("@VERSION@"); | |
badVersionStrings.add("${version}"); | |
} | |
private String generateModPath(String modId, Path file, MCModInfo info) { | |
String knownPath = modPaths.get(modId); | |
if (knownPath != null) { | |
return "mods/" + knownPath; | |
} | |
String name = file.getFileName().toString(); | |
// fix a few common jar mistakes or similarities | |
name = name.replace("-universal.jar", ".jar"); | |
name = name.replace("--", "-"); | |
// Catch the various minecraft versions | |
List<String> minecraftVersions = new ArrayList<>(); | |
// 1.12.2 | |
minecraftVersions.add(minecraftVersion); | |
// 1.12 | |
String shorterMcVersion = minecraftVersion.substring(0, minecraftVersion.lastIndexOf(".")); | |
minecraftVersions.add(shorterMcVersion); | |
// 1.12.1, 1.12.0 | |
int lastDigit = Integer.parseInt(minecraftVersion.substring(minecraftVersion.lastIndexOf(".") + 1)); | |
for (int i = lastDigit - 1; i >= 0; i--) { | |
minecraftVersions.add(shorterMcVersion + "." + Integer.toString(i)); | |
} | |
String mcVersionRegex = "(?:" + Joiner.on("|").join( | |
minecraftVersions.stream().map(Pattern::quote).collect(Collectors.toSet())) | |
+ ")"; | |
boolean modVersionIsSubstringOfMcVersion = false; | |
if (info != null && info.version != null && !info.version.isEmpty()) { | |
for (String mcVersion : minecraftVersions) { | |
if (mcVersion.contains(info.version)) { | |
modVersionIsSubstringOfMcVersion = true; | |
break; | |
} | |
} | |
} | |
if (info != null && info.version != null && !info.version.isEmpty()) { | |
// Remove the mod version from the name, except if the mod version is a substring of the minecraft version | |
if (!modVersionIsSubstringOfMcVersion) { | |
name = name.replaceAll("[-_ ]?" + Pattern.quote(info.version), ""); | |
} | |
// Sometimes the mod version has 1.12.2 in it; remove that from the mod version, then again remove the mod | |
// version from the name. | |
// [-_] after mc version: | |
String versionWithoutMc = info.version.replaceAll(mcVersionRegex + "[-_]?", ""); | |
// [-_] before mc version: | |
versionWithoutMc = versionWithoutMc.replaceAll("[-_]?" + mcVersionRegex, ""); | |
if (!modVersionIsSubstringOfMcVersion) { | |
name = name.replaceAll("[-_ ]?" + Pattern.quote(versionWithoutMc), ""); | |
} | |
} | |
// Remove 1.12.2 from the name | |
name = name.replaceAll("[-_]?(?:mc|MC)?" + mcVersionRegex, ""); | |
// Now go back, if the mod version was inside the minecraft version, and remove the mod version. | |
if (modVersionIsSubstringOfMcVersion) { | |
name = name.replaceAll("[-_ ]?" + Pattern.quote(info.version), ""); | |
} | |
// If the mod didn't have a parseable info, then the filename likely has a version number still in it. Try to | |
// parse that out. | |
if (info == null || info.version == null || info.version.isEmpty() || badVersionStrings.contains(info.version)) { | |
// replace like -12.34 or -12.34.56 | |
name = name.replaceAll("[-_ ][0-9]+(?:\\.[0-9]+)+", ""); | |
} | |
return "mods/" + name; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment