Advertisement
TheFastFish

grpconv

Aug 5th, 2016
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.Runtime.InteropServices.ComTypes;
  6.  
  7. namespace ptcgrpconv {
  8.     class MainClass {
  9.         public static void Main(string[] args) {
  10.             if(!(args.Length == 2 || args.Length == 4)) {
  11.                 Console.WriteLine("bad arg count");
  12.                 return;
  13.             }
  14.  
  15.             //check for palette
  16.             string palfile;
  17.             var palette = new ushort[256];
  18.             var idx = Array.IndexOf(args, "-palette");
  19.             if(idx > -1)
  20.                 palfile = args[idx + 1];
  21.             else
  22.                 palfile = "DEFAULT.COL";
  23.  
  24.             var grpfile = args[args.Length - 2];
  25.             var outfile = args[args.Length - 1];
  26.             var grpbytes = new byte[49152];
  27.  
  28.             //load palette file
  29.             try {
  30.                 using(var fs = new FileStream(palfile, FileMode.Open, FileAccess.Read)) {
  31.                     if(!VerifyFile(fs, "PETC0100RCOL"))
  32.                         throw new Exception("Palette file didn't pass checks");
  33.                    
  34.                     //begin the beating
  35.                     fs.Seek(50, SeekOrigin.Begin);
  36.                     var col = new byte[2];
  37.                     for(short i = 1; i < 256; i++) {
  38.                         fs.Read(col, 0, 2);
  39.                         palette[i] = MakeSBColor((ushort)(col[0] << 8 | col[1]));
  40.                     }
  41.                 }
  42.             } catch(Exception ex) {
  43.                 Console.WriteLine(
  44. @"Encountered {0}
  45. -{1}
  46. Stack Trace:
  47. {2}",
  48.                     ex.GetType().ToString(), ex.Message, ex.StackTrace);
  49.             }
  50.  
  51.             //load grp
  52.             try {
  53.                 using(var fs = new FileStream(grpfile, FileMode.Open, FileAccess.Read)) {
  54.                     if(!VerifyFile(fs, "PETC0100RGRP"))
  55.                         throw new Exception("GRP file didn't pass checks");
  56.  
  57.                     //grab all the GRP bytes into a linear array
  58.                     //format is chunked and awful eughhh
  59.                     for(int j = 0; j < 3; j++) {
  60.                         for(int i = 0; i < 4; i++) {
  61.                             for(int y = 0; y < 8; y++) {
  62.                                 for(int x = 0; x < 8; x++) {
  63.                                     for(int yy = 0; yy < 8; yy++) {
  64.                                         for(int xx = 0; xx < 8; xx++) {
  65.                                             fs.Seek((j * 16384) + (i * 4096) + (y * 512) + (x * 64) + (yy * 8) + xx + 48, SeekOrigin.Begin);
  66.                                             grpbytes[(j * 16384) + (i * 64) + (y * 2048) + (x * 8) + (yy * 256) + xx] = (byte)fs.ReadByte();
  67.                                         }
  68.                                     }
  69.                                 }
  70.                             }
  71.                         }
  72.                     }
  73.                 }
  74.  
  75.                 //end it
  76.                 using(var fs = new FileStream(outfile, FileMode.Create, FileAccess.Write)) {
  77.                     var blank = new byte[2];
  78.                     //write the header in here
  79.                     using(var head = new FileStream("HEADER.BIN", FileMode.Open, FileAccess.Read)) {
  80.                         head.CopyTo(fs);
  81.                     }
  82.  
  83.                     //do the GRP
  84.                     var b = new byte[2];
  85.                     for(int y = 0; y < 512; y++) {
  86.                         for(int x = 0; x < 512; x++) {
  87.                             if(y > 191 || x > 255 || grpbytes[y * 256 + x] == 0) {
  88.                                 fs.Write(blank, 0, 2);
  89.                             } else {
  90.                                 b = ToLittleEndianBytes(palette[grpbytes[y * 256 + x]]);
  91.                                 fs.Write(b ,0 ,2);
  92.                             }
  93.                         }
  94.                     }
  95.  
  96.                     //write the footer in here
  97.                     using(var foot = new FileStream("FOOTER.BIN", FileMode.Open, FileAccess.Read)) {
  98.                         foot.CopyTo(fs);
  99.                     }
  100.                 }
  101.             } catch(Exception ex) {
  102.                 Console.WriteLine(
  103. @"Encountered {0}
  104. -{1}
  105. Stack Trace:
  106. {2}",
  107.                     ex.GetType().ToString(), ex.Message, ex.StackTrace);
  108.             }
  109.         }
  110.  
  111.         static ushort MakeSBColor(ushort ptcColor) {
  112.             ushort color = 1;
  113.             var red = (ushort)(ptcColor >> 8 & 0x1F);
  114.             var green = (ushort)(((ptcColor & 3) << 4) | ((ptcColor & 0x80) >> 7) | (((ptcColor & 0xE000) >> 12)) & 0xE);
  115.             var blue = (ushort)(ptcColor >> 2 & 0x1F);
  116.  
  117.             //discard the low green bit
  118.             //maybe we'll use you someday
  119.             green >>= 1;
  120.  
  121.             //prep the color code and return
  122.             color |= (ushort)((red << 11) | (blue << 6) | (green << 1));
  123.             return color;
  124.         }
  125.  
  126.         static bool VerifyFile(FileStream fs, string identifier) {
  127.             //verify file type
  128.             var tag = new byte[12];
  129.             fs.Read(tag, 0, 4);
  130.             if(Encoding.ASCII.GetString(tag, 0, 4) != "PX01")
  131.                 return false;
  132.             fs.Seek(36, SeekOrigin.Begin);
  133.             fs.Read(tag, 0, 12);
  134.             if(Encoding.ASCII.GetString(tag) != identifier)
  135.                 return false;
  136.  
  137.             return true;
  138.         }
  139.  
  140.         static byte[] ToLittleEndianBytes(ushort i) {
  141.             var output = new byte[2];
  142.  
  143.             output[0] = (byte)(i & 0xFF);
  144.             output[1] = (byte)(i >> 8 & 0xFF);
  145.  
  146.             return output;
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement