This repository was archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathentry.js
More file actions
394 lines (393 loc) · 9.76 KB
/
Copy pathentry.js
File metadata and controls
394 lines (393 loc) · 9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
const fs = require('fs')
const path = require('path')
const File = require('!io/File')
const CONFIG = require('!config/mc-extra')
const CompilerError = require('!errors/CompilerError')
const logger = require('!logger')
const mc = require('!lang/mc')
const tagLoopup = {
blocks: 'blocks',
entities: 'entity_types',
fluids: 'fluids',
functions: 'functions',
items: 'items',
}
let state = {}
const evaluate = (line, token) => {
try {
return mc.transpiler.evaluators.code.evaluateCodeWithEnv(`return ${line}`, mc.getEnv())
} catch (e) {
return true
}
}
function targetedJSONAction(trigger, getPath) {
return {
match: ({ token }) => token.startsWith(trigger),
exec(_, tokens) {
const { token } = tokens.shift()
const firstSpace = token.indexOf(' ') + 1
const name = token.substr(firstSpace, token.length)
const file = new File()
file.setPath(mc.transpiler.evaluate_str(getPath(name, _)))
mc.transpiler.validate_next_destructive(tokens, '{')
let count = 1
let content = ['{']
while (count && tokens.length) {
const item = tokens.shift().token
content.push(mc.transpiler.evaluate_str(item))
if (item === '{') count++
if (item === '}') count--
}
file.setContents(content.join(''))
file.confirm()
},
}
}
const tagConsumer = mc.transpiler.list({
getToken: list => list[0],
actions: [
{
match: ({ token }) => /^LOOP/.test(token),
exec(file, tokens, tag) {
const { token } = tokens.shift()
mc.transpiler.consumer.Loop(file, token, tokens, tag, tagConsumer)
},
},
{
match: ({ token }) => /^!IF\(/.test(token),
exec(file, tokens, tag) {
const _token = tokens.shift()
const { token } = _token
const condition = token.substr(4, token.length - 5)
tokens.shift()
mc.transpiler.validate_next_destructive(tokens, '{')
if (evaluate(condition, _token)) {
while (tokens[0].token != '}') {
tagConsumer(file, tokens, tag)
}
mc.transpiler.validate_next_destructive(tokens, '}')
} else {
let count = 1
while (count && tokens.length) {
let item = tokens.shift().token
if (item === '{') count++
if (item === '}') count--
}
}
},
},
{
match: ({ token }) => /^!.+/.test(token),
exec(file, tokens, tag) {
const { token } = tokens[0]
const condition = token.substr(1)
tokens.shift()
mc.transpiler.validate_next_destructive(tokens, '{')
if (evaluate(condition)) {
while (tokens[0].token != '}') {
tagConsumer(file, tokens, tag)
}
mc.transpiler.validate_next_destructive(tokens, '}')
} else {
let count = 1
while (count && tokens.length) {
let item = tokens.shift().token
if (item === '{') count++
if (item === '}') count--
}
}
},
},
],
def: (file, tokens, tag) => {
const { token } = tokens.shift()
tag.values.add(mc.transpiler.evaluate_str(token))
},
})
module.exports = function MC_EXTRA(registry) {
logger.info(`loading mc-extra in ${CONFIG.integrated ? 'integration' : 'file'} mode`)
if (CONFIG.integrated === true) {
const transpiler = mc.transpiler
const GenericConsumer = transpiler.consumer.Generic
const EntryConsumer = transpiler.consumer.EntryOp
GenericConsumer.addAction({
match: ({ token }) => token.startsWith('tags'),
exec(file, tokens, func) {
const _token = tokens.shift()
const { token } = _token
// tags bla add stone;
const [, name, action, value] = token.split(/\s+/)
switch (action) {
case 'add':
state.tags[name].values.add(mc.transpiler.evaluate_str(value))
break
default:
throw new CompilerError(`invalid tag action '${action}'`, _token.line)
}
},
})
EntryConsumer.addAction({
match: ({ token }) => /^(blocks|entities|fluids|functions|items)/.test(token),
exec(file, tokens) {
const { token } = tokens.shift()
const [type, name, replace] = token.split(/\s+/)
const tag = (state.tags[mc.transpiler.evaluate_str(name)] = {
type: tagLoopup[type],
replace: replace === 'replace',
values: new Set(),
namespace: mc.getNamespace(),
})
mc.transpiler.validate_next_destructive(tokens, '{')
while (tokens[0].token != '}') {
tagConsumer(file, tokens, tag)
}
tokens.shift()
},
})
EntryConsumer.addAction(
targetedJSONAction('loot', name => {
const namespace = mc.getNamespace()
return path.resolve(
process.cwd(),
'data',
namespace.namespace,
'loot_tables',
namespace.path + name + '.json'
)
})
)
EntryConsumer.addAction(
targetedJSONAction('predicate', name => {
const namespace = mc.getNamespace()
return path.resolve(
process.cwd(),
'data',
namespace.namespace,
'predicates',
namespace.path + name + '.json'
)
})
)
EntryConsumer.addAction(
targetedJSONAction('modifier', name => {
const namespace = mc.getNamespace()
return path.resolve(
process.cwd(),
'data',
namespace.namespace,
'item_modifiers',
namespace.path + name + '.json'
)
})
)
EntryConsumer.addAction(
targetedJSONAction('recipe', name => {
const namespace = mc.getNamespace()
return path.resolve(
process.cwd(),
'data',
namespace.namespace,
'recipes',
namespace.path + name + '.json'
)
})
)
EntryConsumer.addAction(
targetedJSONAction('advancement', name => {
const namespace = mc.getNamespace()
return path.resolve(
process.cwd(),
'data',
namespace.namespace,
'advancements',
namespace.path + name + '.json'
)
})
)
EntryConsumer.addAction(
targetedJSONAction('dimension', name => {
const namespace = mc.getNamespace()
return path.resolve(
process.cwd(),
'data',
namespace.namespace,
'dimensions',
namespace.path + name + '.json'
)
})
)
EntryConsumer.addAction(
targetedJSONAction('worldgen', name => {
const namespace = mc.getNamespace()
return path.resolve(
process.cwd(),
'data',
namespace.namespace,
'worldgen',
namespace.path + name + '.json'
)
})
)
mc.on('start', e => {
state = {
tags: {},
}
})
mc.on('end', e => {
for (const name in state.tags) {
const tag = state.tags[name]
const file = new File()
file.setPath(
path.resolve(
process.cwd(),
'data',
tag.namespace.namespace,
'tags',
tag.type,
tag.namespace.path + name + '.json'
)
)
file.setContents(
JSON.stringify({
replace: tag.replace,
values: Array.from(tag.values),
})
)
file.confirm()
}
})
} else {
const EntryOp = mc.transpiler.list({
def: (file, tokens) => {
throw new CompilerError(`unexpected token '${tokens[0].token}'`)
},
actions: [],
getToken: list => list[0],
})
EntryOp.addAction({
match: ({ token }) => /^(blocks|entities|fluids|functions|items)/.test(token),
exec(file, tokens) {
const { token } = tokens.shift()
const [type, name, replace] = token.split(/\s+/)
const parts = path.win32
.normalize(path.relative(path.resolve(process.cwd(), 'src'), file.split('.')[0]))
.split('\\')
const tag = (state.tags[name] = {
type: tagLoopup[type],
replace: replace === 'replace',
values: new Set(),
namespace: {
namespace: parts[0],
path: parts.slice(1).join('/') + (parts.length > 1 ? '/' : ''),
},
})
mc.transpiler.validate_next_destructive(tokens, '{')
while (tokens[0].token != '}') {
tagConsumer(file, tokens, tag)
}
tokens.shift()
},
})
EntryOp.addAction(
targetedJSONAction('loot', (name, file) => {
const parts = path.win32
.normalize(path.relative(path.resolve(process.cwd(), 'src'), file.split('.')[0]))
.split('\\')
return path.resolve(
process.cwd(),
'data',
parts[0],
'loot_tables',
[...parts.splice(1), name].join('/') + '.json'
)
})
)
EntryOp.addAction(
targetedJSONAction('predicate', (name, file) => {
const namespace = mc.getNamespace()
return path.resolve(
process.cwd(),
'data',
namespace.namespace,
'predicates',
namespace.path + name + '.json'
)
})
)
EntryOp.addAction(
targetedJSONAction('modifier', (name, file) => {
const namespace = mc.getNamespace()
return path.resolve(
process.cwd(),
'data',
namespace.namespace,
'item_modifiers',
namespace.path + name + '.json'
)
})
)
EntryOp.addAction(
targetedJSONAction('recipe', (name, file) => {
const namespace = mc.getNamespace()
return path.resolve(
process.cwd(),
'data',
namespace.namespace,
'recipes',
namespace.path + name + '.json'
)
})
)
EntryOp.addAction(
targetedJSONAction('advancement', (name, file) => {
const namespace = mc.getNamespace()
return path.resolve(
process.cwd(),
'data',
namespace.namespace,
'advancements',
namespace.path + name + '.json'
)
})
)
const EntryConsumer = (file, tokens) => {
while (tokens.length > 0) {
EntryOp(file, tokens)
}
}
registry.set('.mce', function (file) {
if (fs.existsSync(file)) {
state = {
tags: {},
}
const tokens = mc.transpiler.tokenize(fs.readFileSync(file, 'utf-8'))
EntryConsumer(file, tokens)
for (const name in state.tags) {
const tag = state.tags[name]
const file = new File()
file.setPath(
path.resolve(
process.cwd(),
'data',
tag.namespace.namespace,
'tags',
tag.type,
tag.namespace.path + name + '.json'
)
)
file.setContents(
JSON.stringify({
replace: tag.replace,
values: Array.from(tag.values),
})
)
file.confirm()
}
}
})
logger.info(`registered handler or extension for '.mce'`)
}
return {
exported: {},
}
}