|
| 1 | +package org.skriptlang.skript.bukkit.pdc; |
| 2 | + |
| 3 | +import ch.njol.skript.classes.ClassInfo; |
| 4 | +import ch.njol.skript.classes.Serializer; |
| 5 | +import ch.njol.skript.registrations.Classes; |
| 6 | +import ch.njol.yggdrasil.Fields; |
| 7 | +import org.bukkit.Bukkit; |
| 8 | +import org.bukkit.NamespacedKey; |
| 9 | +import org.bukkit.persistence.PersistentDataAdapterContext; |
| 10 | +import org.bukkit.persistence.PersistentDataContainer; |
| 11 | +import org.bukkit.persistence.PersistentDataType; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | +import org.jetbrains.annotations.Unmodifiable; |
| 14 | +import org.skriptlang.skript.lang.converter.Converters; |
| 15 | + |
| 16 | +import java.io.NotSerializableException; |
| 17 | +import java.io.StreamCorruptedException; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +/** |
| 24 | + * A serializer that can serialize and deserialize Yggsdrasil serializable objects to and from PersistentDataContainers. |
| 25 | + */ |
| 26 | +public class PDCSerializer { |
| 27 | + |
| 28 | + /** |
| 29 | + * Types that are directly serializable to PDC, and therefore do not need to be handled through Fields. |
| 30 | + * Never add a custom type that uses {@link PersistentDataContainer} as the primitive. That will cause |
| 31 | + * the {@link SkriptDataType} to not be used. |
| 32 | + */ |
| 33 | + private static final Map<Class<?>, PersistentDataType<?, ?>> REPRESENTABLE_TYPES = new HashMap<>(); |
| 34 | + |
| 35 | + static { |
| 36 | + REPRESENTABLE_TYPES.put(Byte.class, PersistentDataType.BYTE); |
| 37 | + REPRESENTABLE_TYPES.put(Short.class, PersistentDataType.SHORT); |
| 38 | + REPRESENTABLE_TYPES.put(Integer.class, PersistentDataType.INTEGER); |
| 39 | + REPRESENTABLE_TYPES.put(Long.class, PersistentDataType.LONG); |
| 40 | + REPRESENTABLE_TYPES.put(Double.class, PersistentDataType.DOUBLE); |
| 41 | + REPRESENTABLE_TYPES.put(Float.class, PersistentDataType.FLOAT); |
| 42 | + REPRESENTABLE_TYPES.put(Boolean.class, PersistentDataType.BOOLEAN); |
| 43 | + REPRESENTABLE_TYPES.put(String.class, PersistentDataType.STRING); |
| 44 | + } |
| 45 | + |
| 46 | + public static @Unmodifiable Collection<PersistentDataType<?, ?>> getRepresentablePDCTypes() { |
| 47 | + return Collections.unmodifiableCollection(REPRESENTABLE_TYPES.values()); |
| 48 | + } |
| 49 | + |
| 50 | + public static PersistentDataType<?, ?> getPDCType(ClassInfo<?> classInfo) { |
| 51 | + if (REPRESENTABLE_TYPES.containsKey(classInfo.getC())) { |
| 52 | + return REPRESENTABLE_TYPES.get(classInfo.getC()); |
| 53 | + } else { |
| 54 | + return SkriptDataType.get(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @SuppressWarnings("unchecked") |
| 59 | + public static @NotNull PersistentDataContainer serialize( |
| 60 | + @NotNull Object unserializedData, |
| 61 | + @NotNull PersistentDataAdapterContext context |
| 62 | + ) { |
| 63 | + // temporary |
| 64 | + assert Bukkit.isPrimaryThread(); |
| 65 | + |
| 66 | + ClassInfo<?> classInfo = Classes.getSuperClassInfo(unserializedData.getClass()); |
| 67 | + if (classInfo.getSerializeAs() != null) { |
| 68 | + classInfo = Classes.getExactClassInfo(classInfo.getSerializeAs()); |
| 69 | + if (classInfo == null) { |
| 70 | + assert false : unserializedData.getClass(); |
| 71 | + return null; |
| 72 | + } |
| 73 | + unserializedData = Converters.convert(unserializedData, classInfo.getC()); |
| 74 | + if (unserializedData == null) { |
| 75 | + assert false : classInfo.getCodeName(); |
| 76 | + return null; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + var serializer = (Serializer<Object>) classInfo.getSerializer(); |
| 81 | + if (serializer == null) // value cannot be saved |
| 82 | + throw new IllegalArgumentException("Cannot serialize " + classInfo.getCodeName() + " because it has no serializer"); |
| 83 | + |
| 84 | + assert !serializer.mustSyncDeserialization() || Bukkit.isPrimaryThread(); |
| 85 | + var container = context.newPersistentDataContainer(); |
| 86 | + |
| 87 | + // shortcut for primitives |
| 88 | + if (REPRESENTABLE_TYPES.containsKey(classInfo.getC())) { |
| 89 | + container.set(new NamespacedKey("skript", "type"), PersistentDataType.STRING, classInfo.getCodeName()); |
| 90 | + var tag = new NamespacedKey("skript", "value"); |
| 91 | + var pdcType = (PersistentDataType<Object, Object>) REPRESENTABLE_TYPES.get(classInfo.getC()); |
| 92 | + container.set(tag, pdcType, unserializedData); |
| 93 | + return container; |
| 94 | + } |
| 95 | + |
| 96 | + // If not a primitive, serialize normally and use Fields to store data |
| 97 | + try { |
| 98 | + Fields fields = serializer.serialize(unserializedData); |
| 99 | + container.set(new NamespacedKey("skript", "type"), PersistentDataType.STRING, classInfo.getCodeName()); |
| 100 | + for (var field : fields) { |
| 101 | + var tag = new NamespacedKey("skript", field.getID()); |
| 102 | + var data = field.isPrimitive() ? field.getPrimitive() : field.getObject(); |
| 103 | + if (data == null) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + if (field.isPrimitive() || data instanceof String) { |
| 107 | + var type = REPRESENTABLE_TYPES.get(data.getClass()); |
| 108 | + if (type == null) { |
| 109 | + throw new NotSerializableException("Unsupported primitive type: " + data.getClass()); |
| 110 | + } |
| 111 | + container.set(tag, (PersistentDataType<Object, Object>) type, data); |
| 112 | + } else { |
| 113 | + // write a nested PDC |
| 114 | + data = PDCSerializer.serialize(data, context); |
| 115 | + container.set(tag, PersistentDataType.TAG_CONTAINER, (PersistentDataContainer) data); |
| 116 | + } |
| 117 | + } |
| 118 | + } catch (NotSerializableException | StreamCorruptedException e) { |
| 119 | + throw new RuntimeException(e); |
| 120 | + } |
| 121 | + return container; |
| 122 | + } |
| 123 | + |
| 124 | + public static @NotNull Object deserialize( |
| 125 | + @NotNull PersistentDataContainer serializedData, |
| 126 | + @NotNull PersistentDataAdapterContext context |
| 127 | + ) { |
| 128 | + String typeName = serializedData.get(new NamespacedKey("skript", "type"), PersistentDataType.STRING); |
| 129 | + if (typeName == null) { |
| 130 | + throw new IllegalArgumentException("Cannot deserialize PDC because it has no type"); |
| 131 | + } |
| 132 | + ClassInfo<?> classInfo = Classes.getClassInfo(typeName); |
| 133 | + //noinspection unchecked |
| 134 | + var serializer = (Serializer<Object>) classInfo.getSerializer(); |
| 135 | + if (serializer == null) { |
| 136 | + throw new IllegalArgumentException("Cannot deserialize " + classInfo.getCodeName() + " because it has no serializer"); |
| 137 | + } |
| 138 | + |
| 139 | + // shortcut for primitives |
| 140 | + if (REPRESENTABLE_TYPES.containsKey(classInfo.getC())) { |
| 141 | + var tag = new NamespacedKey("skript", "value"); |
| 142 | + //noinspection unchecked |
| 143 | + var pdcType = (PersistentDataType<Object, Object>) REPRESENTABLE_TYPES.get(classInfo.getC()); |
| 144 | + Object value = serializedData.get(tag, pdcType); |
| 145 | + if (value == null) { |
| 146 | + throw new IllegalArgumentException("Cannot deserialize " + classInfo.getCodeName() + " because its value is missing"); |
| 147 | + } |
| 148 | + return value; |
| 149 | + } |
| 150 | + |
| 151 | + // If not a primitive, deserialize normally using Fields |
| 152 | + try { |
| 153 | + Fields fields = new Fields(); |
| 154 | + for (var key : serializedData.getKeys()) { |
| 155 | + if (key.getNamespace().equals("skript") && key.getKey().equals("type")) { |
| 156 | + continue; |
| 157 | + } |
| 158 | + Object data = null; |
| 159 | + boolean primitive = true; |
| 160 | + for (var entry : REPRESENTABLE_TYPES.entrySet()) { |
| 161 | + var type = entry.getValue(); |
| 162 | + if (serializedData.has(key, type)) { |
| 163 | + data = serializedData.get(key, type); |
| 164 | + primitive = entry.getKey().isPrimitive() || isPrimitiveWrapper(entry.getKey()); |
| 165 | + break; |
| 166 | + } |
| 167 | + } |
| 168 | + if (data == null) { |
| 169 | + if (serializedData.has(key, PersistentDataType.TAG_CONTAINER)) { |
| 170 | + PersistentDataContainer nestedContainer = serializedData.get(key, PersistentDataType.TAG_CONTAINER); |
| 171 | + assert nestedContainer != null; |
| 172 | + data = PDCSerializer.deserialize(nestedContainer, context); |
| 173 | + primitive = false; |
| 174 | + } else { |
| 175 | + throw new NotSerializableException("Unsupported data type for key: " + key); |
| 176 | + } |
| 177 | + } |
| 178 | + if (primitive) { |
| 179 | + fields.putPrimitive(key.getKey(), data); |
| 180 | + } else { |
| 181 | + fields.putObject(key.getKey(), data); |
| 182 | + } |
| 183 | + } |
| 184 | + assert !serializer.mustSyncDeserialization() || Bukkit.isPrimaryThread(); |
| 185 | + return serializer.deserialize(classInfo.getC(), fields); |
| 186 | + } catch (Exception e) { |
| 187 | + throw new RuntimeException(e); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + private static boolean isPrimitiveWrapper(Class<?> key) { |
| 192 | + return key == Byte.class || key == Short.class || key == Integer.class || |
| 193 | + key == Long.class || key == Double.class || key == Float.class || |
| 194 | + key == Boolean.class || key == Character.class; |
| 195 | + } |
| 196 | +} |
0 commit comments