2023-10-21 05:04:50 +00:00
package funkin . util ;
2023-11-02 03:09:52 +00:00
import haxe . io . Path ;
2023-10-21 05:04:50 +00:00
/ * *
* Utilties for i n t e r p r e t i n g c o m m a n d l i n e a r g u m e n t s .
* /
@ : nullSafety
class CLIUtil
{
/ * *
* If we don ' t d o t h i s , d r a g g i n g a n d d r o p p i n g a f i l e o n t o t h e e x e c u t a b l e
* causes it to be unable to find the assets folder .
* /
public static function resetWorkingDir ( ) : Void
{
#if sys
2023-11-02 03:09:52 +00:00
var cwd: String = Path . addTrailingSlash ( Sys . getCwd ( ) ) ;
2025-04-14 15:20:15 +00:00
var gameDir: String = ' ' ;
#if android
2025-07-09 09:02:55 +00:00
gameDir = Path . addTrailingSlash ( extension . androidtools . content . Context . getExternalFilesDir ( ) ) ;
2025-04-14 15:20:15 +00:00
#elseif ios
// Why? Because for some reason lime.system.System.documentsDirectory is returning a directory that's different and we're unable to read or write from, so it's disabled and no solution is found...
trace ( ' [ W A R N ] : R e s e t i n g t h e C u r r e n t W o r k i n g D i r e c t o r y i s u n a v a i l a b l e o n i O S t a r g e t s ' ) ;
gameDir = cwd ;
#elseif mac
gameDir = Path . addTrailingSlash ( Path . join ( [ Path . directory ( Sys . programPath ( ) ) , ' . . / R e s o u r c e s / ' ] ) ) ;
#else
gameDir = Path . addTrailingSlash ( Path . directory ( Sys . programPath ( ) ) ) ;
#end
if ( cwd == gameDir )
2023-11-02 03:09:52 +00:00
{
trace ( ' W o r k i n g d i r e c t o r y i s a l r e a d y c o r r e c t . ' ) ;
}
e lse
{
2025-04-14 15:20:15 +00:00
trace ( ' C h a n g i n g w o r k i n g d i r e c t o r y f r o m ${ Sys . getCwd ( ) } t o ${ gameDir } ' ) ;
Sys . setCwd ( gameDir ) ;
2023-11-02 03:09:52 +00:00
}
2023-10-21 05:04:50 +00:00
#end
}
public static function processArgs ( ) : CLIParams
{
#if sys
return interpretArgs ( cleanArgs ( Sys . args ( ) ) ) ;
#else
return buildDefaultParams ( ) ;
#end
}
static function interpretArgs ( args : Array < String > ) : CLIParams
{
var result = buildDefaultParams ( ) ;
result . args = [ for ( arg in args ) arg ] ; // Copy the array.
while ( args . length > 0 )
{
var arg: Null < String > = args . shift ( ) ;
if ( arg == null ) continue ;
if ( arg . startsWith ( ' - ' ) )
{
switch ( arg )
{
// Flags
c ase ' - h ' | ' - - h e l p ' :
printUsage ( ) ;
c ase ' - v ' | ' - - v e r s i o n ' :
trace ( Constants . GENERATED_BY ) ;
c ase ' - - c h a r t ' :
if ( args . length == 0 )
{
trace ( ' N o c h a r t p a t h p r o v i d e d . ' ) ;
printUsage ( ) ;
}
e lse
{
result . chart . shouldLoadChart = true ;
result . chart . chartPath = args . shift ( ) ;
}
2025-04-12 16:14:59 +00:00
c ase " - - s t a g e " :
if ( args . length == 0 )
{
trace ( ' N o s t a g e p a t h p r o v i d e d . ' ) ;
printUsage ( ) ;
}
e lse
{
result . stage . shouldLoadStage = true ;
result . stage . stagePath = args . shift ( ) ;
}
2023-10-21 05:04:50 +00:00
}
}
e lse
{
// Make an attempt to interpret the argument.
if ( arg . endsWith ( Constants . EXT_CHART ) )
{
result . chart . shouldLoadChart = true ;
result . chart . chartPath = arg ;
}
2025-04-12 16:14:59 +00:00
e lse if ( arg . endsWith ( Constants . EXT_STAGE ) )
{
result . stage . shouldLoadStage = true ;
result . stage . stagePath = arg ;
}
2023-10-21 05:04:50 +00:00
e lse
{
trace ( ' U n r e c o g n i z e d a r g u m e n t : ${ arg } ' ) ;
printUsage ( ) ;
}
}
}
return result ;
}
static function printUsage ( ) : Void
{
2025-04-12 16:14:59 +00:00
trace ( ' U s a g e : F u n k i n . e x e [ - - c h a r t < c h a r t > ] [ - - s t a g e < s t a g e > ] [ - - h e l p ] [ - - v e r s i o n ] ' ) ;
2023-10-21 05:04:50 +00:00
}
static function buildDefaultParams ( ) : CLIParams
{
return {
args : [ ] ,
chart :
{
shouldLoadChart : false ,
chartPath : null
2025-04-12 16:14:59 +00:00
} ,
stage :
{
shouldLoadStage : false ,
stagePath : null
2023-10-21 05:04:50 +00:00
}
} ;
}
/ * *
* Clean up the arguments passed to the application before parsing them .
* @ param args The arguments to clean up .
* @ return The cleaned up arguments .
* /
static function cleanArgs ( args : Array < String > ) : Array < String >
{
var result: Array < String > = [ ] ;
if ( args == null || args . length == 0 ) return result ;
return args . map ( function ( arg : String ) : String {
if ( arg == null ) return ' ' ;
return arg . trim ( ) ;
} ) . filter ( function ( arg : String ) : Bool {
return arg != null && arg != ' ' ;
} ) ;
}
}
typedef CLIParams =
{
var args: Array < String > ;
var chart: CLIChartParams ;
2025-04-12 16:14:59 +00:00
var stage: CLIStageParams ;
2023-10-21 05:04:50 +00:00
}
typedef CLIChartParams =
{
var shouldLoadChart: Bool ;
var chartPath: Null < String > ;
} ;
2025-04-12 16:14:59 +00:00
typedef CLIStageParams =
{
var shouldLoadStage: Bool ;
var stagePath: Null < String > ;
} ;