All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.skillw.pouvoir.util.FileUtil.kt Maven / Gradle / Ivy

There is a newer version: 1.6.7-beta-6
Show newest version
package com.skillw.pouvoir.util

import com.skillw.pouvoir.Pouvoir
import com.skillw.pouvoir.util.plugin.Pair
import com.skillw.pouvoir.util.script.ColorUtil.uncolored
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.configuration.serialization.ConfigurationSerializable
import org.bukkit.plugin.java.JavaPlugin
import taboolib.common.platform.function.console
import taboolib.common.platform.function.getDataFolder
import taboolib.common.platform.function.warning
import taboolib.module.configuration.Configuration
import taboolib.module.configuration.Type
import taboolib.module.configuration.flatten
import taboolib.module.configuration.util.asMap
import taboolib.module.lang.sendLang
import java.io.File
import java.math.BigInteger
import java.security.MessageDigest

/**
 * IO相关工具类
 *
 * @constructor Create empty File utils
 */


fun File.pathNormalize(): String {
    return absolutePath.replace(Pouvoir.configManager.serverDirectory.absolutePath, "").replace("\\", "/")
}

@Suppress("UNCHECKED_CAST")

fun  loadMultiply(mainFile: File, clazz: Class): List> {
    return mainFile.run {
        val list = ArrayList>()
        for (file in listSubFiles().filter { it.extension == "yml" }) {
            val config = file.loadYaml(true) ?: continue
            for (key in config.getKeys(false)) {
                try {
                    list.add(
                        Pair(
                            (clazz.getMethod(
                                "deserialize",
                                ConfigurationSection::class.java
                            ).invoke(null, config[key]!!) as? T? ?: continue), file
                        )
                    )
                } catch (t: Throwable) {
                    warning("在读取配置文件 ${mainFile.name} (${clazz.name}) 时出错, 报错如下")
                    t.printStackTrace()
                }
            }
        }
        list
    }
}


fun ConfigurationSection.toMap(): Map {
    val newMap = HashMap()
    for (it in this.getKeys(false)) {
        val value = this[it]
        if (value is ConfigurationSection) {
            newMap[it] = value.toMap()
            continue
        }
        newMap[it] = value!!
    }
    return newMap
}


fun File.loadYaml(notice: Boolean = false): YamlConfiguration? {
    val config = YamlConfiguration()
    runCatching {
        config.load(this)
    }.exceptionOrNull()?.let {
        if (!notice) return null
        console().sendLang("wrong-config", name)
        console().sendLang("wrong-config-cause", uncolored(it.message ?: "null"))
        it.printStackTrace()
    }
    return config
}


val serverFolder = getDataFolder().parentFile.parentFile


fun File.listSubFiles(): List {
    val files: MutableList = ArrayList()
    if (isDirectory) {
        listFiles()?.forEach { files.addAll(it.listSubFiles()) }
    } else {
        files.add(this)
    }
    return files
}


fun File.loadYamls(): List {
    val yamls = ArrayList()
    for (subFile in listSubFiles()) {
        if (subFile.isFile && subFile.name.endsWith(".yml")) {
            yamls.add(subFile.loadYaml() ?: continue)
            continue
        }
        if (subFile.isDirectory)
            yamls.addAll(subFile.loadYamls())
    }
    return yamls

}


fun File.md5(): String? {
    val bi: BigInteger
    try {
        val buffer = ByteArray(8192)
        var len: Int
        val md = MessageDigest.getInstance("MD5")
        val fis = this.inputStream()
        while ((fis.read(buffer).also { len = it }) != -1) {
            md.update(buffer, 0, len)
        }
        fis.close()
        val b = md.digest()
        bi = BigInteger(1, b)
    } catch (e: Exception) {
        e.printStackTrace()
        return null
    }
    return bi.toString(16)
}

fun JavaPlugin.completeYaml(file: File, ignores: Set = emptySet()): Map {
    val path = file.path.substringAfter(File.separator).substringAfter(File.separator)
    return completeYaml(path, ignores)
}

fun JavaPlugin.completeYaml(filePath: String, ignores: Set = emptySet()): Map {
    val resource = getResource(filePath) ?: error("No such file called $filePath in the Jar!")
    val default = Configuration.loadFromInputStream(resource)
    val target = Configuration.loadFromFile(File(dataFolder, filePath), Type.YAML)
    default.toMap().flatten()
        .filterKeys { ignores.all { ignore -> ignore !in it } }
        .forEach { (path, value) ->
            // Value
            if (!target.contains(path)) {
                target[path] = value
            }
            // Comment
            target.setComment(path, default.getComment(path) ?: return@forEach)
        }
    target.saveToFile()
    return target.asMap()
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy