refactored huge method in SlipstreamCLI

This commit is contained in:
Leyla Becker 2025-08-16 20:37:09 -05:00
parent 9861d7242e
commit 9e8e830ab1

View file

@ -51,7 +51,6 @@ public class SlipstreamCLI {
private static Thread.UncaughtExceptionHandler exceptionHandler = null; private static Thread.UncaughtExceptionHandler exceptionHandler = null;
public static void main( String[] args ) { public static void main( String[] args ) {
exceptionHandler = new Thread.UncaughtExceptionHandler() { exceptionHandler = new Thread.UncaughtExceptionHandler() {
@ -83,77 +82,16 @@ public class SlipstreamCLI {
System.exit( 0 ); System.exit( 0 );
} }
DelayedDeleteHook deleteHook = new DelayedDeleteHook(); if ( slipstreamCmd.validate ) {
Runtime.getRuntime().addShutdownHook( deleteHook ); boolean success = validate(slipstreamCmd.modFileNames);
System.exit( success ? 0 : 1);
if ( slipstreamCmd.validate ) { // Exits (0/1).
log.info( "Validating..." );
StringBuilder resultBuf = new StringBuilder();
ReportFormatter formatter = new ReportFormatter();
boolean anyInvalid = false;
for ( String modFileName : slipstreamCmd.modFileNames ) {
File modFile = new File( modsDir, modFileName );
if ( modFile.isDirectory() ) {
log.info( String.format( "Zipping dir: %s/", modFile.getName() ) );
try {
modFile = createTempMod( modFile );
deleteHook.addDoomedFile( modFile );
}
catch ( IOException e ) {
log.error( String.format( "Error zipping dir: %s/", modFile.getName() ), e );
List<ReportMessage> tmpMessages = new ArrayList<ReportMessage>();
tmpMessages.add( new ReportMessage( ReportMessage.SECTION, modFileName ) );
tmpMessages.add( new ReportMessage( ReportMessage.EXCEPTION, e.getMessage() ) );
formatter.format( tmpMessages, resultBuf, 0 );
resultBuf.append( "\n" );
anyInvalid = true;
continue;
}
}
Report validateReport = ModUtilities.validateModFile( modFile );
formatter.format( validateReport.messages, resultBuf, 0 );
resultBuf.append( "\n" );
if ( validateReport.outcome == false ) anyInvalid = true;
}
if ( resultBuf.length() == 0 ) {
resultBuf.append( "No mods were checked." );
}
System.out.println();
System.out.println( resultBuf.toString() );
System.exit( anyInvalid ? 1 : 0 );
} }
File configFile = new File( "modman.cfg" ); File configFile = new File( "modman.cfg" );
SlipstreamConfig appConfig = getConfig( configFile ); SlipstreamConfig appConfig = getConfig( configFile );
if ( slipstreamCmd.listMods ) { // Exits. if ( slipstreamCmd.listMods ) {
log.info( "Listing mods..." ); listMods(appConfig);
boolean allowZip = appConfig.getProperty( SlipstreamConfig.ALLOW_ZIP, "false" ).equals( "true" );
File[] modFiles = modsDir.listFiles( new ModAndDirFileFilter( allowZip, true ) );
List<String> dirList = new ArrayList<String>();
List<String> fileList = new ArrayList<String>();
for ( File f : modFiles ) {
if ( f.isDirectory() )
dirList.add( f.getName() +"/" );
else
fileList.add( f.getName() );
}
Collections.sort( dirList );
Collections.sort( fileList );
for ( String s : dirList ) System.out.println( s );
for ( String s : fileList ) System.out.println( s );
System.exit( 0 ); System.exit( 0 );
} }
@ -164,7 +102,27 @@ public class SlipstreamCLI {
datsDir = getDatsDir( appConfig ); datsDir = getDatsDir( appConfig );
} }
if ( slipstreamCmd.extractDatsDir != null ) { // Exits (0/1). if ( slipstreamCmd.extractDatsDir != null ) {
extractDatsDir(slipstreamCmd, datsDir);
System.exit( 0 );
}
if ( slipstreamCmd.patch ) {
boolean success = patch(slipstreamCmd, datsDir);
if (!success) {
System.exit( 1 );
}
}
if ( slipstreamCmd.runftl ) {
boolean success = runFtl(appConfig, datsDir);
System.exit(success ? 0 : 1 );
}
System.exit( 0 );
}
private static void extractDatsDir(SlipstreamCommand slipstreamCmd, File datsDir) {
log.info( "Extracting dats..." ); log.info( "Extracting dats..." );
File extractDir = slipstreamCmd.extractDatsDir; File extractDir = slipstreamCmd.extractDatsDir;
@ -225,13 +183,14 @@ public class SlipstreamCLI {
catch ( IOException ex ) {} catch ( IOException ex ) {}
} }
} }
System.exit( 0 );
} }
if ( slipstreamCmd.patch ) { // Exits sometimes (1 on failure). private static boolean patch(SlipstreamCommand slipstreamCmd, File datsDir) {
log.info( "Patching..." ); log.info( "Patching..." );
DelayedDeleteHook deleteHook = new DelayedDeleteHook();
Runtime.getRuntime().addShutdownHook( deleteHook );
List<File> modFiles = new ArrayList<File>(); List<File> modFiles = new ArrayList<File>();
if ( slipstreamCmd.modFileNames != null ) { if ( slipstreamCmd.modFileNames != null ) {
for ( String modFileName : slipstreamCmd.modFileNames ) { for ( String modFileName : slipstreamCmd.modFileNames ) {
@ -245,7 +204,7 @@ public class SlipstreamCLI {
} }
catch ( IOException e ) { catch ( IOException e ) {
log.error( String.format( "Error zipping dir: %s/", modFile.getName() ), e ); log.error( String.format( "Error zipping dir: %s/", modFile.getName() ), e );
System.exit( 1 ); return false;
} }
} }
@ -266,10 +225,80 @@ public class SlipstreamCLI {
catch ( InterruptedException e ) {} catch ( InterruptedException e ) {}
} }
if ( !patchObserver.hasSucceeded() ) System.exit( 1 ); if ( !patchObserver.hasSucceeded() ) return false;
return true;
} }
if ( slipstreamCmd.runftl ) { // Exits (0/1). private static boolean validate(String[] modFileNames) {
DelayedDeleteHook deleteHook = new DelayedDeleteHook();
Runtime.getRuntime().addShutdownHook( deleteHook );
log.info( "Validating..." );
StringBuilder resultBuf = new StringBuilder();
ReportFormatter formatter = new ReportFormatter();
boolean anyInvalid = false;
for ( String modFileName : modFileNames ) {
File modFile = new File( modsDir, modFileName );
if ( modFile.isDirectory() ) {
log.info( String.format( "Zipping dir: %s/", modFile.getName() ) );
try {
modFile = createTempMod( modFile );
deleteHook.addDoomedFile( modFile );
}
catch ( IOException e ) {
log.error( String.format( "Error zipping dir: %s/", modFile.getName() ), e );
List<ReportMessage> tmpMessages = new ArrayList<ReportMessage>();
tmpMessages.add( new ReportMessage( ReportMessage.SECTION, modFileName ) );
tmpMessages.add( new ReportMessage( ReportMessage.EXCEPTION, e.getMessage() ) );
formatter.format( tmpMessages, resultBuf, 0 );
resultBuf.append( "\n" );
anyInvalid = true;
continue;
}
}
Report validateReport = ModUtilities.validateModFile( modFile );
formatter.format( validateReport.messages, resultBuf, 0 );
resultBuf.append( "\n" );
if ( validateReport.outcome == false ) anyInvalid = true;
}
if ( resultBuf.length() == 0 ) {
resultBuf.append( "No mods were checked." );
}
System.out.println();
System.out.println(resultBuf);
return !anyInvalid;
}
private static void listMods(SlipstreamConfig appConfig) {
log.info( "Listing mods..." );
boolean allowZip = appConfig.getProperty( SlipstreamConfig.ALLOW_ZIP, "false" ).equals( "true" );
File[] modFiles = modsDir.listFiles( new ModAndDirFileFilter( allowZip, true ) );
List<String> dirList = new ArrayList<String>();
List<String> fileList = new ArrayList<String>();
for ( File f : modFiles ) {
if ( f.isDirectory() )
dirList.add( f.getName() +"/" );
else
fileList.add( f.getName() );
}
Collections.sort( dirList );
Collections.sort( fileList );
for ( String s : dirList ) System.out.println( s );
for ( String s : fileList ) System.out.println( s );
}
private static boolean runFtl(SlipstreamConfig appConfig, File datsDir) {
log.info( "Running FTL..." ); log.info( "Running FTL..." );
File exeFile = null; File exeFile = null;
@ -313,21 +342,17 @@ public class SlipstreamCLI {
} }
catch ( Exception e ) { catch ( Exception e ) {
log.error( "Error launching FTL", e ); log.error( "Error launching FTL", e );
System.exit( 1 ); return false;
} }
} }
else { else {
log.error( "No executables were found to launch FTL" ); log.error( "No executables were found to launch FTL" );
System.exit( 1 ); return false;
} }
System.exit( 0 ); return true;
} }
System.exit( 0 );
}
/** /**
* Loads settings from a config file. * Loads settings from a config file.
* *