Advertisement
Guest User

Untitled

a guest
Dec 24th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. package com.sigmatauproductions.sc2k;
  2.  
  3. import java.io.IOException;
  4. import java.util.List;
  5. import java.util.ArrayList;
  6.  
  7. public class IFFChunk {
  8.  
  9. private String chunkType = null;
  10. private int size = 0;
  11. private byte[] contents = null;
  12.  
  13. public IFFChunk(String type, int size, byte[] contents) throws IOException {
  14. chunkType = type;
  15. this.size = size;
  16. this.contents = contents;
  17. }
  18.  
  19. public String getType() {
  20. return chunkType;
  21. }
  22.  
  23. public int getSize() {
  24. return size;
  25. }
  26.  
  27. public byte[] getContents() {
  28. return contents;
  29. }
  30.  
  31. public void decompress() {
  32. List<Byte> buffer = new ArrayList<Byte>();
  33.  
  34. int counter = 0; // debug purposes
  35. System.out.println(contents.length + " bytes at start of decompression operation");
  36.  
  37. // Begin debug block
  38. System.out.println("Creating dump of original active block");
  39. int bytesRemaining = contents.length;
  40. System.out.print("0: ");
  41. while (bytesRemaining > 0) {
  42. int startingIndex = contents.length-bytesRemaining;
  43. byte firstByte = contents[startingIndex];
  44. int firstByteAsInt;
  45. if (firstByte < 0) {
  46. firstByteAsInt = (255+firstByte);
  47. } else {
  48. firstByteAsInt = firstByte;
  49. }
  50. System.out.print(firstByteAsInt + " ");
  51. if (firstByteAsInt < 9) { System.out.print(" "); }
  52. if (firstByteAsInt < 99) { System.out.print(" "); }
  53. if (counter % 16 == 0 && counter != 0) { System.out.print("\n"+(counter+1)+": "); }
  54. bytesRemaining--;
  55. counter++; // debug purposes
  56. }
  57. System.out.println("End of dump.");
  58. // End debug block
  59.  
  60. bytesRemaining = contents.length;
  61. while (bytesRemaining > 0) {
  62. int startingIndex = contents.length-bytesRemaining;
  63. byte firstByte = contents[startingIndex];
  64. byte[] extracted = null;
  65.  
  66. // SimCity stores its values as unsigned shorts.
  67. // Java doesn't have unsigned integers, so we have to account for
  68. // that. Yay for Java being shit as usual! I need uint8 :(
  69. int firstUnsigned;
  70. if (firstByte < 0) {
  71. firstUnsigned = (255+firstByte);
  72. } else {
  73. firstUnsigned = firstByte;
  74. }
  75.  
  76. System.out.println("Starting index is " + startingIndex); // debug
  77. if (firstUnsigned <= 127) {
  78. System.out.print("Detected normal subblock " + firstUnsigned
  79. + " bytes in size at position "+startingIndex+"\n{ "); // debug
  80. extracted = GenericIFF.extractSegment(contents, startingIndex+1, startingIndex+(firstUnsigned));
  81.  
  82. for (byte b : extracted) {
  83. buffer.add(b);
  84. System.out.print(b + " "); // debug
  85. }
  86.  
  87. System.out.println("}"); //debug
  88.  
  89. bytesRemaining -= (extracted.length+1);
  90. } else {
  91. int repetitions = firstUnsigned-127;
  92. System.out.println("Detected compressed subblock of " + repetitions
  93. + " repetitions."); // debug
  94. byte source = contents[startingIndex+1];
  95. extracted = expandData(repetitions, source);
  96.  
  97.  
  98.  
  99. System.out.print("{ ");
  100. for (byte b : extracted) {
  101. buffer.add(b);
  102. System.out.print(b + " ");
  103. }
  104. System.out.println("}"); // debug
  105.  
  106. bytesRemaining -= 2;
  107. }
  108.  
  109. System.out.println(buffer.size() + " bytes total in result");
  110. System.out.println(bytesRemaining + " bytes remain in source");
  111. System.out.println("-------");
  112.  
  113. counter++; // debug purposes
  114. }
  115.  
  116.  
  117.  
  118. System.out.println();
  119. System.out.println();
  120. System.out.println(buffer.size() + " bytes at end of decompression operation");
  121. }
  122.  
  123. protected byte[] expandData(int repetitions, byte data) {
  124. if (repetitions <= 0) { return null; }
  125. byte[] repeated = new byte[repetitions];
  126. for (int i = 0; i < repeated.length; i++) {
  127. repeated[i] = data;
  128. }
  129. return repeated;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement