This commit is contained in:
ZekerZhayard 2022-02-14 20:39:16 +08:00 committed by Yuhui Huang
parent 17ad6362c3
commit 69ddc939b3
3 changed files with 64 additions and 17 deletions

View File

@ -237,4 +237,15 @@ public final class LibraryAnalyzer implements Iterable<LibraryAnalyzer.LibraryMa
public static final String LAUNCH_WRAPPER_MAIN = "net.minecraft.launchwrapper.Launch";
public static final String MOD_LAUNCHER_MAIN = "cpw.mods.modlauncher.Launcher";
public static final String BOOTSTRAP_LAUNCHER_MAIN = "cpw.mods.bootstraplauncher.BootstrapLauncher";
public static final String[] FORGE_TWEAKERS = new String[] {
"net.minecraftforge.legacy._1_5_2.LibraryFixerTweaker", // 1.5.2
"cpw.mods.fml.common.launcher.FMLTweaker", // 1.6.1 ~ 1.7.10
"net.minecraftforge.fml.common.launcher.FMLTweaker" // 1.8 ~ 1.12.2
};
public static final String[] OPTIFINE_TWEAKERS = new String[] {
"optifine.OptiFineTweaker",
"optifine.OptiFineForgeTweaker"
};
public static final String LITELOADER_TWEAKER = "com.mumfrey.liteloader.launch.LiteLoaderTweaker";
}

View File

