9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-28 03:19:14 +00:00

资源包验证一阶段

This commit is contained in:
XiaoMoMi
2025-06-10 04:04:33 +08:00
parent 9b4ae7e691
commit 614f482a1e
8 changed files with 48 additions and 23 deletions

View File

@@ -57,10 +57,11 @@ public abstract class AbstractPackManager implements PackManager {
public static final Map<Key, JsonObject> PRESET_LEGACY_MODELS_ITEM = new HashMap<>();
public static final Map<Key, JsonObject> PRESET_MODELS_BLOCK = new HashMap<>();
public static final Map<Key, JsonObject> PRESET_ITEMS = new HashMap<>();
public static final Set<Key> VANILLA_ITEM_TEXTURES = new HashSet<>();
public static final Set<Key> VANILLA_BLOCK_TEXTURES = new HashSet<>();
public static final Set<Key> VANILLA_FONT_TEXTURES = new HashSet<>();
public static final Set<Key> VANILLA_TEXTURES = new HashSet<>();
public static final Set<Key> VANILLA_MODELS = new HashSet<>();
private static final byte[] EMPTY_IMAGE;
private static final String[] TRIM_ITEMS = {"boots_trim","chestplate_trim","helmet_trim","leggings_trim"};
private static final String[] TRIM_COLOR_PALETTES = {"amethyst","copper","diamond","diamond_darker","emerald","gold","gold_darker","iron","iron_darker","lapis","netherite","netherite_darker","quartz","redstone","resin","trim_palette"};
static {
var stream = new ByteArrayOutputStream();
try {
@@ -98,13 +99,21 @@ public abstract class AbstractPackManager implements PackManager {
private void initInternalData() {
loadInternalData("internal/models/item/legacy/_all.json", PRESET_LEGACY_MODELS_ITEM::put);
loadInternalData("internal/models/item/modern/_all.json", PRESET_MODERN_MODELS_ITEM::put);
loadInternalData("internal/models/item/_all.json", PRESET_MODERN_MODELS_ITEM::put);
loadInternalData("internal/models/block/_all.json", PRESET_MODELS_BLOCK::put);
loadInternalData("internal/items/_all.json", PRESET_ITEMS::put);
loadInternalList("internal/textures/block/_list.json", VANILLA_BLOCK_TEXTURES::add);
loadInternalList("internal/textures/item/_list.json", VANILLA_ITEM_TEXTURES::add);
loadInternalList("internal/textures/font/_list.json", VANILLA_FONT_TEXTURES::add);
loadInternalList("textures", "block/", VANILLA_TEXTURES::add);
loadInternalList("textures", "item/", VANILLA_TEXTURES::add);
loadInternalList("textures", "font/", VANILLA_TEXTURES::add);
for (String trimItem : TRIM_ITEMS) {
for (String trimColorPalette : TRIM_COLOR_PALETTES) {
VANILLA_TEXTURES.add(Key.of("minecraft", "trims/items/" + trimItem + "_" + trimColorPalette));
}
}
loadInternalList("models", "block/", VANILLA_MODELS::add);
loadInternalList("models", "item/", VANILLA_MODELS::add);
}
private void loadInternalData(String path, BiConsumer<Key, JsonObject> callback) {
@@ -122,19 +131,25 @@ public abstract class AbstractPackManager implements PackManager {
}
}
private void loadInternalList(String path, Consumer<Key> callback) {
try (InputStream inputStream = this.plugin.resourceStream(path)) {
private void loadInternalList(String type, String prefix, Consumer<Key> callback) {
try (InputStream inputStream = this.plugin.resourceStream("internal/" + type + "/" + prefix + "_list.json")) {
if (inputStream != null) {
JsonObject listJson = JsonParser.parseReader(new InputStreamReader(inputStream)).getAsJsonObject();
JsonArray list = listJson.getAsJsonArray("files");
for (JsonElement element : list) {
JsonArray fileList = listJson.getAsJsonArray("files");
for (JsonElement element : fileList) {
if (element instanceof JsonPrimitive primitive) {
callback.accept(Key.of(FileUtils.pathWithoutExtension(primitive.getAsString())));
callback.accept(Key.of(prefix + FileUtils.pathWithoutExtension(primitive.getAsString())));
}
}
JsonArray directoryList = listJson.getAsJsonArray("directories");
for (JsonElement element : directoryList) {
if (element instanceof JsonPrimitive primitive) {
loadInternalList(type, prefix + primitive.getAsString() + "/", callback);
}
}
}
} catch (IOException e) {
this.plugin.logger().warn("Failed to load " + path, e);
this.plugin.logger().warn("Failed to load internal _list.json" + prefix, e);
}
}

View File

@@ -59,6 +59,8 @@ public class Config {
protected boolean resource_pack$protection$crash_tools$method_2;
protected boolean resource_pack$protection$crash_tools$method_3;
protected boolean resource_pack$validate$enable;
protected boolean resource_pack$protection$obfuscation$enable;
protected long resource_pack$protection$obfuscation$seed;
protected boolean resource_pack$protection$obfuscation$fake_directory;
@@ -259,6 +261,7 @@ public class Config {
resource_pack$protection$obfuscation$resource_location$bypass_models = config.getStringList("resource-pack.protection.obfuscation.resource-location.bypass-models");
resource_pack$protection$obfuscation$resource_location$bypass_sounds = config.getStringList("resource-pack.protection.obfuscation.resource-location.bypass-sounds");
resource_pack$protection$obfuscation$resource_location$bypass_equipments = config.getStringList("resource-pack.protection.obfuscation.resource-location.bypass-equipments");
resource_pack$validate$enable = config.getBoolean("resource-pack.validate.enable", true);
try {
resource_pack$duplicated_files_handler = config.getMapList("resource-pack.duplicated-files-handler").stream().map(it -> {
@@ -729,6 +732,10 @@ public class Config {
return instance.chunk_system$injection$target;
}
public static boolean validateResourcePack() {
return instance.resource_pack$validate$enable;
}
public YamlDocument loadOrCreateYamlData(String fileName) {
Path path = this.plugin.dataFolderPath().resolve(fileName);
if (!Files.exists(path)) {

View File

@@ -30,7 +30,7 @@ public class StringKeyConstructor extends SafeConstructor {
Object value = constructObject(valueNode);
Object previous = map.put(key, value);
if (previous != null) {
TranslationManager.instance().log("warning.config.yaml.duplicated_key", this.path.toString(), key, String.valueOf(node.getStartMark().getLine() + 1));
TranslationManager.instance().log("warning.config.yaml.duplicated_key", this.path.toAbsolutePath().toString(), key, String.valueOf(node.getStartMark().getLine() + 1));
}
}
return map;