Skip to content

Commit e2eb48a

Browse files
committed
重构了生成数据包的部分
1 parent 45b7fbe commit e2eb48a

File tree

6 files changed

+90
-178
lines changed

6 files changed

+90
-178
lines changed

src/main/kotlin/top/mcfpp/io/DatapackCreator.kt

Lines changed: 70 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import top.mcfpp.Project
66
import top.mcfpp.model.*
77
import top.mcfpp.model.field.GlobalField
88
import top.mcfpp.model.function.ExtensionFunction
9+
import top.mcfpp.model.function.Function
910
import top.mcfpp.util.LogProcessor
10-
import top.mcfpp.util.StringHelper
11+
import top.mcfpp.util.StringHelper.toSnakeCase
1112
import top.mcfpp.util.Utils
1213
import java.io.*
1314
import java.nio.file.*
@@ -113,152 +114,81 @@ object DatapackCreator {
113114
}
114115
}
115116

117+
private fun genFunction(currPath: String, f: Function){
118+
if (f is Native) return
119+
LogProcessor.debug("Writing File: $currPath\\${f.nameWithNamespace}.mcfunction")
120+
f.commands.analyzeAll()
121+
val path = if(f is ExtensionFunction){
122+
"$currPath\\ex"
123+
}else{
124+
currPath
125+
}
126+
Files.createDirectories(Paths.get(path))
127+
Files.write(Paths.get("$path\\${f.identifier}.mcfunction"), f.cmdStr.toByteArray())
128+
if(f.compiledFunctions.isNotEmpty()){
129+
for (cf in f.compiledFunctions.values) {
130+
LogProcessor.debug("Writing File: $currPath\\${cf.identifier}.mcfunction")
131+
f.commands.analyzeAll()
132+
Files.write(Paths.get("$path\\${f.identifier}.mcfunction"), cf.cmdStr.toByteArray())
133+
}
134+
}
135+
}
136+
137+
private fun genTemplateFunction(currPath: String, f: Function){
138+
if (f is Native) return
139+
val path = if(f is ExtensionFunction) "$currPath\\ex" else currPath
140+
Files.createDirectories(Paths.get(path))
141+
for (cf in f.compiledFunctions.values) {
142+
LogProcessor.debug("Writing File: $currPath\\${cf.identifier}.mcfunction")
143+
f.commands.analyzeAll()
144+
Files.write(Paths.get("$path\\${f.identifier}.mcfunction"), cf.cmdStr.toByteArray())
145+
}
146+
}
147+
148+
private fun genObject(currPath: String, obj: CompoundData){
149+
//成员
150+
obj.field.forEachFunction {
151+
genFunction("${currPath}\\function\\${obj.identifier.toSnakeCase()}\\static", it)
152+
}
153+
}
154+
155+
private fun genTemplate(currPath: String, t: DataTemplate){
156+
//成员
157+
t.field.forEachFunction {
158+
genTemplateFunction("$currPath\\function\\${t.identifier.toSnakeCase()}", it)
159+
}
160+
t.constructors.forEach {
161+
genTemplateFunction("$currPath\\function\\${t.identifier.toSnakeCase()}", it)
162+
}
163+
}
164+
165+
private fun genClass(currPath: String, cls: Class) {
166+
//成员
167+
cls.field.forEachFunction {
168+
genFunction("$currPath\\function\\${cls.identifier.toSnakeCase()}", it)
169+
}
170+
cls.constructors.forEach {
171+
genFunction("$currPath\\function\\${cls.identifier.toSnakeCase()}", it)
172+
}
173+
}
174+
116175
private fun genNamespace(path: String, namespace: MutableMap.MutableEntry<String, Namespace>) {
117176
val currPath = "$path\\${Project.config.name}\\data\\${namespace.key}"
118-
namespace.value.field.forEachFunction {f ->
119-
run {
120-
if (f is Native) {
121-
return@run
122-
}
123-
LogProcessor.debug("Writing File: $currPath\\function\\${f.nameWithNamespace}.mcfunction")
124-
f.commands.analyzeAll()
125-
Files.createDirectories(Paths.get("$currPath/function"))
126-
Files.write(
127-
Paths.get("$currPath/function/${f.nameWithNamespace}.mcfunction"),
128-
f.cmdStr.toByteArray()
129-
)
130-
if(f.compiledFunctions.isNotEmpty()){
131-
for (cf in f.compiledFunctions.values){
132-
LogProcessor.debug("Writing File: $currPath\\function\\${cf.nameWithNamespace}.mcfunction")
133-
f.commands.analyzeAll()
134-
Files.createDirectories(Paths.get("$currPath/function"))
135-
Files.write(
136-
Paths.get("$currPath/function/${cf.nameWithNamespace}.mcfunction"),
137-
cf.cmdStr.toByteArray()
138-
)
139-
}
140177

141-
}
142-
}
178+
namespace.value.field.forEachFunction {
179+
genFunction("$currPath\\function", it)
143180
}
144-
namespace.value.field.forEachClass { cls ->
145-
run {
146-
//成员
147-
cls.field.forEachFunction { f->
148-
run {
149-
if (f is Native) {
150-
return@run
151-
}
152-
LogProcessor.debug("Writing File: $currPath\\function\\" + f.nameWithNamespace + ".mcfunction")
153-
f.commands.analyzeAll()
154-
//TODO 可能无法正确创建文件夹
155-
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(cls.identifier)))
156-
if (f is ExtensionFunction){
157-
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(cls.identifier) + "/ex"))
158-
}
159-
Files.write(
160-
Paths.get("$currPath/function/" + f.nameWithNamespace + ".mcfunction"),
161-
f.cmdStr.toByteArray()
162-
)
163-
if(f.compiledFunctions.isNotEmpty()){
164-
for (cf in f.compiledFunctions.values) {
165-
LogProcessor.debug("Writing File: $currPath\\function\\" + cf.nameWithNamespace + ".mcfunction")
166-
f.commands.analyzeAll()
167-
//TODO 可能无法正确创建文件夹
168-
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(cls.identifier)))
169-
Files.write(Paths.get("$currPath/function/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
170-
}
171-
}
172-
}
173-
}
174-
//构造函数
175-
cls.constructors.forEach{ c ->
176-
run {
177-
LogProcessor.debug("Writing File: $currPath\\function\\" + c.nameWithNamespace + ".mcfunction")
178-
c.commands.analyzeAll()
179-
//TODO 可能无法正确创建文件夹
180-
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(cls.identifier)))
181-
Files.write(
182-
Paths.get("$currPath/function/" + c.nameWithNamespace + ".mcfunction"),
183-
c.cmdStr.toByteArray()
184-
)
185-
if(c.compiledFunctions.isNotEmpty()){
186-
for (cf in c.compiledFunctions.values) {
187-
LogProcessor.debug("Writing File: $currPath\\function\\" + cf.nameWithNamespace + ".mcfunction")
188-
c.commands.analyzeAll()
189-
//TODO 可能无法正确创建文件夹
190-
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(cls.identifier)))
191-
Files.write(Paths.get("$currPath/function/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
192-
}
193-
}
194-
}
195-
}
196-
}
181+
182+
namespace.value.field.forEachClass {
183+
genClass(currPath, it)
197184
}
198185

199-
namespace.value.field.forEachTemplate { t ->
200-
run {
201-
//成员
202-
t.field.forEachFunction { f->
203-
run {
204-
if (f is Native) {
205-
return@run
206-
}
207-
LogProcessor.debug("Writing File: $currPath\\function\\" + f.nameWithNamespace + ".mcfunction")
208-
f.commands.analyzeAll()
209-
//TODO 可能无法正确创建文件夹
210-
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(t.identifier)))
211-
if (f is ExtensionFunction){
212-
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(t.identifier) + "/ex"))
213-
}
214-
Files.write(
215-
Paths.get("$currPath/function/" + f.nameWithNamespace + ".mcfunction"),
216-
f.cmdStr.toByteArray()
217-
)
218-
if(f.compiledFunctions.isNotEmpty()){
219-
for (cf in f.compiledFunctions.values) {
220-
LogProcessor.debug("Writing File: $currPath\\function\\" + cf.nameWithNamespace + ".mcfunction")
221-
f.commands.analyzeAll()
222-
//TODO 可能无法正确创建文件夹
223-
Files.createDirectories(Paths.get("$currPath/function/" + StringHelper.toLowerCase(t.identifier)))
224-
Files.write(Paths.get("$currPath/function/" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
225-
}
226-
}
227-
}
228-
}
229-
}
186+
namespace.value.field.forEachTemplate {
187+
genTemplate(currPath, it)
230188
}
231-
namespace.value.field.forEachObject { obj ->
232-
run {
233-
//成员
234-
obj.field.forEachFunction { f->
235-
run {
236-
if (f is Native) {
237-
return@run
238-
}
239-
LogProcessor.debug("Writing File: $currPath\\function\\" + f.nameWithNamespace + ".mcfunction")
240-
f.commands.analyzeAll()
241-
//TODO 可能无法正确创建文件夹
242-
Files.createDirectories(Paths.get("$currPath\\function\\" + StringHelper.toLowerCase(obj.identifier) + "\\static"))
243-
if (f is ExtensionFunction){
244-
Files.createDirectories(Paths.get("$currPath\\function\\" + StringHelper.toLowerCase(obj.identifier) + "\\static\\ex"))
245-
}
246-
Files.write(
247-
Paths.get("$currPath\\function\\" + f.nameWithNamespace + ".mcfunction"),
248-
f.cmdStr.toByteArray()
249-
)
250-
if(f.compiledFunctions.isNotEmpty()){
251-
for (cf in f.compiledFunctions.values) {
252-
LogProcessor.debug("Writing File: $currPath\\function\\" + cf.nameWithNamespace + ".mcfunction")
253-
f.commands.analyzeAll()
254-
//TODO 可能无法正确创建文件夹
255-
Files.createDirectories(Paths.get("$currPath\\function\\" + StringHelper.toLowerCase(obj.identifier)))
256-
Files.write(Paths.get("$currPath\\function\\" + cf.nameWithNamespace + ".mcfunction"), cf.cmdStr.toByteArray())
257-
}
258-
}
259-
}
260-
}
261-
}
189+
190+
namespace.value.field.forEachObject {
191+
genObject(currPath, it)
262192
}
263193
}
264194

