|
| 1 | +package pl.mikigal.config.serializer.universal; |
| 2 | + |
| 3 | +import org.bukkit.configuration.ConfigurationSection; |
| 4 | +import pl.mikigal.config.BukkitConfiguration; |
| 5 | +import pl.mikigal.config.exception.InvalidConfigFileException; |
| 6 | +import pl.mikigal.config.exception.MissingSerializerException; |
| 7 | +import pl.mikigal.config.serializer.Serializer; |
| 8 | +import pl.mikigal.config.serializer.Serializers; |
| 9 | +import pl.mikigal.config.util.ConversionUtils; |
| 10 | +import pl.mikigal.config.util.TypeUtils; |
| 11 | + |
| 12 | +import java.lang.reflect.Array; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.Objects; |
| 15 | +import java.util.Set; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +public class UniversalArraySerializer extends Serializer<Object[]> { |
| 19 | + |
| 20 | + @Override |
| 21 | + protected void saveObject(String path, Object[] object, BukkitConfiguration configuration) { |
| 22 | + if (object.length == 0) { |
| 23 | + throw new IllegalStateException("Can't set empty array to config"); |
| 24 | + } |
| 25 | + |
| 26 | + Class<?> generic = TypeUtils.getArrayGeneric(object); |
| 27 | + Serializer<?> serializer = Serializers.of(generic); |
| 28 | + if (serializer == null && !TypeUtils.isSimpleType(generic)) { |
| 29 | + throw new MissingSerializerException(generic); |
| 30 | + } |
| 31 | + |
| 32 | + configuration.set(path + ".type", generic.getName()); |
| 33 | + |
| 34 | + int index = 0; |
| 35 | + for (Object element : object) { |
| 36 | + if (serializer == null) { |
| 37 | + configuration.set(path + "." + index, element); |
| 38 | + } |
| 39 | + else { |
| 40 | + serializer.serialize(path + "." + index, element, configuration); |
| 41 | + } |
| 42 | + |
| 43 | + index++; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Object[] deserialize(String path, BukkitConfiguration configuration) { |
| 49 | + ConfigurationSection section = configuration.getConfigurationSection(path); |
| 50 | + Set<String> keys = section.getKeys(false); |
| 51 | + keys.stream() |
| 52 | + .filter(key -> !key.equals("type")) |
| 53 | + .forEach(key -> { |
| 54 | + if (ConversionUtils.asInt(key) == Integer.MIN_VALUE) { |
| 55 | + throw new InvalidConfigFileException("Invalid index: " + key + " in " + path + " (should be integer)"); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + String type = section.getString("type"); |
| 60 | + Objects.requireNonNull(type, "Serializer type is not defined for " + path); |
| 61 | + |
| 62 | + try { |
| 63 | + Serializer<?> serializer = Serializers.of(type); |
| 64 | + Class<?> typeClass = Class.forName(type); |
| 65 | + |
| 66 | + if (serializer == null && !TypeUtils.isSimpleType(typeClass)) { |
| 67 | + throw new MissingSerializerException(typeClass); |
| 68 | + } |
| 69 | + |
| 70 | + int length = Collections.max(keys |
| 71 | + .stream() |
| 72 | + .filter(key -> !key.equals("type")) |
| 73 | + .map(Integer::parseInt) |
| 74 | + .collect(Collectors.toList())) + 1; |
| 75 | + |
| 76 | + Object[] array = (Object[]) Array.newInstance(typeClass, length); |
| 77 | + for (String key : keys) { |
| 78 | + if (key.equals("type")) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + int index = Integer.parseInt(key); |
| 83 | + if (serializer == null) { |
| 84 | + array[index] = configuration.get(path + "." + index); |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + array[index] = serializer.deserialize(path + "." + index, configuration); |
| 89 | + } |
| 90 | + |
| 91 | + return array; |
| 92 | + } catch (ClassNotFoundException e) { |
| 93 | + throw new RuntimeException(e); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments