Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Runtime.InteropServices.ComTypes;
- namespace ptcgrpconv {
- class MainClass {
- public static void Main(string[] args) {
- if(!(args.Length == 2 || args.Length == 4)) {
- Console.WriteLine("bad arg count");
- return;
- }
- //check for palette
- string palfile;
- var palette = new ushort[256];
- var idx = Array.IndexOf(args, "-palette");
- if(idx > -1)
- palfile = args[idx + 1];
- else
- palfile = "DEFAULT.COL";
- var grpfile = args[args.Length - 2];
- var outfile = args[args.Length - 1];
- var grpbytes = new byte[49152];
- //load palette file
- try {
- using(var fs = new FileStream(palfile, FileMode.Open, FileAccess.Read)) {
- if(!VerifyFile(fs, "PETC0100RCOL"))
- throw new Exception("Palette file didn't pass checks");
- //begin the beating
- fs.Seek(50, SeekOrigin.Begin);
- var col = new byte[2];
- for(short i = 1; i < 256; i++) {
- fs.Read(col, 0, 2);
- palette[i] = MakeSBColor((ushort)(col[0] << 8 | col[1]));
- }
- }
- } catch(Exception ex) {
- Console.WriteLine(
- @"Encountered {0}
- -{1}
- Stack Trace:
- {2}",
- ex.GetType().ToString(), ex.Message, ex.StackTrace);
- }
- //load grp
- try {
- using(var fs = new FileStream(grpfile, FileMode.Open, FileAccess.Read)) {
- if(!VerifyFile(fs, "PETC0100RGRP"))
- throw new Exception("GRP file didn't pass checks");
- //grab all the GRP bytes into a linear array
- //format is chunked and awful eughhh
- for(int j = 0; j < 3; j++) {
- for(int i = 0; i < 4; i++) {
- for(int y = 0; y < 8; y++) {
- for(int x = 0; x < 8; x++) {
- for(int yy = 0; yy < 8; yy++) {
- for(int xx = 0; xx < 8; xx++) {
- fs.Seek((j * 16384) + (i * 4096) + (y * 512) + (x * 64) + (yy * 8) + xx + 48, SeekOrigin.Begin);
- grpbytes[(j * 16384) + (i * 64) + (y * 2048) + (x * 8) + (yy * 256) + xx] = (byte)fs.ReadByte();
- }
- }
- }
- }
- }
- }
- }
- //end it
- using(var fs = new FileStream(outfile, FileMode.Create, FileAccess.Write)) {
- var blank = new byte[2];
- //write the header in here
- using(var head = new FileStream("HEADER.BIN", FileMode.Open, FileAccess.Read)) {
- head.CopyTo(fs);
- }
- //do the GRP
- var b = new byte[2];
- for(int y = 0; y < 512; y++) {
- for(int x = 0; x < 512; x++) {
- if(y > 191 || x > 255 || grpbytes[y * 256 + x] == 0) {
- fs.Write(blank, 0, 2);
- } else {
- b = ToLittleEndianBytes(palette[grpbytes[y * 256 + x]]);
- fs.Write(b ,0 ,2);
- }
- }
- }
- //write the footer in here
- using(var foot = new FileStream("FOOTER.BIN", FileMode.Open, FileAccess.Read)) {
- foot.CopyTo(fs);
- }
- }
- } catch(Exception ex) {
- Console.WriteLine(
- @"Encountered {0}
- -{1}
- Stack Trace:
- {2}",
- ex.GetType().ToString(), ex.Message, ex.StackTrace);
- }
- }
- static ushort MakeSBColor(ushort ptcColor) {
- ushort color = 1;
- var red = (ushort)(ptcColor >> 8 & 0x1F);
- var green = (ushort)(((ptcColor & 3) << 4) | ((ptcColor & 0x80) >> 7) | (((ptcColor & 0xE000) >> 12)) & 0xE);
- var blue = (ushort)(ptcColor >> 2 & 0x1F);
- //discard the low green bit
- //maybe we'll use you someday
- green >>= 1;
- //prep the color code and return
- color |= (ushort)((red << 11) | (blue << 6) | (green << 1));
- return color;
- }
- static bool VerifyFile(FileStream fs, string identifier) {
- //verify file type
- var tag = new byte[12];
- fs.Read(tag, 0, 4);
- if(Encoding.ASCII.GetString(tag, 0, 4) != "PX01")
- return false;
- fs.Seek(36, SeekOrigin.Begin);
- fs.Read(tag, 0, 12);
- if(Encoding.ASCII.GetString(tag) != identifier)
- return false;
- return true;
- }
- static byte[] ToLittleEndianBytes(ushort i) {
- var output = new byte[2];
- output[0] = (byte)(i & 0xFF);
- output[1] = (byte)(i >> 8 & 0xFF);
- return output;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement