Skip to content

Commit

Permalink
Handle early mod config registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Nov 26, 2023
1 parent bf94a0a commit 9f71ab4
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 15 deletions.
23 changes: 22 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ println("Project version: $version")
allprojects {
apply(plugin = "java")
apply(plugin = "dev.architectury.loom")
apply(plugin = "maven-publish")

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
Expand All @@ -63,6 +64,19 @@ allprojects {
remapperIsolation.set(false)
}
}

afterEvaluate {
extensions.getByType<PublishingExtension>().run {
if (publications.isEmpty()) {
publications {
create<MavenPublication>("mavenJava") {
group = rootProject.group
from(components["java"])
}
}
}
}
}
}

subprojects {
Expand All @@ -74,6 +88,10 @@ subprojects {
}
}

configurations.runtimeElements {
setExtendsFrom(listOf(configurations.include.get()))
}

repositories {
maven {
name = "Cursemaven"
Expand All @@ -96,14 +114,17 @@ dependencies {
includeProject("continuity-compat")
includeProject("amecs-api")
includeProject("forgeconfigapiport")
includeProject("extras-utils")

// Misc
modImplementation("curse.maven:mcpitanlibarch-682213:4723157")
}

fun DependencyHandlerScope.includeProject(name: String) {
runtimeOnly(project(path = ":$name", configuration = "namedElements"))
include(project(":$name"))
api(include(project(":$name")) {
isTransitive = false
})
}

val relocateDirectory: Jar.(String, String) -> Unit by extra { from, to ->
Expand Down
2 changes: 1 addition & 1 deletion emi-bridge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies {
mappings(loom.officialMojangMappings())
forge("net.minecraftforge:forge:$versionMc-$versionForge")

compileOnly(rootProject)
compileOnly(project(":extras-utils"))

modImplementation(group = "dev.su5ed.sinytra", name = "fabric-loader", version = "2.3.4+0.14.21+1.20.1")
modImplementation(group = "dev.su5ed.sinytra.fabric-api", name = "fabric-transfer-api-v1", version = "3.3.1+6acac45477")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.su5ed.sinytra.connectorextras.emibridge;

import com.mojang.logging.LogUtils;
import dev.su5ed.sinytra.connectorextras.HackyModuleInjector;
import dev.su5ed.sinytra.connectorextras.util.HackyModuleInjector;
import net.minecraftforge.fml.common.Mod;
import org.slf4j.Logger;

Expand Down
15 changes: 15 additions & 0 deletions extras-utils/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
id("dev.architectury.loom")
}

val versionMc: String by rootProject
val versionForge: String by rootProject

dependencies {
mappings(loom.officialMojangMappings())
forge(group = "net.minecraftforge", name = "forge", version = "$versionMc-$versionForge")
}

tasks.jar {
manifest.attributes("FMLModType" to "LIBRARY")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.su5ed.sinytra.connectorextras;
package dev.su5ed.sinytra.connectorextras.util;

import com.mojang.logging.LogUtils;
import cpw.mods.jarhandling.SecureJar;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.su5ed.sinytra.connectorextras.forgeconfigapiport;

import fuzs.forgeconfigapiport.api.config.v2.ForgeConfigRegistry;
import fuzs.forgeconfigapiport.api.config.v2.ModConfigEvents;
import net.fabricmc.fabric.api.event.Event;
import net.minecraftforge.eventbus.api.IEventBus;
Expand All @@ -18,6 +19,8 @@ public ForgeConfigApiPortImpl() {
bus.addListener(ForgeConfigApiPortImpl::onConfigLoad);
bus.addListener(ForgeConfigApiPortImpl::onConfigReload);
bus.addListener(ForgeConfigApiPortImpl::onConfigUnload);

((ForgeConfigRegistryImpl) ForgeConfigRegistry.INSTANCE).processConfigs();
}

private static void onConfigLoad(ModConfigEvent.Loading event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,79 @@
package dev.su5ed.sinytra.connectorextras.forgeconfigapiport;

import cpw.mods.modlauncher.api.LamdbaExceptionUtils;
import fuzs.forgeconfigapiport.api.config.v2.ForgeConfigRegistry;
import net.minecraftforge.fml.ModContainer;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.config.IConfigSpec;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.unsafe.UnsafeHacks;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class ForgeConfigRegistryImpl implements ForgeConfigRegistry {
private final Map<String, ModConfig> configs = new HashMap<>();

@Override
public ModConfig register(String modId, ModConfig.Type type, IConfigSpec<?> spec) {
ModContainer container = getModContainer(modId);
ModConfig modConfig = new ModConfig(type, spec, container);
container.addConfig(modConfig);
return modConfig;
return createModConfig(modId, c -> new ModConfig(type, spec, c));
}

@Override
public ModConfig register(String modId, ModConfig.Type type, IConfigSpec<?> spec, String fileName) {
ModContainer container = getModContainer(modId);
ModConfig modConfig = new ModConfig(type, spec, container, fileName);
container.addConfig(modConfig);
return modConfig;
return createModConfig(modId, c -> new ModConfig(type, spec, c, fileName));
}

public void processConfigs() {
Field containerField = LamdbaExceptionUtils.uncheck(() -> {
Field field = ModConfig.class.getDeclaredField("container");
field.setAccessible(true);
return field;
});
configs.forEach((modid, config) -> {
ModContainer container = getModContainer(modid);
container.addConfig(config);
try {
UnsafeHacks.setField(containerField, config, container);
} catch (Throwable t) {
throw new RuntimeException(t);
}
});
configs.clear();
}

private ModConfig createModConfig(String modid, Function<ModContainer, ModConfig> configConstructor) {
ModList modList = ModList.get();
if (modList == null) {
ModContainer container = createPlaceholderContainer(modid);
ModConfig config = configConstructor.apply(container);
configs.put(modid, config);
return config;
} else {
ModContainer container = getModContainer(modid);
ModConfig config = configConstructor.apply(container);
container.addConfig(config);
return config;
}
}

private static ModContainer createPlaceholderContainer(String modId) {
try {
Class<?> cls = Class.forName("net.minecraftforge.fml.ModLoader$ErroredModContainer");
Constructor<?> ctr = cls.getDeclaredConstructor();
ctr.setAccessible(true);
ModContainer container = (ModContainer) ctr.newInstance();
Field modidField = ModContainer.class.getDeclaredField("modId");
modidField.setAccessible(true);
UnsafeHacks.setField(modidField, container, modId);

return container;
} catch (Throwable t) {
throw new RuntimeException(t);
}
}

private static ModContainer getModContainer(String modid) {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Sets default memory used for gradle commands. Can be overridden by user or command line properties.
# This is required to provide enough memory for the Minecraft decompilation process.
org.gradle.jvmargs=-Xmx4G
org.gradle.daemon=false
org.gradle.parallel=true

loom.platform=forge
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ include(
"modmenu-bridge",
"continuity-compat",
"amecs-api",
"forgeconfigapiport"
"forgeconfigapiport",
"extras-utils"
)
2 changes: 1 addition & 1 deletion terrablender-bridge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies {
mappings(loom.officialMojangMappings())
forge("net.minecraftforge:forge:$versionMc-$versionForge")

compileOnly(rootProject)
compileOnly(project(":extras-utils"))

modImplementation(group = "dev.su5ed.sinytra", name = "fabric-loader", version = "2.3.4+0.14.21+1.20.1")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.su5ed.sinytra.connectorextras.terrablenderbridge;

import com.mojang.logging.LogUtils;
import dev.su5ed.sinytra.connectorextras.HackyModuleInjector;
import dev.su5ed.sinytra.connectorextras.util.HackyModuleInjector;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
Expand Down

0 comments on commit 9f71ab4

Please sign in to comment.