Changed FTLDat to allow opening dats in read-only mode

This commit is contained in:
Vhati 2013-09-29 07:58:57 -04:00
parent 7b65cc5d73
commit b94440e0a0
7 changed files with 55 additions and 14 deletions

View file

@ -1,5 +1,8 @@
Changelog Changelog
???:
- Changed FTLDat to allow opening dats in read-only mode
1.4: 1.4:
- Cleaned up some dodgy code when initially prompting for FTL's location - Cleaned up some dodgy code when initially prompting for FTL's location
- Added another common game location: "C:\Program Files (x86)\FTL" - Added another common game location: "C:\Program Files (x86)\FTL"

View file

@ -313,10 +313,12 @@ public class FTLDat {
public static class FolderPack extends AbstractPack { public static class FolderPack extends AbstractPack {
private File rootDir; private File rootDir;
public FolderPack( File rootDir ) { public FolderPack( File rootDir ) {
this.rootDir = rootDir; this.rootDir = rootDir;
} }
@Override @Override
public String getName() { public String getName() {
return rootDir.getName(); return rootDir.getName();
@ -447,20 +449,56 @@ public class FTLDat {
private Map<String,Integer> pathToIndexMap = null; private Map<String,Integer> pathToIndexMap = null;
private ByteBuffer byteBuffer = null; private ByteBuffer byteBuffer = null;
public FTLPack( File datFile, boolean create ) throws IOException {
this( datFile, create, 2048 ); /**
* Opens or creates a dat in various modes.
* When creating, the initial index size will be 2048.
*
* @see FTLPack(File datFile, String mode, int indexSize)
*/
public FTLPack( File datFile, String mode ) throws IOException {
this( datFile, mode, 2048 );
} }
public FTLPack( File datFile, boolean create, int indexSize ) throws IOException { /**
this.datFile = datFile; * Opens or creates a dat in various modes.
raf = new RandomAccessFile( datFile, "rw" ); *
* The mode must be one of the following:
* r - opens an existing dat, read-only.
* r+ - opens an existing dat, read/write.
* w+ - creates a new empty dat, read/write.
*
* @param datFile a file to open/create
* @param mode see above
* @param indexSize size of the initial index if creating
*/
public FTLPack( File datFile, String mode, int indexSize ) throws IOException {
if ( mode.equals( "r" ) ) {
if ( !datFile.exists() )
throw new FileNotFoundException( String.format( "The datFile was not found: %s", datFile.getPath() ) );
if ( create ) { this.datFile = datFile;
createIndex( indexSize ); raf = new RandomAccessFile( datFile, "r" );
} else {
readIndex(); readIndex();
} }
else if ( mode.equals( "r+" ) ) {
if ( !datFile.exists() )
throw new FileNotFoundException( String.format( "The datFile was not found: %s", datFile.getPath() ) );
this.datFile = datFile;
raf = new RandomAccessFile( datFile, "rw" );
readIndex();
} }
else if ( mode.equals( "w+" ) ) {
this.datFile = datFile;
raf = new RandomAccessFile( datFile, "rw" );
createIndex( indexSize );
}
else {
throw new IllegalArgumentException( String.format( "FTLPack constructor's mode arg was not 'r', 'r+', or 'w+' (%s).", mode ) );
}
}
/** /**
* Reads a little-endian unsigned int. * Reads a little-endian unsigned int.

View file

@ -25,7 +25,7 @@ public class FTLModManager {
private static final Logger log = LogManager.getLogger(FTLModManager.class); private static final Logger log = LogManager.getLogger(FTLModManager.class);
public static final String APP_NAME = "Slipstream Mod Manager"; public static final String APP_NAME = "Slipstream Mod Manager";
public static final ComparableVersion APP_VERSION = new ComparableVersion( "1.4" ); public static final ComparableVersion APP_VERSION = new ComparableVersion( "???" );
public static final String APP_URL = "http://www.ftlgame.com/forum/viewtopic.php?f=12&t=17102"; public static final String APP_URL = "http://www.ftlgame.com/forum/viewtopic.php?f=12&t=17102";
public static final String APP_AUTHOR = "Vhati"; public static final String APP_AUTHOR = "Vhati";

View file

@ -201,7 +201,7 @@ public class SlipstreamCLI {
dstP = new FTLDat.FolderPack( extractDir ); dstP = new FTLDat.FolderPack( extractDir );
for ( File datFile : datFiles ) { for ( File datFile : datFiles ) {
srcP = new FTLDat.FTLPack( datFile, false ); srcP = new FTLDat.FTLPack( datFile, "r" );
List<String> innerPaths = srcP.list(); List<String> innerPaths = srcP.list();
for ( String innerPath : innerPaths ) { for ( String innerPath : innerPaths ) {

View file

@ -156,8 +156,8 @@ public class ModPatchThread extends Thread {
return true; return true;
} }
dataP = new FTLPack( dataDat.datFile, false ); dataP = new FTLPack( dataDat.datFile, "r+" );
resP = new FTLPack( resDat.datFile, false ); resP = new FTLPack( resDat.datFile, "r+" );
Map<String,AbstractPack> topFolderMap = new HashMap<String,AbstractPack>(); Map<String,AbstractPack> topFolderMap = new HashMap<String,AbstractPack>();
topFolderMap.put( "data", dataP ); topFolderMap.put( "data", dataP );

View file

@ -84,7 +84,7 @@ public class DatExtractDialog extends ProgressDialog {
dstP = new FTLDat.FolderPack( extractDir ); dstP = new FTLDat.FolderPack( extractDir );
for ( File datFile : datFiles ) { for ( File datFile : datFiles ) {
srcP = new FTLDat.FTLPack( datFile, false ); srcP = new FTLDat.FTLPack( datFile, "r" );
progress = 0; progress = 0;
List<String> innerPaths = srcP.list(); List<String> innerPaths = srcP.list();
setProgressLater( progress, innerPaths.size() ); setProgressLater( progress, innerPaths.size() );

View file

@ -322,7 +322,7 @@ public class ModXMLSandbox extends JFrame implements ActionListener {
FTLDat.FTLPack dataP = null; FTLDat.FTLPack dataP = null;
InputStream is = null; InputStream is = null;
try { try {
dataP = new FTLDat.FTLPack( dataDatFile, false ); dataP = new FTLDat.FTLPack( dataDatFile, "r" );
List<String> innerPaths = dataP.list(); List<String> innerPaths = dataP.list();
String innerPath = promptForInnerPath( innerPaths ); String innerPath = promptForInnerPath( innerPaths );