SegaChief

Untitled

Jan 30th, 2023
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. public class Kernel2TextCompressor
  2. {
  3. // So we send a byte array of each section to be decompressed then send it back; do this for each one.
  4. // Need to adjust the parts where it expects 9 sections for each 'file' as we will only be converting
  5. // one section at a time. Also need to adapt the headers and any other special field-specific logic.
  6. public static void Kernel2Decompress(byte[] input)
  7. {
  8. var ms = new MemoryStream();
  9. var uncompress = new MemoryStream(input);
  10. uncompress.Position = 4; // Starts at 4th byte to skip header - Kernel2 is one giant LZS file with a single 4byte header giving length
  11. Lzs.Decode(uncompress, ms); // All the data is now decoded (do we need to shave off the header first?)
  12. System.Diagnostics.Debug.WriteLine("FF:Kernel2Decompress:LZS expanded {0} bytes to {1} bytes", input.Length,
  13. ms.Length);
  14.  
  15. string kernel2StringsOutput = Directory.GetCurrentDirectory() + "\\Kernel2 Strings\\";
  16. byte[] uncompressedKernel2 = new byte[ms.Length];
  17. int uncompressedSize;
  18. byte[] header = new byte[4];
  19. int headerOffset = 0;
  20. int r = 9;
  21.  
  22. ms.Position = 0;
  23. ms.Read(uncompressedKernel2, 0, uncompressedKernel2.Length);
  24.  
  25. while (r < 27)
  26. {
  27. if (r == 25)
  28. {
  29.  
  30. }
  31.  
  32. using (var outputStream = File.Create(kernel2StringsOutput + "kernel2.bin" + r))
  33. {
  34. // Writes the kernel2 section using header to get length
  35. outputStream.Seek(0, SeekOrigin.Begin);
  36.  
  37. header[0] = uncompressedKernel2[headerOffset];
  38. header[1] = uncompressedKernel2[headerOffset + 1];
  39. header[2] = uncompressedKernel2[headerOffset + 2];
  40. header[3] = uncompressedKernel2[headerOffset + 3];
  41.  
  42. // Section 26 can't have the 4 bytes sheared off the end; one of life's mysteries
  43. if (r == 27)
  44. {
  45. uncompressedSize = EndianConvert.GetLittleEndianInt(header, 0);
  46. outputStream.Write(uncompressedKernel2, headerOffset + 4, uncompressedSize);
  47. }
  48. else
  49. {
  50. uncompressedSize = EndianConvert.GetLittleEndianInt(header, 0);
  51. outputStream.Write(uncompressedKernel2, headerOffset + 4, uncompressedSize);
  52. }
  53.  
  54. headerOffset += uncompressedSize + 4;
  55. }
  56.  
  57. r++;
  58. }
  59. }
  60.  
  61. public static void Kernel2Recompress(byte[] input)
  62. {
  63. // Re-encodes the data as LZSS format
  64. var ms = new MemoryStream(input);
  65. var compress = new MemoryStream();
  66. ms.Position = 0;
  67. Lzs.Encode(ms, compress);
  68. System.Diagnostics.Debug.WriteLine("FF:Kernel2Decompress:LZS shrank {0} bytes to {1} bytes", input.Length,
  69. compress.Length);
  70.  
  71. // Container for our compressed data; adds 4 to include header space
  72. byte[] data = new byte[compress.Length + 4];
  73.  
  74. // Produces the header (4 bytes)
  75. byte[] header = BitConverter.GetBytes(data.Length - 4);
  76. data[0] = header[0];
  77. data[1] = header[1];
  78. data[2] = header[2];
  79. data[3] = header[3];
  80.  
  81. // Writes the encoded data to a byte array
  82. compress.Position = 0;
  83. compress.Read(data, 4, (int)compress.Length);
  84.  
  85. // Produces the finished file
  86. string produceFile = Directory.GetCurrentDirectory() + "\\Output Files\\kernel2.bin";
  87. using (var outputStream = File.Create(produceFile))
  88. {
  89. outputStream.Position = 0;
  90. outputStream.Write(data, 0, data.Length);
  91. }
  92. }
  93.  
  94. // To use the LZS Decompress on random misc files; unrelated to randomiser
  95. public static void MiscDecompress(byte[] input)
  96. {
  97. var ms = new MemoryStream();
  98. var uncompress = new MemoryStream(input);
  99. uncompress.Position = 4; // Starts at 4th byte to skip header
  100. Lzs.Decode(uncompress, ms); // All the data is now decoded
  101. System.Diagnostics.Debug.WriteLine("FF:MiscFile:LZS expanded {0} bytes to {1} bytes", input.Length,
  102. ms.Length);
  103.  
  104. byte[] timHeader =
  105. {
  106. 0x10, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x10, 0x00,
  107. 0xEF, 0x01, 0x10, 0x00, 0x01, 0x00, 0xFF, 0x7F, 0xF8, 0x0D, 0xD7, 0x4A, 0x74, 0x3E,
  108. 0x73, 0x15, 0xF0, 0x35, 0xEE, 0x00, 0x8D, 0x29, 0x6B, 0x49, 0xE9, 0x18, 0x67, 0x08,
  109. 0xE5, 0x2C, 0x45, 0x00, 0x82, 0x20, 0x21, 0x04, 0x00, 0x00, 0x0C, 0x03, 0x00, 0x00,
  110. 0xF8, 0x03, 0x50, 0x00, 0x08, 0x00, 0x30, 0x00
  111. };
  112. int r = 0;
  113. while (r < 127)
  114. {
  115. string miscFileOutput = Directory.GetCurrentDirectory() + "\\MiscOutput\\image" + r;
  116. byte[] uncompressedMiscFile = new byte[ms.Length];
  117.  
  118. ms.Position = 16;
  119. ms.Read(uncompressedMiscFile, 0, 512);
  120.  
  121. using (var outputStream = File.Create(miscFileOutput))
  122. {
  123. outputStream.Seek(0, SeekOrigin.Begin);
  124. outputStream.Write(timHeader, 0, timHeader.Length);
  125. outputStream.Write(uncompressedMiscFile, 0, 512);
  126. }
  127. r++;
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment