From 3a5e79473653e254bd81bd15bebcc775e90268a6 Mon Sep 17 00:00:00 2001
From: Li
Date: Mon, 8 Aug 2022 13:04:52 +1200
Subject: [PATCH] add support for reading PS3 Theme Files .p3t
---
CXMLCli/CXMLBuilder.cs | 35 +-
CXMLCli/CXMLCli.csproj.user | 2 +-
CXMLCli/CXMLReader.cs | 5 +
CXMLCli/Tools.cs | 662 ++++++++++++++++++------------------
4 files changed, 353 insertions(+), 351 deletions(-)
diff --git a/CXMLCli/CXMLBuilder.cs b/CXMLCli/CXMLBuilder.cs
index ae38b09..dc90a4a 100644
--- a/CXMLCli/CXMLBuilder.cs
+++ b/CXMLCli/CXMLBuilder.cs
@@ -351,21 +351,17 @@ namespace CXMLDecompiler
Console.WriteLine("AttributeType: " + Type.ToString());
switch (Type)
{
- case AttributeType.TYPE_NONE:
- Console.WriteLine("UNSUPPORTED TYPE @ " + TreeTable.Position);
- Console.ReadKey();
- break;
case AttributeType.TYPE_INT:
int intWrite = Int32.Parse(attribute.Value, CultureInfo.InvariantCulture);
tools.WriteInt32(WorkRam, intWrite);
tools.WriteInt32(WorkRam, 0x00);
- Console.WriteLine("Int - Value: " + intWrite.ToString());
+ Console.WriteLine("Int: " + intWrite.ToString());
break;
case AttributeType.TYPE_FLOAT:
float FloatValue = Single.Parse(attribute.Value.Replace("f", ""), CultureInfo.InvariantCulture);
tools.WriteSingle(WorkRam, FloatValue);
tools.WriteInt32(WorkRam, 0x00);
- Console.WriteLine("Float - Value: " + FloatValue.ToString());
+ Console.WriteLine("Float: " + FloatValue.ToString());
break;
case AttributeType.TYPE_STRING:
int StringOffset = AddGetStringTable(attribute.Value);
@@ -391,7 +387,7 @@ namespace CXMLDecompiler
int HashTableSize = 4;
tools.WriteInt32(WorkRam, HashTableOffset);
tools.WriteInt32(WorkRam, HashTableSize);
- Console.WriteLine("Hash ID Offset:" + HashTableOffset.ToString() + " size: " + HashTableSize);
+ Console.WriteLine("Hash Offset:" + HashTableOffset.ToString() + " size: " + HashTableSize);
break;
case AttributeType.TYPE_INTEGER_ARRAY:
string[] arr = attribute.Value.Replace("[", "").Replace("]", "").Replace(" ", "").Split(',');
@@ -432,38 +428,39 @@ namespace CXMLDecompiler
int IdTableOffset = AddGetIdTable(attribute.Value, Convert.ToInt32(TreeTable.Position));
tools.WriteInt32(WorkRam, IdTableOffset);
tools.WriteInt32(WorkRam, 0x00);
- Console.WriteLine("String Loopback: " + ElementName + " " + TreeTable.Position.ToString("X") + " (" + TreeTable.Position.ToString("X") + ")");
- Console.WriteLine("Loopback ID String: " + IdTableOffset + " sz: 0");
+ Console.WriteLine("ID Ref: " + ElementName + " " + TreeTable.Position.ToString("X") + " (" + TreeTable.Position.ToString("X") + ")");
+ Console.WriteLine("ID Ref Offset: " + IdTableOffset + " sz: 0");
break;
case AttributeType.TYPE_ID:
IdTableOffset = AddGetIdTable(attribute.Value, -1);
tools.WriteInt32(WorkRam, IdTableOffset);
tools.WriteInt32(WorkRam, 0x00);
- Console.WriteLine("ID String: " + IdTableOffset + " sz: 0");
+ Console.WriteLine("ID : " + IdTableOffset + " sz: 0");
break;
case AttributeType.TYPE_ID_HASH_REF:
int hash = Int32.Parse(attribute.Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
if (HashStrings)
hash = Int32.Parse(Tools.GenerateShortHash(Encoding.UTF8.GetBytes(attribute.Value)), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
- int IntIdTableOffset = AddGetHashIdTable(hash, Convert.ToInt32(TreeTable.Position));
- tools.WriteInt32(WorkRam, IntIdTableOffset);
+ int HashIdTableOffset = AddGetHashIdTable(hash, Convert.ToInt32(TreeTable.Position));
+ tools.WriteInt32(WorkRam, HashIdTableOffset);
tools.WriteInt32(WorkRam, 0x00);
- Console.WriteLine("Int Loopback: " + ElementName + " " + TreeTable.Position.ToString("X") + " (" + TreeTable.Position.ToString("X") + ")");
- Console.WriteLine("Loopback ID Int: " + IntIdTableOffset + " sz: 0");
+ Console.WriteLine("Hash ID Ref: " + ElementName + " " + TreeTable.Position.ToString("X") + " (" + TreeTable.Position.ToString("X") + ")");
+ Console.WriteLine("Hash ID REF offset: " + HashIdTableOffset + " sz: 0");
break;
case AttributeType.TYPE_ID_HASH:
hash = Int32.Parse(attribute.Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
if (HashStrings)
hash = Int32.Parse(Tools.GenerateShortHash(Encoding.UTF8.GetBytes(attribute.Value)), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
- IntIdTableOffset = AddGetHashIdTable(hash, -1);
+ HashIdTableOffset = AddGetHashIdTable(hash, -1);
- tools.WriteInt32(WorkRam, IntIdTableOffset);
+ tools.WriteInt32(WorkRam, HashIdTableOffset);
tools.WriteInt32(WorkRam, 0x00);
- Console.WriteLine("Int Id: " + IntIdTableOffset + " sz: 0");
+ Console.WriteLine("Hash ID: " + HashIdTableOffset + " sz: 0");
+ break;
+ case AttributeType.TYPE_NONE:
break;
default:
- Console.WriteLine("UNKNOWN TYPE @ " + TreeTable.Position);
- Console.ReadKey();
+ throw new NotImplementedException("UNKNOWN TYPE @ " + TreeTable.Position);
break;
}
Console.WriteLine(attribute.Name + "=" + attribute.Value);
diff --git a/CXMLCli/CXMLCli.csproj.user b/CXMLCli/CXMLCli.csproj.user
index c9ea63d..6f7e643 100644
--- a/CXMLCli/CXMLCli.csproj.user
+++ b/CXMLCli/CXMLCli.csproj.user
@@ -2,6 +2,6 @@
PvrResource.cxml -ps3 -le -f -d
- C:\Users\User\Documents\git\cxml-decompiler\CXMLCli\bin\x64\Debug
+ C:\Users\Li\Documents\git\cxml-decompiler\CXMLCli\bin\x64\Debug
\ No newline at end of file
diff --git a/CXMLCli/CXMLReader.cs b/CXMLCli/CXMLReader.cs
index f437755..f84bdd1 100644
--- a/CXMLCli/CXMLReader.cs
+++ b/CXMLCli/CXMLReader.cs
@@ -32,6 +32,10 @@ namespace CXMLDecompiler
{
return true;
}
+ else if (Magic.StartsWith("P3TF")) // ps3 theme file
+ {
+ return true;
+ }
else if (Magic.StartsWith("CXML")) // cxml.py default
{
return true;
@@ -679,6 +683,7 @@ namespace CXMLDecompiler
{
case AttributeType.TYPE_INT:
AttributeValue = tools.ReadInt32(TreeTable);
+
int sz = tools.ReadInt32(TreeTable);
Console.WriteLine("Int - Value: " + AttributeValue.ToString() + " sz:" + sz);
break;
diff --git a/CXMLCli/Tools.cs b/CXMLCli/Tools.cs
index ed8ffff..321022b 100644
--- a/CXMLCli/Tools.cs
+++ b/CXMLCli/Tools.cs
@@ -1,69 +1,69 @@
-using System;
-using System.Drawing;
-using System.IO;
-using System.Linq;
-using System.Security.Cryptography;
-using System.Text;
-
-namespace General
+using System;
+using System.Drawing;
+using System.IO;
+using System.Linq;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace General
{
enum Endianness
{
LITTLE_ENDIAN,
BIG_ENDIAN,
UNCHANGED
- };
-
- class Tools
- {
+ };
+
+ class Tools
+ {
public Tools(bool bigEndain)
{
BigEndain = bigEndain;
- }
- public bool BigEndain = false;
-
- public static byte[] bmp = Encoding.ASCII.GetBytes("BM"); // BMP
- public static byte[] gif = Encoding.ASCII.GetBytes("GIF"); // GIF
- public static byte[] png = new byte[] { 137, 80, 78, 71 }; // PNG
- public static byte[] tiff = new byte[] { 73, 73, 42 }; // TIFF
- public static byte[] tiff2 = new byte[] { 77, 77, 42 }; // TIFF
+ }
+ public bool BigEndain = false;
+
+ public static byte[] bmp = Encoding.ASCII.GetBytes("BM"); // BMP
+ public static byte[] gif = Encoding.ASCII.GetBytes("GIF"); // GIF
+ public static byte[] png = new byte[] { 137, 80, 78, 71 }; // PNG
+ public static byte[] tiff = new byte[] { 73, 73, 42 }; // TIFF
+ public static byte[] tiff2 = new byte[] { 77, 77, 42 }; // TIFF
public static byte[] jpeg = new byte[] { 255, 216, 255 }; // jpeg
public static Random rng = new Random(Guid.NewGuid().GetHashCode());
- public static void WriteStringToStream(Stream s, String str)
- {
- Byte[] bytes = Encoding.UTF8.GetBytes(str);
- s.Write(bytes, 0x00, bytes.Length);
- }
- public static void WriteUtf16StringToStream(Stream s, String str)
- {
- Byte[] bytes = Encoding.Unicode.GetBytes(str);
- s.Write(bytes, 0x00, bytes.Length);
- }
- public static MemoryStream ByteToStream(byte[] Array)
- {
- MemoryStream ms = new MemoryStream();
- ms.Write(Array, 0x00, Array.Length);
- ms.Seek(0, SeekOrigin.Begin);
- return ms;
- }
-
- public static byte[] StreamToByte(Stream stream)
- {
- int StreamLen = (int)stream.Length;
- byte[] Bytes = new byte[StreamLen];
- stream.Seek(0, SeekOrigin.Begin);
- stream.Read(Bytes, 0x00, StreamLen);
- return Bytes;
+ public static void WriteStringToStream(Stream s, String str)
+ {
+ Byte[] bytes = Encoding.UTF8.GetBytes(str);
+ s.Write(bytes, 0x00, bytes.Length);
+ }
+ public static void WriteUtf16StringToStream(Stream s, String str)
+ {
+ Byte[] bytes = Encoding.Unicode.GetBytes(str);
+ s.Write(bytes, 0x00, bytes.Length);
+ }
+ public static MemoryStream ByteToStream(byte[] Array)
+ {
+ MemoryStream ms = new MemoryStream();
+ ms.Write(Array, 0x00, Array.Length);
+ ms.Seek(0, SeekOrigin.Begin);
+ return ms;
+ }
+
+ public static byte[] StreamToByte(Stream stream)
+ {
+ int StreamLen = (int)stream.Length;
+ byte[] Bytes = new byte[StreamLen];
+ stream.Seek(0, SeekOrigin.Begin);
+ stream.Read(Bytes, 0x00, StreamLen);
+ return Bytes;
}
public static string GenerateReplacePattern()
{
byte[] RandomNumber = new byte[0x20];
- rng.NextBytes(RandomNumber);
+ rng.NextBytes(RandomNumber);
return "{{" + BitConverter.ToString(RandomNumber).Replace("-", "") + "}}";
}
- public static string GetFileExtension(byte[] Bytes)
+ public static string GetFileExtension(byte[] Bytes)
{
if (bmp.SequenceEqual(Bytes.Take(bmp.Length)))
{
@@ -88,296 +88,296 @@ namespace General
else if (jpeg.SequenceEqual(Bytes.Take(jpeg.Length)))
{
return ".jpg";
- }
- else if (IsZlib(Bytes))
- {
- return ".z";
- }
- else if (IsRcf(Bytes))
- {
- return ".rcs";
- }
- else if (IsDDS(Bytes))
- {
- return ".dds";
- }
- else if (IsVAG(Bytes))
- {
- return ".vag";
- }
- else if (IsGim(Bytes))
- {
- return ".gim";
- }
- else if (!HasBinaryContent(Encoding.UTF8.GetString(Bytes)))
- {
- return ".txt";
- }
- else
- {
- return ".bin";
- }
- }
-
- public static bool IsVAG(byte[] Bytes)
- {
- MemoryStream Data = ByteToStream(Bytes);
- String header = ReadString(Data, 4);
- if (header.StartsWith("VAG"))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- public static bool IsDDS(byte[] Bytes)
- {
- MemoryStream Data = ByteToStream(Bytes);
- String header = ReadString(Data, 4);
- if (header.StartsWith("DDS"))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
- public static bool IsRcf(byte[] Bytes)
- {
- MemoryStream Data = ByteToStream(Bytes);
- String header = ReadString(Data, 5);
- if (header.StartsWith("RCSF"))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- public static bool IsGim(byte[] Bytes)
- {
- MemoryStream Data = ByteToStream(Bytes);
- String header = ReadString(Data, 4);
- if (header.StartsWith("MIG"))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- public static bool IsZlib(byte[] Bytes)
- {
- if (Bytes[0] == 0x78)
- {
- if (Bytes[1] == 0x01)
- return true;
- if (Bytes[1] == 0x9C)
- return true;
- if (Bytes[1] == 0xDA)
- return true;
- }
- return false;
- }
-
-
-
- public static Bitmap GetBitmap(byte[] BitmapBytes)
- {
- MemoryStream ms = ByteToStream(BitmapBytes);
- Bitmap bmp = new Bitmap(ms);
- ms.Dispose();
- return bmp;
- }
-
- public static String ReadStringAt(Stream ms, int location)
- {
- long ogPos = ms.Position;
- ms.Seek(location, SeekOrigin.Begin);
- String str = ReadString(ms);
- ms.Seek(ogPos, SeekOrigin.Begin);
- return str;
- }
-
- public static int ReadLittleEndainIntAt(Stream ms, int location)
- {
- long ogPos = ms.Position;
- ms.Seek(location, SeekOrigin.Begin);
- int i = ReadLittleEndainInt(ms);
- ms.Seek(ogPos, SeekOrigin.Begin);
- return i;
- }
-
- public int ReadIntAt(Stream ms, int location)
- {
- long ogPos = ms.Position;
- ms.Seek(location, SeekOrigin.Begin);
- int i = ReadInt32(ms);
- ms.Seek(ogPos, SeekOrigin.Begin);
- return i;
- }
- public static bool HasBinaryContent(string content)
- {
- return content.Any(ch => char.IsControl(ch) && ch != '\r' && ch != '\n');
+ }
+ else if (IsZlib(Bytes))
+ {
+ return ".z";
+ }
+ else if (IsRcf(Bytes))
+ {
+ return ".rcs";
+ }
+ else if (IsDDS(Bytes))
+ {
+ return ".dds";
+ }
+ else if (IsVAG(Bytes))
+ {
+ return ".vag";
+ }
+ else if (IsGim(Bytes))
+ {
+ return ".gim";
+ }
+ else if (!HasBinaryContent(Encoding.UTF8.GetString(Bytes)))
+ {
+ return ".txt";
+ }
+ else
+ {
+ return ".bin";
+ }
}
-
- public void WriteInt32(Stream ms, int val)
- {
- if (BigEndain)
- WriteBigEndainInt(ms, val);
- else
- WriteLittleEndainInt(ms, val);
- }
-
- public void WriteSingle(Stream ms, Single val)
- {
- if (BigEndain)
- WriteBigEndainSingle(ms, val);
- else
- WriteLittleEndainSingle(ms, val);
- }
-
- public int ReadInt32(Stream ms)
- {
- if (BigEndain)
- return ReadBigEndainInt(ms);
- else
- return ReadLittleEndainInt(ms);
- }
-
- public Single ReadSingle(Stream ms)
- {
- if (BigEndain)
- return ReadBigEndainSingle(ms);
- else
- return ReadLittleEndainSingle(ms);
- }
-
- public static int ReadBigEndainIntAt(Stream ms, int location)
- {
- long ogPos = ms.Position;
- ms.Seek(location, SeekOrigin.Begin);
- int i = ReadBigEndainInt(ms);
- ms.Seek(ogPos, SeekOrigin.Begin);
- return i;
- }
- public static Single ReadBigEndainSingle(Stream ms)
- {
- byte[] SingleBytes = new byte[4];
- ms.Read(SingleBytes, 0x00, 4);
- SingleBytes = SingleBytes.Reverse().ToArray();
- Single val = BitConverter.ToSingle(SingleBytes, 0x00);
- return val;
- }
- public static Single ReadLittleEndainSingle(Stream ms)
- {
- byte[] SingleBytes = new byte[4];
- ms.Read(SingleBytes, 0x00, 4);
- SingleBytes = SingleBytes.ToArray();
- Single val = BitConverter.ToSingle(SingleBytes, 0x00);
- return val;
- }
- public static void WriteLittleEndainSingle(Stream ms, Single val)
+ public static bool IsVAG(byte[] Bytes)
{
- byte[] SingleBytes = BitConverter.GetBytes(val);
- ms.Write(SingleBytes, 0x00, SingleBytes.Length);
- }
- public static void WriteBigEndainSingle(Stream ms, Single val)
- {
- byte[] SingleBytes = BitConverter.GetBytes(val);
- SingleBytes = SingleBytes.Reverse().ToArray();
- ms.Write(SingleBytes, 0x00, SingleBytes.Length);
+ MemoryStream Data = ByteToStream(Bytes);
+ String header = ReadString(Data, 4);
+ if (header.StartsWith("VAG"))
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
}
- public static void WriteLittleEndainInt(Stream ms, int val)
+ public static bool IsDDS(byte[] Bytes)
{
- byte[] IntBytes = BitConverter.GetBytes(val);
- ms.Write(IntBytes, 0x00, IntBytes.Length);
- }
- public static void WriteBigEndainInt(Stream ms, int val)
- {
- byte[] IntBytes = BitConverter.GetBytes(val);
- IntBytes = IntBytes.Reverse().ToArray();
- ms.Write(IntBytes, 0x00, IntBytes.Length);
+ MemoryStream Data = ByteToStream(Bytes);
+ String header = ReadString(Data, 4);
+ if (header.StartsWith("DDS"))
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
}
- public static int ReadLittleEndainInt(Stream ms)
+
+ public static bool IsRcf(byte[] Bytes)
{
- byte[] IntBytes = new byte[4];
- ms.Read(IntBytes, 0x00, 4);
- IntBytes = IntBytes.ToArray();
- int i = BitConverter.ToInt32(IntBytes, 0x00);
- return i;
- }
- public static int ReadBigEndainInt(Stream ms)
- {
- byte[] IntBytes = new byte[4];
- ms.Read(IntBytes, 0x00, 4);
- IntBytes = IntBytes.Reverse().ToArray();
- int i = BitConverter.ToInt32(IntBytes, 0x00);
- return i;
- }
- public static String GetRootFolder(string path)
- {
- while (true)
- {
- string temp = Path.GetDirectoryName(path);
- if (String.IsNullOrEmpty(temp))
- break;
- path = temp;
- }
- return path;
- }
-
- public static String GenerateHash(byte[] Data)
- {
- SHA1 sha = SHA1.Create();
- byte[] ShaBytes = sha.ComputeHash(Data);
- return BitConverter.ToString(ShaBytes).Replace("-", "").ToUpper();
- }
-
- public static string GenerateShortHash(byte[] Data)
- {
- SHA1 sha = SHA1.Create();
- byte[] ShaBytes = sha.ComputeHash(Data);
- return BitConverter.ToInt32(ShaBytes, 0).ToString("X8");
- }
-
- public static String ReadString(Stream ms, int limit = -1)
- {
- int i = 0xFF;
- int counter = 0;
-
- MemoryStream StringStream = new MemoryStream();
-
- do
- {
- i = ms.ReadByte();
- if (i == 0 || counter == limit)
- break;
- StringStream.WriteByte((byte)i);
- counter += 1;
- }
- while (true);
-
- byte[] StringData = StringStream.ToArray();
-
- String str = Encoding.UTF8.GetString(StringData);
-
- return str;
-
- }
- }
-
-}
+ MemoryStream Data = ByteToStream(Bytes);
+ String header = ReadString(Data, 5);
+ if (header.StartsWith("RCSF"))
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ public static bool IsGim(byte[] Bytes)
+ {
+ MemoryStream Data = ByteToStream(Bytes);
+ String header = ReadString(Data, 4);
+ if (header.StartsWith("MIG") || header.EndsWith("GIM"))
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ public static bool IsZlib(byte[] Bytes)
+ {
+ if (Bytes[0] == 0x78)
+ {
+ if (Bytes[1] == 0x01)
+ return true;
+ if (Bytes[1] == 0x9C)
+ return true;
+ if (Bytes[1] == 0xDA)
+ return true;
+ }
+ return false;
+ }
+
+
+
+ public static Bitmap GetBitmap(byte[] BitmapBytes)
+ {
+ MemoryStream ms = ByteToStream(BitmapBytes);
+ Bitmap bmp = new Bitmap(ms);
+ ms.Dispose();
+ return bmp;
+ }
+
+ public static String ReadStringAt(Stream ms, int location)
+ {
+ long ogPos = ms.Position;
+ ms.Seek(location, SeekOrigin.Begin);
+ String str = ReadString(ms);
+ ms.Seek(ogPos, SeekOrigin.Begin);
+ return str;
+ }
+
+ public static int ReadLittleEndainIntAt(Stream ms, int location)
+ {
+ long ogPos = ms.Position;
+ ms.Seek(location, SeekOrigin.Begin);
+ int i = ReadLittleEndainInt(ms);
+ ms.Seek(ogPos, SeekOrigin.Begin);
+ return i;
+ }
+
+ public int ReadIntAt(Stream ms, int location)
+ {
+ long ogPos = ms.Position;
+ ms.Seek(location, SeekOrigin.Begin);
+ int i = ReadInt32(ms);
+ ms.Seek(ogPos, SeekOrigin.Begin);
+ return i;
+ }
+ public static bool HasBinaryContent(string content)
+ {
+ return content.Any(ch => char.IsControl(ch) && ch != '\r' && ch != '\n');
+ }
+
+
+ public void WriteInt32(Stream ms, int val)
+ {
+ if (BigEndain)
+ WriteBigEndainInt(ms, val);
+ else
+ WriteLittleEndainInt(ms, val);
+ }
+
+ public void WriteSingle(Stream ms, Single val)
+ {
+ if (BigEndain)
+ WriteBigEndainSingle(ms, val);
+ else
+ WriteLittleEndainSingle(ms, val);
+ }
+
+ public int ReadInt32(Stream ms)
+ {
+ if (BigEndain)
+ return ReadBigEndainInt(ms);
+ else
+ return ReadLittleEndainInt(ms);
+ }
+
+ public Single ReadSingle(Stream ms)
+ {
+ if (BigEndain)
+ return ReadBigEndainSingle(ms);
+ else
+ return ReadLittleEndainSingle(ms);
+ }
+
+ public static int ReadBigEndainIntAt(Stream ms, int location)
+ {
+ long ogPos = ms.Position;
+ ms.Seek(location, SeekOrigin.Begin);
+ int i = ReadBigEndainInt(ms);
+ ms.Seek(ogPos, SeekOrigin.Begin);
+ return i;
+ }
+ public static Single ReadBigEndainSingle(Stream ms)
+ {
+ byte[] SingleBytes = new byte[4];
+ ms.Read(SingleBytes, 0x00, 4);
+ SingleBytes = SingleBytes.Reverse().ToArray();
+ Single val = BitConverter.ToSingle(SingleBytes, 0x00);
+ return val;
+ }
+ public static Single ReadLittleEndainSingle(Stream ms)
+ {
+ byte[] SingleBytes = new byte[4];
+ ms.Read(SingleBytes, 0x00, 4);
+ SingleBytes = SingleBytes.ToArray();
+ Single val = BitConverter.ToSingle(SingleBytes, 0x00);
+ return val;
+ }
+ public static void WriteLittleEndainSingle(Stream ms, Single val)
+ {
+ byte[] SingleBytes = BitConverter.GetBytes(val);
+ ms.Write(SingleBytes, 0x00, SingleBytes.Length);
+ }
+ public static void WriteBigEndainSingle(Stream ms, Single val)
+ {
+ byte[] SingleBytes = BitConverter.GetBytes(val);
+ SingleBytes = SingleBytes.Reverse().ToArray();
+ ms.Write(SingleBytes, 0x00, SingleBytes.Length);
+ }
+
+ public static void WriteLittleEndainInt(Stream ms, int val)
+ {
+ byte[] IntBytes = BitConverter.GetBytes(val);
+ ms.Write(IntBytes, 0x00, IntBytes.Length);
+ }
+ public static void WriteBigEndainInt(Stream ms, int val)
+ {
+ byte[] IntBytes = BitConverter.GetBytes(val);
+ IntBytes = IntBytes.Reverse().ToArray();
+ ms.Write(IntBytes, 0x00, IntBytes.Length);
+ }
+
+ public static int ReadLittleEndainInt(Stream ms)
+ {
+ byte[] IntBytes = new byte[4];
+ ms.Read(IntBytes, 0x00, 4);
+ IntBytes = IntBytes.ToArray();
+ int i = BitConverter.ToInt32(IntBytes, 0x00);
+ return i;
+ }
+ public static int ReadBigEndainInt(Stream ms)
+ {
+ byte[] IntBytes = new byte[4];
+ ms.Read(IntBytes, 0x00, 4);
+ IntBytes = IntBytes.Reverse().ToArray();
+ int i = BitConverter.ToInt32(IntBytes, 0x00);
+ return i;
+ }
+ public static String GetRootFolder(string path)
+ {
+ while (true)
+ {
+ string temp = Path.GetDirectoryName(path);
+ if (String.IsNullOrEmpty(temp))
+ break;
+ path = temp;
+ }
+ return path;
+ }
+
+ public static String GenerateHash(byte[] Data)
+ {
+ SHA1 sha = SHA1.Create();
+ byte[] ShaBytes = sha.ComputeHash(Data);
+ return BitConverter.ToString(ShaBytes).Replace("-", "").ToUpper();
+ }
+
+ public static string GenerateShortHash(byte[] Data)
+ {
+ SHA1 sha = SHA1.Create();
+ byte[] ShaBytes = sha.ComputeHash(Data);
+ return BitConverter.ToInt32(ShaBytes, 0).ToString("X8");
+ }
+
+ public static String ReadString(Stream ms, int limit = -1)
+ {
+ int i = 0xFF;
+ int counter = 0;
+
+ MemoryStream StringStream = new MemoryStream();
+
+ do
+ {
+ i = ms.ReadByte();
+ if (i == 0 || counter == limit)
+ break;
+ StringStream.WriteByte((byte)i);
+ counter += 1;
+ }
+ while (true);
+
+ byte[] StringData = StringStream.ToArray();
+
+ String str = Encoding.UTF8.GetString(StringData);
+
+ return str;
+
+ }
+ }
+
+}