SonicAudioTools/Source/SonicAudioCmd/utf_to_cs_memory.txt
2016-11-12 19:02:48 +03:00

140 lines
4.9 KiB
Plaintext

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.ComponentModel;
using System.Collections;
using SonicAudioLib;
using SonicAudioLib.Archive;
using SonicAudioLib.Collections;
using SonicAudioLib.IO;
using SonicAudioLib.CriMw;
using System.Xml;
namespace SonicAudioCmd
{
class Program
{
static void Main(string[] args)
{
byte[] bytes = File.ReadAllBytes(args[0]);
for (int i = 0; i < bytes.Length; i++)
{
if (bytes[i] == '@' && bytes[i + 1] == 'U' && bytes[i + 2] == 'T' && bytes[i + 3] == 'F')
{
using (MemoryStream memoryStream = new MemoryStream(bytes))
{
memoryStream.Position = i;
CreateCSharpSource(memoryStream);
}
}
}
}
static void CreateCSharpSource(Stream stream)
{
using (CriTableReader reader = CriTableReader.Create(stream))
using (TextWriter textWriter = File.CreateText(reader.TableName + ".cs"))
{
textWriter.WriteLine("using System.IO;");
textWriter.WriteLine("using SonicAudioLib.CriMw;");
textWriter.WriteLine();
textWriter.WriteLine("namespace {0} {{", "SonicAudioLib.CriMw");
textWriter.WriteLine(" [CriSerializable(\"{0}\")]", reader.TableName);
textWriter.WriteLine(" public class {0} {{", reader.TableName);
for (int i = 0; i < reader.NumberOfFields; i++)
{
textWriter.Write(" private {0} _{1}", GetSimplifiedName(reader.GetFieldType(i)), reader.GetFieldName(i));
object defaultValue = reader.GetFieldValue(i);
if (defaultValue != null)
{
textWriter.Write(" = {0}", defaultValue);
}
textWriter.WriteLine(";");
}
for (int i = 0; i < reader.NumberOfFields; i++)
{
string name = reader.GetFieldName(i);
Type type = reader.GetFieldType(i);
object defaultValue = reader.GetFieldValue(i);
string fieldFlag = reader.GetFieldFlag(i).ToString("X2");
textWriter.WriteLine();
textWriter.WriteLine(" // Field flag: {0}", fieldFlag);
textWriter.WriteLine(" [CriField(\"{0}\", {1})]", name, i);
textWriter.WriteLine(" public {0} {1} {{", GetSimplifiedName(reader.GetFieldType(i)), name);
textWriter.WriteLine(" get {");
textWriter.WriteLine(" return _{0};", name);
textWriter.WriteLine(" }");
textWriter.WriteLine(" set {");
textWriter.WriteLine(" _{0} = value;", name);
textWriter.WriteLine(" }");
textWriter.WriteLine(" }");
}
textWriter.WriteLine(" }");
textWriter.WriteLine("}");
while (reader.Read())
{
for (int i = 0; i < reader.NumberOfFields; i++)
{
if (reader.GetFieldType(i) == typeof(byte[]))
{
stream.Position = reader.GetPosition(i);
if (EndianStream.ReadInt32(stream) == 0x46545540)
{
CreateCSharpSource(reader.GetSubstream(i));
}
}
}
}
}
}
static string GetSimplifiedName(Type type)
{
switch (Type.GetTypeCode(type))
{
case TypeCode.Byte:
return "byte";
case TypeCode.SByte:
return "sbyte";
case TypeCode.Int16:
return "short";
case TypeCode.UInt16:
return "ushort";
case TypeCode.Int32:
return "int";
case TypeCode.UInt32:
return "uint";
case TypeCode.Int64:
return "long";
case TypeCode.UInt64:
return "ulong";
case TypeCode.Single:
return "float";
case TypeCode.Double:
return "double";
case TypeCode.String:
return "string";
}
if (type == typeof(byte[]))
{
return "FileInfo";
}
return "DBNull";
}
}
}