@openfairygui/functions 0.1.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/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/index.cjs +3110 -0
- package/dist/index.d.cts +422 -0
- package/dist/index.d.ts +422 -0
- package/dist/index.js +3099 -0
- package/package.json +54 -0
- package/src/atlas.ts +1651 -0
- package/src/codegen-templates.ts +67 -0
- package/src/codegen.ts +656 -0
- package/src/index.ts +26 -0
- package/src/inspect.ts +146 -0
- package/src/max-rects-compat.ts +431 -0
- package/src/max-rects-packer-compat.ts +412 -0
- package/src/prune.ts +86 -0
- package/src/publish.ts +1093 -0
- package/src/rename.ts +66 -0
- package/src/shared-types.ts +65 -0
- package/src/utils.ts +9 -0
- package/src/validate.ts +186 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import { Document, FileSystem, Package, ProjectSettings, PublishSettings, Transform } from "@openfairygui/core";
|
|
2
|
+
|
|
3
|
+
//#region src/inspect.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Summary report for a single resource category.
|
|
6
|
+
*/
|
|
7
|
+
interface InspectCategoryReport {
|
|
8
|
+
count: number;
|
|
9
|
+
details: Array<{
|
|
10
|
+
name: string;
|
|
11
|
+
id: string;
|
|
12
|
+
path?: string;
|
|
13
|
+
exported?: boolean;
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Full inspection report for a FairyGUI project.
|
|
18
|
+
*/
|
|
19
|
+
interface InspectReport {
|
|
20
|
+
projectId: string;
|
|
21
|
+
projectType: number;
|
|
22
|
+
version: string;
|
|
23
|
+
packages: Array<{
|
|
24
|
+
name: string;
|
|
25
|
+
id: string;
|
|
26
|
+
publishName: string;
|
|
27
|
+
resources: {
|
|
28
|
+
images: InspectCategoryReport;
|
|
29
|
+
sounds: InspectCategoryReport;
|
|
30
|
+
fonts: InspectCategoryReport;
|
|
31
|
+
movieClips: InspectCategoryReport;
|
|
32
|
+
components: InspectCategoryReport;
|
|
33
|
+
};
|
|
34
|
+
componentDetails: Array<{
|
|
35
|
+
name: string;
|
|
36
|
+
id: string;
|
|
37
|
+
childCount: number;
|
|
38
|
+
controllerCount: number;
|
|
39
|
+
transitionCount: number;
|
|
40
|
+
}>;
|
|
41
|
+
}>;
|
|
42
|
+
totals: {
|
|
43
|
+
packages: number;
|
|
44
|
+
images: number;
|
|
45
|
+
sounds: number;
|
|
46
|
+
fonts: number;
|
|
47
|
+
movieClips: number;
|
|
48
|
+
components: number;
|
|
49
|
+
displayObjects: number;
|
|
50
|
+
gears: number;
|
|
51
|
+
controllers: number;
|
|
52
|
+
transitions: number;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Generates a detailed report of the project contents.
|
|
57
|
+
*
|
|
58
|
+
* Unlike other transforms, `inspect()` does NOT modify the document —
|
|
59
|
+
* it returns a structured report.
|
|
60
|
+
*
|
|
61
|
+
* ```ts
|
|
62
|
+
* const report = inspect(doc);
|
|
63
|
+
* console.log(`${report.totals.packages} packages, ${report.totals.components} components`);
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
declare function inspect(doc: Document): InspectReport;
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/validate.d.ts
|
|
69
|
+
/**
|
|
70
|
+
* Severity of a validation issue.
|
|
71
|
+
*/
|
|
72
|
+
declare enum ValidationSeverity {
|
|
73
|
+
ERROR = "error",
|
|
74
|
+
WARNING = "warning",
|
|
75
|
+
INFO = "info"
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* A single validation issue found in the project.
|
|
79
|
+
*/
|
|
80
|
+
interface ValidationIssue {
|
|
81
|
+
severity: ValidationSeverity;
|
|
82
|
+
message: string;
|
|
83
|
+
/** Package name where the issue was found. */
|
|
84
|
+
packageName?: string;
|
|
85
|
+
/** Component name where the issue was found. */
|
|
86
|
+
componentName?: string;
|
|
87
|
+
/** Resource/object name related to the issue. */
|
|
88
|
+
resourceName?: string;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Result returned by `validate()`.
|
|
92
|
+
*/
|
|
93
|
+
interface ValidationResult {
|
|
94
|
+
ok: boolean;
|
|
95
|
+
errors: ValidationIssue[];
|
|
96
|
+
warnings: ValidationIssue[];
|
|
97
|
+
infos: ValidationIssue[];
|
|
98
|
+
}
|
|
99
|
+
interface ValidateOptions {
|
|
100
|
+
/** If true, the transform throws on errors. Default: false. */
|
|
101
|
+
throwOnError?: boolean;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Validates a FairyGUI project for common issues:
|
|
105
|
+
* - Missing resource IDs
|
|
106
|
+
* - Broken `ui://` references (src pointing to non-existent resources)
|
|
107
|
+
* - Empty components (no children)
|
|
108
|
+
* - Controllers with no pages
|
|
109
|
+
* - Duplicate resource IDs within a package
|
|
110
|
+
*
|
|
111
|
+
* The validation result is stored in `doc.getRoot().getExtras()._validation`.
|
|
112
|
+
*
|
|
113
|
+
* ```ts
|
|
114
|
+
* await doc.transform(validate({ throwOnError: true }));
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
declare function validate(_options?: ValidateOptions): Transform;
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region src/prune.d.ts
|
|
120
|
+
interface PruneOptions {
|
|
121
|
+
/** Remove components with no children. Default: false. */
|
|
122
|
+
emptyComponents?: boolean;
|
|
123
|
+
/** Remove unreferenced resources (images, sounds, fonts, movieclips not used in any component). Default: true. */
|
|
124
|
+
unusedResources?: boolean;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Removes unreferenced resources from the project.
|
|
128
|
+
*
|
|
129
|
+
* By default, removes image/sound/font/movieclip resources that are not
|
|
130
|
+
* referenced by any component's display objects via `src` or `ui://` URLs.
|
|
131
|
+
*
|
|
132
|
+
* ```ts
|
|
133
|
+
* await doc.transform(prune());
|
|
134
|
+
* await doc.transform(prune({ emptyComponents: true }));
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
declare function prune(_options?: PruneOptions): Transform;
|
|
138
|
+
//#endregion
|
|
139
|
+
//#region src/rename.d.ts
|
|
140
|
+
interface RenameOptions {
|
|
141
|
+
/** Package name to rename from. Required. */
|
|
142
|
+
packageName: string;
|
|
143
|
+
/** Resource name to rename from. Required. */
|
|
144
|
+
resourceName: string;
|
|
145
|
+
/** New name for the resource. Required. */
|
|
146
|
+
newName: string;
|
|
147
|
+
/** If true, also update all `ui://` references pointing to this resource. Default: true. */
|
|
148
|
+
updateReferences?: boolean;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Renames a resource and optionally updates all references to it.
|
|
152
|
+
*
|
|
153
|
+
* This searches all display objects' `src` attributes across all packages
|
|
154
|
+
* for `ui://` URLs that point to the renamed resource, and updates them
|
|
155
|
+
* to reflect the new name (the resource ID doesn't change, so references
|
|
156
|
+
* are already valid — but the name stored in package.xml is updated).
|
|
157
|
+
*
|
|
158
|
+
* ```ts
|
|
159
|
+
* await doc.transform(rename({
|
|
160
|
+
* packageName: 'Basics',
|
|
161
|
+
* resourceName: 'Button',
|
|
162
|
+
* newName: 'PrimaryButton',
|
|
163
|
+
* }));
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
declare function rename(options: RenameOptions): Transform;
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/atlas.d.ts
|
|
169
|
+
interface AtlasOptions {
|
|
170
|
+
/**
|
|
171
|
+
* Sharp module instance, injected by the caller.
|
|
172
|
+
* Required for actual image compositing and trimImage.
|
|
173
|
+
*
|
|
174
|
+
* ```ts
|
|
175
|
+
* import sharp from 'sharp';
|
|
176
|
+
* await doc.transform(atlas({ encoder: sharp }));
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
encoder?: unknown;
|
|
180
|
+
/** Maximum atlas texture size (width and height). Default: 2048. */
|
|
181
|
+
maxSize?: number;
|
|
182
|
+
/** Whether to use the fast editor-compatible packing heuristics. Default: true. */
|
|
183
|
+
fast?: boolean;
|
|
184
|
+
/** Allow rotating sprites 90° for better packing. Default: true. */
|
|
185
|
+
allowRotation?: boolean;
|
|
186
|
+
/** Pixel padding between sprites. Default: 1. */
|
|
187
|
+
padding?: number;
|
|
188
|
+
/** Constrain atlas dimensions to powers of two. Default: false. */
|
|
189
|
+
powerOfTwo?: boolean;
|
|
190
|
+
/** Force square atlas (width === height). Default: false. */
|
|
191
|
+
square?: boolean;
|
|
192
|
+
/** Allow spilling into multiple atlas pages. Default: true. */
|
|
193
|
+
multiPage?: boolean;
|
|
194
|
+
/**
|
|
195
|
+
* Trim transparent pixels from image edges before packing.
|
|
196
|
+
* Requires encoder (sharp). Stores offset/originalSize in Sprite nodes.
|
|
197
|
+
* Default: false.
|
|
198
|
+
*/
|
|
199
|
+
trimImage?: boolean;
|
|
200
|
+
/**
|
|
201
|
+
* Base path for reading source images. If not set, images must have
|
|
202
|
+
* their pixel data stored in extras._imageData as Uint8Array.
|
|
203
|
+
*/
|
|
204
|
+
basePath?: string;
|
|
205
|
+
/**
|
|
206
|
+
* Output directory for generated atlas PNGs.
|
|
207
|
+
* Required when encoder is provided.
|
|
208
|
+
*/
|
|
209
|
+
outputPath?: string;
|
|
210
|
+
/**
|
|
211
|
+
* Optional mkdir function to ensure output directory exists.
|
|
212
|
+
* If not provided, the outputPath directory must already exist.
|
|
213
|
+
*/
|
|
214
|
+
mkdir?: (path: string) => Promise<void>;
|
|
215
|
+
/**
|
|
216
|
+
* Optional raw file reader for reading .jta MovieClip files.
|
|
217
|
+
* Required for MovieClip frame atlas packing.
|
|
218
|
+
*/
|
|
219
|
+
readFileRaw?: (path: string) => Promise<Uint8Array>;
|
|
220
|
+
/**
|
|
221
|
+
* Keep original input order when MaxRects tie-break scores are equal.
|
|
222
|
+
* This is an internal publish detail used to mirror editor/CLI behavior.
|
|
223
|
+
*/
|
|
224
|
+
preserveInputOrderOnTie?: boolean;
|
|
225
|
+
/**
|
|
226
|
+
* Internal publish detail used by Unity binary output:
|
|
227
|
+
* allow single untrimmed PNG image packages to bypass the packer and
|
|
228
|
+
* write atlas0 directly, matching the reference CLI behavior.
|
|
229
|
+
*/
|
|
230
|
+
directSingleImageOutput?: boolean;
|
|
231
|
+
/**
|
|
232
|
+
* Internal publish detail used by the direct-image-output path.
|
|
233
|
+
* When extractAlpha is enabled, the direct output shortcut must be disabled.
|
|
234
|
+
*/
|
|
235
|
+
extractAlpha?: boolean;
|
|
236
|
+
/**
|
|
237
|
+
* When branchProcessing keeps branch resources, publish branch images into
|
|
238
|
+
* separate atlas pages/files per branch instead of mixing them with main.
|
|
239
|
+
*/
|
|
240
|
+
separatedAtlasForBranch?: boolean;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Packs image resources into texture atlases.
|
|
244
|
+
*
|
|
245
|
+
* This transform performs MaxRects bin-packing on all ImageResource items
|
|
246
|
+
* within each package, creating Atlas and Sprite property nodes. When an
|
|
247
|
+
* `encoder` (sharp) is provided, it also composites the actual PNG files.
|
|
248
|
+
*
|
|
249
|
+
* When `trimImage` is enabled and encoder is available, transparent pixels
|
|
250
|
+
* at image edges are trimmed before packing. The trimmed offset and original
|
|
251
|
+
* dimensions are stored in the Sprite nodes for runtime reconstruction.
|
|
252
|
+
*
|
|
253
|
+
* ```ts
|
|
254
|
+
* import sharp from 'sharp';
|
|
255
|
+
* await doc.transform(atlas({
|
|
256
|
+
* encoder: sharp,
|
|
257
|
+
* maxSize: 2048,
|
|
258
|
+
* trimImage: true,
|
|
259
|
+
* basePath: './assets/',
|
|
260
|
+
* outputPath: './dist/',
|
|
261
|
+
* }));
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
declare function atlas(_options?: AtlasOptions): Transform;
|
|
265
|
+
//#endregion
|
|
266
|
+
//#region src/shared-types.d.ts
|
|
267
|
+
type ExtrasMap = Record<string, unknown>;
|
|
268
|
+
interface CliCodeGenerationSettings extends NonNullable<PublishSettings['codeGeneration']> {
|
|
269
|
+
allowGenCode?: boolean;
|
|
270
|
+
classNamePrefix?: string;
|
|
271
|
+
codePath?: string;
|
|
272
|
+
codeType?: string;
|
|
273
|
+
getMemberByName?: boolean;
|
|
274
|
+
ignoreNoname?: boolean;
|
|
275
|
+
memberNamePrefix?: string;
|
|
276
|
+
packageName?: string;
|
|
277
|
+
}
|
|
278
|
+
interface CliAtlasSettings extends NonNullable<PublishSettings['atlasSetting']> {
|
|
279
|
+
maxSize?: number;
|
|
280
|
+
paging?: boolean;
|
|
281
|
+
sizeOption?: string;
|
|
282
|
+
forceSquare?: boolean;
|
|
283
|
+
fast?: boolean;
|
|
284
|
+
allowRotation?: boolean;
|
|
285
|
+
padding?: number;
|
|
286
|
+
trimImage?: boolean;
|
|
287
|
+
extractAlpha?: boolean;
|
|
288
|
+
}
|
|
289
|
+
interface CliPublishSettings extends PublishSettings {
|
|
290
|
+
atlasSetting?: CliAtlasSettings;
|
|
291
|
+
codeGeneration?: CliCodeGenerationSettings;
|
|
292
|
+
}
|
|
293
|
+
type RootProjectSettings = ProjectSettings & {
|
|
294
|
+
publish?: CliPublishSettings;
|
|
295
|
+
};
|
|
296
|
+
interface PublishDependency {
|
|
297
|
+
id: string;
|
|
298
|
+
name: string;
|
|
299
|
+
}
|
|
300
|
+
interface HasOptionalFont {
|
|
301
|
+
getFont?(): string | string[] | null | undefined;
|
|
302
|
+
}
|
|
303
|
+
interface HasOptionalSrc {
|
|
304
|
+
getSrc?(): string | undefined;
|
|
305
|
+
}
|
|
306
|
+
interface HasOptionalUrl {
|
|
307
|
+
getUrl?(): string | undefined;
|
|
308
|
+
}
|
|
309
|
+
type PublishFileSystem = Pick<FileSystem, 'join' | 'mkdir' | 'writeFileRaw'> & {
|
|
310
|
+
deleteFile?: (path: string) => Promise<void>;
|
|
311
|
+
exists?: FileSystem['exists'];
|
|
312
|
+
readdir?: FileSystem['readdir'];
|
|
313
|
+
readFileRaw?: FileSystem['readFileRaw'];
|
|
314
|
+
};
|
|
315
|
+
//#endregion
|
|
316
|
+
//#region src/codegen.d.ts
|
|
317
|
+
declare const AUTO_GENERATED_CODE_MARK = "/** This is an automatically generated class by FairyGUI. Please do not modify it. **/";
|
|
318
|
+
interface PublishCodeGenerationOptions {
|
|
319
|
+
basePath?: string;
|
|
320
|
+
fs: PublishFileSystem;
|
|
321
|
+
packages: Package[];
|
|
322
|
+
}
|
|
323
|
+
declare function publishCodeGeneration(doc: Document, options: PublishCodeGenerationOptions): Promise<void>;
|
|
324
|
+
//#endregion
|
|
325
|
+
//#region src/publish.d.ts
|
|
326
|
+
interface PublishOptions {
|
|
327
|
+
/**
|
|
328
|
+
* Output directory for published files (.fui + atlas PNGs).
|
|
329
|
+
* Required.
|
|
330
|
+
*/
|
|
331
|
+
output: string;
|
|
332
|
+
/**
|
|
333
|
+
* Compress the binary data with zlib raw deflate. Default: false.
|
|
334
|
+
*/
|
|
335
|
+
compressed?: boolean;
|
|
336
|
+
/**
|
|
337
|
+
* File extension for the binary output. Default: 'fui'.
|
|
338
|
+
* Unity projects typically use 'bytes'.
|
|
339
|
+
*/
|
|
340
|
+
fileExtension?: string;
|
|
341
|
+
/**
|
|
342
|
+
* Sharp module instance for atlas image compositing.
|
|
343
|
+
* If not provided, atlas packing only computes layout (no PNGs generated).
|
|
344
|
+
*/
|
|
345
|
+
encoder?: unknown;
|
|
346
|
+
/**
|
|
347
|
+
* Base path for reading source images (project assets root).
|
|
348
|
+
* Required when encoder is provided.
|
|
349
|
+
*/
|
|
350
|
+
basePath?: string;
|
|
351
|
+
/**
|
|
352
|
+
* Atlas packing options.
|
|
353
|
+
*/
|
|
354
|
+
atlas?: Omit<AtlasOptions, 'encoder' | 'basePath' | 'outputPath'>;
|
|
355
|
+
/**
|
|
356
|
+
* Filter which packages to publish by name. If not set, all packages are published.
|
|
357
|
+
*/
|
|
358
|
+
packages?: string[];
|
|
359
|
+
/**
|
|
360
|
+
* FileSystem abstraction for writing output files.
|
|
361
|
+
* Required for actual file output. Without it, only the Document model
|
|
362
|
+
* is updated (atlas layout computed, sprite nodes created).
|
|
363
|
+
*/
|
|
364
|
+
fs?: PublishFileSystem;
|
|
365
|
+
/**
|
|
366
|
+
* Active branch name used when branchProcessing is "主干合并活跃分支".
|
|
367
|
+
* Empty or omitted means publishing the main branch.
|
|
368
|
+
*/
|
|
369
|
+
branch?: string;
|
|
370
|
+
}
|
|
371
|
+
interface ResolvedPublishAtlasOptions extends Pick<AtlasOptions, 'maxSize' | 'fast' | 'allowRotation' | 'padding' | 'powerOfTwo' | 'square' | 'multiPage' | 'trimImage' | 'extractAlpha'> {}
|
|
372
|
+
interface ResolvePublishOptionsOverrides {
|
|
373
|
+
compressed?: boolean;
|
|
374
|
+
fileExtension?: string;
|
|
375
|
+
packages?: string[];
|
|
376
|
+
atlas?: Partial<ResolvedPublishAtlasOptions>;
|
|
377
|
+
}
|
|
378
|
+
interface ResolvedPublishOptions {
|
|
379
|
+
compressed: boolean;
|
|
380
|
+
fileExtension: string;
|
|
381
|
+
packages?: string[];
|
|
382
|
+
atlas: ResolvedPublishAtlasOptions;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Resolve publish defaults from the document's project settings.
|
|
386
|
+
*
|
|
387
|
+
* This keeps the editor-aligned publish rules reusable across environments,
|
|
388
|
+
* while callers still provide environment-specific concerns such as fs/encoder/basePath.
|
|
389
|
+
*/
|
|
390
|
+
declare function resolvePublishOptions(doc: Document, overrides?: ResolvePublishOptionsOverrides): ResolvedPublishOptions;
|
|
391
|
+
/**
|
|
392
|
+
* Publishes a FairyGUI project.
|
|
393
|
+
*
|
|
394
|
+
* Orchestrates:
|
|
395
|
+
* 1. Atlas packing (MaxRects layout + optional sharp compositing)
|
|
396
|
+
* 2. Per-package .fui binary serialization
|
|
397
|
+
* 3. File writing to the output directory
|
|
398
|
+
*
|
|
399
|
+
* ```ts
|
|
400
|
+
* import sharp from 'sharp';
|
|
401
|
+
* const io = new NodeIO();
|
|
402
|
+
* const doc = await io.readProject('./project.fairy');
|
|
403
|
+
*
|
|
404
|
+
* await doc.transform(publish({
|
|
405
|
+
* output: './release/',
|
|
406
|
+
* compressed: true,
|
|
407
|
+
* encoder: sharp,
|
|
408
|
+
* basePath: './assets/',
|
|
409
|
+
* fileExtension: 'bytes',
|
|
410
|
+
* fs: io.createFileSystem(),
|
|
411
|
+
* }));
|
|
412
|
+
* ```
|
|
413
|
+
*/
|
|
414
|
+
declare function publish(options: PublishOptions): Transform;
|
|
415
|
+
//#endregion
|
|
416
|
+
//#region src/utils.d.ts
|
|
417
|
+
/**
|
|
418
|
+
* Wraps a transform function, assigning it a name for the transform stack.
|
|
419
|
+
*/
|
|
420
|
+
declare function createTransform(name: string, fn: Transform): Transform;
|
|
421
|
+
//#endregion
|
|
422
|
+
export { AUTO_GENERATED_CODE_MARK, type AtlasOptions, type CliAtlasSettings, type CliPublishSettings, type ExtrasMap, type HasOptionalFont, type HasOptionalSrc, type HasOptionalUrl, type InspectCategoryReport, type InspectReport, type PruneOptions, type PublishCodeGenerationOptions, type PublishDependency, type PublishFileSystem, type PublishOptions, type RenameOptions, type ResolvePublishOptionsOverrides, type ResolvedPublishAtlasOptions, type ResolvedPublishOptions, type RootProjectSettings, type ValidateOptions, type ValidationIssue, type ValidationResult, ValidationSeverity, atlas, createTransform, inspect, prune, publish, publishCodeGeneration, rename, resolvePublishOptions, validate };
|