@@ -8,15 +8,14 @@ import io.github.rothes.esu.core.configuration.serializer.*
88import io.github.rothes.esu.core.module.configuration.EmptyConfiguration
99import io.github.rothes.esu.core.util.extension.ClassExt.jarFile
1010import io.github.rothes.esu.core.util.tree.TreeNode
11- import io.github.rothes.esu.lib.configurate.BasicConfigurationNode
12- import io.github.rothes.esu.lib.configurate.CommentedConfigurationNode
13- import io.github.rothes.esu.lib.configurate.CommentedConfigurationNodeIntermediary
14- import io.github.rothes.esu.lib.configurate.ConfigurationNode
11+ import io.github.rothes.esu.lib.configurate.*
1512import io.github.rothes.esu.lib.configurate.loader.HeaderMode
13+ import io.github.rothes.esu.lib.configurate.objectmapping.ConfigSerializable
1614import io.github.rothes.esu.lib.configurate.objectmapping.ObjectMapper
1715import io.github.rothes.esu.lib.configurate.objectmapping.meta.NodeResolver
1816import io.github.rothes.esu.lib.configurate.objectmapping.meta.Processor
1917import io.github.rothes.esu.lib.configurate.serialize.ScalarSerializer
18+ import io.github.rothes.esu.lib.configurate.serialize.TypeSerializer
2019import io.github.rothes.esu.lib.configurate.util.MapFactories
2120import io.github.rothes.esu.lib.configurate.yaml.NodeStyle
2221import io.github.rothes.esu.lib.configurate.yaml.YamlConfigurationLoader
@@ -219,32 +218,11 @@ object ConfigLoader {
219218 .lineLength(150 )
220219 .commentsEnabled(true )
221220 .defaultOptions { options ->
221+ val commentProcesser = CommentProcesser (resourceNode)
222222 val factory: ObjectMapper .Factory = ObjectMapper .factoryBuilder()
223223 .addProcessor(Comment ::class .java) { data, _ ->
224224 Processor { _, destination ->
225- if (destination is CommentedConfigurationNodeIntermediary <* >) {
226- val resourceComment = (resourceNode?.node(destination.path()) as ? CommentedConfigurationNodeIntermediary <* >)?.comment()
227- if (resourceComment != null && destination.comment() == data.value.trimIndent()) {
228- destination.comment(resourceComment)
229- return @Processor
230- }
231- val resourceOld = resourceNode?.node(" _oc_" )?.node(destination.path()) // _oc_ = _old_comments_
232- val allOld = (
233- if (resourceOld != null && resourceOld.isList)
234- resourceOld.getList(String ::class .java)!! .plus(data.overrideOld)
235- else
236- data.overrideOld.toList()
237- ).map { it.toString().trimIndent() }
238-
239- if (allOld.isEmpty() || destination.comment() == null ) {
240- destination.commentIfAbsent(resourceComment ? : data.value.trimIndent())
241- } else if (data.overrideOld.firstOrNull() == OVERRIDE_ALWAYS ) {
242- destination.comment(resourceComment ? : data.value.trimIndent())
243- } else if (allOld.contains(destination.comment())) {
244- destination.comment(resourceComment ? : data.value.trimIndent())
245- }
246-
247- }
225+ commentProcesser.process(data, destination)
248226 }
249227 }
250228 .addProcessor(MoveToTop ::class .java) { _, _ ->
@@ -314,6 +292,7 @@ object ConfigLoader {
314292 } else null
315293 }
316294 .build()
295+ val objectSerializer = ClassProceedSerializer (commentProcesser, factory.asTypeSerializer())
317296 options.mapFactory(MapFactories .insertionOrdered())
318297 .serializers {
319298 if (serverAdventure) {
@@ -328,8 +307,12 @@ object ConfigLoader {
328307 GenericTypeReflector .erase(type).let { clazz ->
329308 ConfigurationPart ::class .java.isAssignableFrom(clazz)
330309 }
331- }, factory.asTypeSerializer())
332- .registerAnnotatedObjects(factory)
310+ }, objectSerializer)
311+ .register( // registerAnnotatedObjects(factory)
312+ { type ->
313+ GenericTypeReflector .annotate(type).isAnnotationPresent(ConfigSerializable ::class .java)
314+ }, objectSerializer
315+ )
333316 .register(TypeToken .get(List ::class .java), ListSerializer )
334317 .register(TypeToken .get(Map ::class .java), MapSerializer )
335318 .register(TypeToken .get(Optional ::class .java), OptionalSerializer )
@@ -365,7 +348,7 @@ object ConfigLoader {
365348 false
366349 }
367350 }
368- }, factory.asTypeSerializer()
351+ }, objectSerializer
369352 )
370353 }.let {
371354 if (resourceNode != null ) {
@@ -625,4 +608,53 @@ object ConfigLoader {
625608 }
626609 }
627610
611+ private class CommentProcesser (val resourceNode : ConfigurationNode ? ) {
612+
613+ fun process (comment : Comment , node : ConfigurationNode ) {
614+ if (node is CommentedConfigurationNodeIntermediary <* >) {
615+ val resourceComment =
616+ (resourceNode?.node(node.path()) as ? CommentedConfigurationNodeIntermediary <* >)?.comment()
617+ if (resourceComment != null && node.comment() == comment.value.trimIndent()) {
618+ node.comment(resourceComment)
619+ return
620+ }
621+ val resourceOld = resourceNode?.node(" _oc_" )?.node(node.path()) // _oc_ = _old_comments_
622+ val allOld = (if (resourceOld != null && resourceOld.isList) resourceOld.getList(String ::class .java)!!
623+ .plus(comment.overrideOld)
624+ else comment.overrideOld.toList()).map { it.toString().trimIndent() }
625+
626+ if (allOld.isEmpty() || node.comment() == null ) {
627+ node.commentIfAbsent(resourceComment ? : comment.value.trimIndent())
628+ } else if (comment.overrideOld.firstOrNull() == OVERRIDE_ALWAYS ) {
629+ node.comment(resourceComment ? : comment.value.trimIndent())
630+ } else if (allOld.contains(node.comment())) {
631+ node.comment(resourceComment ? : comment.value.trimIndent())
632+ }
633+ }
634+ }
635+ }
636+
637+ private class ClassProceedSerializer (
638+ private val commentProcesser : CommentProcesser ,
639+ private val factory : TypeSerializer <Any >,
640+ ): TypeSerializer<Any> {
641+
642+ override fun deserialize (type : Type ? , node : ConfigurationNode ? ): Any? {
643+ return factory.deserialize(type, node)
644+ }
645+
646+ override fun serialize (type : Type ? , obj : Any? , node : ConfigurationNode ? ) {
647+ factory.serialize(type, obj, node)
648+
649+ node ? : return
650+ val comment = obj?.javaClass?.getAnnotation(Comment ::class .java) ? : return
651+ commentProcesser.process(comment, node)
652+ }
653+
654+ override fun emptyValue (specificType : Type ? , options : ConfigurationOptions ? ): Any? {
655+ return factory.emptyValue(specificType, options)
656+ }
657+
658+ }
659+
628660}
0 commit comments