@@ -2,7 +2,7 @@ import { MAX_PROGRESS, PROGRESS, PROGRESS_DESCRIPTION } from '../interface/expor
22import { isResourcePackPath , toSafeFuntionName } from '../util/minecraftUtil'
33import { TRANSPARENT_TEXTURE } from '../variants'
44import { IRenderedNodes , IRenderedRig } from './rigRenderer'
5- import { replacePathPart , sortObjectKeys , zip } from './util'
5+ import { sortObjectKeys , zip } from './util'
66
77interface IPredicateItemModel {
88 parent : string
@@ -117,6 +117,53 @@ class PredicateItemModel {
117117 }
118118}
119119
120+ class ResourcePackAJMeta {
121+ public files = new Set < string > ( )
122+ public oldFiles = new Set < string > ( )
123+ private oldContent : Record < string , { files ?: string [ ] } > = { }
124+
125+ constructor (
126+ public path : string ,
127+ public exportNamespace : string ,
128+ public lastUsedExportNamespace : string ,
129+ public resourcePackFolder : string
130+ ) { }
131+
132+ read ( ) {
133+ if ( ! fs . existsSync ( this . path ) ) return
134+ this . oldContent = JSON . parse ( fs . readFileSync ( this . path , 'utf-8' ) )
135+ const data = this . oldContent [ this . exportNamespace ]
136+ const lastData = this . oldContent [ this . lastUsedExportNamespace ]
137+ if ( lastData ) {
138+ if ( ! Array . isArray ( lastData . files ) ) lastData . files = [ ]
139+ for ( const file of lastData . files ) {
140+ this . oldFiles . add ( PathModule . join ( this . resourcePackFolder , file ) )
141+ }
142+ delete this . oldContent [ this . lastUsedExportNamespace ]
143+ }
144+ if ( data ) {
145+ if ( ! Array . isArray ( data . files ) ) data . files = [ ]
146+ for ( const file of data . files ) {
147+ this . oldFiles . add ( PathModule . join ( this . resourcePackFolder , file ) )
148+ }
149+ delete this . oldContent [ this . exportNamespace ]
150+ }
151+ }
152+
153+ write ( ) {
154+ const folder = PathModule . dirname ( this . path )
155+ const content : ResourcePackAJMeta [ 'oldContent' ] = {
156+ ...this . oldContent ,
157+ [ this . exportNamespace ] : {
158+ files : Array . from ( this . files ) . map ( v =>
159+ PathModule . relative ( folder , v ) . replace ( / \\ / g, '/' )
160+ ) ,
161+ } ,
162+ }
163+ fs . writeFileSync ( this . path , autoStringify ( sortObjectKeys ( content ) ) )
164+ }
165+ }
166+
120167export async function compileResourcePack ( options : {
121168 rig : IRenderedRig
122169 displayItemPath : string
@@ -131,6 +178,36 @@ export async function compileResourcePack(options: {
131178 PROGRESS_DESCRIPTION . set ( 'Compiling Resource Pack...' )
132179 console . log ( 'Compiling resource pack...' , options )
133180
181+ const ajmeta = new ResourcePackAJMeta (
182+ PathModule . join ( options . resourcePackFolder , 'assets.ajmeta' ) ,
183+ aj . export_namespace ,
184+ lastUsedExportNamespace ,
185+ options . resourcePackFolder
186+ )
187+ if ( aj . resource_pack_export_mode === 'raw' ) {
188+ ajmeta . read ( )
189+
190+ PROGRESS_DESCRIPTION . set ( 'Removing Old Resource Pack Files...' )
191+ PROGRESS . set ( 0 )
192+ MAX_PROGRESS . set ( ajmeta . oldFiles . size )
193+
194+ const removedFolders = new Set < string > ( )
195+ for ( const file of ajmeta . oldFiles ) {
196+ if ( fs . existsSync ( file ) ) await fs . promises . unlink ( file )
197+ let folder = PathModule . dirname ( file )
198+ while (
199+ ! removedFolders . has ( folder ) &&
200+ fs . existsSync ( folder ) &&
201+ ( await fs . promises . readdir ( folder ) ) . length === 0
202+ ) {
203+ await fs . promises . rm ( folder , { recursive : true } )
204+ removedFolders . add ( folder )
205+ folder = PathModule . dirname ( folder )
206+ }
207+ PROGRESS . set ( PROGRESS . get ( ) + 1 )
208+ }
209+ }
210+
134211 const exportedFiles = new Map < string , string | Buffer > ( )
135212
136213 // Internal Models
@@ -228,27 +305,8 @@ export async function compileResourcePack(options: {
228305 // Do nothing
229306 console . log ( 'Plugin mode enabled. Skipping resource pack export.' )
230307 } else if ( aj . resource_pack_export_mode === 'raw' ) {
231- PROGRESS_DESCRIPTION . set ( 'Removing Old Resource Pack Files...' )
232-
233- await fs . promises . rm (
234- replacePathPart ( modelExportFolder , aj . export_namespace , lastUsedExportNamespace ) ,
235- {
236- recursive : true ,
237- force : true ,
238- }
239- )
240- await fs . promises . rm (
241- replacePathPart ( textureExportFolder , aj . export_namespace , lastUsedExportNamespace ) ,
242- {
243- recursive : true ,
244- force : true ,
245- }
246- )
247- for ( const variant of Object . keys ( rig . variantModels ) ) {
248- await fs . promises . mkdir ( PathModule . join ( modelExportFolder , variant ) , {
249- recursive : true ,
250- } )
251- }
308+ ajmeta . files = new Set ( exportedFiles . keys ( ) )
309+ ajmeta . write ( )
252310
253311 PROGRESS_DESCRIPTION . set ( 'Writing Resource Pack...' )
254312 PROGRESS . set ( 0 )
0 commit comments