import java.io.*; import java.util.zip.*; import java.math.*; class Repack2 { public static byte[] rawData; public static void main ( String args[] ) throws IOException { if ( args.length != 2 && args.length != 3 && args.length != 1) { //System.out.print("\n Repack 2.0 by CplFilth\n"); //System.out.print("\n Usage : repack TxtFileIn AdbFileOut"); //System.out.print("\n For example : repack elements.txt elements.adb\n"); System.out.print("\n Repack v. 2.02 © CplFilth\n\n Usage : \n 1. Repack txtFileIn\n For example : Repack elements.txt\n Outputs to the correspondingly named .adb file \n\n 2. Repack txtFile adbFile\n For example : Repack c:\\temp\\elements.txt e:\\elements.adb\n\n"); System.exit(0); } String copyright = new String("Copyright (c) 1999 Atomic Games, Inc."); int index=0; String cha=new String("."); File txtFile = new File(args[0]); String txtFileString = txtFile.getName(); String adbFileString = ""; while ( cha.compareTo(txtFileString.substring(index,index+1)) != 0) { adbFileString = adbFileString + txtFileString.substring(index,index+1); index++; } File adbFile; if ( args.length > 1 ) { adbFile = new File(args[1]); } else { adbFile = new File(adbFileString + ".adb"); } DataInputStream read = new DataInputStream(new FileInputStream(txtFile)); int txtLength = read.available(); rawData = new byte[txtLength]; read.readFully(rawData); read.close(); int fileType=0; if ( args.length == 3 ) fileType = Integer.parseInt(args[2]); else { if ( txtLength % 156 == 0 && test(1) == true ) fileType = 1; if ( ( ( txtLength % 108 == 0 && test(2) == true ) || ( txtLength % 124 == 0 && test(2) == true ) ) ) fileType = 2; if ( (txtLength % 380 == 0 && test(3) == true ) || ( txtLength % 384 == 0 && test(3) == true ) ) fileType = 3; if ( ( txtLength % 452 == 0 && test(4) == true ) || ( txtLength % 468 == 0 && test(4) == true)) if (fileType != 1 ) fileType = 4; if ( txtLength % 424 == 0 && test(5) == true ) fileType = 5; if ( txtLength % 168 == 0 && test(6) == true ) fileType = 6; if ( txtLength % 260 == 0 && test(7) == true ) fileType = 7; } //System.out.println(fileType); if ( fileType == 0 ) { System.out.print("\n Could not determine file type.\n Possible cause : Filesize incorrect"); System.exit(0); } ByteArrayInputStream in = new ByteArrayInputStream(rawData); ByteArrayOutputStream compStream = new ByteArrayOutputStream(); ZipOutputStream out = new ZipOutputStream(compStream); // Add ZIP entry to output stream. int entryLength=0; try { ZipEntry aZipEntry = new ZipEntry(txtFileString); out.putNextEntry(aZipEntry); byte[] bu = new byte[1024]; int len; while ((len = in.read(bu)) != -1 ) { out.write(bu,0,len); } out.closeEntry(); entryLength = (int) aZipEntry.getCompressedSize(); out.close(); in.close(); } catch (IOException e) { } byte[] zipData = new byte[compStream.size()]; zipData = compStream.toByteArray(); //in.close(); //out.close(); compStream.close(); DataInputStream compIn = new DataInputStream(new ByteArrayInputStream(zipData)); byte[] compData = new byte[entryLength]; compIn.skipBytes(30+CountChars(txtFileString)); compIn.readFully(compData); compIn.close(); DataInputStream adlerIn = new DataInputStream(new ByteArrayInputStream(rawData)); Adler32 crcValue ; crcValue = new Adler32(); int ind; for ( ind = 0; ind < rawData.length ; ind++ ) { crcValue.update(adlerIn.read()); } long checksum=0; checksum = crcValue.getValue(); adlerIn.close(); RandomAccessFile adbOut = new RandomAccessFile(adbFile,"rw"); adbOut.writeBytes(copyright); adbOut.write(0); int entries=0; if ( fileType == 1 ) { entries = txtLength / 156;adbOut.writeShort(shortIntelToMac(entries)); } // elements # 2 bytes if ( fileType == 2 & txtLength % 108 == 0 ) { entries = txtLength / 108;adbOut.write((entries));} // soldiers # 4 bytes if ( fileType == 2 & txtLength % 124 == 0 ) { entries = txtLength / 124;adbOut.write((entries));} // soldiers # 4 bytes if ( fileType == 3 & txtLength % 380 == 0 ) { entries = txtLength / 380;adbOut.write(entries);} // weapons # 1 byte if ( fileType == 3 & txtLength % 384 == 0 ) { entries = txtLength / 384;adbOut.writeInt(intelToMac(entries));} // weapons # 4 bytes if ( fileType == 4 & txtLength % 452 == 0) { entries = txtLength / 452;adbOut.write(entries);} // vehicles # 1 byte if ( fileType == 4 & txtLength % 468 == 0) { entries = txtLength / 468;adbOut.write(entries);} // vehicles # 1 byte if ( fileType == 5 ) { entries = txtLength / 424;adbOut.writeInt( intelToMac(rawData[rawData.length-424]+1));adbOut.writeInt(intelToMac(entries));} // fpools # 4 bytes if ( fileType == 6 ) { entries = txtLength / 168;adbOut.writeInt(intelToMac(entries));} // bgroups # 4 bytes if ( fileType == 7 ) { entries = txtLength / 260;adbOut.writeShort(shortIntelToMac(entries));} // teams # 2 bytes adbOut.writeInt(intelToMac(entryLength+6)); adbOut.write(0x78);adbOut.write(0x9C); adbOut.write(compData); adbOut.writeInt(((int) checksum)); if (fileType==7) { adbOut.write(38);adbOut.write(0);adbOut.write(0);adbOut.write(0); adbOut.write(120);adbOut.write(156);adbOut.write(237);adbOut.write(193); adbOut.write(49);adbOut.write(1);adbOut.write(0);adbOut.write(0); adbOut.write(0);adbOut.write(194);adbOut.write(160);adbOut.write(245); adbOut.write(79);adbOut.write(109);adbOut.write(9);adbOut.write(79); adbOut.write(160);adbOut.write(0);adbOut.write(0);adbOut.write(0); adbOut.write(0);adbOut.write(0);adbOut.write(0);adbOut.write(0); adbOut.write(0);adbOut.write(0);adbOut.write(0);adbOut.write(0); adbOut.write(0);adbOut.write(0);adbOut.write(0);adbOut.write(0); adbOut.write(110);adbOut.write(6);adbOut.write(60);adbOut.write(195); adbOut.write(0);adbOut.write(1); } adbOut.writeBytes(";)"); adbOut.close(); } public static int CountChars(String stringin) throws IOException { String stringhere = stringin; int count=0; int tempo; StringReader thisString = new StringReader(stringhere); while ( (tempo = thisString.read() ) != -1 ) { count++; } thisString.close(); return(count); } public static boolean test(int type) { boolean test=true; int dataL = rawData.length; int i; if ( type == 1 ) { if ( dataL % 108 == 0 || dataL % 124 == 0 ) if ( test(2) == true) return(false); if ( dataL % 380 == 0 || dataL % 384 == 0 ) if ( test(3) == true) return(false); if ( dataL % 452 == 0 || dataL % 468 == 0 ) if ( test(4) == true) return(false); //if ( test(5) == true ) return(false); if ( test(6) == true ) return(false); if ( test(7) == true ) return(false); } if ( type == 2 ) { for ( i = 0 ; i < 5 ; i++ ) { if ( dataL % 108 == 0 && rawData[i*108] != i ) return(false); if ( dataL % 124 == 0 && rawData[i*124] != i ) return(false); } } if ( type == 3 ) { for ( i = 0 ; i < 5 ; i++ ) { if ( ( dataL % 380 == 0 ) && rawData[i*380+20] != i ) return(false); if ( ( dataL % 384 == 0 ) && rawData[i*384+20] != i ) return(false); } } if ( type == 4 ) { for ( i = 0 ; i < 5 ; i++ ) { if ( dataL % 452 == 0 && rawData[i*452+37] != i ) return(false); if ( dataL % 468 == 0 && rawData[i*468+37] != i ) return(false); } } if ( type == 5 ) { for ( i = 0 ; i < 5 ; i++ ) { if ( dataL % 53000 == 0 ) if ( dataL > i*53000) { if ( rawData[i*53000] != i ) return(false);} else return(false); if ( dataL % 33920 == 0 ) if ( dataL > i*33920) { if ( rawData[i*33920] != i ) return(false);} else return(false); } } if ( type == 6 ) { for ( i = 0 ; i < 5 ; i++ ) { if ( rawData[i*168] != i ) return(false); } } if ( type == 7 ) { for ( i = 0 ; i < 5 ; i++ ) { if ( rawData[i*260+100] != i ) return(false); } } return(test); } public static int intelToMac(int i) { int byte0 = i & 0xff; int byte1 = (i>>8) & 0xff; int byte2 = (i>>16) & 0xff; int byte3 = (i>>24) & 0xff; // swap the byte order return (byte0<<24) | (byte1<<16) | (byte2<<8) | byte3; } public static int shortIntelToMac(int i) { int byte0 = i & 0xff; int byte1 = (i>>8) & 0xff; // swap the byte order return (byte0<<8) | byte1 ; } }