Advertisement
IllidanS4

Untitled

Aug 2nd, 2015
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 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.                 for(int i = 0; i < first.RecordData.Length; i++)
  23.                 {
  24.                     var data = first.RecordData[i];
  25.                     File.WriteAllBytes("out2/"+i, data);
  26.                 }
  27.             }
  28.             Console.ReadKey(true);
  29.         }
  30.     }
  31.    
  32.     class Subfile
  33.     {
  34.         public long SubfileStart{get; private set;} //Absolute starting offset
  35.         public int Tags{get; private set;} //Number of "tags"
  36.         public int Start{get; private set;} //Relative data offset (to SubfileStart)
  37.         public int Records{get; private set;} //Number of records
  38.         public byte[][] RecordData{get; private set;} //Record data
  39.         public Dictionary<string, int> TagData{get; private set;} //Tags
  40.         public long SubfileEnd{get; private set;} //Absolute end offset (computed as maximum record end offset)
  41.        
  42.         public Subfile(BinaryReader reader, bool first)
  43.         {
  44.             var stream = reader.BaseStream;
  45.             SubfileStart = stream.Position;
  46.             Tags = reader.ReadInt32();
  47.             Start = reader.ReadInt32();
  48.             if(first)
  49.             {
  50.                 Start -= 0x10;
  51.             }
  52.             stream.Position += 0x08;
  53.            
  54.             TagData = new Dictionary<string, int>();
  55.             for(int i = 0; i < Tags; i++)
  56.             {
  57.                 TagData[new string(reader.ReadChars(4))] = reader.ReadInt32();
  58.             }
  59.            
  60.             Records = (Start - 0x10 - Tags * 8)/20;
  61.             RecordInfo[] infos = new RecordInfo[Records];
  62.             for(int i = 0; i < Records; i++)
  63.             {
  64.                 infos[i] = new RecordInfo(reader);
  65.             }
  66.             RecordData = new byte[Records][];
  67.             for(int i = 0; i < Records; i++)
  68.             {
  69.                 int start = infos[i].Start;
  70.                 int length = infos[i].Length;
  71.                 byte[] data = new byte[length];
  72.                 if(length > 0)
  73.                 {
  74.                     //stream.Position = SubfileStart+start;
  75.                     stream.Position = SubfileStart-0x10+start;
  76.                     stream.Read(data, 0, length);
  77.                 }
  78.                 RecordData[i] = data;
  79.             }
  80.             SubfileEnd = SubfileStart+infos.Max(i => i.Start+i.Length);
  81.         }
  82.        
  83.         public struct RecordInfo
  84.         {
  85.             public int Start{get; private set;}
  86.             public int Length{get; private set;}
  87.            
  88.             public RecordInfo(BinaryReader reader) : this()
  89.             {
  90.                 Start = reader.ReadInt32();
  91.                 Length = reader.ReadInt32();
  92.                 reader.ReadBytes(20-8);
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement