Skip to content

Commit

Permalink
small cleanup and performance fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Frotty committed Jul 24, 2018
1 parent ef571ee commit 680703b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
6 changes: 3 additions & 3 deletions src/main/java/systems/crigges/jmpq3/HashTable.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package systems.crigges.jmpq3;

import systems.crigges.jmpq3.security.MPQHashGenerator;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import systems.crigges.jmpq3.security.MPQHashGenerator;

/**
* MPQ hash table. Used to map file paths to block table indices.
* <p>
Expand Down Expand Up @@ -384,7 +384,7 @@ public void readFromBuffer(ByteBuffer src) {
public void writeToBuffer(ByteBuffer dest) {
dest.order(ByteOrder.LITTLE_ENDIAN);
dest.putLong(key);
dest.putShort((short) locale);
dest.putShort(locale);
dest.putShort((short) 0); // platform not used
dest.putInt(blockTableIndex);
}
Expand Down
64 changes: 32 additions & 32 deletions src/main/java/systems/crigges/jmpq3/JMpqEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import systems.crigges.jmpq3.security.MPQEncryption;
import systems.crigges.jmpq3.security.MPQHashGenerator;

import java.io.*;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
Expand Down Expand Up @@ -90,7 +93,7 @@ public class JMpqEditor implements AutoCloseable {
/** The block table. */
private BlockTable blockTable;
/** The list file. */
private Listfile listFile;
private Listfile listFile = new Listfile();
/** The internal filename. */
private IdentityHashMap<ByteBuffer, String> internalFilename = new IdentityHashMap<>();
/** The files to add. */
Expand Down Expand Up @@ -208,18 +211,18 @@ private void readAttributesFile() {
}
}

private void readListFile() throws IOException {
private void readListFile() {
if (hasFile("(listfile)")) {
try {
File tempFile = File.createTempFile("list", "file", JMpqEditor.tempDir);
tempFile.deleteOnExit();
extractFile("(listfile)", tempFile);
listFile = new Listfile(Files.readAllBytes(tempFile.toPath()));
} catch (Exception e) {
loadDefaultListFile();
log.warn("Extracting the mpq's listfile failed. It cannot be rebuild.", e);
}
} else {
loadDefaultListFile();
log.warn("The mpq doesn't contain a listfile. It cannot be rebuild.");
}
}

Expand Down Expand Up @@ -273,10 +276,7 @@ private void setupTempDir() throws JMpqException {

File[] files = JMpqEditor.tempDir.listFiles();
for (File f : files) {
// Delete existing tempfiles that are older than 1 day
if ((System.currentTimeMillis() - f.lastModified()) > 1000 * 60 * 60 * 24) {
f.delete();
}
f.delete();
}
} catch (IOException e) {
try {
Expand All @@ -287,28 +287,28 @@ private void setupTempDir() throws JMpqException {
}
}

/**
* Loads a default listfile for mpqs that have none
* Makes the archive readonly.
*/
private void loadDefaultListFile() throws IOException {
log.warn("The mpq doesn't come with a listfile so it cannot be rebuild");
InputStream resource = getClass().getClassLoader().getResourceAsStream("DefaultListfile.txt");
if (resource != null) {
File tempFile = File.createTempFile("jmpq", "lf", tempDir);
tempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(tempFile)) {
//copy stream
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = resource.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
listFile = new Listfile(Files.readAllBytes(tempFile.toPath()));
canWrite = false;
}
}
// /**
// * Loads a default listfile for mpqs that have none
// * Makes the archive readonly.
// */
// private void loadDefaultListFile() throws IOException {
// log.warn("The mpq doesn't come with a listfile so it cannot be rebuild");
// InputStream resource = getClass().getClassLoader().getResourceAsStream("DefaultListfile.txt");
// if (resource != null) {
// File tempFile = File.createTempFile("jmpq", "lf", tempDir);
// tempFile.deleteOnExit();
// try (FileOutputStream out = new FileOutputStream(tempFile)) {
// //copy stream
// byte[] buffer = new byte[1024];
// int bytesRead;
// while ((bytesRead = resource.read(buffer)) != -1) {
// out.write(buffer, 0, bytesRead);
// }
// }
// listFile = new Listfile(Files.readAllBytes(tempFile.toPath()));
// canWrite = false;
// }
// }


/**
Expand Down Expand Up @@ -775,7 +775,7 @@ public void close(boolean buildListfile, boolean buildAttributes, RecompressOpti
log.debug("Added file " + internalFilename.get(newFile));
}
log.debug("Added new files");
if (buildListfile) {
if (buildListfile && !listFile.getFiles().isEmpty()) {
// Add listfile
newFiles.add("(listfile)");
byte[] listfileArr = listFile.asByteArray();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/systems/crigges/jmpq3/Listfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public Listfile(byte[] file) {
sc.close();
}

public Listfile() {
}

public HashSet<String> getFiles() {
return this.files;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/systems/crigges/jmpq3/MpqFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private void extractCompressedBlock(OutputStream writer) throws IOException {
int end = sotBuffer.getInt();
int finalSize = 0;
for (int i = 0; i < sectorCount - 1; i++) {
buf.position(0 + start);
buf.position(start);
byte[] arr = getSectorAsByteArray(buf, end - start);
if (isEncrypted) {
new MPQEncryption(baseKey + i, true).processSingle(ByteBuffer.wrap(arr));
Expand Down

0 comments on commit 680703b

Please sign in to comment.