src/main/kotlin/top/mcfpp/io/MCFPPFile.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import top.mcfpp.model.field.FileField
1111
import top.mcfpp.model.field.GlobalField
1212
import top.mcfpp.model.function.Function
1313
import top.mcfpp.util.LogProcessor
14-
import top.mcfpp.util.StringHelper
14+
import top.mcfpp.util.StringHelper.pathToNamespace
15+
import top.mcfpp.util.StringHelper.toSnakeCase
1516
import java.io.File
1617
import java.io.FileInputStream
1718
import java.io.IOException
@@ -35,15 +36,13 @@ class MCFPPFile : File {
3536
var namespace: String
3637

3738
//TODO 同名文件的顶级函数之间的命名冲突
38-
val topFunction: Function = Function(StringHelper.toLegalIdentifier(this.name), context = null)
39+
val topFunction: Function = Function(this.name.toSnakeCase(), context = null)
3940

4041
var syntaxError = false
4142

4243
constructor(path: String) : super(path) {
4344
val n = Project.config.sourcePath!!.toAbsolutePath().relativize(this.toPath().toAbsolutePath().parent).toString()
44-
namespace = Project.config.rootNamespace + "." + StringHelper.toLegalIdentifier(
45-
n.replace("\\", ".").replace("/", ".")
46-
)
45+
namespace = Project.config.rootNamespace + "." + n.pathToNamespace().toSnakeCase()
4746
}
4847

4948
constructor(file: File) : this(file.absolutePath)
@@ -144,7 +143,7 @@ class MCFPPFile : File {
144143
Project.currNamespace = namespace
145144
//创建默认函数
146145
val func = Function(
147-
StringHelper.toLowerCase(nameWithoutExtension + "_default"), Project.currNamespace,
146+
(nameWithoutExtension + "_default").toSnakeCase(), Project.currNamespace,
148147
context = null
149148
)
150149
Function.currFunction = func

src/main/kotlin/top/mcfpp/model/function/ExtensionFunction.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package top.mcfpp.model.function
22

33
import top.mcfpp.Project
44
import top.mcfpp.antlr.mcfppParser.FunctionBodyContext
5-
import top.mcfpp.type.MCFPPBaseType
6-
import top.mcfpp.type.MCFPPType
75
import top.mcfpp.model.CompoundData
8-
import top.mcfpp.util.StringHelper
6+
import top.mcfpp.util.StringHelper.toSnakeCase
97

108
open class ExtensionFunction: Function {
119

@@ -32,7 +30,7 @@ open class ExtensionFunction: Function {
3230
for (p in normalParams) {
3331
re.append("_").append(p.typeIdentifier)
3432
}
35-
return StringHelper.toLowerCase(re.toString())
33+
return re.toString().toSnakeCase()
3634
}
3735

3836
/**
@@ -49,7 +47,7 @@ open class ExtensionFunction: Function {
4947
for (p in normalParams) {
5048
re.append("_").append(p.typeIdentifier)
5149
}
52-
return StringHelper.toLowerCase(re.toString())
50+
return re.toString().toSnakeCase()
5351
}
5452

5553
/**

src/main/kotlin/top/mcfpp/model/function/Function.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import top.mcfpp.model.field.GlobalField
1919
import top.mcfpp.model.generic.Generic
2020
import top.mcfpp.type.*
2121
import top.mcfpp.util.LogProcessor
22-
import top.mcfpp.util.StringHelper
22+
import top.mcfpp.util.StringHelper.toSnakeCase
2323
import top.mcfpp.util.TextTranslator
2424
import top.mcfpp.util.TextTranslator.translate
2525
import java.io.Serializable
@@ -238,7 +238,7 @@ open class Function : Member, FieldContainer, Serializable, WithDocument {
238238
for (p in normalParams) {
239239
re.append("_").append(p.typeIdentifier)
240240
}
241-
return StringHelper.toLowerCase(re.toString())
241+
return re.toString().toSnakeCase()
242242
}
243243

244244
/**
@@ -258,7 +258,7 @@ open class Function : Member, FieldContainer, Serializable, WithDocument {
258258
for (p in normalParams) {
259259
re.append("_").append(p.typeIdentifier)
260260
}
261-
return StringHelper.toLowerCase(re.toString())
261+
return re.toString().toSnakeCase()
262262
}
263263

264264
/**

src/main/kotlin/top/mcfpp/util/StringHelper.kt

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,9 @@ package top.mcfpp.util
22

33
object StringHelper {
44

5-
/**
6-
* 将字符串转换为可以被mc识别的小写形式。默认将“大写字母”改为“_小写字母”
7-
* @param str
8-
* @return
9-
*/
10-
fun toLowerCase(str: String): String {
5+
fun String.toSnakeCase(): String {
116
val s = StringBuilder("")
12-
for (c in str.withIndex()) {
13-
if(c.value.isUpperCase()){
14-
s.append("_").append(c.value.lowercase())
15-
}else{
16-
s.append(c.value)
17-
}
18-
}
19-
return s.toString()
20-
}
21-
22-
fun toLegalIdentifier(str: String): String {
23-
val s = StringBuilder("")
24-
for (c in str.withIndex()) {
7+
for (c in this.withIndex()) {
258
if(c.value.isLowerCase() || c.value.isDigit() || c.value == '_' || c.value == '-' || c.value == '.'){
269
s.append(c.value)
2710
}else if (c.value.isUpperCase()){
@@ -33,8 +16,12 @@ object StringHelper {
3316
return s.toString()
3417
}
3518

36-
fun splitNamespaceID(str: String): Pair<String?, String>{
37-
val s = str.split(":")
19+
fun String.pathToNamespace(): String{
20+
return this.replace("\\", ".").replace("/", ".")
21+
}
22+
23+
fun String.splitNamespaceID(): Pair<String?, String>{
24+
val s = this.split(":")
3825
if(s.size == 1){
3926
return Pair(null, s[0])
4027
}

src/test/kotlin/top/mcfpp/test/StringTest.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package top.mcfpp.test
33
import org.antlr.v4.runtime.CharStream
44
import org.antlr.v4.runtime.CharStreams
55
import org.antlr.v4.runtime.CommonTokenStream
6-
import org.checkerframework.checker.units.qual.A
7-
import top.mcfpp.Project
86
import top.mcfpp.antlr.mcfppLexer
97
import top.mcfpp.antlr.mcfppParser
108
import top.mcfpp.util.StringHelper
@@ -19,7 +17,7 @@ class StringTest{
1917

2018
@Test
2119
fun test1() {
22-
println(StringHelper.toLegalIdentifier("你好qwq000AAA"))
20+
println(StringHelper.toSnakeCase("你好qwq000AAA"))
2321
}
2422

2523
@Test

0 commit comments

Comments
 (0)