This commit is contained in:
JFronny 2021-05-29 10:58:13 +02:00
parent a2693f08ad
commit 44ce5f772b
No known key found for this signature in database
GPG Key ID: BEC5ACBBD4EE17E5
4 changed files with 48 additions and 40 deletions

View File

@ -15,5 +15,5 @@ dependencies {
download("https://gitlab.com/jfmods/LibJF/-/jobs/artifacts/master/raw/latest-dev.jar?job=build_test", "libjf") download("https://gitlab.com/jfmods/LibJF/-/jobs/artifacts/master/raw/latest-dev.jar?job=build_test", "libjf")
modImplementation "com.terraformersmc:modmenu:1.16.9" modImplementation "com.terraformersmc:modmenu:2.0.0-beta.5"
} }

View File

@ -2,8 +2,8 @@
org.gradle.jvmargs=-Xmx1G org.gradle.jvmargs=-Xmx1G
# Fabric Properties # Fabric Properties
# check these on https://modmuss50.me/fabric.html # check these on https://modmuss50.me/fabric.html
minecraft_version=1.16.5 minecraft_version=1.17-pre1
yarn_mappings=1.16.5+build.9 yarn_mappings=1.17-pre1+build.6
loader_version=0.11.3 loader_version=0.11.3
# Mod Properties # Mod Properties
mod_version=2.2 mod_version=2.2
@ -12,7 +12,7 @@ archives_base_name=modsmod
modrinth_id=4GhX11Ed modrinth_id=4GhX11Ed
modrinth_required_dependencies=7a9qcRLy modrinth_required_dependencies=7a9qcRLy
modrinth_optional_dependencies=Gz5wa6j2 modrinth_optional_dependencies=EDbIonje
curseforge_id=405095 curseforge_id=405095
curseforge_required_dependencies=libjf curseforge_required_dependencies=libjf
curseforge_optional_dependencies=modmenu curseforge_optional_dependencies=modmenu

View File

@ -1,14 +1,18 @@
package io.gitlab.jfronny.modsmod; package io.gitlab.jfronny.modsmod;
import net.fabricmc.loader.util.FileSystemUtil;
import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.file.FileSystem; import java.nio.file.FileSystem;
import java.nio.file.Path;
public class ModMeta { public class ModMeta {
public final FileSystem fs; public final FileSystem fs;
public final URL url; public final URL url;
public ModMeta(FileSystem fs, URL url) { public ModMeta(Path f) throws IOException {
this.fs = fs; this.fs = FileSystemUtil.getJarFileSystem(f, false).get();
this.url = url; this.url = f.toUri().toURL();
} }
} }

View File

