@bedrock-core/builder-validation 0.2.0 → 0.3.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/CHANGELOG.md +16 -0
- package/README.md +1 -1
- package/dist/package.json +3 -4
- package/dist/src/creator-tools.d.ts +19 -0
- package/dist/src/creator-tools.js +265 -0
- package/dist/src/document-rules.d.ts +6 -0
- package/dist/src/document-rules.js +203 -0
- package/dist/src/document-types.d.ts +18 -0
- package/dist/src/document-types.js +187 -0
- package/dist/src/index.d.ts +30 -27
- package/dist/src/index.js +253 -210
- package/dist/src/types-release.d.ts +20 -0
- package/dist/src/types-release.js +76 -0
- package/package.json +4 -8
- package/dist/schemas/languages.schema.json +0 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @bedrock-core/builder-validation
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 8adcb6e: Expose native Bedrock document types through the `@bedrock-core/bedrock-types` package root. Keep schema preparation, corrections, declaration compilation and distribution builds in the types package. The generator evaluates content templates using published document types.
|
|
8
|
+
|
|
9
|
+
Provide detailed animations, controller state machines and render controllers. Share client-resource descriptions and scripts between entities and attachables, retaining their document-specific fields and conditional item bindings.
|
|
10
|
+
|
|
11
|
+
Expose extensible component registries and reuse their maps in native documents. Standardize declaration cleanup, semantic duplicate detection, bounded-array output and interface spacing.
|
|
12
|
+
|
|
13
|
+
Use extensionless declaration imports and resolve generated definitions in the package's authoring project. Keep vanilla-only block metadata outside the public authoring API.
|
|
14
|
+
|
|
15
|
+
Run optional Creator Tools validation on the assembled behavior and resource packs and report rule diagnostics with pack paths.
|
|
16
|
+
|
|
17
|
+
Check final JSON documents against the installed native Bedrock types for the project's target version. Expose the release as `typesVersion` and each checked document as a `category`. Use `typesRoot` to select an explicit types package.
|
|
18
|
+
|
|
3
19
|
## 0.2.0
|
|
4
20
|
|
|
5
21
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# @bedrock-core/builder-validation
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Check pack JSON syntax and document shapes using the native Bedrock types for the project target.
|
|
4
4
|
|
|
5
5
|
https://bedrock-core.drav.dev/docs/builder/api/validatePackContent
|
package/dist/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bedrock-core/builder-validation",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@
|
|
7
|
-
"ajv": "8.17.1",
|
|
6
|
+
"@bedrock-core/bedrock-types": "26.50.5",
|
|
8
7
|
"jsonc-parser": "3.3.1",
|
|
9
|
-
"
|
|
8
|
+
"typescript-native": "npm:typescript@7.1.0-beta"
|
|
10
9
|
}
|
|
11
10
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ContentDiagnostic, ContentPack } from './index.ts';
|
|
2
|
+
export interface CreatorToolsValidationOptions {
|
|
3
|
+
mode: 'warn' | 'strict';
|
|
4
|
+
/** Project directory from which to resolve @minecraft/creator-tools. */
|
|
5
|
+
projectRoot?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface CreatorToolsDiagnostic extends ContentDiagnostic {
|
|
8
|
+
kind: 'creator-tools';
|
|
9
|
+
severity: 'error' | 'warning' | 'recommendation';
|
|
10
|
+
rule: string;
|
|
11
|
+
}
|
|
12
|
+
export interface CreatorToolsResult {
|
|
13
|
+
version?: string;
|
|
14
|
+
/** Rules that ran on the assembled pack set. */
|
|
15
|
+
rules: string[];
|
|
16
|
+
diagnostics: CreatorToolsDiagnostic[];
|
|
17
|
+
}
|
|
18
|
+
/** Run Mojang's CLI on the complete behavior/resource pack set before export. */
|
|
19
|
+
export declare function validateWithCreatorTools(packs: readonly ContentPack[], options: CreatorToolsValidationOptions, signal?: AbortSignal, targetVersion?: string): Promise<CreatorToolsResult>;
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
2
|
+
if (value !== null && value !== void 0) {
|
|
3
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
+
var dispose, inner;
|
|
5
|
+
if (async) {
|
|
6
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
+
dispose = value[Symbol.asyncDispose];
|
|
8
|
+
}
|
|
9
|
+
if (dispose === void 0) {
|
|
10
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
+
dispose = value[Symbol.dispose];
|
|
12
|
+
if (async) inner = dispose;
|
|
13
|
+
}
|
|
14
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
16
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
17
|
+
}
|
|
18
|
+
else if (async) {
|
|
19
|
+
env.stack.push({ async: true });
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
24
|
+
return function (env) {
|
|
25
|
+
function fail(e) {
|
|
26
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
27
|
+
env.hasError = true;
|
|
28
|
+
}
|
|
29
|
+
var r, s = 0;
|
|
30
|
+
function next() {
|
|
31
|
+
while (r = env.stack.pop()) {
|
|
32
|
+
try {
|
|
33
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
34
|
+
if (r.dispose) {
|
|
35
|
+
var result = r.dispose.call(r.value);
|
|
36
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
37
|
+
}
|
|
38
|
+
else s |= 1;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
45
|
+
if (env.hasError) throw env.error;
|
|
46
|
+
}
|
|
47
|
+
return next();
|
|
48
|
+
};
|
|
49
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
50
|
+
var e = new Error(message);
|
|
51
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
|
+
});
|
|
53
|
+
import { execFile } from 'node:child_process';
|
|
54
|
+
import { cp, lstat, mkdir, mkdtemp, open, readFile, readdir, rm } from 'node:fs/promises';
|
|
55
|
+
import { createRequire } from 'node:module';
|
|
56
|
+
import { tmpdir } from 'node:os';
|
|
57
|
+
import path from 'node:path';
|
|
58
|
+
import { promisify } from 'node:util';
|
|
59
|
+
const exec = promisify(execFile);
|
|
60
|
+
const packageName = '@minecraft/creator-tools';
|
|
61
|
+
const timeout = 300000;
|
|
62
|
+
function creatorTools(project) {
|
|
63
|
+
try {
|
|
64
|
+
return createRequire(path.join(project, 'package.json')).resolve(packageName + '/package.json');
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (error.code !== 'MODULE_NOT_FOUND') {
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
throw new Error('Install @minecraft/creator-tools in the project to enable its validation.', { cause: error });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function severity(type) {
|
|
74
|
+
switch (type) {
|
|
75
|
+
case 'error':
|
|
76
|
+
case 'internalProcessingError':
|
|
77
|
+
case 'testFail':
|
|
78
|
+
return 'error';
|
|
79
|
+
case 'warning':
|
|
80
|
+
return 'warning';
|
|
81
|
+
case 'recommendation':
|
|
82
|
+
return 'recommendation';
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Creator Tools counts nested textures as pack icons. Confirm the root icon for a duplicate finding. */
|
|
86
|
+
async function validRootPackIcon(directory) {
|
|
87
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
88
|
+
try {
|
|
89
|
+
const entries = await readdir(directory, { withFileTypes: true });
|
|
90
|
+
const icons = entries.filter(entry => entry.isFile() && entry.name.includes('pack_icon') && entry.name.endsWith('.png'));
|
|
91
|
+
if (icons.length !== 1 || icons[0].name !== 'pack_icon.png') {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
const icon = __addDisposableResource(env_1, await open(path.join(directory, 'pack_icon.png')), true);
|
|
95
|
+
const header = Buffer.alloc(33);
|
|
96
|
+
const { bytesRead } = await icon.read(header);
|
|
97
|
+
if (bytesRead !== header.length || header.subarray(0, 8).toString('hex') !== '89504e470d0a1a0a'
|
|
98
|
+
|| header.readUInt32BE(8) !== 13 || header.toString('ascii', 12, 16) !== 'IHDR') {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
const width = header.readUInt32BE(16), height = header.readUInt32BE(20);
|
|
102
|
+
return width === height && width >= 2 && width <= 256 && (width & (width - 1)) === 0;
|
|
103
|
+
}
|
|
104
|
+
catch (e_1) {
|
|
105
|
+
env_1.error = e_1;
|
|
106
|
+
env_1.hasError = true;
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
const result_1 = __disposeResources(env_1);
|
|
110
|
+
if (result_1)
|
|
111
|
+
await result_1;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** Check the installed release SDK when the CLI reports a beta version from its own catalog. */
|
|
115
|
+
async function matchesInstalledBeta(message, project, targetVersion) {
|
|
116
|
+
const issue = /^For (@minecraft\/[\w-]+), using an out of date beta version (\d+\.\d+\.\d+-beta) compared to the current version$/.exec(message ?? '');
|
|
117
|
+
if (!issue || !targetVersion) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
const [, moduleName, manifestVersion] = issue;
|
|
121
|
+
try {
|
|
122
|
+
const file = createRequire(path.join(project, 'package.json')).resolve(moduleName + '/package.json');
|
|
123
|
+
const sdk = JSON.parse(await readFile(file, 'utf8'));
|
|
124
|
+
const release = /^(\d+\.\d+\.\d+-beta)\.(\d+\.\d+\.\d+)-(?:stable|preview\.(\d+))$/.exec(sdk.version);
|
|
125
|
+
return sdk.name === moduleName && release !== null && release[1] === manifestVersion
|
|
126
|
+
&& (release[2] === targetVersion || (release[3] !== undefined && release[2] + '.' + release[3] === targetVersion));
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/** Run Mojang's CLI on the complete behavior/resource pack set before export. */
|
|
133
|
+
export async function validateWithCreatorTools(packs, options, signal, targetVersion) {
|
|
134
|
+
const selected = packs.filter(pack => pack.kind !== 'other');
|
|
135
|
+
if (!selected.length) {
|
|
136
|
+
return { rules: [], diagnostics: [] };
|
|
137
|
+
}
|
|
138
|
+
const size = 64 * 1024 * 1024;
|
|
139
|
+
let version;
|
|
140
|
+
let workspace;
|
|
141
|
+
try {
|
|
142
|
+
const projectRoot = path.resolve(options.projectRoot ?? process.cwd());
|
|
143
|
+
const manifestFile = creatorTools(projectRoot);
|
|
144
|
+
const manifest = JSON.parse(await readFile(manifestFile, 'utf8'));
|
|
145
|
+
const directory = path.dirname(manifestFile);
|
|
146
|
+
const executable = path.resolve(directory, manifest.bin?.mct ?? '');
|
|
147
|
+
const relative = path.relative(directory, executable);
|
|
148
|
+
if (manifest.name !== packageName || !relative || relative.startsWith('..' + path.sep) || relative === '..' || path.isAbsolute(relative)) {
|
|
149
|
+
throw new Error('The installed Creator Tools package has no valid mct entry point.');
|
|
150
|
+
}
|
|
151
|
+
version = manifest.version;
|
|
152
|
+
workspace = await mkdtemp(path.join(tmpdir(), 'bedrock-creator-tools-'));
|
|
153
|
+
const project = path.join(workspace, 'project');
|
|
154
|
+
const folders = new Map();
|
|
155
|
+
for (const [index, pack] of selected.entries()) {
|
|
156
|
+
signal?.throwIfAborted();
|
|
157
|
+
const folder = `${pack.kind}_packs/pack-${index}`;
|
|
158
|
+
const destination = path.join(project, folder);
|
|
159
|
+
await mkdir(path.dirname(destination), { recursive: true });
|
|
160
|
+
await cp(pack.path, destination, {
|
|
161
|
+
recursive: true,
|
|
162
|
+
verbatimSymlinks: true,
|
|
163
|
+
async filter(source) {
|
|
164
|
+
signal?.throwIfAborted();
|
|
165
|
+
if ((await lstat(source)).isSymbolicLink()) {
|
|
166
|
+
throw new Error('Creator Tools cannot validate pack links: ' + pack.id);
|
|
167
|
+
}
|
|
168
|
+
return true;
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
folders.set(folder, pack);
|
|
172
|
+
}
|
|
173
|
+
const args = [executable, 'validate', 'main', '-i', project, '-o', path.join(workspace, 'reports'), '--single', '--offline', '--json', '--force'];
|
|
174
|
+
const childOptions = {
|
|
175
|
+
cwd: workspace,
|
|
176
|
+
env: { ...process.env, MCTOOLS_DATA_DIR: path.join(workspace, 'data') },
|
|
177
|
+
maxBuffer: size,
|
|
178
|
+
signal,
|
|
179
|
+
timeout,
|
|
180
|
+
windowsHide: true,
|
|
181
|
+
};
|
|
182
|
+
const stdout = await exec(process.execPath, args, childOptions).then(result => result.stdout, (error) => {
|
|
183
|
+
// Completed checks can return nonzero alongside their structured findings.
|
|
184
|
+
if (typeof error.code === 'number' && [1, 3, 4, 5].includes(error.code) && error.stdout) {
|
|
185
|
+
return error.stdout;
|
|
186
|
+
}
|
|
187
|
+
throw error;
|
|
188
|
+
});
|
|
189
|
+
const report = JSON.parse(stdout.trim().replace(/^\uFEFF/, ''));
|
|
190
|
+
if (report.command !== 'validate' || !report.projects?.length || report.projects.some(project => !Array.isArray(project.items))) {
|
|
191
|
+
throw new Error('Creator Tools returned no validation project.');
|
|
192
|
+
}
|
|
193
|
+
const items = report.projects.flatMap(project => project.items);
|
|
194
|
+
const detailed = new Set(items.filter(item => item.type === 'error' || item.type === 'internalProcessingError').map(item => item.generatorId));
|
|
195
|
+
const rules = new Set();
|
|
196
|
+
const diagnostics = [];
|
|
197
|
+
for (const item of items) {
|
|
198
|
+
if (item.generatorId && item.type !== 'unknown') {
|
|
199
|
+
rules.add(item.generatorId);
|
|
200
|
+
}
|
|
201
|
+
const level = severity(item.type);
|
|
202
|
+
if (!level || (item.type === 'testFail' && detailed.has(item.generatorId))) {
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
if (item.generatorId === 'CPACKICON' && level === 'error') {
|
|
206
|
+
const duplicate = /^Found multiple pack icon files for (pack-\d+), there should be only one\.$/.exec(item.message ?? '');
|
|
207
|
+
const folder = duplicate && [...folders.keys()].find(folder => path.posix.basename(folder) === duplicate[1]);
|
|
208
|
+
if (folder && await validRootPackIcon(path.join(project, folder))) {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
const name = item.path?.replaceAll('\\', '/').replace(/^\/+/, '') ?? '';
|
|
213
|
+
const folder = [...folders.keys()].find(folder => name === folder || name.startsWith(folder + '/'));
|
|
214
|
+
const rule = item.generatorId ?? 'MCT';
|
|
215
|
+
if (rule === 'SCRIPTMODULE' && level === 'error' && folder && name === folder + '/manifest.json'
|
|
216
|
+
&& await matchesInstalledBeta(item.message, projectRoot, targetVersion)) {
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const detail = typeof item.data === 'string' ? ' — ' + item.data : '';
|
|
220
|
+
diagnostics.push({
|
|
221
|
+
kind: 'creator-tools',
|
|
222
|
+
severity: level,
|
|
223
|
+
rule,
|
|
224
|
+
pack: folder ? folders.get(folder).id : 'project',
|
|
225
|
+
file: folder ? name.slice(folder.length + 1) : name,
|
|
226
|
+
pointer: '',
|
|
227
|
+
message: `[${rule}] ${item.message ?? 'Creator Tools validation issue'}${detail}`,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
if (!rules.size && !diagnostics.length) {
|
|
231
|
+
throw new Error('Creator Tools did not run any validation rules.');
|
|
232
|
+
}
|
|
233
|
+
return { version, rules: [...rules].sort(), diagnostics };
|
|
234
|
+
}
|
|
235
|
+
catch (error) {
|
|
236
|
+
signal?.throwIfAborted();
|
|
237
|
+
// Child-process errors can include the entire command and report output.
|
|
238
|
+
const failure = error;
|
|
239
|
+
const detail = failure.killed
|
|
240
|
+
? `timed out after ${timeout / 1000} seconds`
|
|
241
|
+
: failure.signal
|
|
242
|
+
? 'process ended with ' + failure.signal
|
|
243
|
+
: failure.code
|
|
244
|
+
? 'process error ' + failure.code
|
|
245
|
+
: error instanceof SyntaxError ? 'invalid JSON report' : failure.message;
|
|
246
|
+
return {
|
|
247
|
+
version,
|
|
248
|
+
rules: [],
|
|
249
|
+
diagnostics: [{
|
|
250
|
+
kind: 'creator-tools',
|
|
251
|
+
severity: 'error',
|
|
252
|
+
rule: 'MCT',
|
|
253
|
+
pack: 'project',
|
|
254
|
+
file: '',
|
|
255
|
+
pointer: '',
|
|
256
|
+
message: 'Creator Tools validation could not complete: ' + detail,
|
|
257
|
+
}],
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
finally {
|
|
261
|
+
if (workspace && path.dirname(workspace) === path.resolve(tmpdir()) && path.basename(workspace).startsWith('bedrock-creator-tools-')) {
|
|
262
|
+
await rm(workspace, { recursive: true, force: true });
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
type Report = (pointer: string, message: string) => void;
|
|
2
|
+
/** Camera presets share their directory with separate aim assist documents. */
|
|
3
|
+
export declare function matchesDocumentType(category: string, document: unknown): boolean;
|
|
4
|
+
/** Prepare the in-memory JSON literal for native type checking and apply content rules. */
|
|
5
|
+
export declare function prepareCheckedDocument(category: string, document: unknown, report: Report): void;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
function record(value) {
|
|
2
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value) ? value : undefined;
|
|
3
|
+
}
|
|
4
|
+
function segment(key) {
|
|
5
|
+
return key.replaceAll('~', '~0').replaceAll('/', '~1');
|
|
6
|
+
}
|
|
7
|
+
// https://github.com/Blockception/Minecraft-bedrock-json-schemas/pull/647
|
|
8
|
+
const baseBlockComponents = [
|
|
9
|
+
'minecraft:block_entity',
|
|
10
|
+
'minecraft:embedded_visual',
|
|
11
|
+
'minecraft:flower_pottable',
|
|
12
|
+
'minecraft:item_visual',
|
|
13
|
+
'minecraft:redstone_consumer',
|
|
14
|
+
'minecraft:support',
|
|
15
|
+
];
|
|
16
|
+
function beforeFormat(formatVersion, minor, patch) {
|
|
17
|
+
const version = typeof formatVersion === 'string' ? /^1\.(\d+)(?:\.(\d+))?$/.exec(formatVersion) : null;
|
|
18
|
+
return version !== null && (Number(version[1]) < minor || (Number(version[1]) === minor && Number(version[2] ?? 0) < patch));
|
|
19
|
+
}
|
|
20
|
+
function checkStates(definition, report) {
|
|
21
|
+
const states = record(record(definition.description)?.states);
|
|
22
|
+
// Array length and shape are checked by BlockStates. JSON also needs integer and uniqueness checks.
|
|
23
|
+
// https://github.com/Blockception/Minecraft-bedrock-json-schemas/tree/main/source/behavior/blocks/format/states
|
|
24
|
+
for (const [name, values] of Object.entries(states ?? {})) {
|
|
25
|
+
const pointer = '/minecraft:block/description/states/' + segment(name);
|
|
26
|
+
if (Array.isArray(values)) {
|
|
27
|
+
if (new Set(values).size < values.length) {
|
|
28
|
+
report(pointer, 'Block state values must be unique.');
|
|
29
|
+
}
|
|
30
|
+
for (const [index, value] of values.entries()) {
|
|
31
|
+
if (typeof value === 'number' && !Number.isInteger(value)) {
|
|
32
|
+
report(pointer + '/' + index, 'Numeric block state values must be integers.');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const bounds = record(record(values)?.values);
|
|
38
|
+
const min = bounds?.min;
|
|
39
|
+
const max = bounds?.max;
|
|
40
|
+
if (typeof min === 'number' && typeof max === 'number'
|
|
41
|
+
&& (!Number.isInteger(min) || !Number.isInteger(max) || max < min || max - min > 15)) {
|
|
42
|
+
report(pointer + '/values', 'A block state range must contain 1 through 16 consecutive integers.');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function checkMaterials(materials, pointer, legacy, report) {
|
|
48
|
+
// https://learn.microsoft.com/minecraft/creator/reference/content/blockreference/examples/blockcomponents/minecraftblock_material_instances
|
|
49
|
+
for (const [face, value] of Object.entries(record(materials) ?? {})) {
|
|
50
|
+
const material = record(value);
|
|
51
|
+
const occlusion = material?.ambient_occlusion;
|
|
52
|
+
if (material && typeof occlusion === 'boolean') {
|
|
53
|
+
if (!legacy) {
|
|
54
|
+
report(pointer + '/' + segment(face) + '/ambient_occlusion', 'Ambient occlusion requires a numeric value from format_version 1.26.20.');
|
|
55
|
+
}
|
|
56
|
+
material.ambient_occlusion = Number(occlusion);
|
|
57
|
+
}
|
|
58
|
+
else if (!legacy && typeof occlusion === 'number' && (occlusion < 0 || occlusion > 10)) {
|
|
59
|
+
report(pointer + '/' + segment(face) + '/ambient_occlusion', 'Ambient occlusion must be between 0 and 10.');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function checkBlockComponents(components, pointer, legacy, permutation, report) {
|
|
64
|
+
const redstone = record(components['minecraft:redstone_consumer']);
|
|
65
|
+
const power = redstone?.min_power;
|
|
66
|
+
// https://learn.microsoft.com/minecraft/creator/reference/content/blockreference/examples/blockcomponents/minecraftblock_redstone_consumer
|
|
67
|
+
if (typeof power === 'number' && (!Number.isInteger(power) || power < 0 || power > 15)) {
|
|
68
|
+
report(pointer + '/minecraft:redstone_consumer/min_power', 'Minimum redstone power must be an integer from 0 through 15.');
|
|
69
|
+
}
|
|
70
|
+
const directions = record(components['minecraft:neighbor_change'])?.neighbor_directions;
|
|
71
|
+
// https://feedback.minecraft.net/hc/en-us/articles/49274834093453-Minecraft-Beta-Preview-26-60-29
|
|
72
|
+
if (Array.isArray(directions)) {
|
|
73
|
+
const directionPointer = pointer + '/minecraft:neighbor_change/neighbor_directions';
|
|
74
|
+
if (directions.length < 1 || directions.length > 6) {
|
|
75
|
+
report(directionPointer, 'Choose between one and six neighbor directions.');
|
|
76
|
+
}
|
|
77
|
+
else if (directions.includes('all') && directions.some(direction => direction !== 'all')) {
|
|
78
|
+
report(directionPointer, 'The all neighbor direction cannot be combined with specific faces.');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
checkMaterials(components['minecraft:material_instances'], pointer + '/minecraft:material_instances', legacy, report);
|
|
82
|
+
for (const key of ['minecraft:embedded_visual', 'minecraft:item_visual']) {
|
|
83
|
+
checkMaterials(record(components[key])?.material_instances, pointer + '/' + key + '/material_instances', legacy, report);
|
|
84
|
+
}
|
|
85
|
+
if (permutation) {
|
|
86
|
+
for (const key of baseBlockComponents) {
|
|
87
|
+
// https://learn.microsoft.com/minecraft/creator/documents/update1.26.20
|
|
88
|
+
if (Object.hasOwn(components, key) && !(legacy && key === 'minecraft:flower_pottable')) {
|
|
89
|
+
report(pointer + '/' + key, key + ' belongs in the base block components.');
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function checkParameterDepth(value, pointer, depth, report) {
|
|
95
|
+
const array = Array.isArray(value);
|
|
96
|
+
const container = array ? value : record(value);
|
|
97
|
+
if (!container) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
// At the final level, an array of scalar values is still valid.
|
|
101
|
+
// https://github.com/Blockception/Minecraft-bedrock-json-schemas/pull/642
|
|
102
|
+
if (depth >= (array ? 3 : 2)) {
|
|
103
|
+
report(pointer, 'Custom component parameters exceed the object and array depth limit.');
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
for (const [key, child] of Object.entries(container)) {
|
|
107
|
+
checkParameterDepth(child, pointer + '/' + segment(key), depth + 1, report);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function prepareCustomComponents(components, pointer, report, item = false) {
|
|
111
|
+
// Native fields are checked with the declarations. Custom V2 keys refer to script registrations.
|
|
112
|
+
// https://learn.microsoft.com/minecraft/creator/documents/scripting/custom-components
|
|
113
|
+
for (const [key, value] of Object.entries(components)) {
|
|
114
|
+
if (/^[a-z0-9_.-]+:[a-z0-9_./-]+$/.test(key) && !key.startsWith('minecraft:')) {
|
|
115
|
+
const componentPointer = pointer + '/' + segment(key);
|
|
116
|
+
if (item && value === null) {
|
|
117
|
+
report(componentPointer, 'A custom item component requires a parameter value. Use an empty object for a component without parameters.');
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
checkParameterDepth(value, componentPointer, 0, report);
|
|
121
|
+
}
|
|
122
|
+
delete components[key];
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function checkDimension(document, report) {
|
|
127
|
+
const root = record(document);
|
|
128
|
+
const dimension = record(root?.['minecraft:dimension']);
|
|
129
|
+
const components = record(dimension?.components);
|
|
130
|
+
if (!components) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
// https://learn.microsoft.com/minecraft/creator/documents/scripting/custom-dimension-api-tutorial
|
|
134
|
+
const pointer = '/minecraft:dimension/components';
|
|
135
|
+
const identifier = record(dimension?.description)?.identifier;
|
|
136
|
+
const bounds = record(components['minecraft:dimension_bounds']);
|
|
137
|
+
if (beforeFormat(root?.format_version, 26, 50) && !Object.hasOwn(components, 'minecraft:dimension_height')
|
|
138
|
+
&& bounds && typeof bounds.min === 'number' && typeof bounds.max === 'number') {
|
|
139
|
+
components['minecraft:dimension_height'] = { min_y: bounds.min, height_range: bounds.max - bounds.min };
|
|
140
|
+
delete components['minecraft:dimension_bounds'];
|
|
141
|
+
}
|
|
142
|
+
if (typeof identifier === 'string' && !identifier.startsWith('minecraft:') && !Object.hasOwn(components, 'minecraft:generation')) {
|
|
143
|
+
report(pointer + '/minecraft:generation', 'A custom dimension requires the generation component.');
|
|
144
|
+
}
|
|
145
|
+
const height = record(components['minecraft:dimension_height']);
|
|
146
|
+
const min = height?.min_y;
|
|
147
|
+
const range = height?.height_range;
|
|
148
|
+
if (typeof min === 'number' && typeof range === 'number'
|
|
149
|
+
&& (min % 16 !== 0 || range % 16 !== 0 || min < -512 || range < 16 || range > 1024 || min + range > 512)) {
|
|
150
|
+
report(pointer + '/minecraft:dimension_height', 'Dimension bounds must use multiples of 16, span 16 through 1024 blocks, and stay between -512 and 512.');
|
|
151
|
+
}
|
|
152
|
+
const clouds = record(components['minecraft:clouds'])?.height;
|
|
153
|
+
if (typeof clouds === 'number' && (!Number.isInteger(clouds) || clouds < -512 || clouds > 512)) {
|
|
154
|
+
report(pointer + '/minecraft:clouds/height', 'Cloud height must be an integer between -512 and 512.');
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/** Camera presets share their directory with separate aim assist documents. */
|
|
158
|
+
export function matchesDocumentType(category, document) {
|
|
159
|
+
if (category !== 'bp/cameras/presets') {
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
const root = record(document);
|
|
163
|
+
return !root || Object.hasOwn(root, 'minecraft:camera_preset')
|
|
164
|
+
|| (!Object.hasOwn(root, 'minecraft:aim_assist_preset') && !Object.hasOwn(root, 'minecraft:aim_assist_categories'));
|
|
165
|
+
}
|
|
166
|
+
/** Prepare the in-memory JSON literal for native type checking and apply content rules. */
|
|
167
|
+
export function prepareCheckedDocument(category, document, report) {
|
|
168
|
+
if (category === 'bp/dimensions') {
|
|
169
|
+
checkDimension(document, report);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (category !== 'bp/blocks' && category !== 'bp/items') {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const root = record(document);
|
|
176
|
+
const block = category === 'bp/blocks';
|
|
177
|
+
const key = block ? 'minecraft:block' : 'minecraft:item';
|
|
178
|
+
const definition = record(root?.[key]);
|
|
179
|
+
if (!definition) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const legacy = beforeFormat(root?.format_version, 26, 20);
|
|
183
|
+
const components = record(definition.components);
|
|
184
|
+
if (components) {
|
|
185
|
+
if (block) {
|
|
186
|
+
checkBlockComponents(components, '/' + key + '/components', legacy, false, report);
|
|
187
|
+
}
|
|
188
|
+
prepareCustomComponents(components, '/' + key + '/components', report, !block);
|
|
189
|
+
}
|
|
190
|
+
if (block) {
|
|
191
|
+
checkStates(definition, report);
|
|
192
|
+
if (Array.isArray(definition.permutations)) {
|
|
193
|
+
for (const [index, value] of definition.permutations.entries()) {
|
|
194
|
+
const overrides = record(record(value)?.components);
|
|
195
|
+
if (overrides) {
|
|
196
|
+
const pointer = '/' + key + '/permutations/' + index + '/components';
|
|
197
|
+
checkBlockComponents(overrides, pointer, legacy, true, report);
|
|
198
|
+
prepareCustomComponents(overrides, pointer, report);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type API } from 'typescript-native/sync';
|
|
2
|
+
export declare function documentCategory(kind: 'behavior' | 'resource', file: string): string;
|
|
3
|
+
export declare function declaredCategories(api: API, file: string): ReadonlySet<string>;
|
|
4
|
+
export interface DocumentInput {
|
|
5
|
+
category: string;
|
|
6
|
+
literal: string;
|
|
7
|
+
}
|
|
8
|
+
export interface TypeFinding {
|
|
9
|
+
code: number;
|
|
10
|
+
message: string;
|
|
11
|
+
offset: number;
|
|
12
|
+
}
|
|
13
|
+
export declare function documentLiteral(value: unknown): string;
|
|
14
|
+
/** Type-check fresh JSON literals without executing their contents. */
|
|
15
|
+
export declare function createDocumentChecker(api: API, distribution: string, { additionalRoots, documentTypes }?: {
|
|
16
|
+
additionalRoots?: readonly string[];
|
|
17
|
+
documentTypes?: string;
|
|
18
|
+
}): (pending: readonly DocumentInput[], signal?: AbortSignal) => TypeFinding[][];
|