Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 47 additions & 45 deletions src/main/kotlin/top/mcfpp/io/DatapackCreator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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


/**
Expand Down Expand Up @@ -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(
Expand All @@ -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...")
Expand All @@ -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<String, Namespace>) {
val currPath = "$path\\${Project.config.name}\\data\\${namespace.key}"
private fun genNamespace(datapackPath: Path, namespace: MutableMap.MutableEntry<String, Namespace>) {
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 {
Expand Down