@@ -21,15 +21,14 @@ import org.spongepowered.configurate.objectmapping.ObjectMapper
2121import org.spongepowered.configurate.objectmapping.meta.Constraint
2222import org.spongepowered.configurate.objectmapping.meta.NodeResolver
2323import org.spongepowered.configurate.serialize.SerializationException
24+ import org.spongepowered.configurate.serialize.TypeSerializerCollection
2425import org.spongepowered.configurate.yaml.NodeStyle
2526import org.spongepowered.configurate.yaml.YamlConfigurationLoader
2627import java.io.Serial
2728import java.io.UncheckedIOException
28- import java.lang.reflect.Type
2929import java.nio.file.Path
3030import java.text.MessageFormat
3131import java.util.*
32- import java.util.function.Predicate
3332import java.util.regex.Pattern
3433
3534/* *
@@ -243,75 +242,12 @@ class SpongeConfigManager<C> private constructor(
243242 it.serializers { serializers ->
244243 surfSpongeConfigSerializers.buildSerializersModule().accept(serializers)
245244
246- // Backwards compatibility before sponge relocation
247- val predicate = Predicate <Type > { type ->
248- GenericTypeReflector .annotate(type)
249- .isAnnotationPresent(OldSpongeReflections .OLD_CONFIG_SERIALIZABLE_ANNOTATION )
245+ try {
246+ OldSpongeReflections .OLD_CONFIG_SERIALIZABLE_ANNOTATION
247+ serializers.registerBackwardsCompatibleSerializers()
248+ } catch (_: Exception ) {
249+ // no none relocated annotations, no need to register
250250 }
251-
252- serializers.register(
253- predicate,
254- ObjectMapper .factoryBuilder()
255- .addDiscoverer(dataClassFieldDiscoverer())
256- .addProcessor(OldSpongeReflections .OLD_COMMENT_ANNOTATION ) { data, _ ->
257- { _, destination ->
258- if (destination is CommentedConfigurationNodeIntermediary <* >) {
259- if (OldSpongeReflections .isCommentOverride(data)) {
260- destination.comment(OldSpongeReflections .getCommentValue(data))
261- } else {
262- destination.commentIfAbsent(OldSpongeReflections .getCommentValue(data))
263- }
264- }
265- }
266- }
267- .addConstraint(
268- OldSpongeReflections .OLD_MATCHES_ANNOTATION ,
269- String ::class .java
270- ) { data, _ ->
271- val value = OldSpongeReflections .getMatchesValue(data)
272- val flags = OldSpongeReflections .getMatchesFlags(data)
273- val failureMessage = OldSpongeReflections .getMatchesFailureMessage(data)
274-
275- val test = Pattern .compile(value, flags)
276- val format = MessageFormat (failureMessage, Locale .getDefault())
277-
278- Constraint { toValidate ->
279- if (toValidate != null ) {
280- val match = test.matcher(toValidate)
281- if (! match.matches()) {
282- throw SerializationException (format.format(arrayOf(toValidate, value)))
283- }
284- }
285- }
286- }
287- .addConstraint(OldSpongeReflections .OLD_REQUIRED_ANNOTATION , Constraint .required())
288- .addNodeResolver(fun (name, element): NodeResolver ? {
289- if (element.isAnnotationPresent(OldSpongeReflections .OLD_SETTING_ANNOTATION )) {
290- val annotation =
291- element.getAnnotation(OldSpongeReflections .OLD_SETTING_ANNOTATION )
292- val key = OldSpongeReflections .getSettingValue(annotation)
293- if (key.isNotEmpty()) {
294- return { node -> node.node(key) }
295- }
296- }
297-
298- return null
299- })
300- .addNodeResolver(fun (name, element): NodeResolver ? {
301- if (element.isAnnotationPresent(OldSpongeReflections .OLD_SETTING_ANNOTATION )) {
302- val annotation =
303- element.getAnnotation(OldSpongeReflections .OLD_SETTING_ANNOTATION )
304- val nodeFromParent = OldSpongeReflections .isSettingNodeFromParent(annotation)
305- if (nodeFromParent) {
306- return { node -> node }
307- }
308- }
309-
310- return null
311- })
312- .build()
313- .asTypeSerializer()
314- )
315251 }.shouldCopyDefaults(true )
316252 }
317253 .build()
@@ -350,6 +286,75 @@ class SpongeConfigManager<C> private constructor(
350286 throw LoadConfigException (e)
351287 }
352288 }
289+
290+ private fun TypeSerializerCollection.Builder.registerBackwardsCompatibleSerializers () {
291+ register(
292+ { type ->
293+ GenericTypeReflector .annotate(type)
294+ .isAnnotationPresent(OldSpongeReflections .OLD_CONFIG_SERIALIZABLE_ANNOTATION )
295+ },
296+ ObjectMapper .factoryBuilder()
297+ .addDiscoverer(dataClassFieldDiscoverer())
298+ .addProcessor(OldSpongeReflections .OLD_COMMENT_ANNOTATION ) { data, _ ->
299+ { _, destination ->
300+ if (destination is CommentedConfigurationNodeIntermediary <* >) {
301+ if (OldSpongeReflections .isCommentOverride(data)) {
302+ destination.comment(OldSpongeReflections .getCommentValue(data))
303+ } else {
304+ destination.commentIfAbsent(OldSpongeReflections .getCommentValue(data))
305+ }
306+ }
307+ }
308+ }
309+ .addConstraint(
310+ OldSpongeReflections .OLD_MATCHES_ANNOTATION ,
311+ String ::class .java
312+ ) { data, _ ->
313+ val value = OldSpongeReflections .getMatchesValue(data)
314+ val flags = OldSpongeReflections .getMatchesFlags(data)
315+ val failureMessage = OldSpongeReflections .getMatchesFailureMessage(data)
316+
317+ val test = Pattern .compile(value, flags)
318+ val format = MessageFormat (failureMessage, Locale .getDefault())
319+
320+ Constraint { toValidate ->
321+ if (toValidate != null ) {
322+ val match = test.matcher(toValidate)
323+ if (! match.matches()) {
324+ throw SerializationException (format.format(arrayOf(toValidate, value)))
325+ }
326+ }
327+ }
328+ }
329+ .addConstraint(OldSpongeReflections .OLD_REQUIRED_ANNOTATION , Constraint .required())
330+ .addNodeResolver(fun (name, element): NodeResolver ? {
331+ if (element.isAnnotationPresent(OldSpongeReflections .OLD_SETTING_ANNOTATION )) {
332+ val annotation =
333+ element.getAnnotation(OldSpongeReflections .OLD_SETTING_ANNOTATION )
334+ val key = OldSpongeReflections .getSettingValue(annotation)
335+ if (key.isNotEmpty()) {
336+ return { node -> node.node(key) }
337+ }
338+ }
339+
340+ return null
341+ })
342+ .addNodeResolver(fun (name, element): NodeResolver ? {
343+ if (element.isAnnotationPresent(OldSpongeReflections .OLD_SETTING_ANNOTATION )) {
344+ val annotation =
345+ element.getAnnotation(OldSpongeReflections .OLD_SETTING_ANNOTATION )
346+ val nodeFromParent = OldSpongeReflections .isSettingNodeFromParent(annotation)
347+ if (nodeFromParent) {
348+ return { node -> node }
349+ }
350+ }
351+
352+ return null
353+ })
354+ .build()
355+ .asTypeSerializer()
356+ )
357+ }
353358 }
354359}
355360
0 commit comments