|
| 1 | +/* |
| 2 | + * Hello Minecraft! Launcher |
| 3 | + * Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package org.glavo.nbt.internal.schema; |
| 19 | + |
| 20 | +import org.glavo.nbt.tag.CompoundTag; |
| 21 | +import org.glavo.nbt.tag.Tag; |
| 22 | +import org.glavo.nbt.tag.TagType; |
| 23 | +import org.glavo.nbt.validation.NBTSchema; |
| 24 | +import org.glavo.nbt.validation.NBTValidationException; |
| 25 | +import org.jetbrains.annotations.Unmodifiable; |
| 26 | + |
| 27 | +import java.util.LinkedHashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Objects; |
| 30 | + |
| 31 | +public final class CompoundTagSchema implements TagSchema<CompoundTag> { |
| 32 | + private final @Unmodifiable List<SchemaEntry> entries; |
| 33 | + |
| 34 | + public CompoundTagSchema(@Unmodifiable List<SchemaEntry> entries) { |
| 35 | + this.entries = entries; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public boolean testTag(Tag tag) { |
| 40 | + if (!(tag instanceof CompoundTag compoundTag)) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + for (SchemaEntry entry : entries) { |
| 45 | + Tag subTag = compoundTag.get(entry.name()); |
| 46 | + if (subTag == null) { |
| 47 | + if (entry.required) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + } else { |
| 51 | + if (!entry.schema.testTag(subTag)) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void validateTag(Tag tag) throws NBTValidationException { |
| 61 | + if (!(tag instanceof CompoundTag compoundTag)) { |
| 62 | + throw new NBTValidationException("Expected TAG_Compound, but got " + tag.getType()); |
| 63 | + } |
| 64 | + |
| 65 | + for (SchemaEntry entry : entries) { |
| 66 | + Tag subTag = compoundTag.get(entry.name()); |
| 67 | + if (subTag != null) { |
| 68 | + try { |
| 69 | + entry.schema.validateTag(subTag); |
| 70 | + } catch (NBTValidationException e) { |
| 71 | + throw new NBTValidationException("Invalid subtag " + entry.name(), e); |
| 72 | + } |
| 73 | + } else { |
| 74 | + if (entry.required) { |
| 75 | + throw new NBTValidationException("Missing required subtag: " + entry.name()); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public record SchemaEntry(String name, TagSchema<?> schema, boolean required) { |
| 82 | + } |
| 83 | + |
| 84 | + public static final class Builder implements NBTSchema.Builder.OfCompoundTag { |
| 85 | + private final LinkedHashMap<String, SchemaEntry> entries = new LinkedHashMap<>(); |
| 86 | + |
| 87 | + |
| 88 | + private void add(String name, NBTSchema<?> schema, boolean required) { |
| 89 | + entries.put(name, new SchemaEntry(name, (TagSchema<?>) schema, required)); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public OfCompoundTag addRequired(String name, NBTSchema<?> schema) { |
| 94 | + Objects.requireNonNull(name, "name"); |
| 95 | + Objects.requireNonNull(schema, "schema"); |
| 96 | + |
| 97 | + add(name, schema, true); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public OfCompoundTag addOptional(String name, NBTSchema<?> schema) { |
| 103 | + Objects.requireNonNull(name, "name"); |
| 104 | + Objects.requireNonNull(schema, "schema"); |
| 105 | + |
| 106 | + add(name, schema, false); |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public OfCompoundTag addRequired(String name, TagType<?> type) { |
| 112 | + Objects.requireNonNull(name, "name"); |
| 113 | + Objects.requireNonNull(type, "type"); |
| 114 | + |
| 115 | + add(name, new TypeIsSchema<>(type), true); |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public OfCompoundTag addOptional(String name, TagType<?> type) { |
| 121 | + Objects.requireNonNull(name, "name"); |
| 122 | + Objects.requireNonNull(type, "type"); |
| 123 | + |
| 124 | + add(name, new TypeIsSchema<>(type), false); |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public OfCompoundTag addRequired(String name, Tag tag) { |
| 130 | + Objects.requireNonNull(name, "name"); |
| 131 | + Objects.requireNonNull(tag, "tag"); |
| 132 | + |
| 133 | + add(name, new MatchSchema<>(tag.clone()), true); |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public OfCompoundTag addOptional(String name, Tag tag) { |
| 139 | + Objects.requireNonNull(name, "name"); |
| 140 | + Objects.requireNonNull(tag, "tag"); |
| 141 | + |
| 142 | + add(name, new MatchSchema<>(tag.clone()), false); |
| 143 | + return this; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public NBTSchema<CompoundTag> build() { |
| 148 | + return new CompoundTagSchema(List.copyOf(entries.values())); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments