mirror of
https://github.com/jie65535/mirai-console-jcr-plugin.git
synced 2026-10-30 13:42:09 +08:00
重制cpp索引,增加更新索引命令 /jcr update
This commit is contained in:
1 parent
d45b7f8276
commit
866ce352a2
7 files changed
+113
-14254
No files matched your search
+2
-2
@@ -3,11 +3,11 @@ plugins {
|
|||||||
kotlin("jvm") version kotlinVersion
|
kotlin("jvm") version kotlinVersion
|
||||||
kotlin("plugin.serialization") version kotlinVersion
|
kotlin("plugin.serialization") version kotlinVersion
|
||||||
|
|
||||||
id("net.mamoe.mirai-console") version "2.10.0"
|
id("net.mamoe.mirai-console") version "2.12.2"
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "top.jie65535"
|
group = "top.jie65535"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
maven("https://maven.aliyun.com/repository/public") // 阿里云国内代理仓库
|
maven("https://maven.aliyun.com/repository/public") // 阿里云国内代理仓库
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package top.jie65535.jcr
|
||||||
|
|
||||||
|
import kotlinx.coroutines.runInterruptible
|
||||||
|
import kotlinx.serialization.decodeFromString
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
|
|
||||||
|
@OptIn(kotlinx.serialization.ExperimentalSerializationApi::class)
|
||||||
|
object Data {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据
|
||||||
|
*/
|
||||||
|
lateinit var indexes: Array<Index>
|
||||||
|
|
||||||
|
private val Json = Json {
|
||||||
|
ignoreUnknownKeys = true
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化数据
|
||||||
|
*/
|
||||||
|
fun initData() {
|
||||||
|
val linkMap = JCppReferencePlugin.resolveDataFile("linkmap.json")
|
||||||
|
indexes = if (linkMap.exists()) {
|
||||||
|
Json.decodeFromString(linkMap.readText())
|
||||||
|
} else {
|
||||||
|
val resource = this::class.java.getResource("/linkmap.json")
|
||||||
|
if (resource == null) {
|
||||||
|
JCppReferencePlugin.logger.error("索引资源不存在!请更新索引")
|
||||||
|
arrayOf()
|
||||||
|
} else {
|
||||||
|
Json.decodeFromString(resource.readText())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val httpClient by lazy { OkHttpClient() }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新数据
|
||||||
|
*/
|
||||||
|
suspend fun updateData() {
|
||||||
|
val call = httpClient.newCall(Request.Builder()
|
||||||
|
.url("https://cdn.jsdelivr.net/npm/@gytx/cppreference-index/dist/generated.json")
|
||||||
|
.build())
|
||||||
|
JCppReferencePlugin.logger.info("正在下载索引")
|
||||||
|
runInterruptible {
|
||||||
|
val response = call.execute()
|
||||||
|
if (response.isSuccessful) {
|
||||||
|
val json = response.body!!.string()
|
||||||
|
indexes = Json.decodeFromString(json)
|
||||||
|
// 保存到文件
|
||||||
|
JCppReferencePlugin.resolveDataFile("linkmap.json")
|
||||||
|
.writeText(json)
|
||||||
|
JCppReferencePlugin.logger.info("索引更新完成")
|
||||||
|
} else {
|
||||||
|
JCppReferencePlugin.logger.error("下载失败 HTTP Code: ${response.code}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取索引
|
||||||
|
*/
|
||||||
|
fun getIndex(word: String): Index? {
|
||||||
|
return indexes.filter { it.name.contains(word) }
|
||||||
|
.sortedWith { a, b -> a.name.length - b.name.length }
|
||||||
|
.firstOrNull()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package top.jie65535.jcr
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
class Index(
|
||||||
|
val type: String,
|
||||||
|
val name: String,
|
||||||
|
val link: String,
|
||||||
|
)
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
package top.jie65535.jcr
|
package top.jie65535.jcr
|
||||||
|
|
||||||
|
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
|
||||||
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
|
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
|
||||||
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
||||||
import net.mamoe.mirai.event.GlobalEventChannel
|
import net.mamoe.mirai.event.GlobalEventChannel
|
||||||
import net.mamoe.mirai.event.subscribeMessages
|
import net.mamoe.mirai.event.subscribeMessages
|
||||||
|
import net.mamoe.mirai.message.data.MessageSource.Key.quote
|
||||||
import net.mamoe.mirai.utils.info
|
import net.mamoe.mirai.utils.info
|
||||||
import java.io.BufferedReader
|
import java.io.BufferedReader
|
||||||
import java.io.InputStreamReader
|
import java.io.InputStreamReader
|
||||||
@@ -12,7 +14,7 @@ object JCppReferencePlugin : KotlinPlugin(
|
|||||||
JvmPluginDescription(
|
JvmPluginDescription(
|
||||||
id = "top.jie65535.mirai-console-jcr-plugin",
|
id = "top.jie65535.mirai-console-jcr-plugin",
|
||||||
name = "J Cpp Reference Plugin",
|
name = "J Cpp Reference Plugin",
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
) {
|
) {
|
||||||
author("jie65535")
|
author("jie65535")
|
||||||
info("cppreference.com 帮助插件")
|
info("cppreference.com 帮助插件")
|
||||||
@@ -40,30 +42,30 @@ object JCppReferencePlugin : KotlinPlugin(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val indexC by lazy { loadMap("/devhelp-index-c.txt") }
|
private val indexC by lazy { loadMap("/devhelp-index-c.txt") }
|
||||||
private val indexCpp by lazy { loadMap("/devhelp-index-cpp.txt") }
|
// private val indexCpp by lazy { loadMap("/devhelp-index-cpp.txt") }
|
||||||
|
|
||||||
override fun onEnable() {
|
override fun onEnable() {
|
||||||
logger.info { "Plugin loaded" }
|
logger.info { "Plugin loaded" }
|
||||||
|
PluginCommands.register()
|
||||||
|
Data.initData()
|
||||||
|
|
||||||
val eventChannel = GlobalEventChannel.parentScope(this)
|
val eventChannel = GlobalEventChannel.parentScope(this)
|
||||||
eventChannel.subscribeMessages {
|
eventChannel.subscribeMessages {
|
||||||
startsWith("c ") {
|
startsWith("c ") { keyword ->
|
||||||
val keyword = it.trim()
|
|
||||||
if (keyword.isEmpty()) return@startsWith
|
if (keyword.isEmpty()) return@startsWith
|
||||||
logger.info("check c \"$keyword\"")
|
logger.info("check c \"$keyword\"")
|
||||||
val link = indexC[keyword]
|
val link = indexC[keyword]
|
||||||
if (link != null) {
|
if (link != null) {
|
||||||
subject.sendMessage(cppreferencePrefix + link)
|
subject.sendMessage(message.quote() + cppreferencePrefix + link)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
startsWith("cpp ") {
|
startsWith("cpp ") { keyword ->
|
||||||
val keyword = it.trim()
|
|
||||||
if (keyword.isEmpty()) return@startsWith
|
if (keyword.isEmpty()) return@startsWith
|
||||||
logger.info("check cpp \"$keyword\"")
|
logger.info("check cpp \"$keyword\"")
|
||||||
val link = indexCpp[keyword]
|
val index = Data.getIndex(keyword)
|
||||||
if (link != null) {
|
if (index != null) {
|
||||||
subject.sendMessage(cppreferencePrefix + link)
|
subject.sendMessage(message.quote() + cppreferencePrefix + index.link)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package top.jie65535.jcr
|
||||||
|
|
||||||
|
import net.mamoe.mirai.console.command.CommandSender
|
||||||
|
import net.mamoe.mirai.console.command.CompositeCommand
|
||||||
|
|
||||||
|
object PluginCommands : CompositeCommand(
|
||||||
|
JCppReferencePlugin, "jcr",
|
||||||
|
description = "J CppReference Commands"
|
||||||
|
) {
|
||||||
|
@SubCommand
|
||||||
|
@Description("更新索引")
|
||||||
|
suspend fun CommandSender.update() {
|
||||||
|
Data.updateData()
|
||||||
|
sendMessage("OK")
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1 @@
|
|||||||
|
[{"type":"symbol","symbolType":"macro","name":"__alignas_is_defined","link":"cpp/types","marks":{"since":"c++11"},"description":"C compatibility macro constant, expands to integer literal 1"},{"type":"symbol","symbolType":"macro","name":"__bool_true_false_are_defined","link":"cpp/types","marks":{"since":"c++11"},"description":"C compatibility macro constant, expands to integer literal 1"},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_adaptor_iterator_pair_constructor","link":"cpp/utility/feature_test","marks":{"since":"c++23"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_addressof_constexpr","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_algorithm_iterator_requirements","link":"cpp/utility/feature_test","marks":{"since":"c++23"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_allocate_at_least","link":"cpp/utility/feature_test","marks":{"since":"c++23"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_allocator_traits_is_always_equal","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_any","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_apply","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_array_constexpr","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_as_const","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_associative_heterogeneous_erasure","link":"cpp/utility/feature_test","marks":{"since":"c++23"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_assume_aligned","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_atomic_flag_test","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_atomic_float","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_atomic_is_always_lock_free","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_atomic_lock_free_type_aliases","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_atomic_ref","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_atomic_shared_ptr","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_atomic_value_initialization","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_atomic_wait","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_barrier","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_bind_back","link":"cpp/utility/feature_test","marks":{"since":"c++23"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_bind_front","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_bit_cast","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_bitops","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_bool_constant","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_bounded_array_traits","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_boyer_moore_searcher","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_byte","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_byteswap","link":"cpp/utility/feature_test","marks":{"since":"c++23"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_char8_t","link":"cpp/utility/feature_test","marks":{"since":"c++20"},"description":""},{"type":"symbol","symbolType":"macro","name":"__cpp_lib_cLine truncated
|
||||||
Reference in new issue
Block a user