home *** CD-ROM | disk | FTP | other *** search
/ Total Destruction / Total_Destruction.iso / util / Quake2Tool.exe / Unpack.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-01-14  |  7.5 KB  |  267 lines

  1. import java.awt.Button;
  2. import java.awt.Component;
  3. import java.awt.Container;
  4. import java.awt.Event;
  5. import java.awt.FileDialog;
  6. import java.awt.FlowLayout;
  7. import java.awt.Frame;
  8. import java.awt.Label;
  9. import java.awt.List;
  10. import java.awt.Panel;
  11. import java.awt.TextField;
  12. import java.io.DataInputStream;
  13. import java.io.DataOutputStream;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.FileOutputStream;
  17. import java.io.FilterOutputStream;
  18. import java.io.IOException;
  19. import java.io.RandomAccessFile;
  20.  
  21. class Unpack extends Panel {
  22.    int ident;
  23.    int dirofs;
  24.    int dirlen;
  25.    int numLumps;
  26.    byte[] name = new byte[56];
  27.    String nameString;
  28.    int filepos;
  29.    int filelen;
  30.    RandomAccessFile readLump;
  31.    DataInputStream directory;
  32.    String pakName;
  33.    String pattern;
  34.    FileDialog pakFileDialog;
  35.    FileDialog outputFileDialog;
  36.    TextField patternText;
  37.    List pakList;
  38.    String[] pakListSelections;
  39.    Button unpackButton;
  40.    Button listButton;
  41.    boolean finished = false;
  42.    protected static final int IDPAKHEADER = 1262698832;
  43.  
  44.    protected String GetPattern() {
  45.       return this.patternText.getText().trim();
  46.    }
  47.  
  48.    static void usage() {
  49.       System.out.println("Usage: unpack <packfile> <match> <basedir>");
  50.       System.out.println("   or: unpack -list <packfile>");
  51.       System.out.println("<match> may contain a single * wildcard");
  52.       System.exit(1);
  53.    }
  54.  
  55.    public boolean isFinished() {
  56.       return this.finished;
  57.    }
  58.  
  59.    protected void SetOutputDirectory() {
  60.       this.outputFileDialog.show();
  61.       System.out.println("The output directory is: " + this.outputFileDialog.getDirectory());
  62.    }
  63.  
  64.    public Unpack(Frame parent) {
  65.       ((Component)this).resize(200, 200);
  66.       this.pakFileDialog = new FileDialog(parent, "Pak File");
  67.       this.pakFileDialog.setFile("*.pak");
  68.       this.outputFileDialog = new FileDialog(parent, "Output File");
  69.       this.outputFileDialog.setFile("output.log");
  70.       this.patternText = new TextField("*", 20);
  71.       ((Container)this).setLayout(new FlowLayout());
  72.       this.pakList = new List(10, true);
  73.       ((Container)this).add(this.pakList);
  74.       ((Container)this).add(new Label("Pattern:"));
  75.       ((Container)this).add(this.patternText);
  76.       this.listButton = new Button("Unpack");
  77.       this.unpackButton = new Button("List");
  78.       ((Container)this).add(this.unpackButton);
  79.       ((Container)this).add(this.listButton);
  80.    }
  81.  
  82.    protected void CleanUp() {
  83.       this.pakFileDialog.dispose();
  84.       this.outputFileDialog.dispose();
  85.  
  86.       try {
  87.          this.directory.close();
  88.          this.readLump.close();
  89.       } catch (IOException var3) {
  90.          ((Throwable)var3).printStackTrace();
  91.       }
  92.  
  93.    }
  94.  
  95.    protected boolean patternMatch(String pattern, String s) {
  96.       if (pattern.equals(s)) {
  97.          return true;
  98.       } else {
  99.          int index = pattern.indexOf(42);
  100.          if (index == -1) {
  101.             return false;
  102.          } else if (!pattern.regionMatches(0, s, 0, index)) {
  103.             return false;
  104.          } else {
  105.             ++index;
  106.             int remaining = pattern.length() - index;
  107.             if (s.length() < remaining) {
  108.                return false;
  109.             } else {
  110.                return pattern.regionMatches(index, s, s.length() - remaining, remaining);
  111.             }
  112.          }
  113.       }
  114.    }
  115.  
  116.    protected int intSwap(int i) {
  117.       int a = i & 255;
  118.       int b = i >> 8 & 255;
  119.       int c = i >> 16 & 255;
  120.       int d = i >> 24 & 255;
  121.       return (a << 24) + (b << 16) + (c << 8) + d;
  122.    }
  123.  
  124.    public void WriteFiles() {
  125.       try {
  126.          this.directory = new DataInputStream(new FileInputStream(this.pakName));
  127.          this.directory.skipBytes(12 + (this.dirofs - 12));
  128.          String logName;
  129.          if (this.outputFileDialog.getFile() == null) {
  130.             logName = this.outputFileDialog.getDirectory() + "output.log";
  131.          } else {
  132.             logName = this.outputFileDialog.getDirectory() + this.outputFileDialog.getFile();
  133.          }
  134.  
  135.          RandomAccessFile log = new RandomAccessFile(logName, "rw");
  136.          log.writeBytes("FILES CREATED:\r\n");
  137.  
  138.          for(int i = 0; i < this.numLumps; ++i) {
  139.             this.directory.readFully(this.name);
  140.             this.filepos = this.intSwap(this.directory.readInt());
  141.             this.filelen = this.intSwap(this.directory.readInt());
  142.             this.nameString = new String(this.name, 0);
  143.             this.nameString = this.nameString.substring(0, this.nameString.indexOf(0));
  144.             if (this.Selected(this.nameString)) {
  145.                byte[] buffer = new byte[this.filelen];
  146.                log.writeBytes(this.nameString + "\r\n");
  147.                System.out.println("Unpaking " + this.nameString + " " + this.filelen + " bytes");
  148.                this.readLump.seek((long)this.filepos);
  149.                this.readLump.readFully(buffer);
  150.                StringBuffer fixedString = new StringBuffer(this.outputFileDialog.getDirectory() + this.nameString);
  151.  
  152.                for(int index = 0; index < fixedString.length(); ++index) {
  153.                   if (fixedString.charAt(index) == '/') {
  154.                      fixedString.setCharAt(index, File.separatorChar);
  155.                   }
  156.                }
  157.  
  158.                String finalName = fixedString.toString();
  159.                int var14 = finalName.lastIndexOf(File.separatorChar);
  160.                if (var14 != -1) {
  161.                   String finalPath = finalName.substring(0, var14);
  162.                   File writePath = new File(finalPath);
  163.                   writePath.mkdirs();
  164.                }
  165.  
  166.                File writeFile = new File(finalName);
  167.                DataOutputStream writeLump = new DataOutputStream(new FileOutputStream(writeFile));
  168.                ((FilterOutputStream)writeLump).write(buffer);
  169.                ((FilterOutputStream)writeLump).close();
  170.             }
  171.          }
  172.  
  173.          log.close();
  174.       } catch (IOException var13) {
  175.          System.out.println(((Throwable)var13).toString());
  176.       }
  177.  
  178.    }
  179.  
  180.    protected void UnpackFile(String pattern) {
  181.       try {
  182.          this.directory = new DataInputStream(new FileInputStream(this.pakName));
  183.          this.directory.skipBytes(12 + (this.dirofs - 12));
  184.          this.pakList.clear();
  185.  
  186.          for(int i = 0; i < this.numLumps; ++i) {
  187.             this.directory.readFully(this.name);
  188.             this.filepos = this.intSwap(this.directory.readInt());
  189.             this.filelen = this.intSwap(this.directory.readInt());
  190.             this.nameString = new String(this.name, 0);
  191.             this.nameString = this.nameString.substring(0, this.nameString.indexOf(0));
  192.             if (this.patternMatch(pattern, this.nameString)) {
  193.                this.pakList.addItem(this.nameString);
  194.             }
  195.          }
  196.       } catch (IOException var4) {
  197.          System.err.println("Unexpected Exception");
  198.          ((Throwable)var4).printStackTrace();
  199.       }
  200.  
  201.    }
  202.  
  203.    public void GetPakFile() {
  204.       this.pakFileDialog.show();
  205.       this.pakName = this.pakFileDialog.getDirectory() + this.pakFileDialog.getFile();
  206.       this.ReadPakFile();
  207.    }
  208.  
  209.    public boolean action(Event e, Object o) {
  210.       if (e.target instanceof Button) {
  211.          System.out.println("Object is: " + o.toString());
  212.          if (o.toString().equals("List")) {
  213.             this.pattern = this.GetPattern();
  214.             System.out.println("The pattern is: " + this.pattern);
  215.             this.UnpackFile(this.pattern);
  216.             this.finished = false;
  217.          } else if (o.toString().equals("Unpack")) {
  218.             try {
  219.                this.pakListSelections = this.pakList.getSelectedItems();
  220.                this.WriteFiles();
  221.                this.finished = true;
  222.             } catch (Exception var5) {
  223.                ((Throwable)var5).printStackTrace();
  224.                return false;
  225.             }
  226.          }
  227.  
  228.          return true;
  229.       } else {
  230.          return false;
  231.       }
  232.    }
  233.  
  234.    protected void ReadPakFile() {
  235.       try {
  236.          this.directory = new DataInputStream(new FileInputStream(this.pakName));
  237.          this.readLump = new RandomAccessFile(this.pakName, "r");
  238.          this.ident = this.intSwap(this.directory.readInt());
  239.          this.dirofs = this.intSwap(this.directory.readInt());
  240.          this.dirlen = this.intSwap(this.directory.readInt());
  241.          if (this.ident != 1262698832) {
  242.             System.out.println(this.pakName + " is not a pakfile.");
  243.             System.exit(1);
  244.          }
  245.  
  246.          this.directory.skipBytes(this.dirofs - 12);
  247.          this.numLumps = this.dirlen / 64;
  248.          this.pakListSelections = new String[this.numLumps];
  249.          System.out.println(this.numLumps + " lumps in " + this.pakName);
  250.       } catch (IOException var3) {
  251.          System.out.println("Invalid IO operation");
  252.          ((Throwable)var3).printStackTrace();
  253.       }
  254.  
  255.    }
  256.  
  257.    protected boolean Selected(String item) {
  258.       for(int i = 0; i < this.pakListSelections.length; ++i) {
  259.          if (this.pakListSelections[i].equals(item)) {
  260.             return true;
  261.          }
  262.       }
  263.  
  264.       return false;
  265.    }
  266. }
  267.