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

Allow array of strings for environment in metadata #689

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
9 changes: 8 additions & 1 deletion src/main/java/net/fabricmc/loader/impl/FabricLoaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import net.fabricmc.loader.impl.discovery.RuntimeModRemapper;
import net.fabricmc.loader.impl.entrypoint.EntrypointStorage;
import net.fabricmc.loader.impl.game.GameProvider;
import net.fabricmc.loader.impl.launch.FabricLauncher;
import net.fabricmc.loader.impl.launch.FabricLauncherBase;
import net.fabricmc.loader.impl.launch.knot.Knot;
import net.fabricmc.loader.impl.metadata.DependencyOverrides;
Expand Down Expand Up @@ -373,7 +374,13 @@ public boolean isModLoaded(String id) {

@Override
public boolean isDevelopmentEnvironment() {
return FabricLauncherBase.getLauncher().isDevelopment();
FabricLauncher launcher = FabricLauncherBase.getLauncher();

if (launcher != null) {
return launcher.isDevelopment();
} else {
return true;
}
}

private void addMod(ModCandidate candidate) throws ModResolutionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ static LoaderModMetadata parse(JsonReader reader) throws IOException, ParseMetad
readProvides(reader, provides);
break;
case "environment":
if (reader.peek() != JsonToken.STRING) {
throw new ParseMetadataException("Environment must be a string", reader);
}

environment = readEnvironment(reader);
break;
case "entrypoints":
Expand Down Expand Up @@ -247,17 +243,48 @@ private static void readProvides(JsonReader reader, List<String> provides) throw
}

private static ModEnvironment readEnvironment(JsonReader reader) throws ParseMetadataException, IOException {
final String environment = reader.nextString().toLowerCase(Locale.ROOT);

if (environment.isEmpty() || environment.equals("*")) {
return ModEnvironment.UNIVERSAL;
} else if (environment.equals("client")) {
return ModEnvironment.CLIENT;
} else if (environment.equals("server")) {
return ModEnvironment.SERVER;
ModEnvironment environment = null;
List<String> environmentValues = new ArrayList<>();

if (reader.peek() == JsonToken.STRING) {
environmentValues.add(reader.nextString().toLowerCase(Locale.ROOT));
} else if (reader.peek() == JsonToken.BEGIN_ARRAY) {
reader.beginArray();

while (reader.hasNext()) {
if (reader.peek() == JsonToken.STRING) {
environmentValues.add(reader.nextString().toLowerCase(Locale.ROOT));
} else {
throw new ParseMetadataException("Environment must be a string or an array of strings", reader);
}
}

reader.endArray();
} else {
throw new ParseMetadataException("Invalid environment type: " + environment + "!", reader);
throw new ParseMetadataException("Environment must be a string or an array of strings", reader);
}

for (String environmentString : environmentValues) {
if (environmentString.isEmpty() || environmentString.equals("*")) {
environment = ModEnvironment.UNIVERSAL;
} else if (environmentString.equals("client")) {
if (environment == null) {
environment = ModEnvironment.CLIENT;
} else if (environment == ModEnvironment.SERVER) {
environment = ModEnvironment.UNIVERSAL;
}
} else if (environmentString.equals("server")) {
if (environment == null) {
environment = ModEnvironment.SERVER;
} else if (environment == ModEnvironment.CLIENT) {
environment = ModEnvironment.UNIVERSAL;
}
} else {
throw new ParseMetadataException("Invalid environment type: " + environmentString + "!", reader);
}
}

return environment;
}

private static void readEntrypoints(List<ParseWarning> warnings, JsonReader reader, Map<String, List<EntrypointMetadata>> entrypoints) throws IOException, ParseMetadataException {
Expand Down
13 changes: 10 additions & 3 deletions src/test/java/net/fabricmc/test/V1ModJsonParsingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@
import java.util.Map;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import net.fabricmc.loader.api.SemanticVersion;
import net.fabricmc.loader.api.metadata.CustomValue;
import net.fabricmc.loader.api.metadata.ModEnvironment;
import net.fabricmc.loader.impl.metadata.DependencyOverrides;
import net.fabricmc.loader.impl.metadata.LoaderModMetadata;
import net.fabricmc.loader.impl.metadata.ModMetadataParser;
import net.fabricmc.loader.impl.metadata.ParseMetadataException;
import net.fabricmc.loader.impl.metadata.VersionOverrides;

@Disabled // TODO needs fixing.
final class V1ModJsonParsingTests {
private static Path testLocation;
private static Path specPath;
Expand Down Expand Up @@ -160,6 +159,14 @@ public void testLongFile() throws IOException, ParseMetadataException {
}
}

@Test
@DisplayName("Environment array")
public void testEnvironmentArray() throws IOException, ParseMetadataException {
final LoaderModMetadata modMetadata = parseMetadata(specPath.resolve("environment_array.json"));

assertEquals(ModEnvironment.UNIVERSAL, modMetadata.getEnvironment());
}

/*
* Spec violation tests
*/
Expand Down Expand Up @@ -188,7 +195,7 @@ public void testWarnings() { }

private static LoaderModMetadata parseMetadata(Path path) throws IOException, ParseMetadataException {
try (InputStream is = Files.newInputStream(path)) {
return ModMetadataParser.parseMetadata(null, "dummy", Collections.emptyList(), new VersionOverrides(), new DependencyOverrides(Paths.get("randomMissing")));
return ModMetadataParser.parseMetadata(is, "dummy", Collections.emptyList(), new VersionOverrides(), new DependencyOverrides(Paths.get("randomMissing")));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "v1-parsing-test",
"version": "1.0.0-SNAPSHOT",
"schemaVersion": 1,
"environment": ["client", "server"]
}