aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/config/migration/MigratingConfigLoader.kt
blob: d81805ee0d24c247e547dc229782aa330b3e7059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package at.hannibal2.skyhanni.config.migration

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.ConfigMigrationEvent
import at.hannibal2.skyhanni.utils.allAccessibleFields
import at.hannibal2.skyhanni.utils.nonGeneric
import com.google.gson.JsonElement
import com.google.gson.JsonNull
import com.google.gson.JsonObject
import com.google.gson.JsonPrimitive
import com.google.gson.annotations.Expose
import io.github.moulberry.moulconfig.observer.Property
import net.minecraftforge.common.MinecraftForge
import org.lwjgl.input.Keyboard
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type


class MigratingConfigLoader {
    val logger = SkyHanniMod.getLogger("ConfigMigrator")
    val allFailures = mutableListOf<LoadResult.Failure>()

    val adapters = listOf(
        LoadingAdapter { path, hierarchy, type ->
            val ng = type.nonGeneric ?: return@LoadingAdapter LoadResult.Invalid
            if (!ng.isPrimitive) return@LoadingAdapter LoadResult.Invalid
            @Suppress("RemoveRedundantQualifierName")
            loadElement(
                path, hierarchy, mapOf(
                    java.lang.Integer.TYPE to java.lang.Integer::class.java,
                    java.lang.Boolean.TYPE to java.lang.Boolean::class.java,
                    java.lang.Short.TYPE to java.lang.Short::class.java,
                    java.lang.Float.TYPE to java.lang.Float::class.java,
                    java.lang.Double.TYPE to java.lang.Double::class.java,
                    java.lang.Long.TYPE to java.lang.Long::class.java,
                    java.lang.Byte.TYPE to java.lang.Byte::class.java,
                    java.lang.Character.TYPE to java.lang.Character::class.java,
                )[ng]!!
            )
        },
        directLoader { if (!it.asJsonPrimitive.isString) error("String expected, found $it") else LoadResult.Instance(it.asString) },
        directLoader { if (!it.asJsonPrimitive.isNumber) error("Number expected, found $it") else LoadResult.Instance(it.asInt) },
        directLoader { if (!it.asJsonPrimitive.isNumber) error("Number expected, found $it") else LoadResult.Instance(it.asFloat) },
        directLoader { if (!it.asJsonPrimitive.isNumber) error("Number expected, found $it") else LoadResult.Instance(it.asLong) },
        directLoader { if (!it.asJsonPrimitive.isNumber) error("Number expected, found $it") else LoadResult.Instance(it.asDouble) },
        directLoader { if (!it.asJsonPrimitive.isNumber) error("Number expected, found $it") else LoadResult.Instance(it.asNumber) },
        directLoader { if (!it.asJsonPrimitive.isNumber) error("Number expected, found $it") else LoadResult.Instance(it.asBigDecimal) },
        directLoader { if (!it.asJsonPrimitive.isNumber) error("Number expected, found $it") else LoadResult.Instance(it.asBigInteger) },
        directLoader {
            if (!it.asJsonPrimitive.isBoolean) error("Boolean expected, found $it") else LoadResult.Instance(
                it.asBoolean
            )
        },
        directLoader { if (!it.asJsonPrimitive.isNumber) error("Number expected, found $it") else LoadResult.Instance(it.asShort) },
        directLoader { if (!it.isJsonObject) error("JsonObject expected, found $it") else LoadResult.Instance(it.asJsonObject) },
        directLoader { if (!it.isJsonArray) error("JsonArray expected, found $it") else LoadResult.Instance(it.asJsonArray) },
        directLoader { if (!it.isJsonPrimitive) error("JsonPrimitive expected, found $it") else LoadResult.Instance(it.asJsonPrimitive) },
        directLoader { if (!it.isJsonNull) error("Null expected, found $it") else LoadResult.Instance(it as JsonNull) },
        directLoader { if (!it.asJsonPrimitive.isNumber) error("Number expected, found $it") else LoadResult.Instance(it.asByte) },
        directLoader {
            if (!it.asJsonPrimitive.isString || it.asString.length > 1) error("1-length String expected, found $it") else LoadResult.Instance(
                it.asCharacter
            )
        },
        LoadingAdapter { path, hierarchy, type ->
            val ng = type.nonGeneric ?: return@LoadingAdapter LoadResult.Invalid
            if (ng != List::class.java) return@LoadingAdapter LoadResult.Invalid
            type as ParameterizedType
            val childPath = ResolutionPath.IndirectChild("element", path)
            val builder = mutableListOf<Any?>()
            for (jsonElement in hierarchy.last()!!.asJsonArray) {
                val x = loadElement(childPath, hierarchy + jsonElement, type.actualTypeArguments[0])
                if (x is LoadResult.Instance) {
                    builder.add(x.instance)
                } else if (x is LoadResult.UseDefault) {
                    return@LoadingAdapter LoadResult.Failure(
                        RuntimeException("Cannot UseDefault for list element"),
                        childPath
                    )
                } else {
                    return@LoadingAdapter x
                }
            }
            return@LoadingAdapter LoadResult.Instance(builder)
        },
        LoadingAdapter { path, hierarchy, type ->
            val ng = type.nonGeneric ?: return@LoadingAdapter LoadResult.Invalid
            if (ng != Map::class.java) return@LoadingAdapter LoadResult.Invalid
            type as ParameterizedType
            val builder = mutableMapOf<Any?, Any?>()
            val keyPath = ResolutionPath.IndirectChild("key", path)
            val valuePath = ResolutionPath.IndirectChild("value", path)
            for ((key, value) in (hierarchy.last()!! as JsonObject).entrySet()) {
                val keyEl = loadElement(keyPath, hierarchy + JsonPrimitive(key), type.actualTypeArguments[0])
                if (keyEl !is LoadResult.Instance<*>) {
                    if (keyEl is LoadResult.UseDefault) {
                        return@LoadingAdapter LoadResult.Failure(
                            RuntimeException("Cannot UseDefault for map key"),
                            keyPath
                        )
                    }
                    return@LoadingAdapter keyEl
                }
                val valueEl = loadElement(valuePath, hierarchy + value, type.actualTypeArguments[0])
                if (valueEl !is LoadResult.Instance<*>) {
                    if (valueEl is LoadResult.UseDefault) {
                        return@LoadingAdapter LoadResult.Failure(
                            RuntimeException("Cannot UseDefault for map key"),
                            valuePath
                        )
                    }
                    return@LoadingAdapter keyEl
                }
                builder[keyEl.instance] = valueEl.instance
            }
            return@LoadingAdapter LoadResult.Instance(builder)
        },
        LoadingAdapter { path, hierarchy, type ->
            val ng = type.nonGeneric ?: return@LoadingAdapter LoadResult.Invalid
            if (ng != Property::class.java) return@LoadingAdapter LoadResult.Invalid
            if (type !is ParameterizedType) return@LoadingAdapter LoadResult.Invalid
            loadElement(
                ResolutionPath.FieldChild(ng.getDeclaredField("value").also { it.isAccessible = true }, path),
                hierarchy + hierarchy.last(),
                type.actualTypeArguments[0]
            ).map { Property.of(it) }
        },
        LoadingAdapter { path, hierarchy, type ->
            val ng = type.nonGeneric ?: return@LoadingAdapter LoadResult.Invalid
            if (!ng.isEnum) return@LoadingAdapter LoadResult.Invalid
            ng as Class<out Enum<*>>
            val el = hierarchy.last()!!.asJsonPrimitive