From 99891914e3f89564a55fa34536aa63ed8122cc2b Mon Sep 17 00:00:00 2001 From: Bartosz Skrzypczak Date: Mon, 26 Jun 2023 17:32:05 +0200 Subject: [PATCH] Also guess gradle cache dir from assets location on launchwrapper --- build.gradle | 2 +- src/main/java/ofdev/common/Utils.java | 28 +++++++++++++++++-- .../OptifineDevTransformerWrapper.java | 8 +++++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 24206ed..07d8c62 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,7 @@ buildscript { apply plugin: 'com.github.johnrengelman.shadow' apply plugin: 'java' -version = "2.6.9" +version = "2.6.10" group = "ofdev" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "aa_do_not_rename_OptiFineDevTweaker" diff --git a/src/main/java/ofdev/common/Utils.java b/src/main/java/ofdev/common/Utils.java index a4803f5..8f24762 100644 --- a/src/main/java/ofdev/common/Utils.java +++ b/src/main/java/ofdev/common/Utils.java @@ -15,8 +15,10 @@ import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Set; public class Utils { @@ -84,12 +86,29 @@ public static Path findMinecraftJar(Path maybeFgCache) { return absolutePath; } String mcVersion = mcVersion(); + List attemptedPaths = new ArrayList<>(); if (maybeFgCache != null) { // provided likely FG cache: + Path fg1fg2Path = maybeFgCache.resolve("net/minecraft/minecraft") + .resolve(mcVersion).resolve("minecraft-" + mcVersion + ".jar").toAbsolutePath(); + attemptedPaths.add(fg1fg2Path); + if (Files.exists(fg1fg2Path)) { + LOGGER.info("Found Minecraft jar {} from FG1.x/FG2.x (provided cache)", fg1fg2Path); + return fg1fg2Path; + } + // RetroFuturaGradle https://github.com/GTNewHorizons/RetroFuturaGradle + //caches/retro_futura_gradle/mc-vanilla/1.12.2/ + Path rfgPath = maybeFgCache.resolve("mc-vanilla").resolve(mcVersion).resolve(mcVersion + ".jar").toAbsolutePath(); + attemptedPaths.add(rfgPath); + if (Files.exists(rfgPath)) { + LOGGER.info("Found Minecraft jar {} from RetroFuturaGradle (provided cache)", rfgPath); + return rfgPath; + } // FG3 - FG5 Path fg3plusPath = maybeFgCache.resolve("minecraft_repo/versions") .resolve(mcVersion).resolve("client.jar").toAbsolutePath(); + attemptedPaths.add(fg3plusPath); if (Files.exists(fg3plusPath)) { LOGGER.info("Found Minecraft jar {} from FG3+ (provided cache)", fg3plusPath); return fg3plusPath; @@ -97,6 +116,7 @@ public static Path findMinecraftJar(Path maybeFgCache) { // very old FG3 versions used slightly different path Path oldFg3Path = maybeFgCache.resolve("minecraft_repo/version") .resolve(mcVersion).resolve("client.jar").toAbsolutePath(); + attemptedPaths.add(oldFg3Path); if (Files.exists(oldFg3Path)) { LOGGER.info("Found Minecraft jar {} from old FG3 (provided cache)", oldFg3Path); return oldFg3Path; @@ -112,6 +132,7 @@ public static Path findMinecraftJar(Path maybeFgCache) { // FG1 - FG2 Path fg1fg2Path = gradleHome.resolve("caches/minecraft/net/minecraft/minecraft") .resolve(mcVersion).resolve("minecraft-" + mcVersion + ".jar").toAbsolutePath(); + attemptedPaths.add(fg1fg2Path); if (Files.exists(fg1fg2Path)) { LOGGER.info("Found Minecraft jar {} from FG1.x/FG2.x (global cache)", fg1fg2Path); return fg1fg2Path; @@ -120,6 +141,7 @@ public static Path findMinecraftJar(Path maybeFgCache) { //caches/retro_futura_gradle/mc-vanilla/1.12.2/ Path rfgPath = gradleHome.resolve("caches/retro_futura_gradle/mc-vanilla") .resolve(mcVersion).resolve(mcVersion + ".jar").toAbsolutePath(); + attemptedPaths.add(rfgPath); if (Files.exists(rfgPath)) { LOGGER.info("Found Minecraft jar {} from RetroFuturaGradle (global cache)", rfgPath); return rfgPath; @@ -128,6 +150,7 @@ public static Path findMinecraftJar(Path maybeFgCache) { // FG3 - FG5 Path fg3plusPath = Utils.gradleHome().resolve("caches/forge_gradle/minecraft_repo/versions") .resolve(mcVersion).resolve("client.jar").toAbsolutePath(); + attemptedPaths.add(fg3plusPath); if (Files.exists(fg3plusPath)) { LOGGER.info("Found Minecraft jar {} from FG3+ (global cache)", fg3plusPath); return fg3plusPath; @@ -135,18 +158,19 @@ public static Path findMinecraftJar(Path maybeFgCache) { // very old FG3 versions used slightly different path Path oldFg3Path = Utils.gradleHome().resolve("caches/forge_gradle/minecraft_repo/version") .resolve(mcVersion).resolve("client.jar").toAbsolutePath(); + attemptedPaths.add(oldFg3Path); if (Files.exists(oldFg3Path)) { LOGGER.info("Found Minecraft jar {} from old FG3 (global cache)", oldFg3Path); return oldFg3Path; } // as a last resort, attempt vanilla launcher Path mcLauncherJar = Paths.get(System.getProperty("user.home")).resolve(".minecraft/versions").resolve(mcVersion).resolve(mcVersion + ".jar"); + attemptedPaths.add(mcLauncherJar); if (Files.exists(mcLauncherJar)) { LOGGER.info("Found Minecraft jar {} from Minecraft Launcher", mcLauncherJar); return mcLauncherJar; } - String list = String.join("\n\t", - fg1fg2Path.toString(), rfgPath.toString(), fg3plusPath.toString(), oldFg3Path.toString(), mcLauncherJar.toString()); + String list = String.join("\n\t", attemptedPaths.stream().map(Object::toString).toArray(String[]::new)); throw new IllegalStateException("Could not fine Minecraft jar file. Try specifying Minecraft jar location with -Dofdev.mcjar=path\n\t" + "Attempted locations:\n\t" + list); } diff --git a/src/main/java/ofdev/launchwrapper/OptifineDevTransformerWrapper.java b/src/main/java/ofdev/launchwrapper/OptifineDevTransformerWrapper.java index f26087b..7581435 100644 --- a/src/main/java/ofdev/launchwrapper/OptifineDevTransformerWrapper.java +++ b/src/main/java/ofdev/launchwrapper/OptifineDevTransformerWrapper.java @@ -33,6 +33,7 @@ import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -42,11 +43,16 @@ // this is needed only in dev environment to get deobfuscated version of OptiFine running public class OptifineDevTransformerWrapper implements IClassTransformer { - private static final Path MC_JAR = Utils.findMinecraftJar(null); + private static final Path MC_JAR; private static final FileSystem mcJarFs; static { try { + Map launchArgs = (Map) Launch.blackboard.get("launchArgs"); + String assetsDir = launchArgs == null ? null : launchArgs.get("--assetsDir"); + // it just so happens that FG1,2,3+ and RetroFuturaGradle all have assets dir in about the same place relative to everything else + Path mcGradleCacheDir = assetsDir == null ? null : Paths.get(assetsDir).getParent(); + MC_JAR = Utils.findMinecraftJar(mcGradleCacheDir); mcJarFs = FileSystems.newFileSystem(MC_JAR, Launch.classLoader); Launch.classLoader.addURL(MC_JAR.toUri().toURL()); } catch (IOException e) {