home *** CD-ROM | disk | FTP | other *** search
- import java.awt.Button;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Event;
- import java.awt.FileDialog;
- import java.awt.FlowLayout;
- import java.awt.Frame;
- import java.awt.Label;
- import java.awt.List;
- import java.awt.Panel;
- import java.awt.TextField;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FilterOutputStream;
- import java.io.IOException;
- import java.io.RandomAccessFile;
-
- class Unpack extends Panel {
- int ident;
- int dirofs;
- int dirlen;
- int numLumps;
- byte[] name = new byte[56];
- String nameString;
- int filepos;
- int filelen;
- RandomAccessFile readLump;
- DataInputStream directory;
- String pakName;
- String pattern;
- FileDialog pakFileDialog;
- FileDialog outputFileDialog;
- TextField patternText;
- List pakList;
- String[] pakListSelections;
- Button unpackButton;
- Button listButton;
- boolean finished = false;
- protected static final int IDPAKHEADER = 1262698832;
-
- protected String GetPattern() {
- return this.patternText.getText().trim();
- }
-
- static void usage() {
- System.out.println("Usage: unpack <packfile> <match> <basedir>");
- System.out.println(" or: unpack -list <packfile>");
- System.out.println("<match> may contain a single * wildcard");
- System.exit(1);
- }
-
- public boolean isFinished() {
- return this.finished;
- }
-
- protected void SetOutputDirectory() {
- this.outputFileDialog.show();
- System.out.println("The output directory is: " + this.outputFileDialog.getDirectory());
- }
-
- public Unpack(Frame parent) {
- ((Component)this).resize(200, 200);
- this.pakFileDialog = new FileDialog(parent, "Pak File");
- this.pakFileDialog.setFile("*.pak");
- this.outputFileDialog = new FileDialog(parent, "Output File");
- this.outputFileDialog.setFile("output.log");
- this.patternText = new TextField("*", 20);
- ((Container)this).setLayout(new FlowLayout());
- this.pakList = new List(10, true);
- ((Container)this).add(this.pakList);
- ((Container)this).add(new Label("Pattern:"));
- ((Container)this).add(this.patternText);
- this.listButton = new Button("Unpack");
- this.unpackButton = new Button("List");
- ((Container)this).add(this.unpackButton);
- ((Container)this).add(this.listButton);
- }
-
- protected void CleanUp() {
- this.pakFileDialog.dispose();
- this.outputFileDialog.dispose();
-
- try {
- this.directory.close();
- this.readLump.close();
- } catch (IOException var3) {
- ((Throwable)var3).printStackTrace();
- }
-
- }
-
- protected boolean patternMatch(String pattern, String s) {
- if (pattern.equals(s)) {
- return true;
- } else {
- int index = pattern.indexOf(42);
- if (index == -1) {
- return false;
- } else if (!pattern.regionMatches(0, s, 0, index)) {
- return false;
- } else {
- ++index;
- int remaining = pattern.length() - index;
- if (s.length() < remaining) {
- return false;
- } else {
- return pattern.regionMatches(index, s, s.length() - remaining, remaining);
- }
- }
- }
- }
-
- protected int intSwap(int i) {
- int a = i & 255;
- int b = i >> 8 & 255;
- int c = i >> 16 & 255;
- int d = i >> 24 & 255;
- return (a << 24) + (b << 16) + (c << 8) + d;
- }
-
- public void WriteFiles() {
- try {
- this.directory = new DataInputStream(new FileInputStream(this.pakName));
- this.directory.skipBytes(12 + (this.dirofs - 12));
- String logName;
- if (this.outputFileDialog.getFile() == null) {
- logName = this.outputFileDialog.getDirectory() + "output.log";
- } else {
- logName = this.outputFileDialog.getDirectory() + this.outputFileDialog.getFile();
- }
-
- RandomAccessFile log = new RandomAccessFile(logName, "rw");
- log.writeBytes("FILES CREATED:\r\n");
-
- for(int i = 0; i < this.numLumps; ++i) {
- this.directory.readFully(this.name);
- this.filepos = this.intSwap(this.directory.readInt());
- this.filelen = this.intSwap(this.directory.readInt());
- this.nameString = new String(this.name, 0);
- this.nameString = this.nameString.substring(0, this.nameString.indexOf(0));
- if (this.Selected(this.nameString)) {
- byte[] buffer = new byte[this.filelen];
- log.writeBytes(this.nameString + "\r\n");
- System.out.println("Unpaking " + this.nameString + " " + this.filelen + " bytes");
- this.readLump.seek((long)this.filepos);
- this.readLump.readFully(buffer);
- StringBuffer fixedString = new StringBuffer(this.outputFileDialog.getDirectory() + this.nameString);
-
- for(int index = 0; index < fixedString.length(); ++index) {
- if (fixedString.charAt(index) == '/') {
- fixedString.setCharAt(index, File.separatorChar);
- }
- }
-
- String finalName = fixedString.toString();
- int var14 = finalName.lastIndexOf(File.separatorChar);
- if (var14 != -1) {
- String finalPath = finalName.substring(0, var14);
- File writePath = new File(finalPath);
- writePath.mkdirs();
- }
-
- File writeFile = new File(finalName);
- DataOutputStream writeLump = new DataOutputStream(new FileOutputStream(writeFile));
- ((FilterOutputStream)writeLump).write(buffer);
- ((FilterOutputStream)writeLump).close();
- }
- }
-
- log.close();
- } catch (IOException var13) {
- System.out.println(((Throwable)var13).toString());
- }
-
- }
-
- protected void UnpackFile(String pattern) {
- try {
- this.directory = new DataInputStream(new FileInputStream(this.pakName));
- this.directory.skipBytes(12 + (this.dirofs - 12));
- this.pakList.clear();
-
- for(int i = 0; i < this.numLumps; ++i) {
- this.directory.readFully(this.name);
- this.filepos = this.intSwap(this.directory.readInt());
- this.filelen = this.intSwap(this.directory.readInt());
- this.nameString = new String(this.name, 0);
- this.nameString = this.nameString.substring(0, this.nameString.indexOf(0));
- if (this.patternMatch(pattern, this.nameString)) {
- this.pakList.addItem(this.nameString);
- }
- }
- } catch (IOException var4) {
- System.err.println("Unexpected Exception");
- ((Throwable)var4).printStackTrace();
- }
-
- }
-
- public void GetPakFile() {
- this.pakFileDialog.show();
- this.pakName = this.pakFileDialog.getDirectory() + this.pakFileDialog.getFile();
- this.ReadPakFile();
- }
-
- public boolean action(Event e, Object o) {
- if (e.target instanceof Button) {
- System.out.println("Object is: " + o.toString());
- if (o.toString().equals("List")) {
- this.pattern = this.GetPattern();
- System.out.println("The pattern is: " + this.pattern);
- this.UnpackFile(this.pattern);
- this.finished = false;
- } else if (o.toString().equals("Unpack")) {
- try {
- this.pakListSelections = this.pakList.getSelectedItems();
- this.WriteFiles();
- this.finished = true;
- } catch (Exception var5) {
- ((Throwable)var5).printStackTrace();
- return false;
- }
- }
-
- return true;
- } else {
- return false;
- }
- }
-
- protected void ReadPakFile() {
- try {
- this.directory = new DataInputStream(new FileInputStream(this.pakName));
- this.readLump = new RandomAccessFile(this.pakName, "r");
- this.ident = this.intSwap(this.directory.readInt());
- this.dirofs = this.intSwap(this.directory.readInt());
- this.dirlen = this.intSwap(this.directory.readInt());
- if (this.ident != 1262698832) {
- System.out.println(this.pakName + " is not a pakfile.");
- System.exit(1);
- }
-
- this.directory.skipBytes(this.dirofs - 12);
- this.numLumps = this.dirlen / 64;
- this.pakListSelections = new String[this.numLumps];
- System.out.println(this.numLumps + " lumps in " + this.pakName);
- } catch (IOException var3) {
- System.out.println("Invalid IO operation");
- ((Throwable)var3).printStackTrace();
- }
-
- }
-
- protected boolean Selected(String item) {
- for(int i = 0; i < this.pakListSelections.length; ++i) {
- if (this.pakListSelections[i].equals(item)) {
- return true;
- }
- }
-
- return false;
- }
- }
-