Advertisement
RobertBColton

Untitled

Feb 11th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 KB | None | 0 0
  1. /**
  2. * @file  Extractor.java
  3. * @brief Class implementing a .win resource extractor.
  4. *
  5. * @section License
  6. *
  7. * Copyright (C) 2013-2014 Robert B. Colton
  8. * This file is a part of the GameMaker: Studio decompiler.
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. **/
  23.  
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileNotFoundException;
  27. import java.io.FileOutputStream;
  28. import java.io.IOException;
  29. import java.nio.ByteBuffer;
  30. import java.nio.ByteOrder;
  31.  
  32. import javax.swing.JFileChooser;
  33. import javax.swing.JOptionPane;
  34. import javax.swing.UIManager;
  35. import javax.swing.UnsupportedLookAndFeelException;
  36.  
  37. public class Extractor {
  38.    
  39.     public static int scanFiles(String directory, String ext, byte[] buffer, byte[] start, byte[] end) throws IOException {
  40.         int num = 0;
  41.         int length = buffer.length;
  42.         for (int i = 0; i < length - 3; i++) {
  43.             if (buffer[i] == start[0]
  44.                     && buffer[i + 1] == start[1]
  45.                     && buffer[i + 2] == start[2]
  46.                     && buffer[i + 3] == start[3]) {
  47.                 for (int ii = i + 4; ii < length - 3; ii++) {
  48.                     if (buffer[ii] == end[0] && buffer[ii + 1] == end[1]
  49.                             && buffer[ii + 2] == end[2]
  50.                             && buffer[ii + 3] == end[3]) {
  51.                         File file = new File(directory + num + ext);
  52.                         file.getParentFile().mkdirs();
  53.                         num += 1;
  54.                         FileOutputStream fos = new FileOutputStream(file);
  55.                         fos.write(buffer, i, ii - i + 9);
  56.                         fos.close();
  57.                         i = ii + 8;
  58.                         break;
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.         return num;
  64.     }
  65.    
  66.     public static int scanFileChunks(String directory, String ext, byte[] buffer, byte[] start, byte[] format) throws IOException {
  67.         int num = 0;
  68.         int length = buffer.length;
  69.         for (int i = 0; i < length - 3; i++) {
  70.             if (buffer[i] == start[0]
  71.                 && buffer[i + 1] == start[1]
  72.                 && buffer[i + 2] == start[2]
  73.                 && buffer[i + 3] == start[3]) {
  74.                     File file = new File(directory + num + ext);
  75.                     file.getParentFile().mkdirs();
  76.                     num += 1;
  77.                     FileOutputStream fos = new FileOutputStream(file);
  78.                     ByteBuffer bb = ByteBuffer.wrap(buffer, i + 4, 4);
  79.                     bb.order(ByteOrder.LITTLE_ENDIAN);
  80.                     int filelength = bb.getInt();
  81.                     JOptionPane.showMessageDialog(null, filelength);
  82.                     fos.write(buffer, i, filelength);
  83.                     fos.close();
  84.                     i += filelength;
  85.                     break;
  86.             }
  87.         }
  88.         return num;
  89.     }
  90.    
  91.     public static void main(String args[]) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
  92.         // Set native Java L&F, otherwise file chooser looks like shit
  93.         UIManager.setLookAndFeel(
  94.             UIManager.getSystemLookAndFeelClassName());
  95.        
  96.         JFileChooser fileChooser = new JFileChooser();
  97.         if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
  98.             File file = fileChooser.getSelectedFile();
  99.            
  100.             FileInputStream fis = new FileInputStream(file);
  101.             int length = (int)file.length();  
  102.             byte buffer[] = new byte [length];
  103.             fis.read(buffer);  
  104.             fis.close();
  105.  
  106.             int textures = 0;
  107.             textures += scanFiles(file.getParent() + "/textures/texture", ".png", buffer, new byte[]{ -119, 'P', 'N', 'G' }, new byte[]{ (byte) -82, 66, 96, -126 });
  108.             //textures += scanFiles(file.getParent() + "/textures/texture", ".bmp", buffer, new byte[]{ -119, 'P', 'N', 'G' }, new byte[]{ (byte) -82, 66, 96, -126 });
  109.             JOptionPane.showMessageDialog(null, textures + " Textures Extracted");
  110.            
  111.             int sounds = 0;
  112.             sounds += scanFileChunks(file.getParent() + "/audio/sound", ".wav", buffer, new byte[]{ 'R', 'I', 'F', 'F' }, new byte[]{ 'W', 'A', 'V', 'E' });
  113.             JOptionPane.showMessageDialog(null, sounds + " Sounds Extracted");
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement