Skip to content

Commit

Permalink
Revert "Split mods.toml parsing"
Browse files Browse the repository at this point in the history
This reverts commit ede0495.
  • Loading branch information
shedaniel committed Nov 13, 2023
1 parent ede0495 commit 05f38c5
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 157 deletions.
29 changes: 0 additions & 29 deletions src/main/java/dev/architectury/loom/metadata/ForgeModsToml.java

This file was deleted.

72 changes: 22 additions & 50 deletions src/main/java/dev/architectury/loom/metadata/ModMetadataFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import com.google.common.collect.ImmutableMap;
Expand All @@ -13,7 +14,6 @@
import org.gradle.api.tasks.SourceSet;
import org.jetbrains.annotations.Nullable;

import net.fabricmc.loom.util.ModPlatform;
import net.fabricmc.loom.util.ZipUtils;
import net.fabricmc.loom.util.gradle.SourceSetHelper;

Expand All @@ -22,27 +22,35 @@
*/
public final class ModMetadataFiles {
private static final Logger LOGGER = Logging.getLogger(ModMetadataFiles.class);
private static final Map<String, MetadataFileProvider> SINGLE_FILE_METADATA_TYPES = ImmutableMap.<String, MetadataFileProvider>builder()
.put(QuiltModJson.FILE_NAME, MetadataFileProvider.withPlatform((bytes, platform) -> QuiltModJson.of(bytes), ModPlatform.QUILT))
.put(ArchitecturyCommonJson.FILE_NAME, (bytes, platform) -> ArchitecturyCommonJson.of(bytes))
.put(ModsToml.FILE_PATH, MetadataFileProvider.concat(
MetadataFileProvider.withPlatform((bytes, platform) -> ForgeModsToml.of(bytes), ModPlatform.FORGE),
MetadataFileProvider.withPlatform((bytes, platform) -> NeoForgeModsToml.of(bytes), ModPlatform.NEOFORGE)
).onError("Could not load mods.toml", () -> new ErroringModMetadataFile("mods.toml")))
private static final Map<String, Function<byte[], ModMetadataFile>> SINGLE_FILE_METADATA_TYPES = ImmutableMap.<String, Function<byte[], ModMetadataFile>>builder()
.put(QuiltModJson.FILE_NAME, QuiltModJson::of)
.put(ArchitecturyCommonJson.FILE_NAME, ArchitecturyCommonJson::of)
.put(ModsToml.FILE_PATH, onError(ModsToml::of, "Could not load mods.toml", () -> new ErroringModMetadataFile("mods.toml")))
.build();

private static <A, B> Function<A, B> onError(Function<A, B> fn, String message, Supplier<B> onError) {
return a -> {
try {
return fn.apply(a);
} catch (Exception e) {
LOGGER.info(message, e);
return onError.get();
}
};
}

/**
* Reads the mod metadata file from a jar.
*
* @param jar the path to the jar file
* @return the mod metadata file, or {@code null} if not found
*/
public static @Nullable ModMetadataFile fromJar(Path jar, @Nullable ModPlatform platform) throws IOException {
public static @Nullable ModMetadataFile fromJar(Path jar) throws IOException {
for (final String filePath : SINGLE_FILE_METADATA_TYPES.keySet()) {
final byte @Nullable [] bytes = ZipUtils.unpackNullable(jar, filePath);

if (bytes != null) {
return SINGLE_FILE_METADATA_TYPES.get(filePath).apply(bytes, platform);
return SINGLE_FILE_METADATA_TYPES.get(filePath).apply(bytes);
}
}

Expand All @@ -55,12 +63,12 @@ public final class ModMetadataFiles {
* @param directory the path to the directory
* @return the mod metadata file, or {@code null} if not found
*/
public static @Nullable ModMetadataFile fromDirectory(Path directory, @Nullable ModPlatform platform) throws IOException {
public static @Nullable ModMetadataFile fromDirectory(Path directory) throws IOException {
for (final String filePath : SINGLE_FILE_METADATA_TYPES.keySet()) {
final Path metadataPath = directory.resolve(filePath);

if (Files.exists(metadataPath)) {
return SINGLE_FILE_METADATA_TYPES.get(filePath).apply(Files.readAllBytes(metadataPath), platform);
return SINGLE_FILE_METADATA_TYPES.get(filePath).apply(Files.readAllBytes(metadataPath));
}
}

Expand All @@ -73,51 +81,15 @@ public final class ModMetadataFiles {
* @param sourceSets the source sets to read from
* @return the mod metadata file, or {@code null} if not found
*/
public static @Nullable ModMetadataFile fromSourceSets(@Nullable ModPlatform platform, SourceSet... sourceSets) throws IOException {
public static @Nullable ModMetadataFile fromSourceSets(SourceSet... sourceSets) throws IOException {
for (final String filePath : SINGLE_FILE_METADATA_TYPES.keySet()) {
final @Nullable File file = SourceSetHelper.findFirstFileInResource(filePath, sourceSets);

if (file != null) {
return SINGLE_FILE_METADATA_TYPES.get(filePath).apply(Files.readAllBytes(file.toPath()), platform);
return SINGLE_FILE_METADATA_TYPES.get(filePath).apply(Files.readAllBytes(file.toPath()));
}
}

return null;
}

@FunctionalInterface
private interface MetadataFileProvider {
@Nullable
ModMetadataFile apply(byte[] bytes, @Nullable ModPlatform platform);

default MetadataFileProvider onError(String message, Supplier<ModMetadataFile> onError) {
MetadataFileProvider provider = this;
return (bytes, platform) -> {
try {
return provider.apply(bytes, platform);
} catch (Exception e) {
LOGGER.info(message, e);
return onError.get();
}
};
}

static MetadataFileProvider withPlatform(MetadataFileProvider provider, ModPlatform requiredPlatform) {
return (bytes, platform) -> {
if (requiredPlatform != platform) return null;
return provider.apply(bytes, platform);
};
}

static MetadataFileProvider concat(MetadataFileProvider... providers) {
return (bytes, platform) -> {
for (MetadataFileProvider provider : providers) {
ModMetadataFile file = provider.apply(bytes, platform);
if (file != null) return file;
}

return null;
};
}
}
}
23 changes: 11 additions & 12 deletions src/main/java/dev/architectury/loom/metadata/ModsToml.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.io.ParsingException;
Expand All @@ -21,36 +20,36 @@
import net.fabricmc.loom.configuration.ifaceinject.InterfaceInjectionProcessor;
import net.fabricmc.loom.util.ExceptionUtil;

public abstract sealed class ModsToml implements ModMetadataFile permits ForgeModsToml, NeoForgeModsToml {
public final class ModsToml implements ModMetadataFile {
public static final String FILE_PATH = "META-INF/mods.toml";
protected final Config config;
private final Config config;

protected ModsToml(Config config) {
private ModsToml(Config config) {
this.config = Objects.requireNonNull(config);
}

static <T extends ModsToml> T of(byte[] utf8, Function<Config, T> constructor) {
return of(new String(utf8, StandardCharsets.UTF_8), constructor);
public static ModsToml of(byte[] utf8) {
return of(new String(utf8, StandardCharsets.UTF_8));
}

static <T extends ModsToml> T of(String text, Function<Config, T> constructor) {
public static ModsToml of(String text) {
try {
return constructor.apply(new TomlParser().parse(text));
return new ModsToml(new TomlParser().parse(text));
} catch (ParsingException e) {
throw ExceptionUtil.createDescriptiveWrapper(IllegalArgumentException::new, "Could not parse mods.toml", e);
}
}

static <T extends ModsToml> T of(Path path, Function<Config, T> constructor) throws IOException {
public static ModsToml of(Path path) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
return constructor.apply(new TomlParser().parse(reader));
return new ModsToml(new TomlParser().parse(reader));
} catch (ParsingException e) {
throw ExceptionUtil.createDescriptiveWrapper(IllegalArgumentException::new, "Could not parse mods.toml", e);
}
}

static <T extends ModsToml> T of(File file, Function<Config, T> constructor) throws IOException {
return of(file.toPath(), constructor);
public static ModsToml of(File file) throws IOException {
return of(file.toPath());
}

@Override
Expand Down
47 changes: 0 additions & 47 deletions src/main/java/dev/architectury/loom/metadata/NeoForgeModsToml.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import dev.architectury.loom.metadata.ModMetadataFile;
import dev.architectury.loom.metadata.ModMetadataFiles;

import net.fabricmc.loom.util.ModPlatform;
import net.fabricmc.loom.util.ZipUtils;
import net.fabricmc.loom.util.function.CollectionUtil;

Expand All @@ -49,7 +48,7 @@ public record AccessWidenerFile(
/**
* Reads the access-widener contained in a mod jar, or returns null if there is none.
*/
public static AccessWidenerFile fromModJar(Path modJarPath, ModPlatform platform) {
public static AccessWidenerFile fromModJar(Path modJarPath) {
byte[] modJsonBytes;

try {
Expand All @@ -63,7 +62,7 @@ public static AccessWidenerFile fromModJar(Path modJarPath, ModPlatform platform
String awPath;

try {
modMetadata = ModMetadataFiles.fromJar(modJarPath, platform);
modMetadata = ModMetadataFiles.fromJar(modJarPath);

if (modMetadata != null) {
final Set<String> accessWideners = modMetadata.getAccessWideners();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static AccessWidenerData readAccessWidenerData(Path inputJar, ModPlatform
return null;
}

final FabricModJson fabricModJson = FabricModJsonFactory.createFromZip(inputJar, platform);
final FabricModJson fabricModJson = FabricModJsonFactory.createFromZip(inputJar);
final List<String> classTweakers = List.copyOf(fabricModJson.getClassTweakers().keySet());

if (classTweakers.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static List<FabricModJson> getDependentMods(Project project) {
final Set<File> artifacts = entry.getSourceConfiguration().get().resolve();

for (File artifact : artifacts) {
final FabricModJson fabricModJson = FabricModJsonFactory.createFromZipNullable(artifact.toPath(), extension.getPlatform().get());
final FabricModJson fabricModJson = FabricModJsonFactory.createFromZipNullable(artifact.toPath());

if (fabricModJson != null) {
mods.add(fabricModJson);
Expand Down Expand Up @@ -118,7 +118,7 @@ private static Stream<FabricModJson> getCompileRuntimeModsFromRemapConfigs(Proje
.filter(settings -> settings.getApplyDependencyTransforms().get())
.flatMap(resolveArtifacts(project, false))
.filter(runtimeEntries::contains) // Use the intersection of the two configurations.
.map(zipPath -> FabricModJsonFactory.createFromZipOptional(zipPath, extension.getPlatform().get()))
.map(FabricModJsonFactory::createFromZipOptional)
.filter(Optional::isPresent)
.map(Optional::get)
.sorted(Comparator.comparing(FabricModJson::getId));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/fabricmc/loom/task/RemapJarTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private void setupLegacyMixinRefmapRemapping(RemapParams params) {
final MixinExtension mixinExtension = extension.getMixin();

final Collection<String> allMixinConfigs = new LinkedHashSet<>();
final FabricModJson fabricModJson = FabricModJsonFactory.createFromZipNullable(getInputFile().getAsFile().get().toPath(), extension.getPlatform().get());
final FabricModJson fabricModJson = FabricModJsonFactory.createFromZipNullable(getInputFile().getAsFile().get().toPath());

if (fabricModJson != null) {
allMixinConfigs.addAll(fabricModJson.getMixinConfigurations());
Expand Down Expand Up @@ -395,7 +395,7 @@ private boolean injectAccessWidener() throws IOException {
}

private void remapAccessWidener() throws IOException {
final AccessWidenerFile accessWidenerFile = AccessWidenerFile.fromModJar(inputFile, getParameters().getPlatform().get());
final AccessWidenerFile accessWidenerFile = AccessWidenerFile.fromModJar(inputFile);

if (accessWidenerFile == null) {
return;
Expand Down
Loading

0 comments on commit 05f38c5

Please sign in to comment.