Skip to content

Instantly share code, notes, and snippets.

@Raggles
Last active June 30, 2017 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raggles/18dc432367cf81d08418ed31ff4a1b12 to your computer and use it in GitHub Desktop.
Save Raggles/18dc432367cf81d08418ed31ff4a1b12 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
namespace ASECaptureRepair
{
class Program
{
static void Main(string[] args)
{
string fileName = args[0];
if (File.Exists(fileName))
{
int messageCount = 0;
int newAddress;
byte[] address = new byte[4];
var file = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite);
try
{
file.Seek(0x100, SeekOrigin.Begin);
file.Read(address, 0, 4);
messageCount++;
newAddress = byte2int(address);
while (newAddress < file.Length & newAddress != 0)
{
file.Seek(newAddress, SeekOrigin.Begin);
file.Read(address, 0, 4);
messageCount++;
newAddress = byte2int(address);
}
Console.WriteLine(String.Format("Counted {0} messages, attempting to correct header...", messageCount));
file.Seek(0xC, SeekOrigin.Begin);
file.Write(int2byte(messageCount), 0, 4);
file.Seek(0x18, SeekOrigin.Begin);
file.Write(int2byte(messageCount), 0, 4);
file.Seek(0x1C, SeekOrigin.Begin);
file.Write(int2byte(messageCount + 1), 0, 4);
Console.WriteLine("Done.");
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
else
{
Console.WriteLine("File does not exist.");
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
static int byte2int(byte[] bytes)
{
return bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
}
static byte[] int2byte(int i)
{
byte[] intBytes = BitConverter.GetBytes(i);
if (!BitConverter.IsLittleEndian)
Array.Reverse(intBytes);
return intBytes;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment