Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to Fabric Mixin #94

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ modlauncher_version=10.0.9
securejarhandler_version=2.1.23
bootstraplauncher_version=1.1.2
asm_version=9.5
mixin_version=0.8.5
mixin_version=0.12.5+mixin.0.8.5
terminalconsoleappender_version=1.2.0
nightconfig_version=3.6.4
jetbrains_annotations_version=24.0.1
Expand Down
2 changes: 1 addition & 1 deletion loader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {
implementation("net.neoforged.accesstransformers:at-modlauncher:${accesstransformers_version}") {
exclude module: 'modlauncher'
}
implementation("org.spongepowered:mixin:${mixin_version}")
implementation("net.fabricmc:sponge-mixin:${mixin_version}")
marchermans marked this conversation as resolved.
Show resolved Hide resolved
implementation("net.minecrell:terminalconsoleappender:${terminalconsoleappender_version}")
implementation("commons-io:commons-io:${commons_io_version}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ public void addCoreMods()
public void addMixinConfigs() {
modFiles.stream()
.map(ModFileInfo::getFile)
.map(ModFile::getMixinConfigs)
.flatMap(List::stream)
.forEach(DeferredMixinConfigRegistration::addMixinConfig);
.forEach(file -> {
final String modId = file.getModInfos().get(0).getModId();
file.getMixinConfigs().forEach(cfg -> DeferredMixinConfigRegistration.addMixinConfig(cfg, modId));
});
}

public void addAccessTransformers()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.fml.loading;

import cpw.mods.jarhandling.VirtualJar;
import cpw.mods.modlauncher.api.IEnvironment;
import cpw.mods.modlauncher.api.IModuleLayerManager;
import cpw.mods.modlauncher.api.ITransformationService;
import cpw.mods.modlauncher.api.ITransformer;
import cpw.mods.modlauncher.api.IncompatibleEnvironmentException;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.util.Constants;

import java.nio.file.Path;
import java.util.List;
import java.util.Set;

public class MixinSyntheticPackageProvider implements ITransformationService {
@Override
public @NotNull String name() {
return "mixin-synthetic-package";
}

@Override
public void initialize(IEnvironment environment) {

}

@Override
public void onLoad(IEnvironment env, Set<String> otherServices) throws IncompatibleEnvironmentException {

}

@Override
public List<Resource> beginScanning(IEnvironment environment) {
try {
return List.of(
new Resource(IModuleLayerManager.Layer.GAME, List.of(
new VirtualJar("mixinsynthetic", Path.of(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI()),
Constants.SYNTHETIC_PACKAGE, Constants.SYNTHETIC_PACKAGE + ".args")
))
);
} catch (Exception exception) {
throw new RuntimeException("Failed to intialise synthetic Mixin virtual jar", exception);
}
}

@Override
public @NotNull List<ITransformer> transformers() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@

package net.neoforged.fml.loading.mixin;

import net.neoforged.fml.loading.FMLLoader;
import org.apache.logging.log4j.LogManager;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.asm.launch.GlobalProperties;
import org.spongepowered.asm.launch.MixinBootstrap;
import org.spongepowered.asm.mixin.FabricUtil;
import org.spongepowered.asm.mixin.Mixins;
import org.spongepowered.asm.mixin.transformer.Config;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class DeferredMixinConfigRegistration {
private static final Logger LOG = LoggerFactory.getLogger("fml-mixin-setup");

private static boolean added = false;
private static final List<String> mixinConfigs = new ArrayList<>();
record ConfigInfo(String fileName, @Nullable String modId) {}
private static final List<ConfigInfo> mixinConfigs = new ArrayList<>();

static {
// Register our platform agent first
Expand All @@ -25,16 +36,33 @@ public class DeferredMixinConfigRegistration {
}

public static void addMixinConfig(String config) {
addMixinConfig(config, null);
}

public static void addMixinConfig(String config, @Nullable String modId) {
if (added) {
throw new IllegalStateException("Too late to add mixin configs!");
}

mixinConfigs.add(config);
mixinConfigs.add(new ConfigInfo(config, modId));
}

static void registerConfigs() {
added = true;
mixinConfigs.forEach(Mixins::addConfiguration);
mixinConfigs.forEach(cfg -> Mixins.addConfiguration(cfg.fileName()));
final var configMap = Mixins.getConfigs().stream().collect(
Collectors.toMap(Config::getName, Config::getConfig)
);
mixinConfigs.forEach(cfg -> {
if (cfg.modId() == null) return;

final var config = configMap.get(cfg.fileName());
if (config == null) {
LOG.warn("Config file {} was not registered!", cfg.fileName());
} else {
config.decorate(FabricUtil.KEY_MOD_ID, cfg.modId());
}
});
mixinConfigs.clear();
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
net.neoforged.fml.loading.FMLServiceProvider
net.neoforged.fml.loading.FMLServiceProvider
net.neoforged.fml.loading.MixinSyntheticPackageProvider
Loading