Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Program.cs - Tetris DX RLE encode/decode (example) - Ed Kearney
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace TetrisDx_RLE_encode_decode
- {
- class Program
- {
- static void Main(string[] args)
- {
- RLE rle = new RLE(); // create a reference to the RLE class
- bool encoder = true; // if true encode data, if false decode it
- bool useRom = true; // if encoding, setting this to true will write data to the output file at a particular point
- // (i.e. write the graphics data directly in to the ROM file)
- if (!encoder)
- {
- //decode graphics data
- string path1 = "D:\\Emulation\\GB\\Working\\tetris_compressedtiledata_new.gbc"; // input file with encoded data
- string path2 = "D:\\Emulation\\GB\\Working\\tetris_isitgraphics.gbc"; // output file for decoded graphics
- byte[] data = File.ReadAllBytes(path1); // read input data
- byte[] newData = rle.RLEDecodeGraphics(data, 4); // decode graphics data (do 4 blocks worth)
- File.WriteAllBytes(path2, newData); // write decoded data
- }
- else
- {
- //encode graphics data
- string path1 = "D:\\Emulation\\GB\\Working\\tetris_rawtiledata_new2.gbc"; // input file with raw/decoded graphics
- string path2 = "D:\\Emulation\\GB\\Working\\tetris_compressedtiledata.gbc"; // output file for encoded graphics
- // the following refers to the graphics data for Marathon mode (in color mode on the GBC)
- int blockSize = 0x1800; // the length of a "block" of data - this is equivalent to 1 bank of VRAM in Tetris DX
- int packedSize = 0x09FC; // the length of the data that we want to replace
- int blockCount = 1; // the number of blocks of data to encode
- int dataOffset = 0x8179; // the offset at which to write the data (for writing to the ROM file)
- byte[] data = File.ReadAllBytes(path1); //read input data
- byte[] newData = rle.RLEEncodeGraphics(data, blockSize, packedSize, blockCount, true); // encode graphics (see below)
- // 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.
- // Data that is too large will not be written to a ROM file (if using that mode)
- // Data that is shorter than the target will be padded with 0x00
- // blockSize is usually required for the encoding to work (you don't always need it to decode)
- // blockCount can be used to only use a smaller amount of the input data
- if (useRom) // save the ROM
- {
- 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)
- {
- path2 = "D:\\Emulation\\GB\\Working\\Tetris DX (JU) [C][!] - palettes2 - Copy.gbc"; // change the output file to the correct ROM file (me being lazy)
- byte[] originalData = File.ReadAllBytes(path2); // read in the ROM file
- List<byte> newRom = new List<byte>(originalData).GetRange(0, dataOffset); // get the original data before the changed section
- newRom.AddRange(new List<byte>(newData)); // get the changed section
- newRom.AddRange(new List<byte>(originalData).GetRange(dataOffset + packedSize, originalData.Length - dataOffset - packedSize)); // get the original data after the changed section
- File.WriteAllBytes(path2, newRom.ToArray()); // save the changes to the ROM file
- Console.WriteLine("ROM file written too (no corruption will occur).");
- }
- else
- Console.WriteLine("ROM file not written too (as corruption would occur).");
- }
- else // save the "new" binary file
- {
- File.WriteAllBytes(path2, newData); // save the data to the output file
- }
- }
- //Console.ReadKey(); // uncomment to keep the command line window open after the program "finishes"
- }
- }
- }
Add Comment
Please, Sign In to add comment