1+ // Copyright (c) 2025 Andrés E. G.
2+ //
3+ // This software is licensed under the MIT License.
4+ // See the LICENSE file for more details.
5+
6+
17package utils ;
28
39import sys .FileSystem ;
@@ -22,6 +28,14 @@ class AssetsManager {
2228 static var cacheFile : String ;
2329 private static final jsonCacheFileName : String = " HxNX_AssetsCache.json" ;
2430
31+ /*
32+ * List of files found in the assets
33+ */
34+ static var foundFiles : Array <String > = [];
35+
36+ /**
37+ * Search for assets and copy them to the output folder
38+ */
2539 public static function searchAndGetAssets (): Void {
2640 var outputDir = jsonFile .haxeConfig .cppOutDir ;
2741 var basePath = SlushiUtils .getPathFromCurrentTerminal ();
@@ -70,7 +84,7 @@ class AssetsManager {
7084
7185 var versionFolder =
7286 if (libVersion != null ) libVersion .replace (" ." , " ," )
73- else if (FileSystem .exists (Path .join ([libFolder , " .current" ])))
87+ else if (FileSystem .exists (Path .join ([libFolder , " .current" ])) )
7488 File .getContent (Path .join ([libFolder , " .current" ])).trim ().replace (" ." , " ," )
7589 else " " ;
7690
@@ -92,6 +106,9 @@ class AssetsManager {
92106 }
93107 }
94108
109+ // Clean removed assets
110+ cleanRemovedAssets (outputPath );
111+
95112 // Save cache
96113 saveCache ();
97114 }
@@ -107,10 +124,12 @@ class AssetsManager {
107124 for (sub in FileSystem .readDirectory (srcFile )) {
108125 var from = Path .join ([srcFile , sub ]);
109126 var to = Path .join ([dstFile , sub ]);
110- copyIfChanged (from , to , libName + " /ROMFS/" + f + " /" + sub );
127+ var key = libName + " /ROMFS/" + f + " /" + sub ;
128+ copyIfChanged (from , to , key );
111129 }
112130 } else {
113- copyIfChanged (srcFile , dstFile , libName + " /ROMFS/" + f );
131+ var key = libName + " /ROMFS/" + f ;
132+ copyIfChanged (srcFile , dstFile , key );
114133 }
115134 }
116135 }
@@ -119,6 +138,8 @@ class AssetsManager {
119138 var newHash = Md5 .make (File .getBytes (from )).toHex ();
120139 var oldHash = cache .exists (key ) ? cache .get (key ) : null ;
121140
141+ foundFiles .push (key );
142+
122143 if (oldHash != null && oldHash == newHash && FileSystem .exists (to )) {
123144 SlushiUtils .printMsg (" Skipped (unchanged): " + to , INFO );
124145 return ;
@@ -129,6 +150,30 @@ class AssetsManager {
129150 SlushiUtils .printMsg (" Copied: " + from + " -> " + to , SUCCESS );
130151 }
131152
153+ // 🔥 Elimina archivos que estaban en cache pero ya no existen en origen
154+ static function cleanRemovedAssets (outputPath : String ): Void {
155+ var toRemove : Array <String > = [];
156+
157+ for (k in cache .keys ()) {
158+ if (! foundFiles .contains (k )) {
159+ // Archivo estaba en cache pero no se detectó en esta ejecución
160+ var relative = k .split (" /ROMFS/" ).length > 1 ? k .split (" /ROMFS/" )[1 ] : k ;
161+ var dstFile = Path .join ([outputPath , " romfs" , relative ]);
162+ if (FileSystem .exists (dstFile )) {
163+ try {
164+ FileSystem .deleteFile (dstFile );
165+ SlushiUtils .printMsg (" Removed missing asset: " + dstFile , WARN );
166+ } catch (e : Dynamic ) {
167+ SlushiUtils .printMsg (" Failed to remove old asset: " + dstFile + " (" + e + " )" , ERROR );
168+ }
169+ }
170+ toRemove .push (k );
171+ }
172+ }
173+
174+ for (r in toRemove ) cache .remove (r );
175+ }
176+
132177 static function saveCache (): Void {
133178 var obj : Dynamic = {};
134179 for (k in cache .keys ()) {
0 commit comments