@ -13,7 +13,6 @@ import net.fabricmc.loader.util.FileSystemUtil;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem; import java.nio.file.FileSystem;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -22,12 +21,11 @@ import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
public class ModsMod { public class ModsMod {
static HashSet<ModMeta> m; static final HashSet<ModMeta> m = new HashSet<>();
static FabricLoader loader; static final FabricLoader loader = FabricLoader.INSTANCE;
public static final String MOD_ID = "modsmod"; public static final String MOD_ID = "modsmod";
private static final String CACHE_NAME = MOD_ID + "cache"; private static final String CACHE_NAME = MOD_ID + "cache";
public static void prepare() throws IOException { public static void prepare() throws IOException {
loader = FabricLoader.INSTANCE;
//Load config //Load config
Libjf.registerConfig(MOD_ID, Cfg.class); Libjf.registerConfig(MOD_ID, Cfg.class);
Path configDir = loader.getConfigDir(); Path configDir = loader.getConfigDir();
@ -41,7 +39,7 @@ public class ModsMod {
} }
//remove modsmodcache if the cache is outdated //remove modsmodcache if the cache is outdated
try { try {
Path cfgCache = configDir.resolve(CACHE_NAME + "_basecfg"); Path cfgCache = path.resolve("_basecfg");
if (Files.exists(cfgCache)) { if (Files.exists(cfgCache)) {
if (Files.isRegularFile(cfgCache)) { if (Files.isRegularFile(cfgCache)) {
if (!IOUtil.contentEquals(modsmodCfgFile, cfgCache)) { if (!IOUtil.contentEquals(modsmodCfgFile, cfgCache)) {
@ -57,53 +55,59 @@ public class ModsMod {
System.err.println("Failed to validate modsmod config cache, caching will not be available"); System.err.println("Failed to validate modsmod config cache, caching will not be available");
e.printStackTrace(); e.printStackTrace();
} }
m = new HashSet<>(); m.clear();
//Generate mods //Generate mods
for (int i = 0; i < Cfg.modsCount; i++) { for (int i = 0; i < Cfg.modsCount; i++) {
Path f = path.resolve("f" + (i + 1) + ".jar"); Path f = path.resolve("f" + (i + 1) + ".jar");
boolean exists = Files.exists(f); boolean exists = Files.exists(f);
FileSystem fs = FileSystemUtil.getJarFileSystem(f, !exists).get();
m.add(new ModMeta(fs, f.toUri().toURL()));
//Do not load if cached //Do not load if cached
if (exists) { if (exists) {
if (Cfg.cache) continue; if (Cfg.cache) {
else Files.delete(f); m.add(new ModMeta(f));
continue;
} else Files.delete(f);
} }
//META-INF/MANIFEST.MF (java jar file spec) try (FileSystem fs = FileSystemUtil.getJarFileSystem(f, true).get()) {
Path inf = fs.getPath("META-INF"); //META-INF/MANIFEST.MF (java jar file spec)
Files.createDirectory(inf); Path inf = fs.getPath("META-INF");
StringBuilder sb = new StringBuilder(); Files.createDirectory(inf);
sb.append("Manifest-Version: 1.0\n"); StringBuilder sb = new StringBuilder();
Files.write(inf.resolve("MANIFEST.MF"), sb.toString().getBytes(StandardCharsets.UTF_8)); sb.append("Manifest-Version: 1.0\n");
sb.delete(0, sb.length()); Files.writeString(inf.resolve("MANIFEST.MF"), sb.toString());
//fabric.mod.json (fabric mod metadata) sb.delete(0, sb.length());
sb.append("{"); //fabric.mod.json (fabric mod metadata)
sb.append("\"schemaVersion\": 1,"); sb.append("{");
sb.append("\"id\": \"modmod_").append(i + 1).append("\","); sb.append("\"schemaVersion\": 1,");
sb.append("\"version\": \"1.0\","); sb.append("\"id\": \"modmod_").append(i + 1).append("\",");
sb.append("\"name\": \"ModsMod ").append(i + 1).append("\","); sb.append("\"version\": \"1.0\",");
sb.append("\"entrypoints\": {},"); sb.append("\"name\": \"ModsMod ").append(i + 1).append("\",");
sb.append("\"custom\": {"); sb.append("\"entrypoints\": {},");
if (Cfg.parent) { sb.append("\"custom\": {");
sb.append("\"modmenu:parent\": \"modsmod\""); if (Cfg.parent) {
sb.append("\"modmenu:parent\": \"modsmod\"");
}
sb.append("}");
sb.append("}");
Files.writeString(fs.getPath("fabric.mod.json"), sb.toString());
sb.delete(0, sb.length());
} catch (Throwable e) {
e.printStackTrace();
} }
sb.append("}");
sb.append("}");
Files.write(fs.getPath("fabric.mod.json"), sb.toString().getBytes(StandardCharsets.UTF_8));
sb.delete(0, sb.length());
System.gc(); System.gc();
m.add(new ModMeta(f));
} }
FabricLoaderInterface.synchronize(loader); FabricLoaderInterface.synchronize(loader);
} }
public static void init() { public static void init() {
for (ModMeta f : m) { for (ModMeta f : m) {
loadMod(loader, f); loadMod(f);
} }
} }
private static void loadMod(FabricLoader loader, ModMeta meta) { private static void loadMod(ModMeta meta) {
ModCandidate candidate = parseMod(meta.fs.getPath("fabric.mod.json"), meta.url); ModCandidate candidate = parseMod(meta.fs.getPath("fabric.mod.json"), meta.url);
if (loader.isDevelopmentEnvironment()) { if (loader.isDevelopmentEnvironment()) {