mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-18 20:49:12 +00:00
Implement fabric-modId decorations for Forge mods
This commit is contained in:
1 parent
6f074ea1d9
commit
24c5ac4009
2 files changed
+99
No files matched your search
+10
@@ -42,6 +42,7 @@ import org.apache.logging.log4j.core.appender.AbstractAppender;
|
||||
import org.spongepowered.asm.launch.GlobalProperties;
|
||||
import org.spongepowered.asm.launch.platform.container.ContainerHandleURI;
|
||||
import org.spongepowered.asm.launch.platform.container.IContainerHandle;
|
||||
import org.spongepowered.asm.mixin.ForgeUtils;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment;
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment.Phase;
|
||||
@@ -432,6 +433,15 @@ public class MixinPlatformAgentFMLLegacy extends MixinPlatformAgentAbstract impl
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Called after Mixin configs have been loaded. Do Forge-specific initialization.</p>
|
||||
* <p>Note: The mod bootstrapping Mixin must contain an FML plugin for this to get called.</p>
|
||||
*/
|
||||
@Override
|
||||
public void initPrimaryContainer() {
|
||||
ForgeUtils.init();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.launch.platform.MixinPlatformAgentAbstract
|
||||
* #getSideName()
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package org.spongepowered.asm.mixin;
|
||||
|
||||
// Reference: https://github.com/FabricMC/fabric-loader/blob/2a378f1c563b6ec96ae6620a278ecd23fa09da0f/src/main/java/net/fabricmc/loader/impl/launch/FabricMixinBootstrap.java
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import net.minecraft.launchwrapper.Launch;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.spongepowered.asm.mixin.transformer.Config;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
|
||||
public class ForgeUtils {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger();
|
||||
|
||||
public static final String KEY_MOD_ID = "fabric-modId";
|
||||
|
||||
public static void init() {
|
||||
Map<String, String> jarNameToModid = createJarNameToModidMap();
|
||||
|
||||
for(Config config : Mixins.getConfigs()) {
|
||||
URL resource = Launch.classLoader.getResource(config.getName());
|
||||
if(resource != null) {
|
||||
String jarName = getJarNameFromResourceUrl(resource);
|
||||
if(jarName != null) {
|
||||
String modid = jarNameToModid.get(jarName);
|
||||
if(modid != null) {
|
||||
config.getConfig().decorate(KEY_MOD_ID, modid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, String> createJarNameToModidMap() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
try {
|
||||
for (URL url : Collections.list(Launch.classLoader.getResources("mcmod.info"))) {
|
||||
String fileName = getJarNameFromResourceUrl(url);
|
||||
if (fileName != null) {
|
||||
List<ModMetadata> metas = parseMcmodInfo(url);
|
||||
if (!metas.isEmpty()) {
|
||||
String modid = metas.get(0).modid;
|
||||
if (modid != null) {
|
||||
map.put(fileName, modid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {
|
||||
logger.warn("Failed to construct jar name -> modid map, mod names will not be shown in errors.");
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private static List<ModMetadata> parseMcmodInfo(URL url) {
|
||||
try {
|
||||
JsonElement root = new Gson().fromJson(new InputStreamReader(url.openStream()), JsonElement.class);
|
||||
if (root.isJsonArray()) {
|
||||
return Arrays.asList(new Gson().fromJson(new InputStreamReader(url.openStream()), ModMetadata[].class));
|
||||
} else {
|
||||
return Arrays.asList(new Gson().fromJson(new InputStreamReader(url.openStream()), ModMetadata.class));
|
||||
}
|
||||
} catch(Exception e) {
|
||||
logger.warn("Failed to parse mcmod.info at " + url + ": " + e);
|
||||
}
|
||||
return Arrays.asList();
|
||||
}
|
||||
|
||||
private static String getJarNameFromResourceUrl(URL url) {
|
||||
if(url.getPath().contains("!/")) {
|
||||
String filePath = url.getPath().split("!/")[0];
|
||||
String[] parts = filePath.split("/");
|
||||
if(parts.length != 0) {
|
||||
return parts[parts.length - 1];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static class ModMetadata {
|
||||
String modid;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user