Guest User

Tetris DX RLE encode/decode example (Program.cs)

a guest
May 6th, 2020
129
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Program.cs - Tetris DX RLE encode/decode (example) - Ed Kearney
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6.  
  7. namespace TetrisDx_RLE_encode_decode
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             RLE rle = new RLE(); // create a reference to the RLE class
  14.  
  15.             bool encoder = true; // if true encode data, if false decode it
  16.             bool useRom = true; // if encoding, setting this to true will write data to the output file at a particular point
  17.                                // (i.e. write the graphics data directly in to the ROM file)
  18.  
  19.             if (!encoder)
  20.             {
  21.                 //decode graphics data
  22.  
  23.                 string path1 = "D:\\Emulation\\GB\\Working\\tetris_compressedtiledata_new.gbc"; // input file with encoded data
  24.                 string path2 = "D:\\Emulation\\GB\\Working\\tetris_isitgraphics.gbc"; // output file for decoded graphics
  25.                 byte[] data = File.ReadAllBytes(path1); // read input data
  26.                 byte[] newData = rle.RLEDecodeGraphics(data, 4); // decode graphics data (do 4 blocks worth)
  27.                 File.WriteAllBytes(path2, newData); // write decoded data
  28.             }
  29.             else
  30.             {
  31.                 //encode graphics data
  32.  
  33.                 string path1 = "D:\\Emulation\\GB\\Working\\tetris_rawtiledata_new2.gbc"; // input file with raw/decoded graphics
  34.                 string path2 = "D:\\Emulation\\GB\\Working\\tetris_compressedtiledata.gbc"; // output file for encoded graphics
  35.  
  36.                 // the following refers to the graphics data for Marathon mode (in color mode on the GBC)
  37.  
  38.                 int blockSize = 0x1800; // the length of a "block" of data - this is equivalent to 1 bank of VRAM in Tetris DX
  39.                 int packedSize = 0x09FC; // the length of the data that we want to replace
  40.                 int blockCount = 1; // the number of blocks of data to encode
  41.                 int dataOffset = 0x8179; // the offset at which to write the data (for writing to the ROM file)
  42.  
  43.                 byte[] data = File.ReadAllBytes(path1); //read input data
  44.  
  45.                 byte[] newData = rle.RLEEncodeGraphics(data, blockSize, packedSize, blockCount, true); // encode graphics (see below)
  46.                 // To replace a section of encoded data in the ROM, it must be no bigger than the existing data, providing a target size and turning on squeeze mode will make a best effort to do this.
  47.                 // Data that is too large will not be written to a ROM file (if using that mode)
  48.                 // Data that is shorter than the target will be padded with 0x00
  49.                 // blockSize is usually required for the encoding to work (you don't always need it to decode)
  50.                 // blockCount can be used to only use a smaller amount of the input data
  51.  
  52.                 if (useRom) // save the ROM
  53.                 {
  54.                     if (newData.Length == packedSize) // make sure the new data is the correct length before saving (and don't save if it is the wrong size)
  55.                     {
  56.                         path2 = "D:\\Emulation\\GB\\Working\\Tetris DX (JU) [C][!] - palettes2 - Copy.gbc"; // change the output file to the correct ROM file (me being lazy)
  57.  
  58.                         byte[] originalData = File.ReadAllBytes(path2); // read in the ROM file
  59.                         List<byte> newRom = new List<byte>(originalData).GetRange(0, dataOffset); // get the original data before the changed section
  60.                         newRom.AddRange(new List<byte>(newData)); // get the changed section
  61.                         newRom.AddRange(new List<byte>(originalData).GetRange(dataOffset + packedSize, originalData.Length - dataOffset - packedSize)); // get the original data after the changed section
  62.  
  63.                         File.WriteAllBytes(path2, newRom.ToArray()); // save the changes to the ROM file
  64.  
  65.                         Console.WriteLine("ROM file written too (no corruption will occur).");
  66.                     }
  67.                     else
  68.                         Console.WriteLine("ROM file not written too (as corruption would occur).");
  69.  
  70.  
  71.                 }
  72.                 else // save the "new" binary file
  73.                 {
  74.                     File.WriteAllBytes(path2, newData); // save the data to the output file
  75.                 }
  76.                
  77.             }
  78.  
  79.             //Console.ReadKey(); // uncomment to keep the command line window open after the program "finishes"
  80.  
  81.         }
  82.     }
  83. }
RAW Paste Data Copied