From ac12d99f6ffd09ef851ed4dfa02875e5ef8067a0 Mon Sep 17 00:00:00 2001 From: hank9999 Date: Thu, 1 Jan 2026 14:01:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BD=BF=E7=94=A8=20Kotlin=20Path.resol?= =?UTF-8?q?ve()=20=E6=96=B9=E6=B3=95=E4=BF=AE=E5=A4=8D=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/top/mcfpp/io/DatapackCreator.kt | 92 ++++++++++--------- 1 file changed, 47 insertions(+), 45 deletions(-) diff --git a/src/main/kotlin/top/mcfpp/io/DatapackCreator.kt b/src/main/kotlin/top/mcfpp/io/DatapackCreator.kt index 251421c..7cd45c3 100644 --- a/src/main/kotlin/top/mcfpp/io/DatapackCreator.kt +++ b/src/main/kotlin/top/mcfpp/io/DatapackCreator.kt @@ -14,11 +14,11 @@ import top.mcfpp.model.scope.GlobalScope import top.mcfpp.util.LogProcessor import top.mcfpp.util.StringHelper.toSnakeCase import top.mcfpp.util.Utils -import java.io.File import java.io.IOException -import java.nio.file.Files -import java.nio.file.Paths +import java.nio.file.Path import kotlin.io.path.Path +import kotlin.io.path.createDirectories +import kotlin.io.path.writeBytes /** @@ -50,19 +50,23 @@ object DatapackCreator { * @param path 路径 */ fun createDatapack(path: String) { + val basePath = Path(path) + val datapackPath = basePath.resolve(Project.config.name) + LogProcessor.debug("Clearing output folder...") //清空原输出文件夹 - delAllFile(File("$path/${Project.config.name}")) + delAllFile(datapackPath.toFile()) if(Project.config.copyImport){ LogProcessor.debug("Copy libs...") //标准库 - delAllFile(File("$path/Imports")) + val importsPath = basePath.resolve("Imports") + delAllFile(importsPath.toFile()) //新建文件夹 - File("$path/Imports/data").mkdirs() + importsPath.resolve("data").createDirectories() //复制库 for(module in Project.modules){ - module.extract(Path("$path/Imports/data")) + module.extract(importsPath.resolve("data")) } val importMcMeta = DatapackMcMeta( DatapackMcMeta.Pack( @@ -71,8 +75,7 @@ object DatapackCreator { ) ) val importMcMetaJson: String = JSON.toJSONString(importMcMeta) - Files.write(Paths.get("$path/Imports/pack.mcmeta"), importMcMetaJson.toByteArray()) - + importsPath.resolve("pack.mcmeta").writeBytes(importMcMetaJson.toByteArray()) } LogProcessor.debug("Creating datapack...") @@ -86,100 +89,99 @@ object DatapackCreator { val datapackMcMetaJson: String = JSON.toJSONString(datapackMcMeta) //创建文件夹 try { - Files.createDirectories(Paths.get("$path/${Project.config.name}/data")) + datapackPath.resolve("data").createDirectories() //创建pack.mcmeta - Files.write(Paths.get("$path/${Project.config.name}/pack.mcmeta"), datapackMcMetaJson.toByteArray()) + datapackPath.resolve("pack.mcmeta").writeBytes(datapackMcMetaJson.toByteArray()) //写入函数文件 for(namespace in GlobalScope.localNamespaces){ - genNamespace(path, namespace) + genNamespace(datapackPath, namespace) } for (namespace in GlobalScope.stdNamespaces){ - genNamespace(path, namespace) + genNamespace(datapackPath, namespace) } //写入宏函数 for ((function, command) in Project.macroFunction){ - val currPath = "$path/${Project.config.name}/data/mcfpp/function/dynamic/${function}.mcfunction" + val currPath = datapackPath.resolve("data").resolve("mcfpp").resolve("function").resolve("dynamic").resolve("${function}.mcfunction") LogProcessor.debug("Writing File: $currPath") - Files.createDirectories(Paths.get(currPath).parent) - Files.write(Paths.get(currPath), command.toByteArray()) + currPath.parent.createDirectories() + currPath.writeBytes(command.toByteArray()) } //写入标签json文件 for (tag in GlobalScope.functionTags.values) { - LogProcessor.debug("Writing File: " + path + "\\${Project.config.name}\\data\\" + tag.namespace + "\\tags\\function\\" + tag.identifier + ".json") - Files.createDirectories(Paths.get(path + "/${Project.config.name}/data/" + tag.namespace + "/tags/function")) - Files.write( - Paths.get(path + "/${Project.config.name}/data/" + tag.namespace + "/tags/function/" + tag.identifier + ".json"), - tag.tagJSON.toByteArray() - ) + val tagPath = datapackPath.resolve("data").resolve(tag.namespace).resolve("tags").resolve("function") + val jsonPath = tagPath.resolve("${tag.identifier}.json") + LogProcessor.debug("Writing File: ${jsonPath}") + tagPath.createDirectories() + jsonPath.writeBytes(tag.tagJSON.toByteArray()) } } catch (e: IOException) { throw e } } - private fun genFunction(currPath: String, f: Function){ + private fun genFunction(currPath: Path, f: Function){ if (f is Native) return - LogProcessor.debug("Writing File: $currPath\\${f.identifierWithParamType.toSnakeCase()}.mcfunction") + LogProcessor.debug("Writing File: ${currPath.resolve("${f.identifierWithParamType.toSnakeCase()}.mcfunction")}") f.commands.analyzeAll() val path = if(f is ExtensionFunction){ - "$currPath\\ex" + currPath.resolve("ex") }else{ currPath } - Files.createDirectories(Paths.get(path)) - Files.write(Paths.get("$path\\${f.identifierWithParamType.toSnakeCase()}.mcfunction"), f.cmdStr.toByteArray()) + path.createDirectories() + path.resolve("${f.identifierWithParamType.toSnakeCase()}.mcfunction").writeBytes(f.cmdStr.toByteArray()) if(f.compiledFunctions.isNotEmpty()){ for (cf in f.compiledFunctions.values) { - LogProcessor.debug("Writing File: $currPath\\${cf.identifierWithParamType.toSnakeCase()}.mcfunction") + LogProcessor.debug("Writing File: ${currPath.resolve("${cf.identifierWithParamType.toSnakeCase()}.mcfunction")}") f.commands.analyzeAll() - Files.write(Paths.get("$path\\${f.identifierWithParamType.toSnakeCase()}.mcfunction"), cf.cmdStr.toByteArray()) + path.resolve("${f.identifierWithParamType.toSnakeCase()}.mcfunction").writeBytes(cf.cmdStr.toByteArray()) } } } - private fun genTemplateFunction(currPath: String, f: Function){ + private fun genTemplateFunction(currPath: Path, f: Function){ if (f is Native) return - val path = if(f is ExtensionFunction) "$currPath\\ex" else currPath - Files.createDirectories(Paths.get(path)) + val path = if(f is ExtensionFunction) currPath.resolve("ex") else currPath + path.createDirectories() for (cf in f.compiledFunctions.values) { - LogProcessor.debug("Writing File: $currPath\\${cf.identifierWithParamType.toSnakeCase()}.mcfunction") + LogProcessor.debug("Writing File: ${currPath.resolve("${cf.identifierWithParamType.toSnakeCase()}.mcfunction")}") f.commands.analyzeAll() - Files.write(Paths.get("$path\\${f.identifierWithParamType.toSnakeCase()}.mcfunction"), cf.cmdStr.toByteArray()) + path.resolve("${f.identifierWithParamType.toSnakeCase()}.mcfunction").writeBytes(cf.cmdStr.toByteArray()) } } - private fun genObject(currPath: String, obj: CompoundData){ + private fun genObject(currPath: Path, obj: CompoundData){ //成员 obj.field.forEachFunction { - genFunction("${currPath}\\function\\${obj.identifier.toSnakeCase()}\\static", it) + genFunction(currPath.resolve("function").resolve(obj.identifier.toSnakeCase()).resolve("static"), it) } } - private fun genTemplate(currPath: String, t: DataTemplate){ + private fun genTemplate(currPath: Path, t: DataTemplate){ //成员 t.field.forEachFunction { - genTemplateFunction("$currPath\\function\\${t.identifier.toSnakeCase()}", it) + genTemplateFunction(currPath.resolve("function").resolve(t.identifier.toSnakeCase()), it) } t.constructors.forEach { - genTemplateFunction("$currPath\\function\\${t.identifier.toSnakeCase()}", it) + genTemplateFunction(currPath.resolve("function").resolve(t.identifier.toSnakeCase()), it) } } - private fun genClass(currPath: String, cls: Class) { + private fun genClass(currPath: Path, cls: Class) { //成员 cls.field.forEachFunction { - genFunction("$currPath\\function\\${cls.identifier.toSnakeCase()}", it) + genFunction(currPath.resolve("function").resolve(cls.identifier.toSnakeCase()), it) } cls.constructors.forEach { - genFunction("$currPath\\function\\${cls.identifier.toSnakeCase()}", it) + genFunction(currPath.resolve("function").resolve(cls.identifier.toSnakeCase()), it) } } - private fun genNamespace(path: String, namespace: MutableMap.MutableEntry) { - val currPath = "$path\\${Project.config.name}\\data\\${namespace.key}" + private fun genNamespace(datapackPath: Path, namespace: MutableMap.MutableEntry) { + val currPath = datapackPath.resolve("data").resolve(namespace.key) namespace.value.field.forEachFunction { - genFunction("$currPath\\function", it) + genFunction(currPath.resolve("function"), it) } namespace.value.field.forEachClass {