mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-08-24 15:27:23 +00:00
initial: 基本完成的首个版本, 还需要调整一下.
暂时按照当初的计划实现了一个可用版本出来, 发布与否晚些再确定.
This commit is contained in:
57
scalabot-app/src/main/kotlin/util/Serializers.kt
Normal file
57
scalabot-app/src/main/kotlin/util/Serializers.kt
Normal file
@ -0,0 +1,57 @@
|
||||
package net.lamgc.scalabot.util
|
||||
|
||||
import com.google.gson.*
|
||||
import org.eclipse.aether.artifact.Artifact
|
||||
import org.eclipse.aether.artifact.DefaultArtifact
|
||||
import org.telegram.telegrambots.bots.DefaultBotOptions
|
||||
import java.lang.reflect.Type
|
||||
|
||||
object ProxyTypeSerializer : JsonDeserializer<DefaultBotOptions.ProxyType>,
|
||||
JsonSerializer<DefaultBotOptions.ProxyType> {
|
||||
|
||||
override fun deserialize(
|
||||
json: JsonElement,
|
||||
typeOfT: Type?,
|
||||
context: JsonDeserializationContext?
|
||||
): DefaultBotOptions.ProxyType {
|
||||
if (!json.isJsonPrimitive) {
|
||||
throw JsonParseException("Wrong configuration value type.")
|
||||
}
|
||||
val value = json.asString.trim()
|
||||
try {
|
||||
return DefaultBotOptions.ProxyType.valueOf(value.uppercase())
|
||||
} catch (e: IllegalArgumentException) {
|
||||
throw JsonParseException("Invalid value: $value")
|
||||
}
|
||||
}
|
||||
|
||||
override fun serialize(
|
||||
src: DefaultBotOptions.ProxyType,
|
||||
typeOfSrc: Type?,
|
||||
context: JsonSerializationContext?
|
||||
): JsonElement {
|
||||
return JsonPrimitive(src.toString())
|
||||
}
|
||||
}
|
||||
|
||||
object ArtifactSerializer : JsonSerializer<Artifact>, JsonDeserializer<Artifact> {
|
||||
override fun serialize(src: Artifact, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement {
|
||||
val gavBuilder = StringBuilder("${src.groupId}:${src.artifactId}")
|
||||
if (!src.extension.equals("jar")) {
|
||||
gavBuilder.append(':').append(src.extension)
|
||||
}
|
||||
if (src.classifier.isNotEmpty()) {
|
||||
gavBuilder.append(':').append(src.classifier)
|
||||
}
|
||||
return JsonPrimitive(gavBuilder.append(':').append(src.version).toString())
|
||||
}
|
||||
|
||||
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): Artifact {
|
||||
if (!json!!.isJsonPrimitive) {
|
||||
throw JsonParseException("Wrong configuration value type.")
|
||||
}
|
||||
return DefaultArtifact(json.asString.trim())
|
||||
}
|
||||
|
||||
}
|
||||
|
55
scalabot-app/src/main/kotlin/util/Utils.kt
Normal file
55
scalabot-app/src/main/kotlin/util/Utils.kt
Normal file
@ -0,0 +1,55 @@
|
||||
package net.lamgc.scalabot.util
|
||||
|
||||
import org.eclipse.aether.artifact.Artifact
|
||||
import java.io.File
|
||||
import java.io.FileFilter
|
||||
import java.io.FilenameFilter
|
||||
|
||||
internal fun ByteArray.toHaxString(): String = ByteUtils.bytesToHexString(this)
|
||||
|
||||
internal fun Artifact.equalsArtifact(that: Artifact): Boolean =
|
||||
this.groupId.equals(that.groupId) &&
|
||||
this.artifactId.equals(that.artifactId) &&
|
||||
this.version.equals(that.version) &&
|
||||
this.baseVersion.equals(that.baseVersion) &&
|
||||
this.isSnapshot == that.isSnapshot &&
|
||||
this.classifier.equals(that.classifier) &&
|
||||
this.extension.equals(that.extension) &&
|
||||
(if (this.file == null) that.file == null else this.file.equals(that.file)) &&
|
||||
this.properties.equals(that.properties)
|
||||
|
||||
internal fun File.deepListFiles(
|
||||
addSelf: Boolean = false,
|
||||
onlyFile: Boolean = false,
|
||||
fileFilter: FileFilter? = null,
|
||||
filenameFilter: FilenameFilter? = null
|
||||
): Array<File>? {
|
||||
val files = if (fileFilter != null) {
|
||||
this.listFiles(fileFilter)
|
||||
} else if (filenameFilter != null) {
|
||||
this.listFiles(filenameFilter)
|
||||
} else {
|
||||
this.listFiles()
|
||||
}
|
||||
|
||||
if (files == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
val result = if (addSelf) mutableSetOf(this) else mutableSetOf()
|
||||
for (file in files) {
|
||||
if (file.isFile) {
|
||||
result.add(file)
|
||||
} else {
|
||||
if (!onlyFile) {
|
||||
result.add(file)
|
||||
}
|
||||
val subFiles = file.deepListFiles(false, onlyFile, fileFilter, filenameFilter)
|
||||
if (subFiles != null) {
|
||||
result.addAll(subFiles)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.toTypedArray()
|
||||
}
|
||||
|
Reference in New Issue
Block a user