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

Add a method for mod dependent mixins #524

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Optional;

import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.Version;
import net.fabricmc.loader.api.metadata.ContactInformation;
import net.fabricmc.loader.api.metadata.CustomValue;
Expand Down Expand Up @@ -138,7 +139,7 @@ public Collection<NestedJarEntry> getJars() {
}

@Override
public Collection<String> getMixinConfigs(EnvType type) {
public Collection<String> getMixinConfigs(EnvType type, FabricLoader loader) {
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void init(EnvType side, FabricLoaderImpl loader) {
Map<String, ModContainerImpl> configToModMap = new HashMap<>();

for (ModContainerImpl mod : loader.getModsInternal()) {
for (String config : mod.getMetadata().getMixinConfigs(side)) {
for (String config : mod.getMetadata().getMixinConfigs(side, loader)) {
ModContainerImpl prev = configToModMap.putIfAbsent(config, mod);
if (prev != null) throw new RuntimeException(String.format("Non-unique mixin config name %s used by %s and %s", config, prev.getMetadata().getId(), mod.getMetadata().getId()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;

import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;

/**
* Internal variant of the ModMetadata interface.
Expand All @@ -35,7 +36,7 @@ default String getOldStyleLanguageAdapter() {

Map<String, String> getLanguageAdapterDefinitions();
Collection<NestedJarEntry> getJars();
Collection<String> getMixinConfigs(EnvType type);
Collection<String> getMixinConfigs(EnvType type, FabricLoader loader);
/* @Nullable */
String getAccessWidener();
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Optional;

import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.Version;
import net.fabricmc.loader.api.metadata.ContactInformation;
import net.fabricmc.loader.api.metadata.CustomValue;
Expand Down Expand Up @@ -221,7 +222,7 @@ public Collection<String> getEntrypointKeys() {
public void emitFormatWarnings() { }

@Override
public Collection<String> getMixinConfigs(EnvType type) {
public Collection<String> getMixinConfigs(EnvType type, FabricLoader loader) {
List<String> mixinConfigs = new ArrayList<>(this.mixins.common);

switch (type) {
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/net/fabricmc/loader/impl/metadata/V1ModMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedMap;

import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.Version;
import net.fabricmc.loader.api.metadata.ContactInformation;
import net.fabricmc.loader.api.metadata.CustomValue;
Expand Down Expand Up @@ -220,12 +222,12 @@ public Collection<NestedJarEntry> getJars() {
}

@Override
public Collection<String> getMixinConfigs(EnvType type) {
public Collection<String> getMixinConfigs(EnvType type, FabricLoader loader) {
final List<String> mixinConfigs = new ArrayList<>();

// This is only ever called once, so no need to store the result of this.
for (MixinEntry mixin : this.mixins) {
if (mixin.environment.matches(type)) {
if (mixin.environment.matches(type) && mixin.areDependencesMet(loader)) {
mixinConfigs.add(mixin.config);
}
}
Expand Down Expand Up @@ -306,10 +308,22 @@ public String getFile() {
static final class MixinEntry {
private final String config;
private final ModEnvironment environment;
private final Set<String> depends;

MixinEntry(String config, ModEnvironment environment) {
MixinEntry(String config, ModEnvironment environment, Set<String> depends) {
this.config = config;
this.environment = environment;
this.depends = depends;
}

public boolean areDependencesMet(FabricLoader loader) {
for (String modid : depends) {
if (!loader.isModLoaded(modid)) {
return false;
}
}

return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

Expand Down Expand Up @@ -104,7 +107,7 @@ static LoaderModMetadata parse(JsonReader reader) throws IOException, ParseMetad
break;
case "id":
if (reader.peek() != JsonToken.STRING) {
throw new ParseMetadataException("Mod id must be a non-empty string with a length of 3-64 characters.", reader);
throw new ParseMetadataException("Mod id must be a non-empty string with a length of 2-64 characters.", reader);
}

id = reader.nextString();
Expand Down Expand Up @@ -382,13 +385,14 @@ private static void readMixinConfigs(List<ParseWarning> warnings, JsonReader rea
switch (reader.peek()) {
case STRING:
// All mixin configs specified via string are assumed to be universal
mixins.add(new V1ModMetadata.MixinEntry(reader.nextString(), ModEnvironment.UNIVERSAL));
mixins.add(new V1ModMetadata.MixinEntry(reader.nextString(), ModEnvironment.UNIVERSAL, Collections.emptySet()));
break;
case BEGIN_OBJECT:
reader.beginObject();

String config = null;
ModEnvironment environment = null;
Set<String> depends = null;
Chocohead marked this conversation as resolved.
Show resolved Hide resolved

while (reader.hasNext()) {
final String key = reader.nextName();
Expand All @@ -398,6 +402,27 @@ private static void readMixinConfigs(List<ParseWarning> warnings, JsonReader rea
case "environment":
environment = V1ModMetadataParser.readEnvironment(reader);
break;
case "depends":
if (reader.peek() != JsonToken.BEGIN_ARRAY) {
warnings.add(new ParseWarning(reader.getLineNumber(), reader.getColumn(), "Mixin config \"depends\" must be an array"));
reader.skipValue();
continue;
}

reader.beginArray();
depends = new HashSet<>();

while (reader.hasNext()) {
if (reader.peek() == JsonToken.STRING) {
depends.add(reader.nextString());
} else {
warnings.add(new ParseWarning(reader.getLineNumber(), reader.getColumn(), "Invalid mixin config dependency type"));
Chocohead marked this conversation as resolved.
Show resolved Hide resolved
reader.skipValue();
}
}

reader.endArray();
break;
case "config":
if (reader.peek() != JsonToken.STRING) {
throw new ParseMetadataException("Value of \"config\" must be a string", reader);
Expand All @@ -417,11 +442,15 @@ private static void readMixinConfigs(List<ParseWarning> warnings, JsonReader rea
environment = ModEnvironment.UNIVERSAL; // Default to universal
}

if (depends == null) {
depends = Collections.emptySet(); // Default to depending on nothing
}

if (config == null) {
throw new ParseMetadataException.MissingField("Missing mandatory key 'config' in mixin entry!");
}

mixins.add(new V1ModMetadata.MixinEntry(config, environment));
mixins.add(new V1ModMetadata.MixinEntry(config, environment, depends));
break;
default:
warnings.add(new ParseWarning(reader.getLineNumber(), reader.getColumn(), "Invalid mixin entry type"));
Expand Down