Skip to content

Instantly share code, notes, and snippets.

@xupefei
Created April 10, 2013 11:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xupefei/5353701 to your computer and use it in GitHub Desktop.
Save xupefei/5353701 to your computer and use it in GitHub Desktop.
CatSystem2 CST tools
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DataHelper;
namespace CST_Exporter
{
internal class Program
{
private static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("usage: exe <filename>");
Console.ReadKey();
return;
}
string strFileName = args[0];
byte[] byteDecompressed = Decompress(strFileName);
List<string> strStringList = GetStringList(byteDecompressed);
StreamWriter sw = new StreamWriter(strFileName + ".txt");
foreach (string str in strStringList)
sw.WriteLine(str);
sw.Close();
}
private static List<string> GetStringList(byte[] byteBuffer)
{
//Header: 16 bytes
int intDataLength = BitConverter.ToInt32(byteBuffer, 0);
int intClearScreenCount = BitConverter.ToInt32(byteBuffer, 4);
int intStringIndexOffset = BitConverter.ToInt32(byteBuffer, 8);//Offset without Header
int intStringDataOffset = BitConverter.ToInt32(byteBuffer, 12);//Offset without Header
//Remove the Header
byteBuffer = BytesHelper.CopyBlock(byteBuffer, 16, byteBuffer.Length - 16);
//Split offset and data
byte[] byteStringIndex = BytesHelper.CopyBlock(byteBuffer, intStringIndexOffset, intStringDataOffset - intStringIndexOffset);
byte[] byteStringData = BytesHelper.CopyBlock(byteBuffer, intStringDataOffset, intDataLength - intStringDataOffset);
List<string> strStringList = new List<string>();
for (int i = 0; i < byteStringIndex.Length / 4; i++)
{
int intDataOffset = BitConverter.ToInt32(byteStringIndex, i * 4);
byte[] byteLine = BytesHelper.CopyBlock(
byteStringData,
intDataOffset + 1,// +1: Fix for string start mark
BytesHelper.FindByte(byteStringData, (byte)0, intDataOffset) - intDataOffset - 1// -1: Padding for start mark
);
string strLine = Encoding.GetEncoding(932).GetString(byteLine).Replace("\x02", "<CLS>");
strStringList.Add(strLine);
}
return strStringList;
}
private static byte[] Decompress(string strFileName)
{
BinaryReader br = new BinaryReader(new FileStream(strFileName, FileMode.Open));
br.ReadBytes(8);//Maigc
int intLength = br.ReadInt32();//Zipped Length
int intUnzipLength = br.ReadInt32();//Org Length
byte[] byteBuffer = br.ReadBytes((int)br.BaseStream.Length - 16);//16: Header Length
br.Close();
return (zlibdecompress.Decompress(byteBuffer, intUnzipLength));
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace CST_Importer
{
internal class Program
{
private static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("usage: exe <TEXT FILE>");
Console.ReadKey();
return;
}
string strTextFileName = args[0];
string strCstFileName = args[0].Replace(".txt", "");
byte[] bytesCstBuffer = Decompress(strCstFileName);
//Header: 16 bytes
int intDataLength = BitConverter.ToInt32(bytesCstBuffer, 0);//Without Header
int intClearScreenCount = BitConverter.ToInt32(bytesCstBuffer, 4);
int intStringIndexOffset = BitConverter.ToInt32(bytesCstBuffer, 8);//Offset without Header
int intStringDataOffset = BitConverter.ToInt32(bytesCstBuffer, 12);//Offset without Header
byte[] bytesHeader = Amamiya.Data.BytesHelper.CopyBlock(bytesCstBuffer, 0, intStringIndexOffset + 16);
var listString = ReadText(strTextFileName);
var bytesNewIndexTable = new byte[] { };
var bytesNewStringTable = new byte[] { };
int pos = 0;
foreach (string s in listString)
{
string buf = string.Format("\x01{0}\x00", s.Replace("<CLS>", "\x02"));
bytesNewIndexTable = Amamiya.Data.BytesHelper.AppendBytes(bytesNewIndexTable,
BitConverter.GetBytes(pos));
bytesNewStringTable = Amamiya.Data.BytesHelper.AppendBytes(bytesNewStringTable,
Encoding.GetEncoding(936).GetBytes(buf));
pos += Encoding.GetEncoding(936).GetBytes(buf).Length;
}
var bw = new BinaryWriter(new FileStream(strCstFileName + ".new", FileMode.Create));
bw.Write(bytesHeader);
bw.Write(bytesNewIndexTable);
bw.Write(bytesNewStringTable);
bw.Close();
Compress(strCstFileName + ".new");
}
private static List<string> ReadText(string p)
{
var sr = new StreamReader(p, Encoding.UTF8, true);
var list = new List<string>();
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (line != "")
list.Add(line);
}
sr.Close();
return list;
}
private static byte[] Decompress(string strFileName)
{
var br = new BinaryReader(new FileStream(strFileName, FileMode.Open));
br.ReadBytes(8);//Maigc
int intLength = br.ReadInt32();//Zipped Length
int intUnzipLength = br.ReadInt32();//Org Length
byte[] byteBuffer = br.ReadBytes((int)br.BaseStream.Length - 16);//16: Header Length
br.Close();
return (Zlibdecompress.Decompress(byteBuffer, intUnzipLength));
}
private static void Compress(string strFileName)
{
var br = new BinaryReader(new FileStream(strFileName, FileMode.Open));
var length = (int)br.BaseStream.Length;
byte[] bytesCompessed = Zlibcompress.Compress(br.ReadBytes(length),
Zlibcompress.TcompressionLevel.clMax);
br.Close();
byte[] buffer = new byte[] { };
buffer = Amamiya.Data.BytesHelper.AppendBytes(buffer, Encoding.ASCII.GetBytes("CatScene"));
buffer = Amamiya.Data.BytesHelper.AppendBytes(buffer, BitConverter.GetBytes(bytesCompessed.Length));
buffer = Amamiya.Data.BytesHelper.AppendBytes(buffer, BitConverter.GetBytes(length));
buffer = Amamiya.Data.BytesHelper.AppendBytes(buffer, bytesCompessed);
var bw = new BinaryWriter(new FileStream(strFileName, FileMode.Create));
bw.Write(buffer);
bw.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment