diff --git a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/setting/SettingCodegen.kt b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/setting/SettingCodegen.kt new file mode 100644 index 000000000..9c97af271 --- /dev/null +++ b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/setting/SettingCodegen.kt @@ -0,0 +1,11 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.codegen.setting + diff --git a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/util.kt b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/util.kt new file mode 100644 index 000000000..172819f94 --- /dev/null +++ b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/util.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.codegen + +import java.io.File + +fun codegen(targetFile: String, regionName: String, block: StringBuilder.() -> Unit) { + //// region PrimitiveValue CODEGEN START //// + //// region PrimitiveValue CODEGEN END //// + + targetFile.findFileSmart().also { + println("Codegen target: ${it.absolutePath}") + }.apply { + writeText( + readText() + .replace(Regex("""//// region $regionName CODEGEN START ////([\s\S]*?)//// endregion $regionName CODEGEN END ////""")) { + val code = StringBuilder().apply(block).toString() + """ + |//// region $regionName CODEGEN START //// + | + |$code + | + |//// endregion $regionName CODEGEN END //// + """.trimMargin() + } + ) + } +} + +fun String.findFileSmart(): File = kotlin.run { + if (contains("/")) { // absolute + File(this) + } else { + val list = File(".").walk().filter { it.name == this }.toList() + if (list.isNotEmpty()) return list.single() + + File(".").walk().filter { it.name.contains(this) }.single() + } +}.also { + require(it.exists()) { "file doesn't exist" } +} + +fun main() { + codegen("Value.kt", "PrimitiveValue") { + + } +} \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/internal/PluginsLoader.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/internal/PluginsLoader.kt index bf95f9bd0..a9a3d9f85 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/internal/PluginsLoader.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/internal/PluginsLoader.kt @@ -16,7 +16,7 @@ import java.net.URLClassLoader internal class PluginsLoader(private val parentClassLoader: ClassLoader) { private val loggerName = "PluginsLoader" private val pluginLoaders = linkedMapOf() - private val classesCache = mutableMapOf>() + private val classesCache = mutableMapOf>() private val logger = MiraiConsole.newLogger(loggerName) /** @@ -93,9 +93,9 @@ internal class PluginsLoader(private val parentClassLoader: ClassLoader) { * A Adapted URL Class Loader that supports Android and JVM for single URL(File) Class Load */ -internal open class AdaptiveURLClassLoader(file: File, parent: ClassLoader):ClassLoader(){ +internal open class AdaptiveURLClassLoader(file: File, parent: ClassLoader) : ClassLoader() { - private val internalClassLoader:ClassLoader by lazy { + private val internalClassLoader: ClassLoader by lazy { kotlin.runCatching { val loaderClass = Class.forName("dalvik.system.PathClassLoader") loaderClass.getConstructor(String::class.java, ClassLoader::class.java) @@ -110,19 +110,19 @@ internal open class AdaptiveURLClassLoader(file: File, parent: ClassLoader):Clas } - private val internalClassCache = mutableMapOf>() + private val internalClassCache = mutableMapOf>() - internal val classesCache:Map> + internal val classesCache: Map> get() = internalClassCache - internal fun addClassCache(string: String, clazz: Class<*>){ - synchronized(internalClassCache){ + internal fun addClassCache(string: String, clazz: Class<*>) { + synchronized(internalClassCache) { internalClassCache[string] = clazz } } - fun close(){ + fun close() { if (internalClassLoader is URLClassLoader) { (internalClassLoader as URLClassLoader).close() } @@ -135,24 +135,25 @@ internal class PluginClassLoader( file: File, private val pluginsLoader: PluginsLoader, parent: ClassLoader -) :AdaptiveURLClassLoader(file,parent){ +) : AdaptiveURLClassLoader(file, parent) { override fun findClass(name: String): Class<*> { - return findClass(name,true) + return findClass(name, true) } - fun findClass(name: String, global: Boolean = true): Class<*>{ - return classesCache[name]?: kotlin.run { + fun findClass(name: String, global: Boolean = true): Class<*> { + return classesCache[name] ?: kotlin.run { var clazz: Class<*>? = null if (global) { clazz = pluginsLoader.findClassByName(name) } - if(clazz == null) { + if (clazz == null) { clazz = loadClass(name)//这里应该是find, 如果不行就要改 } pluginsLoader.addClassCache(name, clazz) this.addClassCache(name, clazz) - clazz - }!! + @Suppress("UNNECESSARY_NOT_NULL_ASSERTION") + clazz!! // compiler bug + } } } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Value.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Value.kt index 58424dfe2..09b860e0c 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Value.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Value.kt @@ -43,7 +43,7 @@ typealias ValueSerializer = KSerializer> * - [Char], [String] * * Note: The values are actually *boxed* because of the generic type T. - * *Primitive* indicates only it is one of the 8 types mentioned above. + * *Primitive* indicates only it is one of the 9 types mentioned above. */ interface PrimitiveValue : Value diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/ValueCreatorsImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/ValueCreatorsImpl.kt index eece6fca3..e837c8eb1 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/ValueCreatorsImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/ValueCreatorsImpl.kt @@ -15,33 +15,33 @@ import net.mamoe.mirai.console.setting.Setting import net.mamoe.mirai.console.setting.Value import kotlin.reflect.KClass import kotlin.reflect.KType -import kotlin.reflect.full.isSubclassOf @PublishedApi @Suppress("UnsafeCall", "SMARTCAST_IMPOSSIBLE") internal fun Setting.valueFromKTypeImpl(type: KType): Value<*> { - require(type.classifier is KClass<*>) + val classifier = type.classifier + require(classifier is KClass<*>) - if (type.classifier.isPrimitiveOrBuiltInSerializableValue()) { + if (classifier.isPrimitiveOrBuiltInSerializableValue()) { TODO("是基础类型, 可以直接创建 ValueImpl. ") } // 复合类型 when { - type.classifier.isSubclassOf(Map::class) -> { + classifier == Map::class -> { TODO() } - type.classifier.isSubclassOf(List::class) -> { + classifier == List::class -> { TODO() } - type.classifier.isSubclassOf(Set::class) -> { + classifier == Set::class -> { TODO() } - else -> error("Custom composite value is not supported yet (${type.classifier.qualifiedName})") + else -> error("Custom composite value is not supported yet (${classifier.qualifiedName})") } }