|
1 | 1 | package rml.loader.api.config.v2.config.elements; |
2 | 2 |
|
| 3 | +import net.minecraft.util.ResourceLocation; |
3 | 4 | import org.objectweb.asm.MethodVisitor; |
4 | 5 | import org.objectweb.asm.Opcodes; |
5 | 6 | import org.objectweb.asm.Type; |
6 | | - |
7 | | -import rml.loader.api.config.v2.config.ConfigUtils; |
| 7 | +import rml.deserializer.*; |
| 8 | +import rml.loader.api.utils.ObjectHelper; |
| 9 | +import rml.loader.deserialize.Deserializer; |
8 | 10 |
|
9 | 11 | import java.util.HashMap; |
10 | | -import java.util.List; |
11 | 12 | import java.util.Map; |
12 | 13 | import java.util.function.BiConsumer; |
13 | 14 |
|
14 | 15 | public class ConfigMap extends ConfigPrimitive { |
| 16 | + public enum ConfigMapType{ |
| 17 | + INT(Integer.class), |
| 18 | + DOUBLE(Double.class), |
| 19 | + STRING(String.class), |
| 20 | + BOOL(Boolean.class), |
| 21 | + INTS(Integer[].class), |
| 22 | + DOUBLES(Double[].class), |
| 23 | + STRINGS(String[].class), |
| 24 | + BOOLS(Boolean[].class) |
| 25 | + ; |
| 26 | + |
| 27 | + public static final AbstractDeserializer<ConfigMapType> DESERIALIZER = Deserializer.MANAGER.addDefaultEntry( |
| 28 | + new AbstractDeserializer<>(new ResourceLocation("rml", "type"), |
| 29 | + ConfigMapType.class, jsonElement -> { |
| 30 | + try { |
| 31 | + return ConfigMapType.valueOf(Deserializer.decode(String.class, jsonElement)); |
| 32 | + } catch (IllegalArgumentException e) { |
| 33 | + throw new JsonDeserializeException(jsonElement, "ConfigMapType not found", e); |
| 34 | + } |
| 35 | + })); |
| 36 | + |
| 37 | + private final Class<?> cls; |
| 38 | + ConfigMapType(Class<?> cls){ |
| 39 | + this.cls = cls; |
| 40 | + } |
| 41 | + |
| 42 | + public Class<?> getType() { |
| 43 | + return cls; |
| 44 | + } |
| 45 | + } |
| 46 | + public static final AbstractDeserializer<ConfigElement> DESERIALIZER = Deserializer.named(ConfigElement.class, new ResourceLocation("rml", "map")) |
| 47 | + .optionalDefault(ConfigDescription.class, "display", ConfigDescription.EMPTY) |
| 48 | + .require(String.class, "name") |
| 49 | + .require(ConfigMapType.class, "map_type") |
| 50 | + .action((manager, jsonObject, context) -> { |
| 51 | + if (context.getJsonObject().has("defaultValue")) { |
| 52 | + Argument.map("defaultValue", context.get(ConfigMapType.class, "map_type").getType()).execute(Deserializer.MANAGER, context.getJsonObject().get("defaultValue").getAsJsonObject(), context); |
| 53 | + } else context.put("defaultValue", new HashMap<>()); |
| 54 | + }) |
| 55 | + .decode(context -> new ConfigMap( |
| 56 | + context.get(ConfigDescription.class, "display"), |
| 57 | + context.get(String.class, "name"), |
| 58 | + context.get(ConfigMapType.class, "map_type").getType(), |
| 59 | + ObjectHelper.static_cast(context.get(Map.class, "defaultValue")))).build(); |
15 | 60 |
|
16 | 61 | protected Class<?> type; |
17 | 62 | protected Map<String, ?> defaultVal; |
18 | 63 |
|
19 | | - public ConfigMap(ConfigGroup parentIn, String nameIn, Class<?> type, Map<String, ?> defaultVal) { |
| 64 | + public ConfigMap(ConfigDescription parentIn, String nameIn, Class<?> type, Map<String, ?> defaultVal) { |
20 | 65 | super(parentIn, nameIn); |
21 | 66 | this.type = type; |
22 | 67 | this.defaultVal = defaultVal == null ? new HashMap<>() : defaultVal; |
23 | 68 | } |
24 | 69 |
|
25 | 70 | @Override |
26 | 71 | public String createDescription() { |
27 | | - return "Lyouyihj/zenutils/api/config/elements/ConfigMap$HashDataMap;"; |
| 72 | + return "Ljava/util/HashMap;"; |
28 | 73 | } |
29 | 74 |
|
30 | 75 | @Override |
31 | 76 | public String createSignature() { |
32 | | - return "Lyouyihj/zenutils/api/config/elements/ConfigMap$HashDataMap<Ljava/lang/String;"+ Type.getDescriptor(this.type) +">;"; |
| 77 | + return "Ljava/util/HashMap<Ljava/lang/String;"+ Type.getDescriptor(this.type) +">;"; |
33 | 78 | } |
34 | 79 |
|
35 | 80 | @Override |
36 | 81 | public void createToStack(MethodVisitor methodVisitor) { |
37 | | - methodVisitor.visitTypeInsn(Opcodes.NEW, "youyihj/zenutils/api/config/elements/ConfigMap$HashDataMap"); |
| 82 | + methodVisitor.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); |
38 | 83 | methodVisitor.visitInsn(Opcodes.DUP); |
39 | | - methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "youyihj/zenutils/api/config/elements/ConfigMap$HashDataMap", "<init>", "()V", false); |
| 84 | + methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false); |
40 | 85 |
|
41 | 86 | BiConsumer<MethodVisitor, ?> consumer = cast(STACK_PUTTER.get(this.type)); |
42 | 87 |
|
@@ -90,8 +135,4 @@ else if (aBoolean == Boolean.TRUE) { |
90 | 135 | }); |
91 | 136 | addStackPutter(String.class, MethodVisitor::visitLdcInsn); |
92 | 137 | } |
93 | | - |
94 | | - public static class HashDataMap<K extends String, V> extends HashMap<String, V> { |
95 | | - |
96 | | - } |
97 | 138 | } |
0 commit comments