Advertisement
IllidanS4

Untitled

Aug 2nd, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. /* Date: 27.7.2015, Time: 19:54 */
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace walotcsres
  9. {
  10.     class Program
  11.     {
  12.         public static void Main(string[] args)
  13.         {
  14.             string file = "warcr100.rsc";
  15.             var stream = File.Open(file, FileMode.Open);
  16.             using(BinaryReader reader = new BinaryReader(stream, Encoding.ASCII))
  17.             {
  18.                 string sig = new string(reader.ReadChars(4));
  19.                 stream.Position = 0x10; //Start of the main "subfile"
  20.                
  21.                 var first = new Subfile(reader, true); //Covers the whole file
  22.                 var enc = Encoding.GetEncoding(1252);
  23.                 for(int i = 0; i < first.RecordData.Length; i++)
  24.                 {
  25.                     var data = first.RecordData[i];
  26.                     File.WriteAllBytes("outx/"+i, data);
  27.                 }
  28.             }
  29.             Console.ReadKey(true);
  30.         }
  31.     }
  32.    
  33.     class Subfile
  34.     {
  35.         public long SubfileStart{get; private set;} //Absolute starting offset
  36.         public int Tags{get; private set;} //Number of "tags"
  37.         public int Start{get; private set;} //Relative data offset (to SubfileStart)
  38.         public int Records{get; private set;} //Number of records
  39.         public byte[][] RecordData{get; private set;} //Record data
  40.         public RecordInfo[] RecordInfos{get; private set;}
  41.         public Dictionary<string, int> TagData{get; private set;} //Tags
  42.         public long SubfileEnd{get; private set;} //Absolute end offset (computed as maximum record end offset)
  43.        
  44.         public Subfile(BinaryReader reader, bool first)
  45.         {
  46.             var stream = reader.BaseStream;
  47.             SubfileStart = stream.Position;
  48.             Tags = reader.ReadInt32();
  49.             Start = reader.ReadInt32();
  50.             if(first)
  51.             {
  52.                 Start -= 0x10;
  53.             }
  54.             stream.Position += 0x08;
  55.            
  56.             TagData = new Dictionary<string, int>();
  57.             for(int i = 0; i < Tags; i++)
  58.             {
  59.                 TagData[new string(reader.ReadChars(4))] = reader.ReadInt32();
  60.             }
  61.            
  62.             Records = (Start - 0x10 - Tags * 8)/20;
  63.             RecordInfos = new RecordInfo[Records];
  64.             for(int i = 0; i < Records; i++)
  65.             {
  66.                 RecordInfos[i] = new RecordInfo(reader);
  67.             }
  68.             RecordData = new byte[Records][];
  69.             for(int i = 0; i < Records; i++)
  70.             {
  71.                 int start = RecordInfos[i].Start;
  72.                 int length = RecordInfos[i].Length;
  73.                 byte[] data = new byte[length];
  74.                 if(length > 0)
  75.                 {
  76.                     stream.Position = SubfileStart+start;
  77.                     stream.Read(data, 0, length);
  78.                 }
  79.                 RecordData[i] = data;
  80.             }
  81.             SubfileEnd = SubfileStart+RecordInfos.Max(i => i.Start+i.Length);
  82.         }
  83.        
  84.         public struct RecordInfo
  85.         {
  86.             public int Start{get; private set;}
  87.             public int Length{get; private set;}
  88.             public byte[] Unknown{get; private set;}
  89.            
  90.             public RecordInfo(BinaryReader reader) : this()
  91.             {
  92.                 Start = reader.ReadInt32();
  93.                 Length = reader.ReadInt32();
  94.                 Unknown = reader.ReadBytes(20-8);
  95.                 if(Unknown[6] == 3)
  96.                 {
  97.                     Start -= 0x10;
  98.                 }
  99.             }
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement