Skip to content

Commit

Permalink
Also guess gradle cache dir from assets location on launchwrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Barteks2x committed Jun 26, 2023
1 parent bf5d2f8 commit 9989191
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
28 changes: 26 additions & 2 deletions src/main/java/ofdev/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -84,19 +86,37 @@ public static Path findMinecraftJar(Path maybeFgCache) {
return absolutePath;
}
String mcVersion = mcVersion();
List<Path> 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;
}
// 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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -128,25 +150,27 @@ 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;
}
// 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> launchArgs = (Map<String, String>) 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) {
Expand Down

0 comments on commit 9989191

Please sign in to comment.