Moderate code cleanup

This commit is contained in:
Vhati 2018-01-05 13:07:19 -05:00
parent 7528ec271a
commit ad6d87a4bf
4 changed files with 81 additions and 79 deletions

View file

@ -107,15 +107,15 @@ public class FTLModManager {
boolean writeConfig = false;
Properties props = new Properties();
props.setProperty( SlipstreamConfig.ALLOW_ZIP, "false" );
props.setProperty( SlipstreamConfig.FTL_DATS_PATH, "" ); // Prompt.
props.setProperty( SlipstreamConfig.STEAM_DISTRO, "" ); // Prompt.
props.setProperty( SlipstreamConfig.STEAM_EXE_PATH, "" ); // Prompt.
props.setProperty( SlipstreamConfig.RUN_STEAM_FTL, "" ); // Prompt.
props.setProperty( SlipstreamConfig.FTL_DATS_PATH, "" ); // Prompt.
props.setProperty( SlipstreamConfig.STEAM_DISTRO, "" ); // Prompt.
props.setProperty( SlipstreamConfig.STEAM_EXE_PATH, "" ); // Prompt.
props.setProperty( SlipstreamConfig.RUN_STEAM_FTL, "" ); // Prompt.
props.setProperty( SlipstreamConfig.NEVER_RUN_FTL, "false" );
props.setProperty( SlipstreamConfig.UPDATE_CATALOG, "" ); // Prompt.
props.setProperty( SlipstreamConfig.UPDATE_APP, "" ); // Prompt.
props.setProperty( SlipstreamConfig.USE_DEFAULT_UI, "false" );
props.setProperty( SlipstreamConfig.REMEMBER_GEOMETRY, "true" );
// "update_catalog" doesn't have a default.
// "update_app" doesn't have a default.
// "manager_geometry" doesn't have a default.
// Read the config file.

View file

@ -95,7 +95,7 @@ public class SlipstreamConfig {
List<String> allFieldsList = new ArrayList<String>( userFieldsMap.size() + appFieldsMap.size() );
allFieldsList.addAll( userFieldsMap.keySet() );
allFieldsList.addAll( userFieldsMap.keySet() );
allFieldsList.addAll( appFieldsMap.keySet() );
int fieldWidth = 0;
for ( String fieldName : allFieldsList ) {
fieldWidth = Math.max( fieldName.length(), fieldWidth );

View file

@ -405,20 +405,20 @@ public class ManagerFrame extends JFrame implements ActionListener, ModsScanObse
/**
* Extra initialization that must be called after the constructor.
* This must be called on the Swing event thread (use invokeLater()).
*/
public void init() {
ManagerInitThread initThread = new ManagerInitThread( this,
new SlipstreamConfig( appConfig ),
modsDir,
modsTableStateFile,
metadataFile,
catalogFile,
catalogETagFile,
appUpdateFile,
appUpdateETagFile
);
ManagerInitThread initThread = new ManagerInitThread(
this,
new SlipstreamConfig( appConfig ),
modsDir,
modsTableStateFile,
metadataFile,
catalogFile,
catalogETagFile,
appUpdateFile,
appUpdateETagFile
);
initThread.setDaemon( true );
initThread.setPriority( Thread.MIN_PRIORITY );
initThread.setDefaultUncaughtExceptionHandler( this );
@ -551,7 +551,7 @@ public class ManagerFrame extends JFrame implements ActionListener, ModsScanObse
// Links.
infoArea.appendRegularText( String.format( "Version %s: ", appUpdateInfo.getLatestVersion().toString() ) );
boolean first = true;
for ( Map.Entry<String,String> entry : appUpdateInfo.getLatestURLs().entrySet() ) {
for ( Map.Entry<String, String> entry : appUpdateInfo.getLatestURLs().entrySet() ) {
if ( !first ) infoArea.appendRegularText( " " );
infoArea.appendRegularText( "[" );
infoArea.appendLinkText( entry.getValue(), entry.getKey() );
@ -569,7 +569,7 @@ public class ManagerFrame extends JFrame implements ActionListener, ModsScanObse
}
// Changelog.
for ( Map.Entry<ComparableVersion,List<String>> entry : appUpdateInfo.getChangelog().entrySet() ) {
for ( Map.Entry<ComparableVersion, List<String>> entry : appUpdateInfo.getChangelog().entrySet() ) {
if ( appVersion.compareTo( entry.getKey() ) >= 0 ) break;
if ( buf.length() > 0 ) buf.append( "\n" );
@ -1032,32 +1032,20 @@ public class ManagerFrame extends JFrame implements ActionListener, ModsScanObse
}
/**
* Sets the ModDB for the catalog. (thread-safe)
* Sets the ModDB for the catalog.
*/
public void setCatalogModDB( final ModDB newDB ) {
Runnable r = new Runnable() {
@Override
public void run() { catalogModDB = newDB; }
};
if ( SwingUtilities.isEventDispatchThread() ) r.run();
else SwingUtilities.invokeLater( r );
public void setCatalogModDB( ModDB newDB ) {
catalogModDB = newDB;
}
/**
* Sets info about available app updates. (thread-safe)
* Sets info about available app updates.
*/
public void setAppUpdateInfo( final AutoUpdateInfo aui ) {
Runnable r = new Runnable() {
@Override
public void run() {
appUpdateInfo = aui;
boolean isUpdateAvailable = ( appVersion.compareTo(appUpdateInfo.getLatestVersion()) < 0 );
updateBtn.setForeground( isUpdateAvailable ? updateBtnEnabledColor : updateBtnDisabledColor );
updateBtn.setEnabled( isUpdateAvailable );
}
};
if ( SwingUtilities.isEventDispatchThread() ) r.run();
else SwingUtilities.invokeLater( r );
public void setAppUpdateInfo( AutoUpdateInfo aui ) {
appUpdateInfo = aui;
boolean isUpdateAvailable = ( appVersion.compareTo(appUpdateInfo.getLatestVersion()) < 0 );
updateBtn.setForeground( isUpdateAvailable ? updateBtnEnabledColor : updateBtnDisabledColor );
updateBtn.setEnabled( isUpdateAvailable );
}

View file

@ -10,7 +10,6 @@ import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.Lock;
import javax.swing.SwingUtilities;
@ -53,6 +52,7 @@ public class ManagerInitThread extends Thread {
public ManagerInitThread( ManagerFrame frame, SlipstreamConfig appConfig, File modsDir, File modsTableStateFile, File metadataFile, File catalogFile, File catalogETagFile, File appUpdateFile, File appUpdateETagFile ) {
super( "init" );
this.frame = frame;
this.appConfig = appConfig;
this.modsDir = modsDir;
@ -77,6 +77,7 @@ public class ManagerInitThread extends Thread {
private void init() throws InterruptedException {
if ( metadataFile.exists() ) {
// Load cached metadata first, before scanning for new info.
ModDB cachedDB = JacksonCatalogReader.parse( metadataFile );
@ -105,27 +106,24 @@ public class ManagerInitThread extends Thread {
int catalogUpdateInterval = appConfig.getPropertyAsInt( "update_catalog", 0 );
boolean needNewCatalog = false;
if ( catalogFile.exists() ) {
// Load the catalog first, before updating.
reloadCatalog();
// Load the catalog first, before downloading.
if ( catalogFile.exists() ) reloadCatalog();
if ( catalogUpdateInterval > 0 ) {
if ( catalogUpdateInterval > 0 ) {
if ( catalogFile.exists() ) {
// Check if the downloaded catalog is stale.
if ( isFileStale( catalogFile, catalogUpdateInterval ) ) {
log.debug( String.format( "Catalog is older than %d days.", catalogUpdateInterval ) );
log.debug( String.format( "Catalog is older than %d days", catalogUpdateInterval ) );
needNewCatalog = true;
} else {
log.debug( "Catalog isn't stale yet." );
log.debug( "Catalog isn't stale yet" );
}
}
else {
// Catalog file doesn't exist.
needNewCatalog = true;
}
}
else {
// Catalog file doesn't exist.
needNewCatalog = true;
}
// Don't update if the user doesn't want to.
if ( catalogUpdateInterval <= 0 ) needNewCatalog = false;
if ( needNewCatalog ) {
boolean fetched = URLFetcher.refetchURL( ManagerFrame.CATALOG_URL, catalogFile, catalogETagFile );
@ -134,30 +132,27 @@ public class ManagerInitThread extends Thread {
}
}
int appUpdateInterval = appConfig.getPropertyAsInt( "update_app", 0 );
// Load the cached info first, before downloading.
if ( appUpdateFile.exists() ) reloadAppUpdateInfo();
int appUpdateInterval = appConfig.getPropertyAsInt( SlipstreamConfig.UPDATE_APP, 0 );
boolean needAppUpdate = false;
if ( appUpdateFile.exists() ) {
// Load the info first, before downloading.
reloadAppUpdateInfo();
if ( appUpdateInterval > 0 ) {
if ( appUpdateInterval > 0 ) {
if ( appUpdateFile.exists() ) {
// Check if the app update info is stale.
if ( isFileStale( appUpdateFile, appUpdateInterval ) ) {
log.debug( String.format( "App update info is older than %d days.", appUpdateInterval ) );
log.debug( String.format( "App update info is older than %d days", appUpdateInterval ) );
needAppUpdate = true;
} else {
log.debug( "App update info isn't stale yet." );
log.debug( "App update info isn't stale yet" );
}
}
else {
// App update file doesn't exist.
needAppUpdate = true;
}
}
else {
// App update file doesn't exist.
needAppUpdate = true;
}
// Don't update if the user doesn't want to.
if ( appUpdateInterval <= 0 ) needAppUpdate = false;
if ( needAppUpdate ) {
boolean fetched = URLFetcher.refetchURL( ManagerFrame.APP_UPDATE_URL, appUpdateFile, appUpdateETagFile );
@ -186,7 +181,7 @@ public class ManagerInitThread extends Thread {
catch ( FileNotFoundException e ) {
}
catch ( IOException e ) {
log.error( String.format( "Error reading \"%s\".", modsTableStateFile.getName() ), e );
log.error( String.format( "Error reading \"%s\"", modsTableStateFile.getName() ), e );
fileNames.clear();
}
finally {
@ -207,13 +202,27 @@ public class ManagerInitThread extends Thread {
private void reloadCatalog() {
ModDB currentDB = JacksonCatalogReader.parse( catalogFile );
if ( currentDB != null ) frame.setCatalogModDB( currentDB );
final ModDB currentDB = JacksonCatalogReader.parse( catalogFile );
if ( currentDB != null ) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.setCatalogModDB( currentDB );
}
});
}
}
private void reloadAppUpdateInfo() {
AutoUpdateInfo aui = JacksonAutoUpdateReader.parse( appUpdateFile );
if ( aui != null ) frame.setAppUpdateInfo( aui );
final AutoUpdateInfo aui = JacksonAutoUpdateReader.parse( appUpdateFile );
if ( aui != null ) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.setAppUpdateInfo( aui );
}
});
}
}
@ -221,9 +230,14 @@ public class ManagerInitThread extends Thread {
* Returns true if a file is older than N days.
*/
private boolean isFileStale( File f, int maxDays ) {
Date modifiedDate = new Date( f.lastModified() );
Calendar cal = Calendar.getInstance();
cal.add( Calendar.DATE, maxDays * -1 );
return modifiedDate.before( cal.getTime() );
Calendar fileCal = Calendar.getInstance();
fileCal.setTimeInMillis( f.lastModified() );
fileCal.getTimeInMillis(); // Re-calculate calendar fields.
Calendar freshCal = Calendar.getInstance();
freshCal.add( Calendar.DATE, maxDays * -1 );
freshCal.getTimeInMillis(); // Re-calculate calendar fields.
return (fileCal.compareTo( freshCal ) < 0);
}
}