mca-json 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +14 -0
- package/.prettierrc +4 -0
- package/README.md +373 -0
- package/dist/bin/mca-chunks.d.ts +3 -0
- package/dist/bin/mca-chunks.d.ts.map +1 -0
- package/dist/bin/mca-chunks.js +43 -0
- package/dist/bin/mca-chunks.js.map +1 -0
- package/dist/bin/mca-find-chunks-with-signs.d.ts +3 -0
- package/dist/bin/mca-find-chunks-with-signs.d.ts.map +1 -0
- package/dist/bin/mca-find-chunks-with-signs.js +79 -0
- package/dist/bin/mca-find-chunks-with-signs.js.map +1 -0
- package/dist/bin/mca-json.d.ts +3 -0
- package/dist/bin/mca-json.d.ts.map +1 -0
- package/dist/bin/mca-json.js +73 -0
- package/dist/bin/mca-json.js.map +1 -0
- package/dist/bin/mca-trim-chunks-without-signs.d.ts +3 -0
- package/dist/bin/mca-trim-chunks-without-signs.d.ts.map +1 -0
- package/dist/bin/mca-trim-chunks-without-signs.js +117 -0
- package/dist/bin/mca-trim-chunks-without-signs.js.map +1 -0
- package/dist/bin/nbt-get-player-location.d.ts +3 -0
- package/dist/bin/nbt-get-player-location.d.ts.map +1 -0
- package/dist/bin/nbt-get-player-location.js +83 -0
- package/dist/bin/nbt-get-player-location.js.map +1 -0
- package/dist/bin/nbt-json.d.ts +3 -0
- package/dist/bin/nbt-json.d.ts.map +1 -0
- package/dist/bin/nbt-json.js +64 -0
- package/dist/bin/nbt-json.js.map +1 -0
- package/dist/block/banner.d.ts +13 -0
- package/dist/block/banner.d.ts.map +1 -0
- package/dist/block/banner.js +28 -0
- package/dist/block/banner.js.map +1 -0
- package/dist/block/barrel.d.ts +17 -0
- package/dist/block/barrel.d.ts.map +1 -0
- package/dist/block/barrel.js +21 -0
- package/dist/block/barrel.js.map +1 -0
- package/dist/block/beacon.d.ts +14 -0
- package/dist/block/beacon.d.ts.map +1 -0
- package/dist/block/beacon.js +14 -0
- package/dist/block/beacon.js.map +1 -0
- package/dist/block/bed.d.ts +4 -0
- package/dist/block/bed.d.ts.map +1 -0
- package/dist/block/bed.js +4 -0
- package/dist/block/bed.js.map +1 -0
- package/dist/block/beehive.d.ts +9 -0
- package/dist/block/beehive.d.ts.map +1 -0
- package/dist/block/beehive.js +10 -0
- package/dist/block/beehive.js.map +1 -0
- package/dist/block/bell.d.ts +4 -0
- package/dist/block/bell.d.ts.map +1 -0
- package/dist/block/bell.js +4 -0
- package/dist/block/bell.js.map +1 -0
- package/dist/block/block.d.ts +23 -0
- package/dist/block/block.d.ts.map +1 -0
- package/dist/block/block.js +49 -0
- package/dist/block/block.js.map +1 -0
- package/dist/block/generic.d.ts +10 -0
- package/dist/block/generic.d.ts.map +1 -0
- package/dist/block/generic.js +14 -0
- package/dist/block/generic.js.map +1 -0
- package/dist/block/has-entity-data.d.ts +8 -0
- package/dist/block/has-entity-data.d.ts.map +1 -0
- package/dist/block/has-entity-data.js +12 -0
- package/dist/block/has-entity-data.js.map +1 -0
- package/dist/block/mixins/custom-name.d.ts +5 -0
- package/dist/block/mixins/custom-name.d.ts.map +1 -0
- package/dist/block/mixins/custom-name.js +7 -0
- package/dist/block/mixins/custom-name.js.map +1 -0
- package/dist/block/mixins/lock.d.ts +5 -0
- package/dist/block/mixins/lock.d.ts.map +1 -0
- package/dist/block/mixins/lock.js +7 -0
- package/dist/block/mixins/lock.js.map +1 -0
- package/dist/block/mixins/mixin.d.ts +27 -0
- package/dist/block/mixins/mixin.d.ts.map +1 -0
- package/dist/block/mixins/mixin.js +36 -0
- package/dist/block/mixins/mixin.js.map +1 -0
- package/dist/block/sign.d.ts +15 -0
- package/dist/block/sign.d.ts.map +1 -0
- package/dist/block/sign.js +91 -0
- package/dist/block/sign.js.map +1 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/anvil.d.ts +54 -0
- package/dist/lib/anvil.d.ts.map +1 -0
- package/dist/lib/anvil.js +141 -0
- package/dist/lib/anvil.js.map +1 -0
- package/dist/lib/binary-data.d.ts +48 -0
- package/dist/lib/binary-data.d.ts.map +1 -0
- package/dist/lib/binary-data.js +188 -0
- package/dist/lib/binary-data.js.map +1 -0
- package/dist/lib/bit-data.d.ts +16 -0
- package/dist/lib/bit-data.d.ts.map +1 -0
- package/dist/lib/bit-data.js +75 -0
- package/dist/lib/bit-data.js.map +1 -0
- package/dist/lib/block-data.d.ts +20 -0
- package/dist/lib/block-data.d.ts.map +1 -0
- package/dist/lib/block-data.js +125 -0
- package/dist/lib/block-data.js.map +1 -0
- package/dist/lib/chunk.d.ts +112 -0
- package/dist/lib/chunk.d.ts.map +1 -0
- package/dist/lib/chunk.js +299 -0
- package/dist/lib/chunk.js.map +1 -0
- package/dist/nbt/nbt-base.d.ts +20 -0
- package/dist/nbt/nbt-base.d.ts.map +1 -0
- package/dist/nbt/nbt-base.js +30 -0
- package/dist/nbt/nbt-base.js.map +1 -0
- package/dist/nbt/nbt-byte-array.d.ts +13 -0
- package/dist/nbt/nbt-byte-array.d.ts.map +1 -0
- package/dist/nbt/nbt-byte-array.js +30 -0
- package/dist/nbt/nbt-byte-array.js.map +1 -0
- package/dist/nbt/nbt-byte.d.ts +13 -0
- package/dist/nbt/nbt-byte.d.ts.map +1 -0
- package/dist/nbt/nbt-byte.js +25 -0
- package/dist/nbt/nbt-byte.js.map +1 -0
- package/dist/nbt/nbt-compound.d.ts +15 -0
- package/dist/nbt/nbt-compound.d.ts.map +1 -0
- package/dist/nbt/nbt-compound.js +68 -0
- package/dist/nbt/nbt-compound.js.map +1 -0
- package/dist/nbt/nbt-double.d.ts +13 -0
- package/dist/nbt/nbt-double.d.ts.map +1 -0
- package/dist/nbt/nbt-double.js +25 -0
- package/dist/nbt/nbt-double.js.map +1 -0
- package/dist/nbt/nbt-end.d.ts +11 -0
- package/dist/nbt/nbt-end.d.ts.map +1 -0
- package/dist/nbt/nbt-end.js +22 -0
- package/dist/nbt/nbt-end.js.map +1 -0
- package/dist/nbt/nbt-float.d.ts +13 -0
- package/dist/nbt/nbt-float.d.ts.map +1 -0
- package/dist/nbt/nbt-float.js +25 -0
- package/dist/nbt/nbt-float.js.map +1 -0
- package/dist/nbt/nbt-int-array.d.ts +13 -0
- package/dist/nbt/nbt-int-array.d.ts.map +1 -0
- package/dist/nbt/nbt-int-array.js +30 -0
- package/dist/nbt/nbt-int-array.js.map +1 -0
- package/dist/nbt/nbt-int.d.ts +13 -0
- package/dist/nbt/nbt-int.d.ts.map +1 -0
- package/dist/nbt/nbt-int.js +25 -0
- package/dist/nbt/nbt-int.js.map +1 -0
- package/dist/nbt/nbt-list.d.ts +17 -0
- package/dist/nbt/nbt-list.d.ts.map +1 -0
- package/dist/nbt/nbt-list.js +70 -0
- package/dist/nbt/nbt-list.js.map +1 -0
- package/dist/nbt/nbt-long-array.d.ts +13 -0
- package/dist/nbt/nbt-long-array.d.ts.map +1 -0
- package/dist/nbt/nbt-long-array.js +32 -0
- package/dist/nbt/nbt-long-array.js.map +1 -0
- package/dist/nbt/nbt-long.d.ts +13 -0
- package/dist/nbt/nbt-long.d.ts.map +1 -0
- package/dist/nbt/nbt-long.js +25 -0
- package/dist/nbt/nbt-long.js.map +1 -0
- package/dist/nbt/nbt-short.d.ts +13 -0
- package/dist/nbt/nbt-short.d.ts.map +1 -0
- package/dist/nbt/nbt-short.js +25 -0
- package/dist/nbt/nbt-short.js.map +1 -0
- package/dist/nbt/nbt-string.d.ts +13 -0
- package/dist/nbt/nbt-string.d.ts.map +1 -0
- package/dist/nbt/nbt-string.js +27 -0
- package/dist/nbt/nbt-string.js.map +1 -0
- package/dist/nbt/nbt-tag-type.d.ts +16 -0
- package/dist/nbt/nbt-tag-type.d.ts.map +1 -0
- package/dist/nbt/nbt-tag-type.js +17 -0
- package/dist/nbt/nbt-tag-type.js.map +1 -0
- package/dist/nbt/nbt.d.ts +11 -0
- package/dist/nbt/nbt.d.ts.map +1 -0
- package/dist/nbt/nbt.js +57 -0
- package/dist/nbt/nbt.js.map +1 -0
- package/dist/nbt/snbt-data.d.ts +18 -0
- package/dist/nbt/snbt-data.d.ts.map +1 -0
- package/dist/nbt/snbt-data.js +34 -0
- package/dist/nbt/snbt-data.js.map +1 -0
- package/dist/nbt/snbt-parse.d.ts +3 -0
- package/dist/nbt/snbt-parse.d.ts.map +1 -0
- package/dist/nbt/snbt-parse.js +201 -0
- package/dist/nbt/snbt-parse.js.map +1 -0
- package/dist/nbt/snbt-to-nbt.d.ts +7 -0
- package/dist/nbt/snbt-to-nbt.d.ts.map +1 -0
- package/dist/nbt/snbt-to-nbt.js +178 -0
- package/dist/nbt/snbt-to-nbt.js.map +1 -0
- package/dist/types/coords.d.ts +3 -0
- package/dist/types/coords.d.ts.map +1 -0
- package/dist/types/coords.js +2 -0
- package/dist/types/coords.js.map +1 -0
- package/package.json +28 -0
- package/src/bin/mca-chunks.ts +54 -0
- package/src/bin/mca-find-chunks-with-signs.ts +109 -0
- package/src/bin/mca-json.ts +96 -0
- package/src/bin/mca-trim-chunks-without-signs.ts +146 -0
- package/src/bin/nbt-get-player-location.ts +102 -0
- package/src/bin/nbt-json.ts +85 -0
- package/src/block/banner.ts +50 -0
- package/src/block/barrel.ts +34 -0
- package/src/block/beacon.ts +20 -0
- package/src/block/bed.ts +3 -0
- package/src/block/beehive.ts +17 -0
- package/src/block/bell.ts +3 -0
- package/src/block/block.ts +62 -0
- package/src/block/generic.ts +14 -0
- package/src/block/has-entity-data.ts +20 -0
- package/src/block/mixins/custom-name.ts +8 -0
- package/src/block/mixins/lock.ts +8 -0
- package/src/block/mixins/mixin.ts +53 -0
- package/src/block/sign.ts +121 -0
- package/src/index.ts +38 -0
- package/src/lib/anvil.ts +178 -0
- package/src/lib/binary-data.ts +247 -0
- package/src/lib/bit-data.ts +101 -0
- package/src/lib/block-data.ts +180 -0
- package/src/lib/chunk.ts +389 -0
- package/src/nbt/nbt-base.ts +38 -0
- package/src/nbt/nbt-byte-array.ts +38 -0
- package/src/nbt/nbt-byte.ts +31 -0
- package/src/nbt/nbt-compound.ts +95 -0
- package/src/nbt/nbt-double.ts +31 -0
- package/src/nbt/nbt-end.ts +27 -0
- package/src/nbt/nbt-float.ts +31 -0
- package/src/nbt/nbt-int-array.ts +38 -0
- package/src/nbt/nbt-int.ts +31 -0
- package/src/nbt/nbt-list.ts +103 -0
- package/src/nbt/nbt-long-array.ts +40 -0
- package/src/nbt/nbt-long.ts +31 -0
- package/src/nbt/nbt-short.ts +31 -0
- package/src/nbt/nbt-string.ts +35 -0
- package/src/nbt/nbt-tag-type.ts +15 -0
- package/src/nbt/nbt.ts +70 -0
- package/src/nbt/snbt-data.ts +70 -0
- package/src/nbt/snbt-parse.ts +256 -0
- package/src/nbt/snbt-to-nbt.ts +219 -0
- package/src/types/coords.ts +2 -0
- package/src/types/neodoc.d.ts +28 -0
- package/tsconfig.json +25 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import debug from 'debug';
|
|
2
|
+
import { SnbtData, SnbtTokenType } from './snbt-data';
|
|
3
|
+
|
|
4
|
+
const debugLog = debug('snbt:parse');
|
|
5
|
+
|
|
6
|
+
interface TokenParser {
|
|
7
|
+
type: SnbtTokenType;
|
|
8
|
+
pattern: RegExp;
|
|
9
|
+
callback: (
|
|
10
|
+
offset: number,
|
|
11
|
+
type: SnbtTokenType,
|
|
12
|
+
snbtData: SnbtData,
|
|
13
|
+
match: RegExpMatchArray
|
|
14
|
+
) => number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function testParsers(
|
|
18
|
+
snbtData: SnbtData,
|
|
19
|
+
str: string,
|
|
20
|
+
tokenParsers: TokenParser[],
|
|
21
|
+
pos: number
|
|
22
|
+
) {
|
|
23
|
+
for (const { type, pattern, callback } of tokenParsers) {
|
|
24
|
+
const match = str.match(pattern);
|
|
25
|
+
|
|
26
|
+
if (match) {
|
|
27
|
+
debugLog(`Matched ${type} at position ${pos}: ${match[0]}`);
|
|
28
|
+
|
|
29
|
+
return callback(pos, type, snbtData, match);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const tokenParsers: TokenParser[] = [
|
|
37
|
+
{
|
|
38
|
+
type: 'WHITESPACE',
|
|
39
|
+
pattern: /^\s+/,
|
|
40
|
+
callback: (_pos, _type, _snbtData, matches) => {
|
|
41
|
+
return matches[0].length;
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
type: 'COLON',
|
|
46
|
+
pattern: /^:/,
|
|
47
|
+
callback: (pos, type, snbtData) => {
|
|
48
|
+
snbtData.addToken(pos, type, ':');
|
|
49
|
+
|
|
50
|
+
return 1;
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
type: 'COMMA',
|
|
55
|
+
pattern: /^,/,
|
|
56
|
+
callback: (pos, type, snbtData) => {
|
|
57
|
+
snbtData.addToken(pos, type, ',');
|
|
58
|
+
|
|
59
|
+
return 1;
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
type: 'BYTE_ARRAY_START',
|
|
64
|
+
pattern: /^\[\s*[bB]\s*;/,
|
|
65
|
+
callback: (pos, type, snbtData) => {
|
|
66
|
+
snbtData.addToken(pos, type, '[');
|
|
67
|
+
|
|
68
|
+
return 3;
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
type: 'INT_ARRAY_START',
|
|
73
|
+
pattern: /^\[\s*[iI]\s*;/,
|
|
74
|
+
callback: (pos, type, snbtData) => {
|
|
75
|
+
snbtData.addToken(pos, type, '[');
|
|
76
|
+
|
|
77
|
+
return 3;
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
type: 'LONG_ARRAY_START',
|
|
82
|
+
pattern: /^\[\s*[lL]\s*;/,
|
|
83
|
+
callback: (pos, type, snbtData) => {
|
|
84
|
+
snbtData.addToken(pos, type, '[');
|
|
85
|
+
|
|
86
|
+
return 3;
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
type: 'COMPOUND_START',
|
|
91
|
+
pattern: /^\{/,
|
|
92
|
+
callback: (pos, type, snbtData) => {
|
|
93
|
+
snbtData.addToken(pos, type, '{');
|
|
94
|
+
|
|
95
|
+
return 1;
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
type: 'COMPOUND_END',
|
|
100
|
+
pattern: /^\}/,
|
|
101
|
+
callback: (pos, type, snbtData) => {
|
|
102
|
+
snbtData.addToken(pos, type, '}');
|
|
103
|
+
|
|
104
|
+
return 1;
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
type: 'LIST_START',
|
|
109
|
+
pattern: /^\[/,
|
|
110
|
+
callback: (pos, type, snbtData) => {
|
|
111
|
+
snbtData.addToken(pos, type, '[');
|
|
112
|
+
|
|
113
|
+
return 1;
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
type: 'LIST_END',
|
|
118
|
+
pattern: /^\]/,
|
|
119
|
+
callback: (pos, type, snbtData) => {
|
|
120
|
+
snbtData.addToken(pos, type, ']');
|
|
121
|
+
|
|
122
|
+
return 1;
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
// Byte - true / false converts to 1 / 0
|
|
127
|
+
type: 'BYTE',
|
|
128
|
+
pattern: anchorPattern('([tT][rR][uU][eE]|[fF][aA][lL][sS][eE])'),
|
|
129
|
+
callback: (pos, type, snbtData, matches) => {
|
|
130
|
+
snbtData.addToken(
|
|
131
|
+
pos,
|
|
132
|
+
type,
|
|
133
|
+
matches[0].toLowerCase() === 'true' ? '1' : '0'
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
return matches[0].length;
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
// Byte - number with a suffix
|
|
141
|
+
type: 'BYTE',
|
|
142
|
+
pattern: anchorPattern('[-+]?[0-9]+[bB]'),
|
|
143
|
+
callback: (pos, type, snbtData, matches) => {
|
|
144
|
+
snbtData.addToken(pos, type, matches[0]);
|
|
145
|
+
|
|
146
|
+
return matches[0].length;
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
// Double - there's another without the D later
|
|
151
|
+
type: 'DOUBLE',
|
|
152
|
+
pattern: anchorPattern('[-+]?[0-9.]+[dD]'),
|
|
153
|
+
callback: (pos, type, snbtData, matches) => {
|
|
154
|
+
snbtData.addToken(pos, type, matches[0]);
|
|
155
|
+
|
|
156
|
+
return matches[0].length;
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
type: 'FLOAT',
|
|
161
|
+
pattern: anchorPattern('[-+]?[0-9.]+[fF]'),
|
|
162
|
+
callback: (pos, type, snbtData, matches) => {
|
|
163
|
+
snbtData.addToken(pos, type, matches[0]);
|
|
164
|
+
|
|
165
|
+
return matches[0].length;
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
type: 'LONG',
|
|
170
|
+
pattern: anchorPattern('[-+]?[0-9]+[lL]'),
|
|
171
|
+
callback: (pos, type, snbtData, matches) => {
|
|
172
|
+
snbtData.addToken(pos, type, matches[0]);
|
|
173
|
+
|
|
174
|
+
return matches[0].length;
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
type: 'SHORT',
|
|
179
|
+
pattern: anchorPattern('[-+]?[0-9]+[sS]'),
|
|
180
|
+
callback: (pos, type, snbtData, matches) => {
|
|
181
|
+
snbtData.addToken(pos, type, matches[0]);
|
|
182
|
+
|
|
183
|
+
return matches[0].length;
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
// Double - this must come after other suffixed number types but before
|
|
188
|
+
// Int's matcher
|
|
189
|
+
type: 'DOUBLE',
|
|
190
|
+
pattern: anchorPattern('[-+]?[0-9.]+'),
|
|
191
|
+
callback: (pos, type, snbtData, matches) => {
|
|
192
|
+
snbtData.addToken(pos, type, matches[0]);
|
|
193
|
+
|
|
194
|
+
return matches[0].length;
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
// Int - this must come after all other number types
|
|
199
|
+
type: 'INT',
|
|
200
|
+
pattern: anchorPattern('[-+]?[0-9]+'),
|
|
201
|
+
callback: (pos, type, snbtData, matches) => {
|
|
202
|
+
snbtData.addToken(pos, type, matches[0]);
|
|
203
|
+
|
|
204
|
+
return matches[0].length;
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
// String - single or double quotes
|
|
209
|
+
type: 'STRING',
|
|
210
|
+
pattern: /^([\"'])(?:\\.|.)*?\1/,
|
|
211
|
+
callback: (pos, type, snbtData, matches) => {
|
|
212
|
+
const chunk = JSON.parse(`"${matches[0].slice(1, -1)}"`);
|
|
213
|
+
snbtData.addToken(pos, type, chunk);
|
|
214
|
+
|
|
215
|
+
return matches[0].length;
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
// String - not quoted and all else failed
|
|
220
|
+
type: 'STRING',
|
|
221
|
+
pattern: anchorPattern('^[-0-9a-zA-Z_.+]+'),
|
|
222
|
+
callback: (pos, type, snbtData, matches) => {
|
|
223
|
+
snbtData.addToken(pos, type, matches[0]);
|
|
224
|
+
|
|
225
|
+
return matches[0].length;
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
];
|
|
229
|
+
|
|
230
|
+
// Creates a regular expression that will start at the beginning of the string
|
|
231
|
+
// and conclude with an "end of token" marker at the end.
|
|
232
|
+
function anchorPattern(pattern: string) {
|
|
233
|
+
return new RegExp(`^${pattern}\s*(?:$|,|}|]|:)`);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function snbtParse(snbt: string) {
|
|
237
|
+
const snbtData = new SnbtData();
|
|
238
|
+
let pos = 0;
|
|
239
|
+
|
|
240
|
+
while (pos < snbt.length) {
|
|
241
|
+
const increment = testParsers(
|
|
242
|
+
snbtData,
|
|
243
|
+
snbt.slice(pos),
|
|
244
|
+
tokenParsers,
|
|
245
|
+
pos
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
if (!increment) {
|
|
249
|
+
throw new Error(`Unexpected token at position ${pos}`);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
pos += increment;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return snbtData;
|
|
256
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { NbtBase } from '../nbt/nbt-base';
|
|
2
|
+
import { NbtByteArray } from '../nbt/nbt-byte-array';
|
|
3
|
+
import { NbtByte } from '../nbt/nbt-byte';
|
|
4
|
+
import { NbtCompound } from '../nbt/nbt-compound';
|
|
5
|
+
import { NbtDouble } from '../nbt/nbt-double';
|
|
6
|
+
import { NbtFloat } from '../nbt/nbt-float';
|
|
7
|
+
import { NbtIntArray } from '../nbt/nbt-int-array';
|
|
8
|
+
import { NbtInt } from '../nbt/nbt-int';
|
|
9
|
+
import { NbtList } from '../nbt/nbt-list';
|
|
10
|
+
import { NbtLongArray } from '../nbt/nbt-long-array';
|
|
11
|
+
import { NbtLong } from '../nbt/nbt-long';
|
|
12
|
+
import { NbtShort } from '../nbt/nbt-short';
|
|
13
|
+
import { NbtString } from '../nbt/nbt-string';
|
|
14
|
+
import { SnbtData, SnbtToken } from './snbt-data';
|
|
15
|
+
|
|
16
|
+
export const fromSnbtData: {
|
|
17
|
+
[key: string]: (token: SnbtToken, snbtData: SnbtData) => NbtBase<any>;
|
|
18
|
+
} = {
|
|
19
|
+
BYTE: snbtToNbtByte,
|
|
20
|
+
BYTE_ARRAY_START: snbtToNbtByteArray,
|
|
21
|
+
COMPOUND_START: snbtToNbtCompound,
|
|
22
|
+
DOUBLE: snbtToNbtDouble,
|
|
23
|
+
FLOAT: snbtToNbtFloat,
|
|
24
|
+
INT: snbtToNbtInt,
|
|
25
|
+
INT_ARRAY_START: snbtToNbtIntArray,
|
|
26
|
+
LIST_START: snbtToNbtList,
|
|
27
|
+
LONG: snbtToNbtLong,
|
|
28
|
+
LONG_ARRAY_START: snbtToNbtLongArray,
|
|
29
|
+
SHORT: snbtToNbtShort,
|
|
30
|
+
STRING: snbtToNbtString,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export function snbtToNbt(snbtData: SnbtData): NbtBase<any> {
|
|
34
|
+
const token = snbtData.token();
|
|
35
|
+
const processor = fromSnbtData[token.type];
|
|
36
|
+
|
|
37
|
+
if (processor) {
|
|
38
|
+
return processor(token, snbtData);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
throw new Error(`Invalid, non-matching token ${snbtData.showToken(token)}`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function snbtToNbtByte(token: SnbtToken): NbtByte {
|
|
45
|
+
return new NbtByte(parseInt(token.content || ''));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function snbtToNbtByteArray(_token: SnbtToken, snbtData: SnbtData): NbtByteArray {
|
|
49
|
+
const bytes: number[] = [];
|
|
50
|
+
let token = snbtData.token();
|
|
51
|
+
|
|
52
|
+
while (token && token.type === 'BYTE') {
|
|
53
|
+
bytes.push(parseInt(token.content || ''));
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
token = snbtData.token();
|
|
57
|
+
} catch (err) {
|
|
58
|
+
throw new Error(`${err} - Unexpected end of SNBT data while parsing byte array`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (token.type !== 'COMMA') {
|
|
62
|
+
throw new Error(`Expecting a comma but found ${snbtData.showToken(token)}`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
token = snbtData.token();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return new NbtByteArray(bytes);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function snbtToNbtCompound(_token: SnbtToken, snbtData: SnbtData): NbtCompound {
|
|
72
|
+
const data: NbtBase<any>[] = [];
|
|
73
|
+
let token = snbtData.token();
|
|
74
|
+
|
|
75
|
+
while (token && token.type !== 'COMPOUND_END') {
|
|
76
|
+
if (token.type !== 'STRING') {
|
|
77
|
+
throw new Error(`Expecting a string but found ${snbtData.showToken(token)}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const name = token.content;
|
|
81
|
+
let colon;
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
colon = snbtData.token();
|
|
85
|
+
} catch (err) {
|
|
86
|
+
throw new Error(`${err} - Unexpected end of SNBT data while looking for colon`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (colon.type !== 'COLON') {
|
|
90
|
+
throw new Error(`Expecting a colon but found ${snbtData.showToken(colon)}`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const nbt = snbtToNbt(snbtData);
|
|
94
|
+
nbt.name = name;
|
|
95
|
+
data.push(nbt);
|
|
96
|
+
token = snbtData.token();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return new NbtCompound(data);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function snbtToNbtDouble(token: SnbtToken): NbtDouble {
|
|
103
|
+
return new NbtDouble(parseFloat(token.content.slice(0, -1) || ''));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function snbtToNbtFloat(token: SnbtToken): NbtFloat {
|
|
107
|
+
return new NbtFloat(parseFloat(token.content || ''));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function snbtToNbtInt(token: SnbtToken): NbtInt {
|
|
111
|
+
return new NbtInt(parseInt(token.content || ''));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function snbtToNbtIntArray(_token: SnbtToken, snbtData: SnbtData): NbtIntArray {
|
|
115
|
+
const ints: number[] = [];
|
|
116
|
+
let token = snbtData.token();
|
|
117
|
+
|
|
118
|
+
while (token && token.type === 'INT') {
|
|
119
|
+
ints.push(parseInt(token.content || ''));
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
token = snbtData.token();
|
|
123
|
+
} catch (err) {
|
|
124
|
+
throw new Error(`${err} - Unexpected end of SNBT data while parsing int array`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (token.type !== 'COMMA') {
|
|
128
|
+
throw new Error(`Expecting a comma but found ${snbtData.showToken(token)}`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
token = snbtData.token();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return new NbtIntArray(ints);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function snbtToNbtList(_token: SnbtToken, snbtData: SnbtData): NbtList<any> {
|
|
138
|
+
const list: NbtBase<any>[] = [];
|
|
139
|
+
let token;
|
|
140
|
+
|
|
141
|
+
try {
|
|
142
|
+
token = snbtData.token();
|
|
143
|
+
} catch (err) {
|
|
144
|
+
throw new Error(`${err} - Unexpected end of SNBT data while parsing list at beginning`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
while (token.type !== 'LIST_END') {
|
|
148
|
+
snbtData.step(-1);
|
|
149
|
+
let nbt;
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
nbt = snbtToNbt(snbtData);
|
|
153
|
+
} catch (err) {
|
|
154
|
+
throw new Error(`${err} - Error parsing list element`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
list.push(nbt);
|
|
158
|
+
token = snbtData.token();
|
|
159
|
+
|
|
160
|
+
if (token.type === 'COMMA') {
|
|
161
|
+
try {
|
|
162
|
+
token = snbtData.token();
|
|
163
|
+
} catch (err) {
|
|
164
|
+
throw new Error(`${err} - Unexpected end of SNBT data while looking for comma or end of list`);
|
|
165
|
+
}
|
|
166
|
+
} else if (token.type !== 'LIST_END') {
|
|
167
|
+
throw new Error(`Expecting a comma or list end but found ${snbtData.showToken(token)}`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (!list.length) {
|
|
172
|
+
throw new Error('Empty list not allowed in SNBT because the type cannot be inferred');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const firstType = list[0].type;
|
|
176
|
+
|
|
177
|
+
for (let i = 1; i < list.length; i++) {
|
|
178
|
+
if (list[i].type !== firstType) {
|
|
179
|
+
throw new Error(`List contains mixed types: ${firstType} and ${list[i].type}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return new NbtList(list, firstType);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function snbtToNbtLong(token: SnbtToken): NbtLong {
|
|
187
|
+
return new NbtLong(BigInt(token.content.slice(0, -1) || ''));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function snbtToNbtLongArray(_token: SnbtToken, snbtData: SnbtData): NbtLongArray {
|
|
191
|
+
const longs: bigint[] = [];
|
|
192
|
+
let token = snbtData.token();
|
|
193
|
+
|
|
194
|
+
while (token && token.type === 'LONG') {
|
|
195
|
+
longs.push(BigInt(token.content.slice(0, -1) || ''));
|
|
196
|
+
|
|
197
|
+
try {
|
|
198
|
+
token = snbtData.token();
|
|
199
|
+
} catch (err) {
|
|
200
|
+
throw new Error(`${err} - Unexpected end of SNBT data while parsing long array`);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (token.type !== 'COMMA') {
|
|
204
|
+
throw new Error(`Expecting a comma but found ${snbtData.showToken(token)}`);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
token = snbtData.token();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return new NbtLongArray(longs);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function snbtToNbtShort(token: SnbtToken): NbtShort {
|
|
214
|
+
return new NbtShort(parseInt(token.content || ''));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function snbtToNbtString(token: SnbtToken): NbtString {
|
|
218
|
+
return new NbtString(token.content);
|
|
219
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
declare module 'neodoc' {
|
|
2
|
+
export interface ParsedArguments {
|
|
3
|
+
[key: string]: string | string[] | boolean;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export function run(
|
|
7
|
+
usage: string,
|
|
8
|
+
options?: {
|
|
9
|
+
allowUnknown?: boolean,
|
|
10
|
+
argv?: string[],
|
|
11
|
+
dontExit?: boolean,
|
|
12
|
+
env?: { [key: string]: string },
|
|
13
|
+
helpFlags?: string[],
|
|
14
|
+
laxPlacement?: boolean,
|
|
15
|
+
optionsFirst?: boolean,
|
|
16
|
+
repeatableOptions?: boolean,
|
|
17
|
+
requireFlags?: boolean,
|
|
18
|
+
smartOptions?: boolean,
|
|
19
|
+
stopAt?: string[],
|
|
20
|
+
transforms?: {
|
|
21
|
+
postsolve?: ((spec: any) => any)[],
|
|
22
|
+
presolve?: ((spec: any) => any)[],
|
|
23
|
+
},
|
|
24
|
+
version?: string,
|
|
25
|
+
versionFlags?: string[],
|
|
26
|
+
},
|
|
27
|
+
): ParsedArguments;
|
|
28
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowSyntheticDefaultImports": true,
|
|
4
|
+
"alwaysStrict": true,
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"declarationMap": true,
|
|
7
|
+
"experimentalDecorators": true,
|
|
8
|
+
"inlineSources": true,
|
|
9
|
+
"lib": ["esnext"],
|
|
10
|
+
"module": "esnext",
|
|
11
|
+
"moduleResolution": "node",
|
|
12
|
+
"noFallthroughCasesInSwitch": true,
|
|
13
|
+
"noImplicitAny": true,
|
|
14
|
+
"noImplicitOverride": true,
|
|
15
|
+
"noImplicitReturns": true,
|
|
16
|
+
"noImplicitThis": true,
|
|
17
|
+
"noUnusedLocals": true,
|
|
18
|
+
"noUnusedParameters": true,
|
|
19
|
+
"outDir": "dist",
|
|
20
|
+
"sourceMap": true,
|
|
21
|
+
"strict": true,
|
|
22
|
+
"target": "esnext"
|
|
23
|
+
},
|
|
24
|
+
"include": ["src"]
|
|
25
|
+
}
|