@ -102,34 +102,42 @@ public class MaintainTask extends Task<Version> {
VersionLibraryBuilder builder = new VersionLibraryBuilder(version);
String mainClass = null;
if (!libraryAnalyzer.has(FORGE)) {
builder.removeTweakClass("forge");
}
// Installing Forge will override the Minecraft arguments in json, so LiteLoader and OptiFine Tweaker are being re-added.
if (libraryAnalyzer.has(LITELOADER) && !libraryAnalyzer.hasModLauncher()) {
builder.replaceTweakClass("liteloader", "com.mumfrey.liteloader.launch.LiteLoaderTweaker", !reorderTweakClass);
builder.replaceTweakClass(LibraryAnalyzer.LITELOADER_TWEAKER, LibraryAnalyzer.LITELOADER_TWEAKER, !reorderTweakClass, reorderTweakClass);
} else {
builder.removeTweakClass("liteloader");
builder.removeTweakClass(LibraryAnalyzer.LITELOADER_TWEAKER);
}
if (libraryAnalyzer.has(OPTIFINE)) {
if (!libraryAnalyzer.has(LITELOADER) && !libraryAnalyzer.has(FORGE)) {
builder.replaceTweakClass("optifine", "optifine.OptiFineTweaker", !reorderTweakClass);
if (!libraryAnalyzer.has(LITELOADER) && !libraryAnalyzer.has(FORGE) && builder.hasTweakClass(LibraryAnalyzer.OPTIFINE_TWEAKERS[1])) {
builder.replaceTweakClass(LibraryAnalyzer.OPTIFINE_TWEAKERS[1], LibraryAnalyzer.OPTIFINE_TWEAKERS[0], !reorderTweakClass, reorderTweakClass);
} else {
if (libraryAnalyzer.hasModLauncher()) {
// If ModLauncher installed, we use ModLauncher in place of LaunchWrapper.
mainClass = "cpw.mods.modlauncher.Launcher";
builder.replaceTweakClass("optifine", "optifine.OptiFineForgeTweaker", !reorderTweakClass);
mainClass = LibraryAnalyzer.MOD_LAUNCHER_MAIN;
for (String optiFineTweaker : LibraryAnalyzer.OPTIFINE_TWEAKERS) {
builder.removeTweakClass(optiFineTweaker);
}
} else {
// If forge or LiteLoader installed, OptiFine Forge Tweaker is needed.
builder.replaceTweakClass("optifine", "optifine.OptiFineForgeTweaker", !reorderTweakClass);
builder.replaceTweakClass(LibraryAnalyzer.OPTIFINE_TWEAKERS[0], LibraryAnalyzer.OPTIFINE_TWEAKERS[1], !reorderTweakClass, reorderTweakClass);
}
}
} else {
builder.removeTweakClass("optifine");
for (String optiFineTweaker : LibraryAnalyzer.OPTIFINE_TWEAKERS) {
builder.removeTweakClass(optiFineTweaker);
}
}
boolean hasForge = libraryAnalyzer.has(FORGE), hasModLauncher = libraryAnalyzer.hasModLauncher();
for (String forgeTweaker : LibraryAnalyzer.FORGE_TWEAKERS) {
if (!hasForge) {
builder.removeTweakClass(forgeTweaker);
} else if (!hasModLauncher && builder.hasTweakClass(forgeTweaker)) {
builder.replaceTweakClass(forgeTweaker, forgeTweaker, !reorderTweakClass, reorderTweakClass);
}
}
Version ret = builder.build();

View File

@ -58,6 +58,10 @@ public final class VersionLibraryBuilder {
.setLibraries(libraries);
}
public boolean hasTweakClass(String tweakClass) {
return useMcArgs && mcArgs.contains(tweakClass) || game.stream().anyMatch(arg -> arg.toString().equals(tweakClass));
}
public void removeTweakClass(String target) {
replaceTweakClass(target, null, false);
}
@ -94,6 +98,20 @@ public final class VersionLibraryBuilder {
* @param inPlace if true, replace the tweak class in place, otherwise add the tweak class to the end of the argument list without replacement.
*/
public void replaceTweakClass(String target, String replacement, boolean inPlace) {
replaceTweakClass(target, replacement, inPlace, false);
}
/**
* Replace existing tweak class.
* If the tweak class does not exist, the new tweak class will be added to argument list.
* If the tweak class appears more than one time, the tweak classes will be removed excluding the first one.
*
* @param target the tweak class to replace
* @param replacement the new tweak class to be replaced with, if null, remove the tweak class only
* @param inPlace if true, replace the tweak class in place, otherwise add the tweak class to the end of the argument list without replacement.
* @param reserve if true, add the tweak class to the start of the argument list.
*/
public void replaceTweakClass(String target, String replacement, boolean inPlace, boolean reserve) {
if (replacement == null && inPlace)
throw new IllegalArgumentException("Replacement cannot be null in replace mode");
@ -102,7 +120,7 @@ public final class VersionLibraryBuilder {
for (int i = 0; i + 1 < mcArgs.size(); ++i) {
String arg0Str = mcArgs.get(i);
String arg1Str = mcArgs.get(i + 1);
if (arg0Str.equals("--tweakClass") && arg1Str.toLowerCase().contains(target)) {
if (arg0Str.equals("--tweakClass") && arg1Str.equals(target)) {
if (!replaced && inPlace) {
// for the first one, we replace the tweak class only.
mcArgs.set(i + 1, replacement);
@ -124,7 +142,7 @@ public final class VersionLibraryBuilder {
// We need to preserve the tokens
String arg0Str = arg0.toString();
String arg1Str = arg1.toString();
if (arg0Str.equals("--tweakClass") && arg1Str.toLowerCase().contains(target)) {
if (arg0Str.equals("--tweakClass") && arg1Str.equals(target)) {
if (!replaced && inPlace) {
// for the first one, we replace the tweak class only.
game.set(i + 1, new StringArgument(replacement));
@ -141,8 +159,18 @@ public final class VersionLibraryBuilder {
// if the tweak class does not exist, add a new one to the end.
if (!replaced && replacement != null) {
game.add(new StringArgument("--tweakClass"));
game.add(new StringArgument(replacement));
if (reserve) {
if (useMcArgs) {
mcArgs.add(0, replacement);
mcArgs.add(0, "--tweakClass");
} else {
game.add(0, new StringArgument(replacement));
game.add(0, new StringArgument("--tweakClass"));
}
} else {
game.add(new StringArgument("--tweakClass"));
game.add(new StringArgument(replacement));
}
}
}