Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sigmatauproductions.sc2k;
- import java.io.IOException;
- import java.util.List;
- import java.util.ArrayList;
- public class IFFChunk {
- private String chunkType = null;
- private int size = 0;
- private byte[] contents = null;
- public IFFChunk(String type, int size, byte[] contents) throws IOException {
- chunkType = type;
- this.size = size;
- this.contents = contents;
- }
- public String getType() {
- return chunkType;
- }
- public int getSize() {
- return size;
- }
- public byte[] getContents() {
- return contents;
- }
- public void decompress() {
- List<Byte> buffer = new ArrayList<Byte>();
- int counter = 0; // debug purposes
- System.out.println(contents.length + " bytes at start of decompression operation");
- // Begin debug block
- System.out.println("Creating dump of original active block");
- int bytesRemaining = contents.length;
- System.out.print("0: ");
- while (bytesRemaining > 0) {
- int startingIndex = contents.length-bytesRemaining;
- byte firstByte = contents[startingIndex];
- int firstByteAsInt;
- if (firstByte < 0) {
- firstByteAsInt = (255+firstByte);
- } else {
- firstByteAsInt = firstByte;
- }
- System.out.print(firstByteAsInt + " ");
- if (firstByteAsInt < 9) { System.out.print(" "); }
- if (firstByteAsInt < 99) { System.out.print(" "); }
- if (counter % 16 == 0 && counter != 0) { System.out.print("\n"+(counter+1)+": "); }
- bytesRemaining--;
- counter++; // debug purposes
- }
- System.out.println("End of dump.");
- // End debug block
- bytesRemaining = contents.length;
- while (bytesRemaining > 0) {
- int startingIndex = contents.length-bytesRemaining;
- byte firstByte = contents[startingIndex];
- byte[] extracted = null;
- // SimCity stores its values as unsigned shorts.
- // Java doesn't have unsigned integers, so we have to account for
- // that. Yay for Java being shit as usual! I need uint8 :(
- int firstUnsigned;
- if (firstByte < 0) {
- firstUnsigned = (255+firstByte);
- } else {
- firstUnsigned = firstByte;
- }
- System.out.println("Starting index is " + startingIndex); // debug
- if (firstUnsigned <= 127) {
- System.out.print("Detected normal subblock " + firstUnsigned
- + " bytes in size at position "+startingIndex+"\n{ "); // debug
- extracted = GenericIFF.extractSegment(contents, startingIndex+1, startingIndex+(firstUnsigned));
- for (byte b : extracted) {
- buffer.add(b);
- System.out.print(b + " "); // debug
- }
- System.out.println("}"); //debug
- bytesRemaining -= (extracted.length+1);
- } else {
- int repetitions = firstUnsigned-127;
- System.out.println("Detected compressed subblock of " + repetitions
- + " repetitions."); // debug
- byte source = contents[startingIndex+1];
- extracted = expandData(repetitions, source);
- System.out.print("{ ");
- for (byte b : extracted) {
- buffer.add(b);
- System.out.print(b + " ");
- }
- System.out.println("}"); // debug
- bytesRemaining -= 2;
- }
- System.out.println(buffer.size() + " bytes total in result");
- System.out.println(bytesRemaining + " bytes remain in source");
- System.out.println("-------");
- counter++; // debug purposes
- }
- System.out.println();
- System.out.println();
- System.out.println(buffer.size() + " bytes at end of decompression operation");
- }
- protected byte[] expandData(int repetitions, byte data) {
- if (repetitions <= 0) { return null; }
- byte[] repeated = new byte[repetitions];
- for (int i = 0; i < repeated.length; i++) {
- repeated[i] = data;
- }
- return repeated;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement