Skip to content

Commit

Permalink
Merge pull request #129 from 3arthqu4ke/1.9.0
Browse files Browse the repository at this point in the history
[1.9.0] Better Forge Support
  • Loading branch information
3arthqu4ke authored Mar 7, 2024
2 parents 5ef0031 + 0e79acd commit e3bd572
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 32 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
project_version=1.8.1
project_version=1.9.0
3 changes: 2 additions & 1 deletion headlessmc-launcher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies {
jarLibs group: 'com.google.code.gson', name: 'gson', version: '2.10.1'
jarLibs group: 'fr.litarvan', name: 'openauth', version: '1.1.6'

includeJarInResources ('net.kunmc.lab:forgecli:1.1.0:all') {
includeJarInResources ('net.kunmc.lab:forgecli:1.2.0:all') {
// this is to make it build, jar will still have legacy classes
exclude module: 'legacy'
exclude module: 'asm'
Expand All @@ -49,6 +49,7 @@ processResources {
from project(':headlessmc-runtime')['shadowJar']
from project(':headlessmc-lwjgl')['jar']
from configurations.includeJarInResources
rename 'ForgeCLI(.*).jar', 'forge-cli.jar'
rename 'forgecli(.*).jar', 'forge-cli.jar'
rename 'headlessmc-lwjgl(.*).jar', 'headlessmc-lwjgl.jar'
rename 'headlessmc-runtime(.*).jar', 'headlessmc-runtime.jar'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Getter
@RequiredArgsConstructor
public class Launcher implements HeadlessMc {
public static final String VERSION = "1.8.1";
public static final String VERSION = "1.9.0";

@Delegate
private final HeadlessMc headlessMc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public LaunchContext(Launcher ctx) {
add(new LaunchCommand(ctx));
add(new QuitCommand(ctx));
add(new FabricCommand(ctx));
add(new ForgeCommand(ctx));
add(ForgeCommand.lexforge(ctx));
add(ForgeCommand.neoforge(ctx));
add(new JsonCommand(ctx));
add(new HelpCommand(ctx));
add(new JavaCommand(ctx));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@

@CustomLog
public class ForgeCommand extends AbstractVersionCommand {
private final ForgeInstaller installer;
private final ForgeIndexCache cache;

public ForgeCommand(Launcher ctx) {
this(ctx, new ForgeIndexCache());
}

public ForgeCommand(Launcher ctx, ForgeIndexCache cache) {
super(ctx, "forge", "Downloads forge.");
args.put("<version>", "The version to download forge for.");
args.put("--uid", "Specify a specific forge version.");
args.put("-refresh", "Refresh index of forge versions.");
args.put("-list", "List forge versions for the specified version.");
public ForgeCommand(Launcher ctx, String name, ForgeInstaller installer, ForgeIndexCache cache) {
super(ctx, name, "Downloads " + name + ".");
args.put("<version>", "The version to download " + name + " for.");
args.put("--uid", "Specify a specific " + name + " version.");
args.put("-refresh", "Refresh index of " + name + " versions.");
args.put("-list", "List " + name + " versions for the specified version.");
this.installer = installer;
this.cache = cache;
}

Expand Down Expand Up @@ -71,7 +69,7 @@ public void execute(Version ver, String... args) throws CommandException {
val uuid = UUID.randomUUID();
val fm = ctx.getFileManager().createRelative(uuid.toString());
try {
new ForgeInstaller(ctx).install(version, fm);
installer.install(version, fm);
ctx.getVersionService().refresh();
} catch (IOException e) {
val message = "Failed to install forge for version " + ver.getName()
Expand All @@ -96,4 +94,14 @@ private void logTable(Iterable<ForgeVersion> versions) {
.build());
}

public static ForgeCommand lexforge(Launcher launcher) {
ForgeInstaller installer = new ForgeInstaller(ForgeRepoFormat.lexForge(), launcher, "Forge", ForgeRepoFormat.LEX_FORGE_URL);
return new ForgeCommand(launcher, "forge", installer, new ForgeIndexCache(ForgeIndexCache.LEX_FORGE_INDICES));
}

public static ForgeCommand neoforge(Launcher launcher) {
ForgeInstaller installer = new ForgeInstaller(ForgeRepoFormat.neoForge(), launcher, "NeoForge", ForgeRepoFormat.NEO_FORGE_URL);
return new ForgeCommand(launcher, "neoforge", installer, new ForgeIndexCache(ForgeIndexCache.NEO_FORGE_INDICES));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
import me.earth.headlessmc.launcher.util.JsonUtil;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import static me.earth.headlessmc.launcher.util.URLs.url;

@CustomLog
public class ForgeIndexCache extends AbstractDownloadService<ForgeVersion> {
public ForgeIndexCache() {
super(url("https://meta.multimc.org/v1/net.minecraftforge/index.json"));
public static final URL LEX_FORGE_INDICES = url("https://meta.prismlauncher.org/v1/net.minecraftforge/index.json");
public static final URL NEO_FORGE_INDICES = url("https://meta.prismlauncher.org/v1/net.neoforged/index.json");

public ForgeIndexCache(URL url) {
super(url);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
@RequiredArgsConstructor
public class ForgeInstaller {
private static final String FORGE_CLI = "forge-cli.jar";
private static final String BASE_URL =
"https://maven.minecraftforge.net/net/minecraftforge/forge/";

private final ForgeRepoFormat repoFormat;
private final Launcher launcher;
private final String forgeName;
private final String baseUrl;

public void install(ForgeVersion version, FileManager fileManager)
throws IOException {
val cli = new ResourceExtractor(fileManager, FORGE_CLI).extract();
val fileName = "forge-" + version.getFullName() + "-installer.jar";
val url = BASE_URL + version.getFullName() + "/" + fileName;
log.debug("Downloading Installer from " + url);
val fileName = repoFormat.getFileName(version);
// TODO: delete installer?
val installer = fileManager.create(fileName);
downloadInstaller(version, installer, fileName);
downloadInstaller(version, installer);

val java = launcher.getJavaService().findBestVersion(8);
val mc = launcher.getMcFiles();
Expand All @@ -56,7 +56,7 @@ public void install(ForgeVersion version, FileManager fileManager)
+ version.getFullName()
+ ", exitCode: " + exitCode);
} else {
launcher.log("Forge " + version.getFullName()
launcher.log(forgeName + version.getFullName()
+ " installed successfully!");
}
} catch (InterruptedException e) {
Expand All @@ -78,17 +78,17 @@ protected List<String> getCommand(Java java, File mc, File cli, File fml) {
return command;
}

protected void downloadInstaller(ForgeVersion v, File file, String name)
protected void downloadInstaller(ForgeVersion version, File file)
throws IOException {
var url = BASE_URL + v.getFullName() + "/" + name;
var url = repoFormat.getUrl(baseUrl, version);
log.debug("Downloading Installer from " + url);
try {
IOUtil.download(url, file.getAbsolutePath());
} catch (IOException e) {
log.debug("Failed to download Forge from " + url + ": "
+ e.getMessage());
url = BASE_URL + v.getFullName() + "-" + v.getVersion()
+ "/forge-" + v.getFullName() + "-" + v.getVersion()
url = baseUrl + version.getFullName() + "-" + version.getVersion()
+ "/forge-" + version.getFullName() + "-" + version.getVersion()
+ "-installer.jar";
log.debug("Downloading from forge from " + url);
IOUtil.download(url, file.getAbsolutePath());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.earth.headlessmc.launcher.command.forge;

public interface ForgeRepoFormat {
// E.g. https://maven.neoforged.net/releases/net/neoforged/neoforge/20.4.195/neoforge-20.4.195-installer.jar
String NEO_FORGE_URL = " https://maven.neoforged.net/releases/net/neoforged/neoforge/";
// E.g. https://maven.minecraftforge.net/net/minecraftforge/forge/1.20.2-48.1.0/forge-1.20.2-48.1.0-installer.jar
String LEX_FORGE_URL = "https://maven.minecraftforge.net/net/minecraftforge/forge/";

String getFileName(ForgeVersion version);

String getUrl(String baseUrl, ForgeVersion version);

static ForgeRepoFormat lexForge() {
return new ForgeRepoFormat() {
@Override
public String getFileName(ForgeVersion version) {
return "forge-" + version.getFullName() + "-installer.jar";
}

@Override
public String getUrl(String baseUrl, ForgeVersion version) {
return baseUrl + version.getFullName() + "/" + getFileName(version);
}
};
}

static ForgeRepoFormat neoForge() {
return new ForgeRepoFormat() {
@Override
public String getFileName(ForgeVersion version) {
return "neoforge-" + version.getName() + "-installer.jar";
}

@Override
public String getUrl(String baseUrl, ForgeVersion version) {
return baseUrl + version.getName() + "/" + getFileName(version);
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipFile;

@CustomLog
Expand Down Expand Up @@ -102,10 +104,16 @@ private List<Target> processLibraries(Version version, FileManager dlls)
// TODO: proper features
val features = Features.EMPTY;
val targets = new ArrayList<Target>(version.getLibraries().size());
Set<String> libraries = new HashSet<>();
Set<String> libPaths = new HashSet<>();
for (val library : version.getLibraries()) {
if (library.getRule().apply(os, features) == Rule.Action.ALLOW) {
if (library.getRule().apply(os, features) == Rule.Action.ALLOW && libraries.add(library.getName())) {
log.debug("Checking: " + library);
String libPath = library.getPath(os);
if (!libPaths.add(libPath)) {
continue;
}

val path = files.getDir("libraries") + File.separator + libPath;
if (!new File(path).exists()) {
String url = library.getUrl(libPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
public class ForgeIndexCacheTest extends ForgeIndexCache
implements UsesResources {

public ForgeIndexCacheTest() {
super(LEX_FORGE_INDICES);
}

@Test
public void testRead() throws IOException {
val versions = this.read(getJsonElement("forge_index.json"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private void transformModule(ClassNode cn) {
if (cn.module != null) {
cn.module.visitRequire("headlessmc.lwjgl", ACC_MANDATED, null);
cn.module.access |= ACC_OPEN;
cn.module.opens = null; // Forge: InvalidModuleDescriptorException: The opens table for an open module must be 0 length
}
}

Expand Down
2 changes: 1 addition & 1 deletion headlessmc-scripts/hmc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env sh
java -jar headlessmc-launcher-1.8.1.jar --command $@
java -jar headlessmc-launcher-1.9.0.jar --command $@
2 changes: 1 addition & 1 deletion headlessmc-scripts/hmc.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@echo off
"%JAVA_HOME%\bin\java" -jar headlessmc-launcher-1.8.1.jar --command %*
"%JAVA_HOME%\bin\java" -jar headlessmc-launcher-1.9.0.jar --command %*
2 changes: 1 addition & 1 deletion headlessmc-scripts/hmw
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
# when running in docker on windows bash seems to be at /bin/bash TODO: can we make this one script?
java -jar headlessmc-launcher-1.8.1.jar --command $@
java -jar headlessmc-launcher-1.9.0.jar --command $@

0 comments on commit e3bd572

Please sign in to comment.