@@ -6,8 +6,9 @@ import top.mcfpp.Project
6
6
import top.mcfpp.model.*
7
7
import top.mcfpp.model.field.GlobalField
8
8
import top.mcfpp.model.function.ExtensionFunction
9
+ import top.mcfpp.model.function.Function
9
10
import top.mcfpp.util.LogProcessor
10
- import top.mcfpp.util.StringHelper
11
+ import top.mcfpp.util.StringHelper.toSnakeCase
11
12
import top.mcfpp.util.Utils
12
13
import java.io.*
13
14
import java.nio.file.*
@@ -113,152 +114,81 @@ object DatapackCreator {
113
114
}
114
115
}
115
116
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
+
116
175
private fun genNamespace (path : String , namespace : MutableMap .MutableEntry <String , Namespace >) {
117
176
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
- }
140
177
141
- }
142
- }
178
+ namespace.value.field.forEachFunction {
179
+ genFunction( " $currPath \\ function " , it)
143
180
}
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)
197
184
}
198
185
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)
230
188
}
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)
262
192
}
263
193
}
264
194
0 commit comments