Created
March 14, 2017 21:06
-
-
Save Traderain/aa1be1844011c6d36b6b89303bee151f to your computer and use it in GitHub Desktop.
Redscripts reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
using System.Windows.Forms; | |
namespace Redscript | |
{ | |
class Program | |
{ | |
[STAThread] | |
static void Main(string[] args) | |
{ | |
Console.Title = "Redscript reader by Traderain"; | |
using (var of = new OpenFileDialog()) | |
{ | |
of.Title = "Select a redscript file!"; | |
of.Filter = "Redscript files | *.redscripts"; | |
if (of.ShowDialog() == DialogResult.OK) | |
{ | |
var strings = ParseRedscript(of.FileName); | |
for (var index = 0; index < strings.Length; index++) | |
{ | |
Console.WriteLine("[" + index + "]: " + strings[index]); | |
} | |
} | |
} | |
} | |
public static string[] ParseRedscript(string filename) | |
{ | |
var strings = new List<string>(); | |
using (var br = new BinaryReader(new FileStream(filename, FileMode.Open))) | |
{ | |
br.BaseStream.Seek(-4, SeekOrigin.End); | |
var offset = br.ReadUInt32(); | |
Console.WriteLine("Offset: " + offset); | |
br.BaseStream.Seek(offset, SeekOrigin.Begin); | |
var count = br.ReadUInt32(); | |
for (var i = 0; i < count; i++) | |
{ | |
strings.Add(ReadString(br)); | |
} | |
} | |
return strings.ToArray(); | |
} | |
public static string ReadString(BinaryReader br) | |
{ | |
int len = br.ReadByte(); | |
if (len > 191) | |
len = br.ReadByte()*64 + len - 192; | |
else if (len > 127) | |
len = len - 128; | |
else | |
{ | |
throw new Exception("Error can't read length of string at " + br.BaseStream.Position); | |
} | |
return br.ReadCr2WString(len); | |
} | |
} | |
public static class RedscriptBinaryreaderExtensions | |
{ | |
public static string ReadCr2WString(this BinaryReader file, int len = 0) | |
{ | |
string str = null; | |
if (len > 0) | |
{ | |
str = Encoding.Default.GetString(file.ReadBytes(len)); | |
} | |
else | |
{ | |
var sb = new StringBuilder(); | |
while (true) | |
{ | |
var c = (char)file.ReadByte(); | |
if (c == 0) | |
break; | |
sb.Append(c); | |
} | |
str = sb.ToString(); | |
} | |
return str; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment