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

135 lines
4.6 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)
{
CreateCSharpSource(File.OpenRead(args[0]));
}
static void CreateCSharpSource(Stream stream)
{
using (CriTableReader reader = new CriTableReader(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);
textWriter.WriteLine();
if (defaultValue != null)
{
textWriter.WriteLine(" [CriField(\"{0}\", {1}, {2})]", name, defaultValue, i);
}
else
{
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";
}
}
}