From 21bdf7c8a4e09d6448bc0f506d46099ad25eec4f Mon Sep 17 00:00:00 2001 From: Phoenix Date: Wed, 25 Mar 2020 17:51:17 +0800 Subject: [PATCH 1/7] add some inset method and allow get mpqFile by blockTable --- .../systems/crigges/jmpq3/JMpqEditor.java | 189 +++++++++++++----- 1 file changed, 138 insertions(+), 51 deletions(-) diff --git a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java index 23f12c3..d6b8710 100644 --- a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java +++ b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java @@ -616,6 +616,54 @@ public MpqFile getMpqFile(String name) throws IOException { return new MpqFile(buffer, b, discBlockSize, name); } + + /** + * Gets the mpq file. + * + * @param block a block + * @return the mpq file + * + * @throws IOException Signals that an I/O exception has occurred. + */ + public MpqFile getMpqFileByBlock(BlockTable.Block block) throws IOException { + if ((block.getFlags() & MpqFile.ENCRYPTED) == MpqFile.ENCRYPTED){ + throw new IOException("cant access block"); + } + ByteBuffer buffer = ByteBuffer.allocate(block.getCompressedSize()).order(ByteOrder.LITTLE_ENDIAN); + fc.position(headerOffset + block.getFilePos()); + readFully(buffer, fc); + buffer.rewind(); + + return new MpqFile(buffer, block, discBlockSize, ""); + } + + /** + * Gets the mpq files. + * + * @return the mpq files + * + * @throws IOException Signals that an I/O exception has occurred. + */ + public List getMpqFilesByBlockTable() throws IOException { + List mpqFiles = new ArrayList<>(); + ArrayList list = blockTable.getAllVaildBlocks(); + for (Block block : list){ + if ((block.getFlags() & MpqFile.ENCRYPTED) == MpqFile.ENCRYPTED){ + continue; + } + ByteBuffer buffer = ByteBuffer.allocate(block.getCompressedSize()).order(ByteOrder.LITTLE_ENDIAN); + fc.position(headerOffset + block.getFilePos()); + readFully(buffer, fc); + buffer.rewind(); + + try{ + mpqFiles.add(new MpqFile(buffer, block, discBlockSize, "")); + } catch (IOException ignore){ + log.warn("a MpqFile read fail"); + } + } + return mpqFiles; + } /** * Deletes the specified file out of the mpq once you rebuild the mpq. @@ -633,26 +681,55 @@ public void deleteFile(String name) { filenameToData.remove(name); } } - - /** - * Inserts the specified byte array into the mpq once you close the editor. - * - * @param name of the file inside the mpq - * @param input the input byte array - * @throws JMpqException if file is not found or access errors occur - */ - public void insertByteArray(String name, byte[] input) throws NonWritableChannelException, IllegalArgumentException { - if (!canWrite) { - throw new NonWritableChannelException(); - } - - if (listFile.containsFile(name)) { - throw new IllegalArgumentException("Archive already contains file with name: " + name); - } - - listFile.addFile(name); - ByteBuffer data = ByteBuffer.wrap(input); - filenameToData.put(name, data); + + /** + * Inserts the specified byte array into the mpq once you close the editor. + * + * @param name of the file inside the mpq + * @param input the input byte array + * @param override is override file + * @throws NonWritableChannelException the non writable channel exception + * @throws IllegalArgumentException when the mpq has filename and not override + */ + public void insertByteArray(String name, byte[] input,boolean override) throws NonWritableChannelException, + IllegalArgumentException { + if (!canWrite) { + throw new NonWritableChannelException(); + } + + if ((!override) && listFile.containsFile(name)) { + throw new IllegalArgumentException("Archive already contains file with name: " + name); + } + + listFile.addFile(name); + ByteBuffer data = ByteBuffer.wrap(input); + filenameToData.put(name, data); + } + + /** + * Inserts the specified byte array into the mpq once you close the editor. + * + * @param name of the file inside the mpq + * @param input the input byte array + * @throws NonWritableChannelException the non writable channel exception + * @throws IllegalArgumentException when the mpq has filename + */ + public void insertByteArray(String name, byte[] input) throws NonWritableChannelException, IllegalArgumentException { + insertByteArray(name,input,false); + } + + /** + * Inserts the specified file into the mpq once you close the editor. + * + * @param name of the file inside the mpq + * @param file the file + * @param backupFile if true the editors creates a copy of the file to add, so + * further changes won't affect the resulting mpq + * @throws IOException the io exception + * @throws IllegalArgumentException the illegal argument exception + */ + public void insertFile(String name, File file, boolean backupFile) throws IOException, IllegalArgumentException { + insertFile(name,file,backupFile,false); } /** @@ -662,35 +739,36 @@ public void insertByteArray(String name, byte[] input) throws NonWritableChannel * @param file the file * @param backupFile if true the editors creates a copy of the file to add, so * further changes won't affect the resulting mpq + * @param override is override file * @throws JMpqException if file is not found or access errors occur */ - public void insertFile(String name, File file, boolean backupFile) throws IOException, IllegalArgumentException { - if (!canWrite) { - throw new NonWritableChannelException(); - } - - log.info("insert file: " + name); - - if (listFile.containsFile(name)) { - throw new IllegalArgumentException("Archive already contains file with name: " + name); - } - - - try { - listFile.addFile(name); - if (backupFile) { - File temp = File.createTempFile("jmpq", "backup", JMpqEditor.tempDir); - temp.deleteOnExit(); - Files.copy(file.toPath(), temp.toPath(), StandardCopyOption.REPLACE_EXISTING); - ByteBuffer data = ByteBuffer.wrap(Files.readAllBytes(temp.toPath())); - filenameToData.put(name, data); - } else { - ByteBuffer data = ByteBuffer.wrap(Files.readAllBytes(file.toPath())); - filenameToData.put(name, data); - } - } catch (IOException e) { - throw new JMpqException(e); - } + public void insertFile(String name, File file, boolean backupFile, boolean override) throws IOException, + IllegalArgumentException{ + if (!canWrite){ + throw new NonWritableChannelException(); + } + + log.info("insert file: " + name); + + if ((!override) && listFile.containsFile(name)){ + throw new IllegalArgumentException("Archive already contains file with name: " + name); + } + + try{ + listFile.addFile(name); + if (backupFile){ + File temp = File.createTempFile("jmpq", "backup", JMpqEditor.tempDir); + temp.deleteOnExit(); + Files.copy(file.toPath(), temp.toPath(), StandardCopyOption.REPLACE_EXISTING); + ByteBuffer data = ByteBuffer.wrap(Files.readAllBytes(temp.toPath())); + filenameToData.put(name, data); + } else{ + ByteBuffer data = ByteBuffer.wrap(Files.readAllBytes(file.toPath())); + filenameToData.put(name, data); + } + } catch (IOException e){ + throw new JMpqException(e); + } } public void closeReadOnly() throws IOException { @@ -962,14 +1040,23 @@ public boolean isCanWrite() { public void setKeepHeaderOffset(boolean keepHeaderOffset) { this.keepHeaderOffset = keepHeaderOffset; } - - /* + + + /** + * Get block table block table. + * + * @return the block table + */ + public BlockTable getBlockTable(){ + return blockTable; + } + + /** * (non-Javadoc) * * @see java.lang.Object#toString() */ - - @Override + @Override public String toString() { return "JMpqEditor [headerSize=" + headerSize + ", archiveSize=" + archiveSize + ", formatVersion=" + formatVersion + ", discBlockSize=" + discBlockSize + ", hashPos=" + hashPos + ", blockPos=" + blockPos + ", hashSize=" + hashSize + ", blockSize=" + blockSize + ", hashMap=" + hashTable + "]"; From 71b2346f83706305f43b326e5102e8fbebb21fa3 Mon Sep 17 00:00:00 2001 From: Phoenix Date: Wed, 25 Mar 2020 19:19:04 +0800 Subject: [PATCH 2/7] make the unit test --- .../systems/crigges/jmpq3/JMpqEditor.java | 17 ++------- .../systems/crigges/jmpq3test/MpqTests.java | 37 ++++++++++++++++++- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java index d6b8710..ff06dd3 100644 --- a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java +++ b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java @@ -627,7 +627,7 @@ public MpqFile getMpqFile(String name) throws IOException { */ public MpqFile getMpqFileByBlock(BlockTable.Block block) throws IOException { if ((block.getFlags() & MpqFile.ENCRYPTED) == MpqFile.ENCRYPTED){ - throw new IOException("cant access block"); + throw new IOException("cant access this block"); } ByteBuffer buffer = ByteBuffer.allocate(block.getCompressedSize()).order(ByteOrder.LITTLE_ENDIAN); fc.position(headerOffset + block.getFilePos()); @@ -648,19 +648,10 @@ public List getMpqFilesByBlockTable() throws IOException { List mpqFiles = new ArrayList<>(); ArrayList list = blockTable.getAllVaildBlocks(); for (Block block : list){ - if ((block.getFlags() & MpqFile.ENCRYPTED) == MpqFile.ENCRYPTED){ - continue; - } - ByteBuffer buffer = ByteBuffer.allocate(block.getCompressedSize()).order(ByteOrder.LITTLE_ENDIAN); - fc.position(headerOffset + block.getFilePos()); - readFully(buffer, fc); - buffer.rewind(); - try{ - mpqFiles.add(new MpqFile(buffer, block, discBlockSize, "")); - } catch (IOException ignore){ - log.warn("a MpqFile read fail"); - } + MpqFile mpqFile = getMpqFileByBlock(block); + mpqFiles.add(mpqFile); + } catch (IOException ignore){} } return mpqFiles; } diff --git a/src/test/java/systems/crigges/jmpq3test/MpqTests.java b/src/test/java/systems/crigges/jmpq3test/MpqTests.java index a8dfcf2..b9b2614 100644 --- a/src/test/java/systems/crigges/jmpq3test/MpqTests.java +++ b/src/test/java/systems/crigges/jmpq3test/MpqTests.java @@ -248,7 +248,8 @@ public void testDuplicatePaths() throws IOException { Assert.expectThrows(IllegalArgumentException.class, () -> { mpqEditor.insertByteArray("teST", "bytesasdadasdad".getBytes()); }); - + //test override + mpqEditor.insertByteArray("teST", "bytesasdadasdad".getBytes(),true); } } } @@ -329,6 +330,17 @@ private void insertAndDelete(File mpq, String filename) throws IOException { mpqEditor.deleteFile(filename); } + try (JMpqEditor mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0)) { + if (!mpqEditor.isCanWrite()) { + return; + } + //test override + mpqEditor.insertFile(filename, getFile(filename), false,true); + mpqEditor.insertFile(filename, getFile(filename), false,true); + + mpqEditor.deleteFile(filename); + } + try (JMpqEditor mpqEditor = new JMpqEditor(mpq, MPQOpenOption.READ_ONLY, MPQOpenOption.FORCE_V0)) { Assert.assertFalse(mpqEditor.hasFile(filename)); } @@ -396,4 +408,27 @@ public void newBlocksizeBufferOverflow() throws IOException { mpqEditor.close(); } + + @Test() + public void testForGetMpqFileByBlock() throws IOException { + File[] mpqs = getMpqs(); + for (File mpq : mpqs) { + if (mpq.getName().equals("invalidHashSize.scx_copy")) { + continue; + } + try (JMpqEditor mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0)) { + + Assert.assertTrue(mpqEditor.getMpqFilesByBlockTable().size()>0); + BlockTable blockTable = mpqEditor.getBlockTable(); + Assert.assertNotNull(blockTable); + for (BlockTable.Block block : blockTable.getAllVaildBlocks()) + { + if ((block.getFlags() & MpqFile.ENCRYPTED) == MpqFile.ENCRYPTED){ + continue; + } + Assert.assertNotNull(mpqEditor.getMpqFileByBlock(block)); + } + } + } + } } From 016317eaf3bbe6e6f5d6858ce48d6aebd05a91ab Mon Sep 17 00:00:00 2001 From: Phoenix Date: Mon, 30 Mar 2020 01:22:32 +0800 Subject: [PATCH 3/7] change the \t to four space use getFlag(ENCRYPTED) replace the `flag ENCRYPTED` check --- .../systems/crigges/jmpq3/JMpqEditor.java | 248 +++++++++--------- .../systems/crigges/jmpq3test/MpqTests.java | 60 ++--- 2 files changed, 154 insertions(+), 154 deletions(-) diff --git a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java index ff06dd3..627720c 100644 --- a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java +++ b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java @@ -490,7 +490,7 @@ public void extractAllFiles(File dest) throws JMpqException { try { int i = 0; for (Block b : blocks) { - if ((b.getFlags() & MpqFile.ENCRYPTED) == MpqFile.ENCRYPTED) { + if (b.hasFlag(MpqFile.ENCRYPTED)) { continue; } ByteBuffer buf = ByteBuffer.allocate(b.getCompressedSize()).order(ByteOrder.LITTLE_ENDIAN); @@ -616,45 +616,45 @@ public MpqFile getMpqFile(String name) throws IOException { return new MpqFile(buffer, b, discBlockSize, name); } - - /** - * Gets the mpq file. - * - * @param block a block - * @return the mpq file - * - * @throws IOException Signals that an I/O exception has occurred. - */ - public MpqFile getMpqFileByBlock(BlockTable.Block block) throws IOException { - if ((block.getFlags() & MpqFile.ENCRYPTED) == MpqFile.ENCRYPTED){ - throw new IOException("cant access this block"); - } - ByteBuffer buffer = ByteBuffer.allocate(block.getCompressedSize()).order(ByteOrder.LITTLE_ENDIAN); - fc.position(headerOffset + block.getFilePos()); - readFully(buffer, fc); - buffer.rewind(); - - return new MpqFile(buffer, block, discBlockSize, ""); - } - - /** - * Gets the mpq files. - * - * @return the mpq files - * - * @throws IOException Signals that an I/O exception has occurred. - */ - public List getMpqFilesByBlockTable() throws IOException { - List mpqFiles = new ArrayList<>(); - ArrayList list = blockTable.getAllVaildBlocks(); - for (Block block : list){ - try{ - MpqFile mpqFile = getMpqFileByBlock(block); - mpqFiles.add(mpqFile); - } catch (IOException ignore){} - } - return mpqFiles; - } + + /** + * Gets the mpq file. + * + * @param block a block + * @return the mpq file + * + * @throws IOException Signals that an I/O exception has occurred. + */ + public MpqFile getMpqFileByBlock(BlockTable.Block block) throws IOException { + if (block.hasFlag(MpqFile.ENCRYPTED)){ + throw new IOException("cant access this block"); + } + ByteBuffer buffer = ByteBuffer.allocate(block.getCompressedSize()).order(ByteOrder.LITTLE_ENDIAN); + fc.position(headerOffset + block.getFilePos()); + readFully(buffer, fc); + buffer.rewind(); + + return new MpqFile(buffer, block, discBlockSize, ""); + } + + /** + * Gets the mpq files. + * + * @return the mpq files + * + * @throws IOException Signals that an I/O exception has occurred. + */ + public List getMpqFilesByBlockTable() throws IOException { + List mpqFiles = new ArrayList<>(); + ArrayList list = blockTable.getAllVaildBlocks(); + for (Block block : list){ + try{ + MpqFile mpqFile = getMpqFileByBlock(block); + mpqFiles.add(mpqFile); + } catch (IOException ignore){} + } + return mpqFiles; + } /** * Deletes the specified file out of the mpq once you rebuild the mpq. @@ -672,54 +672,54 @@ public void deleteFile(String name) { filenameToData.remove(name); } } - - /** - * Inserts the specified byte array into the mpq once you close the editor. - * - * @param name of the file inside the mpq - * @param input the input byte array - * @param override is override file - * @throws NonWritableChannelException the non writable channel exception - * @throws IllegalArgumentException when the mpq has filename and not override - */ - public void insertByteArray(String name, byte[] input,boolean override) throws NonWritableChannelException, - IllegalArgumentException { - if (!canWrite) { - throw new NonWritableChannelException(); - } - - if ((!override) && listFile.containsFile(name)) { - throw new IllegalArgumentException("Archive already contains file with name: " + name); - } - - listFile.addFile(name); - ByteBuffer data = ByteBuffer.wrap(input); - filenameToData.put(name, data); - } - - /** - * Inserts the specified byte array into the mpq once you close the editor. - * - * @param name of the file inside the mpq - * @param input the input byte array - * @throws NonWritableChannelException the non writable channel exception - * @throws IllegalArgumentException when the mpq has filename - */ - public void insertByteArray(String name, byte[] input) throws NonWritableChannelException, IllegalArgumentException { - insertByteArray(name,input,false); + + /** + * Inserts the specified byte array into the mpq once you close the editor. + * + * @param name of the file inside the mpq + * @param input the input byte array + * @param override is override file + * @throws NonWritableChannelException the non writable channel exception + * @throws IllegalArgumentException when the mpq has filename and not override + */ + public void insertByteArray(String name, byte[] input,boolean override) throws NonWritableChannelException, + IllegalArgumentException { + if (!canWrite) { + throw new NonWritableChannelException(); + } + + if ((!override) && listFile.containsFile(name)) { + throw new IllegalArgumentException("Archive already contains file with name: " + name); + } + + listFile.addFile(name); + ByteBuffer data = ByteBuffer.wrap(input); + filenameToData.put(name, data); + } + + /** + * Inserts the specified byte array into the mpq once you close the editor. + * + * @param name of the file inside the mpq + * @param input the input byte array + * @throws NonWritableChannelException the non writable channel exception + * @throws IllegalArgumentException when the mpq has filename + */ + public void insertByteArray(String name, byte[] input) throws NonWritableChannelException, IllegalArgumentException { + insertByteArray(name,input,false); } - - /** - * Inserts the specified file into the mpq once you close the editor. - * - * @param name of the file inside the mpq - * @param file the file - * @param backupFile if true the editors creates a copy of the file to add, so - * further changes won't affect the resulting mpq - * @throws IOException the io exception - * @throws IllegalArgumentException the illegal argument exception - */ - public void insertFile(String name, File file, boolean backupFile) throws IOException, IllegalArgumentException { + + /** + * Inserts the specified file into the mpq once you close the editor. + * + * @param name of the file inside the mpq + * @param file the file + * @param backupFile if true the editors creates a copy of the file to add, so + * further changes won't affect the resulting mpq + * @throws IOException the io exception + * @throws IllegalArgumentException the illegal argument exception + */ + public void insertFile(String name, File file, boolean backupFile) throws IOException, IllegalArgumentException { insertFile(name,file,backupFile,false); } @@ -735,31 +735,31 @@ public void insertFile(String name, File file, boolean backupFile) throws IOExce */ public void insertFile(String name, File file, boolean backupFile, boolean override) throws IOException, IllegalArgumentException{ - if (!canWrite){ - throw new NonWritableChannelException(); - } - - log.info("insert file: " + name); - - if ((!override) && listFile.containsFile(name)){ - throw new IllegalArgumentException("Archive already contains file with name: " + name); - } - - try{ - listFile.addFile(name); - if (backupFile){ - File temp = File.createTempFile("jmpq", "backup", JMpqEditor.tempDir); - temp.deleteOnExit(); - Files.copy(file.toPath(), temp.toPath(), StandardCopyOption.REPLACE_EXISTING); - ByteBuffer data = ByteBuffer.wrap(Files.readAllBytes(temp.toPath())); - filenameToData.put(name, data); - } else{ - ByteBuffer data = ByteBuffer.wrap(Files.readAllBytes(file.toPath())); - filenameToData.put(name, data); - } - } catch (IOException e){ - throw new JMpqException(e); - } + if (!canWrite){ + throw new NonWritableChannelException(); + } + + log.info("insert file: " + name); + + if ((!override) && listFile.containsFile(name)){ + throw new IllegalArgumentException("Archive already contains file with name: " + name); + } + + try{ + listFile.addFile(name); + if (backupFile){ + File temp = File.createTempFile("jmpq", "backup", JMpqEditor.tempDir); + temp.deleteOnExit(); + Files.copy(file.toPath(), temp.toPath(), StandardCopyOption.REPLACE_EXISTING); + ByteBuffer data = ByteBuffer.wrap(Files.readAllBytes(temp.toPath())); + filenameToData.put(name, data); + } else{ + ByteBuffer data = ByteBuffer.wrap(Files.readAllBytes(file.toPath())); + filenameToData.put(name, data); + } + } catch (IOException e){ + throw new JMpqException(e); + } } public void closeReadOnly() throws IOException { @@ -1031,23 +1031,23 @@ public boolean isCanWrite() { public void setKeepHeaderOffset(boolean keepHeaderOffset) { this.keepHeaderOffset = keepHeaderOffset; } - - - /** - * Get block table block table. - * - * @return the block table - */ - public BlockTable getBlockTable(){ - return blockTable; - } - + + + /** + * Get block table block table. + * + * @return the block table + */ + public BlockTable getBlockTable(){ + return blockTable; + } + /** * (non-Javadoc) * * @see java.lang.Object#toString() */ - @Override + @Override public String toString() { return "JMpqEditor [headerSize=" + headerSize + ", archiveSize=" + archiveSize + ", formatVersion=" + formatVersion + ", discBlockSize=" + discBlockSize + ", hashPos=" + hashPos + ", blockPos=" + blockPos + ", hashSize=" + hashSize + ", blockSize=" + blockSize + ", hashMap=" + hashTable + "]"; diff --git a/src/test/java/systems/crigges/jmpq3test/MpqTests.java b/src/test/java/systems/crigges/jmpq3test/MpqTests.java index b9b2614..8e88e3f 100644 --- a/src/test/java/systems/crigges/jmpq3test/MpqTests.java +++ b/src/test/java/systems/crigges/jmpq3test/MpqTests.java @@ -249,7 +249,7 @@ public void testDuplicatePaths() throws IOException { mpqEditor.insertByteArray("teST", "bytesasdadasdad".getBytes()); }); //test override - mpqEditor.insertByteArray("teST", "bytesasdadasdad".getBytes(),true); + mpqEditor.insertByteArray("teST", "bytesasdadasdad".getBytes(),true); } } } @@ -331,12 +331,12 @@ private void insertAndDelete(File mpq, String filename) throws IOException { } try (JMpqEditor mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0)) { - if (!mpqEditor.isCanWrite()) { - return; - } - //test override - mpqEditor.insertFile(filename, getFile(filename), false,true); - mpqEditor.insertFile(filename, getFile(filename), false,true); + if (!mpqEditor.isCanWrite()) { + return; + } + //test override + mpqEditor.insertFile(filename, getFile(filename), false,true); + mpqEditor.insertFile(filename, getFile(filename), false,true); mpqEditor.deleteFile(filename); } @@ -408,27 +408,27 @@ public void newBlocksizeBufferOverflow() throws IOException { mpqEditor.close(); } - - @Test() - public void testForGetMpqFileByBlock() throws IOException { - File[] mpqs = getMpqs(); - for (File mpq : mpqs) { - if (mpq.getName().equals("invalidHashSize.scx_copy")) { - continue; - } - try (JMpqEditor mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0)) { - - Assert.assertTrue(mpqEditor.getMpqFilesByBlockTable().size()>0); - BlockTable blockTable = mpqEditor.getBlockTable(); - Assert.assertNotNull(blockTable); - for (BlockTable.Block block : blockTable.getAllVaildBlocks()) - { - if ((block.getFlags() & MpqFile.ENCRYPTED) == MpqFile.ENCRYPTED){ - continue; - } - Assert.assertNotNull(mpqEditor.getMpqFileByBlock(block)); - } - } - } - } + + @Test() + public void testForGetMpqFileByBlock() throws IOException { + File[] mpqs = getMpqs(); + for (File mpq : mpqs) { + if (mpq.getName().equals("invalidHashSize.scx_copy")) { + continue; + } + try (JMpqEditor mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0)) { + + Assert.assertTrue(mpqEditor.getMpqFilesByBlockTable().size()>0); + BlockTable blockTable = mpqEditor.getBlockTable(); + Assert.assertNotNull(blockTable); + for (BlockTable.Block block : blockTable.getAllVaildBlocks()) + { + if (block.hasFlag(MpqFile.ENCRYPTED)){ + continue; + } + Assert.assertNotNull(mpqEditor.getMpqFileByBlock(block)); + } + } + } + } } From 4f33140de76173851f1ce988b21539812e80a79b Mon Sep 17 00:00:00 2001 From: Phoenix Date: Mon, 30 Mar 2020 01:27:18 +0800 Subject: [PATCH 4/7] remove useless space --- src/main/java/systems/crigges/jmpq3/JMpqEditor.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java index 627720c..a5fc0a1 100644 --- a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java +++ b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java @@ -672,7 +672,7 @@ public void deleteFile(String name) { filenameToData.remove(name); } } - + /** * Inserts the specified byte array into the mpq once you close the editor. * @@ -687,11 +687,11 @@ public void insertByteArray(String name, byte[] input,boolean override) throws N if (!canWrite) { throw new NonWritableChannelException(); } - + if ((!override) && listFile.containsFile(name)) { throw new IllegalArgumentException("Archive already contains file with name: " + name); } - + listFile.addFile(name); ByteBuffer data = ByteBuffer.wrap(input); filenameToData.put(name, data); @@ -738,9 +738,9 @@ public void insertFile(String name, File file, boolean backupFile, boolean overr if (!canWrite){ throw new NonWritableChannelException(); } - + log.info("insert file: " + name); - + if ((!override) && listFile.containsFile(name)){ throw new IllegalArgumentException("Archive already contains file with name: " + name); } From eff15b1bbe132b60096941d270e48f2a7939e595 Mon Sep 17 00:00:00 2001 From: Phoenix Date: Mon, 30 Mar 2020 01:31:23 +0800 Subject: [PATCH 5/7] remove useless space --- src/main/java/systems/crigges/jmpq3/JMpqEditor.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java index a5fc0a1..6d15249 100644 --- a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java +++ b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java @@ -626,7 +626,7 @@ public MpqFile getMpqFile(String name) throws IOException { * @throws IOException Signals that an I/O exception has occurred. */ public MpqFile getMpqFileByBlock(BlockTable.Block block) throws IOException { - if (block.hasFlag(MpqFile.ENCRYPTED)){ + if (block.hasFlag(MpqFile.ENCRYPTED)) { throw new IOException("cant access this block"); } ByteBuffer buffer = ByteBuffer.allocate(block.getCompressedSize()).order(ByteOrder.LITTLE_ENDIAN); @@ -733,21 +733,20 @@ public void insertFile(String name, File file, boolean backupFile) throws IOExce * @param override is override file * @throws JMpqException if file is not found or access errors occur */ - public void insertFile(String name, File file, boolean backupFile, boolean override) throws IOException, - IllegalArgumentException{ + public void insertFile(String name, File file, boolean backupFile, boolean override) throws IOException,IllegalArgumentException{ if (!canWrite){ throw new NonWritableChannelException(); } log.info("insert file: " + name); - if ((!override) && listFile.containsFile(name)){ + if ((!override) && listFile.containsFile(name)) { throw new IllegalArgumentException("Archive already contains file with name: " + name); } try{ listFile.addFile(name); - if (backupFile){ + if (backupFile) { File temp = File.createTempFile("jmpq", "backup", JMpqEditor.tempDir); temp.deleteOnExit(); Files.copy(file.toPath(), temp.toPath(), StandardCopyOption.REPLACE_EXISTING); @@ -757,7 +756,7 @@ public void insertFile(String name, File file, boolean backupFile, boolean overr ByteBuffer data = ByteBuffer.wrap(Files.readAllBytes(file.toPath())); filenameToData.put(name, data); } - } catch (IOException e){ + } catch (IOException e) { throw new JMpqException(e); } } From b587d9e5da4fe3b746ffd5ae23087324485561ad Mon Sep 17 00:00:00 2001 From: Phoenix Date: Mon, 30 Mar 2020 01:36:17 +0800 Subject: [PATCH 6/7] remove useless space --- src/main/java/systems/crigges/jmpq3/JMpqEditor.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java index 6d15249..8feabbd 100644 --- a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java +++ b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java @@ -734,7 +734,7 @@ public void insertFile(String name, File file, boolean backupFile) throws IOExce * @throws JMpqException if file is not found or access errors occur */ public void insertFile(String name, File file, boolean backupFile, boolean override) throws IOException,IllegalArgumentException{ - if (!canWrite){ + if (!canWrite) { throw new NonWritableChannelException(); } @@ -752,7 +752,7 @@ public void insertFile(String name, File file, boolean backupFile, boolean overr Files.copy(file.toPath(), temp.toPath(), StandardCopyOption.REPLACE_EXISTING); ByteBuffer data = ByteBuffer.wrap(Files.readAllBytes(temp.toPath())); filenameToData.put(name, data); - } else{ + } else { ByteBuffer data = ByteBuffer.wrap(Files.readAllBytes(file.toPath())); filenameToData.put(name, data); } From 32ba049766426587b8d34fde6b6573cd6d88370d Mon Sep 17 00:00:00 2001 From: Phoenix Date: Thu, 23 Apr 2020 18:32:05 +0800 Subject: [PATCH 7/7] format change --- src/main/java/systems/crigges/jmpq3/JMpqEditor.java | 4 ++-- src/test/java/systems/crigges/jmpq3test/MpqTests.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java index 8feabbd..3b783af 100644 --- a/src/main/java/systems/crigges/jmpq3/JMpqEditor.java +++ b/src/main/java/systems/crigges/jmpq3/JMpqEditor.java @@ -706,7 +706,7 @@ public void insertByteArray(String name, byte[] input,boolean override) throws N * @throws IllegalArgumentException when the mpq has filename */ public void insertByteArray(String name, byte[] input) throws NonWritableChannelException, IllegalArgumentException { - insertByteArray(name,input,false); + insertByteArray(name, input, false); } /** @@ -720,7 +720,7 @@ public void insertByteArray(String name, byte[] input) throws NonWritableChannel * @throws IllegalArgumentException the illegal argument exception */ public void insertFile(String name, File file, boolean backupFile) throws IOException, IllegalArgumentException { - insertFile(name,file,backupFile,false); + insertFile(name, file, backupFile, false); } /** diff --git a/src/test/java/systems/crigges/jmpq3test/MpqTests.java b/src/test/java/systems/crigges/jmpq3test/MpqTests.java index 8e88e3f..a85c5e3 100644 --- a/src/test/java/systems/crigges/jmpq3test/MpqTests.java +++ b/src/test/java/systems/crigges/jmpq3test/MpqTests.java @@ -249,7 +249,7 @@ public void testDuplicatePaths() throws IOException { mpqEditor.insertByteArray("teST", "bytesasdadasdad".getBytes()); }); //test override - mpqEditor.insertByteArray("teST", "bytesasdadasdad".getBytes(),true); + mpqEditor.insertByteArray("teST", "bytesasdadasdad".getBytes(), true); } } } @@ -418,7 +418,7 @@ public void testForGetMpqFileByBlock() throws IOException { } try (JMpqEditor mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0)) { - Assert.assertTrue(mpqEditor.getMpqFilesByBlockTable().size()>0); + Assert.assertTrue(mpqEditor.getMpqFilesByBlockTable().size() > 0); BlockTable blockTable = mpqEditor.getBlockTable(); Assert.assertNotNull(blockTable); for (BlockTable.Block block : blockTable.getAllVaildBlocks())