22import type { Img } from '../image/png.ts' ;
33import { encodePngBytes } from '../image/pngraw.ts' ;
44import type { Engine , Project } from '../store/manifest.ts' ;
5- import { zip , type ZipEntry } from '../store/zip.ts' ;
5+ import { fitsEditableZip , MAX_ZIP_BYTES , zip , type ZipEntry } from '../store/zip.ts' ;
66import {
77 bodyCutVariantCount ,
88 bodyCutVariantLimit ,
@@ -17,7 +17,7 @@ import { phaserFiles } from './phaser.ts';
1717import { unityFiles } from './unity.ts' ;
1818import { gdevelopFiles } from './gdevelop.ts' ;
1919import { rpgMakerFiles } from './rpgmaker.ts' ;
20- import { projectEntries , type ImportedCharacter } from '../store/import.ts' ;
20+ import { checkArchivePixels , projectEntries , type ImportedCharacter } from '../store/import.ts' ;
2121import { reservedPathSegment } from './paths.ts' ;
2222
2323export interface ExportOptions {
@@ -31,6 +31,7 @@ export interface ExportResult {
3131 entries : ZipEntry [ ] ;
3232 warnings : string [ ] ;
3333 errors : string [ ] ;
34+ problem ?: string ;
3435}
3536
3637export async function exportFiles ( src : Project , o : ExportOptions ) : Promise < ExportResult > {
@@ -70,7 +71,8 @@ export async function exportFiles(src: Project, o: ExportOptions): Promise<Expor
7071 return {
7172 entries : [ ] ,
7273 warnings : [ ] ,
73- errors : [ 'The base body is stale; freeze a matching base before exporting.' ]
74+ errors : [ 'The base body is stale; freeze a matching base before exporting.' ] ,
75+ problem : 'The body frames no longer match this character.'
7476 } ;
7577 const p = await withTuckVariants ( src ) ; // B14: hair under a helmet ships as a baked "<hair>~<helmet>" item; engines swap to it
7678 const errors = validate ( p ) ;
@@ -89,7 +91,8 @@ export async function exportFiles(src: Project, o: ExportOptions): Promise<Expor
8991 return {
9092 entries : [ ] ,
9193 warnings : [ ] ,
92- errors : [ 'The base body has no visible animation frames to export.' ]
94+ errors : [ 'The base body has no visible animation frames to export.' ] ,
95+ problem : 'The body has no visible frames.'
9396 } ;
9497 }
9598 const { manifest : m , warnings } = exportManifest ( p , o ) ;
@@ -120,7 +123,12 @@ export async function exportFiles(src: Project, o: ExportOptions): Promise<Expor
120123 files . push ( ...godotFiles ( m ) ) ;
121124 } catch ( error ) {
122125 if ( ! ( error instanceof Error ) || ! error . message . startsWith ( 'Godot: ' ) ) throw error ;
123- return { entries : [ ] , warnings, errors : [ error . message ] } ;
126+ return {
127+ entries : [ ] ,
128+ warnings,
129+ errors : [ error . message ] ,
130+ problem : 'Godot could not use these sprite sheets.'
131+ } ;
124132 }
125133 }
126134 if ( o . engines . includes ( 'phaser' ) ) files . push ( ...phaserFiles ( m ) ) ;
@@ -130,7 +138,12 @@ export async function exportFiles(src: Project, o: ExportOptions): Promise<Expor
130138 files . push ( ...gdevelopFiles ( m , sheets ) ) ;
131139 } catch ( error ) {
132140 if ( ! ( error instanceof Error ) || ! error . message . startsWith ( 'GDevelop: ' ) ) throw error ;
133- return { entries : [ ] , warnings, errors : [ error . message ] } ;
141+ return {
142+ entries : [ ] ,
143+ warnings,
144+ errors : [ error . message ] ,
145+ problem : 'GDevelop could not use these sprite sheets.'
146+ } ;
134147 }
135148 }
136149 if ( rpg ) {
@@ -140,15 +153,25 @@ export async function exportFiles(src: Project, o: ExportOptions): Promise<Expor
140153 warnings . push ( ...r . warnings ) ;
141154 } catch ( error ) {
142155 if ( ! ( error instanceof Error ) || ! error . message . startsWith ( 'RPG Maker: ' ) ) throw error ;
143- return { entries : [ ] , warnings, errors : [ error . message ] } ;
156+ return {
157+ entries : [ ] ,
158+ warnings,
159+ errors : [ error . message ] ,
160+ problem : 'RPG Maker needs front, side and back views.'
161+ } ;
144162 }
145163 }
146164 if ( o . engines . includes ( 'aseprite' ) ) {
147165 try {
148166 files . push ( ...( await asepriteFiles ( m , sheets ) ) ) ;
149167 } catch ( error ) {
150168 if ( ! ( error instanceof Error ) || ! error . message . startsWith ( 'Aseprite ' ) ) throw error ;
151- return { entries : [ ] , warnings, errors : [ error . message ] } ;
169+ return {
170+ entries : [ ] ,
171+ warnings,
172+ errors : [ error . message ] ,
173+ problem : 'The animation does not fit the Aseprite format.'
174+ } ;
152175 }
153176 }
154177 const projectFiles = await projectEntries ( src , o . projectData ) ;
@@ -165,7 +188,8 @@ export async function exportFiles(src: Project, o: ExportOptions): Promise<Expor
165188 if ( names . has ( name ) ) errors . push ( `Duplicate export path: ${ name } ` ) ;
166189 names . add ( name ) ;
167190 }
168- if ( errors . length ) return { entries : [ ] , warnings, errors } ;
191+ if ( errors . length )
192+ return { entries : [ ] , warnings, errors, problem : 'Some file names cannot be packaged safely.' } ;
169193 const enc = new TextEncoder ( ) ;
170194 const entries = await Promise . all (
171195 files . map ( async ( f : OutFile ) => ( {
@@ -182,7 +206,36 @@ export async function exportFiles(src: Project, o: ExportOptions): Promise<Expor
182206 return { entries, warnings, errors } ;
183207}
184208
185- export async function exportZip ( p : Project , o : ExportOptions ) {
209+ export async function exportZip ( p : Project , o : ExportOptions , notes ?: string [ ] ) {
186210 const r = await exportFiles ( p , o ) ;
187- return { ...r , zip : r . errors . length ? null : await zip ( r . entries , { deflate : true } ) } ;
211+ if ( r . errors . length ) return { ...r , zip : null } ;
212+ const warnings = [ ...new Set ( [ ...( notes ?? [ ] ) , ...r . warnings ] ) ] ;
213+ if ( notes && warnings . length )
214+ r . entries . push ( {
215+ name : 'Export notes.txt' ,
216+ data : new TextEncoder ( ) . encode ( warnings . join ( '\n' ) )
217+ } ) ;
218+ const oversized = ( ) => ( {
219+ entries : [ ] ,
220+ warnings,
221+ zip : null ,
222+ problem : 'This export is too large.' ,
223+ errors : [ 'The archive exceeds PixelDoll’s size limits.' ]
224+ } ) ;
225+ if ( ! fitsEditableZip ( r . entries ) ) return oversized ( ) ;
226+ try {
227+ checkArchivePixels (
228+ r . entries . filter ( ( { name } ) => name . startsWith ( 'cells/' ) ) . map ( ( { data } ) => data )
229+ ) ;
230+ } catch {
231+ return oversized ( ) ;
232+ }
233+ let bytes : Uint8Array ;
234+ try {
235+ bytes = await zip ( r . entries , { deflate : true } ) ;
236+ } catch ( error ) {
237+ console . warn ( 'Could not compress export; saving without ZIP compression' , error ) ;
238+ bytes = await zip ( r . entries ) ;
239+ }
240+ return bytes . length <= MAX_ZIP_BYTES ? { ...r , warnings, zip : bytes } : oversized ( ) ;
188241}
0 commit comments