2020 * DEALINGS IN THE SOFTWARE.
2121 */
2222
23-
2423package mobile ;
2524
2625#if mobile
2726import lime .utils .Assets as LimeAssets ;
2827import openfl .utils .Assets as OpenFLAssets ;
29- import flixel .addons .util .FlxAsyncLoop ;
3028import flixel .FlxG ;
3129import flixel .text .FlxText ;
3230import flixel .FlxSprite ;
3331import flixel .util .FlxColor ;
32+ import flixel .util .FlxTimer ;
3433import openfl .utils .ByteArray ;
3534import haxe .io .Path ;
35+ import flixel .ui .FlxBar ;
36+ import flixel .ui .FlxBar .FlxBarFillDirection ;
37+ import lime .system .ThreadPool ;
3638#if sys
3739import sys .io .File ;
3840import sys .FileSystem ;
3941#end
40-
4142using StringTools ;
4243
43- #if (android && cpp)
44- @:headerInclude (' sys/utsname.h' )
45- #end
44+ /**
45+ * ...
46+ * @author: Karim Akra
47+ */
4648class CopyState extends MusicBeatState
4749{
50+ private static final textFilesExtensions : Array <String > = [' ini' , ' txt' , ' xml' , ' hxs' , ' hx' , ' lua' , ' json' , ' frag' , ' vert' ];
51+ public static final IGNORE_FOLDER_FILE_NAME : String = " CopyState-Ignore.txt" ;
52+ private static var directoriesToIgnore : Array <String > = [];
4853 public static var locatedFiles : Array <String > = [];
4954 public static var maxLoopTimes : Int = 0 ;
50- public static final IGNORE_FOLDER_FILE_NAME : String = " ignore.txt" ;
5155
5256 public var loadingImage : FlxSprite ;
53- public var bottomBG : FlxSprite ;
57+ public var loadingBar : FlxBar ;
5458 public var loadedText : FlxText ;
55- public var copyLoop : FlxAsyncLoop ;
59+ public var thread : ThreadPool ;
5660
57- var loopTimes : Int = 0 ;
58- var failedFiles : Array <String > = [];
5961 var failedFilesStack : Array <String > = [];
60- var canUpdate : Bool = true ;
62+ var failedFiles : Array < String > = [] ;
6163 var shouldCopy : Bool = false ;
62- #if android
63- var archPrefix : String = " " ;
64- #end
65-
66- private static final textFilesExtensions : Array <String > = [' ini' , ' txt' , ' xml' , ' hxs' , ' hx' , ' lua' , ' json' , ' frag' , ' vert' ];
64+ var canUpdate : Bool = true ;
65+ var loopTimes : Int = 0 ;
6766
6867 override function create ()
6968 {
@@ -77,41 +76,44 @@ class CopyState extends MusicBeatState
7776 }
7877
7978 CoolUtil .showPopUp (" Seems like you have some missing files that are necessary to run the game\n Press OK to begin the copy process" , " Notice!" );
80-
79+
8180 shouldCopy = true ;
8281
8382 add (new FlxSprite (0 , 0 ).makeGraphic (FlxG .width , FlxG .height , 0xffcaff4d ));
8483
85- loadingImage = new FlxSprite (0 , 0 , Paths .image (' funkay' ));
84+ loadingImage = new FlxSprite (0 , 0 , Paths .image (' funkay' , ' shared ' ));
8685 loadingImage .setGraphicSize (0 , FlxG .height );
8786 loadingImage .updateHitbox ();
8887 loadingImage .screenCenter ();
8988 add (loadingImage );
9089
91- bottomBG = new FlxSprite (0 , FlxG .height - 26 ). makeGraphic ( FlxG .width , 26 , 0xFF000000 );
92- bottomBG . alpha = 0.6 ;
93- add (bottomBG );
90+ loadingBar = new FlxBar (0 , FlxG .height - 26 , FlxBarFillDirection . LEFT_TO_RIGHT , FlxG .width , 26 );
91+ loadingBar . setRange ( 0 , maxLoopTimes ) ;
92+ add (loadingBar );
9493
95- loadedText = new FlxText (bottomBG .x , bottomBG .y + 4 , FlxG .width , ' ' , 16 );
94+ loadedText = new FlxText (loadingBar .x , loadingBar .y + 4 , FlxG .width , ' ' , 16 );
9695 loadedText .setFormat (Paths .font (" vcr.ttf" ), 16 , FlxColor .WHITE , CENTER );
9796 add (loadedText );
9897
99- var ticks : Int = 15 ;
100- if (maxLoopTimes <= 15 )
101- ticks = 1 ;
102-
103- copyLoop = new FlxAsyncLoop (maxLoopTimes , copyAsset , ticks );
104- add (copyLoop );
105- copyLoop .start ();
98+ thread = new ThreadPool (0 , CoolUtil .getCPUThreadsCount (), MULTI_THREADED );
99+ new FlxTimer ().start (0.5 , (tmr ) -> {
100+ thread .run (function (poop , shit ) {
101+ for (file in locatedFiles )
102+ {
103+ loopTimes ++ ;
104+ copyAsset (file );
105+ }
106+ }, null );
107+ });
106108
107109 super .create ();
108110 }
109111
110112 override function update (elapsed : Float )
111113 {
112- if (shouldCopy && copyLoop != null )
114+ if (shouldCopy )
113115 {
114- if (copyLoop . finished && canUpdate )
116+ if (loopTimes >= maxLoopTimes && canUpdate )
115117 {
116118 if (failedFiles .length > 0 )
117119 {
@@ -120,29 +122,32 @@ class CopyState extends MusicBeatState
120122 FileSystem .createDirectory (' logs' );
121123 File .saveContent (' logs/' + Date .now ().toString ().replace (' ' , ' -' ).replace (' :' , " '" ) + ' -CopyState' + ' .txt' , failedFilesStack .join (' \n ' ));
122124 }
123- canUpdate = false ;
124- FlxG .sound .play (Paths .sound (' confirmMenu' )).onComplete = () -> {
125+
126+ FlxG .sound .play (Paths .sound (' confirmMenu' )).onComplete = () ->
127+ {
125128 FlxG .switchState (InitState .new );
126129 };
130+
131+ canUpdate = false ;
127132 }
128133
129- if (maxLoopTimes == 0 )
134+ if (loopTimes >= maxLoopTimes )
130135 loadedText .text = " Completed!" ;
131136 else
132137 loadedText .text = ' $loopTimes / $maxLoopTimes ' ;
138+
139+ loadingBar .percent = Math .min ((loopTimes / maxLoopTimes ) * 100 , 100 );
133140 }
134141 super .update (elapsed );
135142 }
136143
137- public function copyAsset ()
144+ public function copyAsset (file : String )
138145 {
139- var file = locatedFiles [loopTimes ];
140- loopTimes ++ ;
141146 if (! FileSystem .exists (file ))
142147 {
143148 var directory = Path .directory (file );
144149 if (! FileSystem .exists (directory ))
145- SUtil . mkDirs (directory );
150+ FileSystem . createDirectory (directory );
146151 try
147152 {
148153 if (OpenFLAssets .exists (getFile (file )))
@@ -176,7 +181,7 @@ class CopyState extends MusicBeatState
176181 if (fileData == null )
177182 fileData = ' ' ;
178183 if (! FileSystem .exists (directory ))
179- SUtil . mkDirs (directory );
184+ FileSystem . createDirectory (directory );
180185 File .saveContent (Path .join ([directory , fileName ]), fileData );
181186 }
182187 catch (e : haxe. Exception )
@@ -188,7 +193,7 @@ class CopyState extends MusicBeatState
188193
189194 public function getFileBytes (file : String ): ByteArray
190195 {
191- switch (Path .extension (file ))
196+ switch (Path .extension (file ). toLowerCase () )
192197 {
193198 case ' otf' | ' ttf' :
194199 return ByteArray .fromFile (file );
@@ -199,11 +204,13 @@ class CopyState extends MusicBeatState
199204
200205 public static function getFile (file : String ): String
201206 {
202- if (OpenFLAssets .exists (file )) return file ;
207+ if (OpenFLAssets .exists (file ))
208+ return file ;
203209
204210 @:privateAccess
205- for (library in LimeAssets .libraries .keys ()){
206- if (OpenFLAssets .exists (' $library : $file ' ) && library != ' default' )
211+ for (library in LimeAssets .libraries .keys ())
212+ {
213+ if (OpenFLAssets .exists (' $library : $file ' ) && library != ' default' )
207214 return ' $library : $file ' ;
208215 }
209216
@@ -213,28 +220,38 @@ class CopyState extends MusicBeatState
213220 public static function checkExistingFiles (): Bool
214221 {
215222 locatedFiles = OpenFLAssets .list ();
216-
223+
217224 // removes unwanted assets
218225 var assets = locatedFiles .filter (folder -> folder .startsWith (' assets/' ));
219226 var mods = locatedFiles .filter (folder -> folder .startsWith (' mods/' ));
220227 locatedFiles = assets .concat (mods );
228+ locatedFiles = locatedFiles .filter (file -> ! FileSystem .exists (file ));
221229
222230 var filesToRemove : Array <String > = [];
223231
224232 for (file in locatedFiles )
225233 {
226- if (FileSystem .exists (file ) || OpenFLAssets .exists (getFile (Path .join ([Path .directory (getFile (file )), IGNORE_FOLDER_FILE_NAME ]))))
234+ if (filesToRemove .contains (file ))
235+ continue ;
236+
237+ if (file .endsWith (IGNORE_FOLDER_FILE_NAME ) && ! directoriesToIgnore .contains (Path .directory (file )))
238+ directoriesToIgnore .push (Path .directory (file ));
239+
240+ if (directoriesToIgnore .length > 0 )
227241 {
228- filesToRemove .push (file );
242+ for (directory in directoriesToIgnore )
243+ {
244+ if (file .startsWith (directory ))
245+ filesToRemove .push (file );
246+ }
229247 }
230248 }
231249
232- for (file in filesToRemove )
233- locatedFiles .remove (file );
250+ locatedFiles = locatedFiles .filter (file -> ! filesToRemove .contains (file ));
234251
235252 maxLoopTimes = locatedFiles .length ;
236253
237- return (maxLoopTimes < 0 );
254+ return (maxLoopTimes <= 0 );
238255 }
239256}
240- #end
257+ #end
0 commit comments