mirror of
https://github.com/FabricMC/fabric.git
synced 2025-03-31 01:00:18 -04:00
ModResourcePackUtil: Properly handle special chars in mod name (#2407)
* ModResourcePackUtil: Properly handle special chars in mod name * Add tests * Fix NPE in testmod
This commit is contained in:
parent
b35fea83d3
commit
e6ea5984c7
2 changed files with 46 additions and 11 deletions
fabric-resource-loader-v0/src
main/java/net/fabricmc/fabric/impl/resource/loader
testmod/java/net/fabricmc/fabric/test/resource/loader
|
@ -19,8 +19,11 @@ package net.fabricmc.fabric.impl.resource.loader;
|
|||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
|
@ -40,6 +43,8 @@ import net.fabricmc.loader.api.metadata.ModMetadata;
|
|||
* Internal utilities for managing resource packs.
|
||||
*/
|
||||
public final class ModResourcePackUtil {
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
private ModResourcePackUtil() {
|
||||
}
|
||||
|
||||
|
@ -71,21 +76,23 @@ public final class ModResourcePackUtil {
|
|||
public static InputStream openDefault(ModMetadata info, ResourceType type, String filename) {
|
||||
switch (filename) {
|
||||
case "pack.mcmeta":
|
||||
String description = info.getName();
|
||||
|
||||
if (description == null) {
|
||||
description = "";
|
||||
} else {
|
||||
description = description.replaceAll("\"", "\\\"");
|
||||
}
|
||||
|
||||
String pack = String.format("{\"pack\":{\"pack_format\":" + type.getPackVersion(SharedConstants.getGameVersion()) + ",\"description\":\"%s\"}}", description);
|
||||
return IOUtils.toInputStream(pack, Charsets.UTF_8);
|
||||
String description = Objects.requireNonNullElse(info.getName(), "");
|
||||
String metadata = serializeMetadata(type.getPackVersion(SharedConstants.getGameVersion()), description);
|
||||
return IOUtils.toInputStream(metadata, Charsets.UTF_8);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String serializeMetadata(int packVersion, String description) {
|
||||
JsonObject pack = new JsonObject();
|
||||
pack.addProperty("pack_format", packVersion);
|
||||
pack.addProperty("description", description);
|
||||
JsonObject metadata = new JsonObject();
|
||||
metadata.add("pack", pack);
|
||||
return GSON.toJson(metadata);
|
||||
}
|
||||
|
||||
public static String getName(ModMetadata info) {
|
||||
if (info.getName() != null) {
|
||||
return info.getName();
|
||||
|
|
|
@ -16,14 +16,18 @@
|
|||
|
||||
package net.fabricmc.fabric.test.resource.loader;
|
||||
|
||||
import org.slf4j.LoggerFactory;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
|
||||
import net.fabricmc.fabric.api.resource.ResourcePackActivationType;
|
||||
import net.fabricmc.fabric.impl.resource.loader.ModResourcePackUtil;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
|
||||
public class BuiltinResourcePackTestMod implements ModInitializer {
|
||||
|
@ -31,6 +35,8 @@ public class BuiltinResourcePackTestMod implements ModInitializer {
|
|||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(BuiltinResourcePackTestMod.class);
|
||||
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
// Should always be present as it's **this** mod.
|
||||
|
@ -42,5 +48,27 @@ public class BuiltinResourcePackTestMod implements ModInitializer {
|
|||
.map(container -> ResourceManagerHelper.registerBuiltinResourcePack(new Identifier(MODID, "test2"),
|
||||
container, ResourcePackActivationType.NORMAL))
|
||||
.filter(success -> !success).ifPresent(success -> LOGGER.warn("Could not register built-in resource pack."));
|
||||
|
||||
// Test various metadata serialization issues (#2407)
|
||||
testMetadataSerialization("");
|
||||
testMetadataSerialization("Quotes: \"\" \"");
|
||||
testMetadataSerialization("Backslash: \\ \\\\");
|
||||
}
|
||||
|
||||
private void testMetadataSerialization(String description) {
|
||||
String metadata = ModResourcePackUtil.serializeMetadata(1, description);
|
||||
JsonObject json;
|
||||
|
||||
try {
|
||||
json = GSON.fromJson(metadata, JsonObject.class);
|
||||
} catch (JsonParseException exc) {
|
||||
throw new AssertionError("Metadata parsing test for description \"%s\" failed".formatted(description), exc);
|
||||
}
|
||||
|
||||
String parsedDescription = json.get("pack").getAsJsonObject().get("description").getAsString();
|
||||
|
||||
if (!description.equals(parsedDescription)) {
|
||||
throw new AssertionError("Metadata parsing test for description failed: expected \"%s\", got \"%s\"".formatted(description, parsedDescription));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue