diff --git a/pk3DS/Subforms/OWSE.cs b/pk3DS/Subforms/OWSE.cs index b1c6401..f24659f 100644 --- a/pk3DS/Subforms/OWSE.cs +++ b/pk3DS/Subforms/OWSE.cs @@ -131,6 +131,11 @@ private void getScriptData() L_MS0C.Text = "Decmp Length: 0x" + BitConverter.ToUInt32(ScriptData, 0xC).ToString("X4"); L_MS10.Text = "Junk Offset: 0x" + BitConverter.ToUInt32(ScriptData, 0x10).ToString("X4"); L_MS14.Text = "Reserved Size: 0x" + BitConverter.ToUInt32(ScriptData, 0x14).ToString("X4"); + + byte[] compressed = ScriptData.Skip(BitConverter.ToInt32(ScriptData, 0x8)).ToArray(); + string c = Util.getHexString(compressed); + byte[] decompressed = Scripts.decompressScript(compressed); + string d = Util.getHexString(decompressed); } } private void getOWSData() @@ -182,7 +187,11 @@ private void getOWSData() L_SL0C.Text = "Decmp Length: 0x" + BitConverter.ToUInt32(ScriptData, 0xC).ToString("X4"); L_SL10.Text = "Junk Offset: 0x" + BitConverter.ToUInt32(ScriptData, 0x10).ToString("X4"); L_SL14.Text = "Reserved Size: 0x" + BitConverter.ToUInt32(ScriptData, 0x14).ToString("X4"); + + byte[] compressed = ScriptData.Skip(BitConverter.ToInt32(ScriptData, 0x8)).ToArray(); + byte[] decompressed = Scripts.decompressScript(compressed); } + } private byte[] setOWSData() diff --git a/pk3DS/Tools/Scripts.cs b/pk3DS/Tools/Scripts.cs new file mode 100644 index 0000000..da76a60 --- /dev/null +++ b/pk3DS/Tools/Scripts.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace pk3DS +{ + // Big thanks to FireFly for figuring out the 7-bit compression routine for scripts. + class Scripts + { + internal static byte[] decompressScript(byte[] data) + { + using (BinaryReader br = new BinaryReader(new MemoryStream(data))) + using (MemoryStream mn = new MemoryStream()) + using (BinaryWriter bw = new BinaryWriter(mn)) + { + // Read away! + while (br.BaseStream.Position < br.BaseStream.Length) + { + // Read until the top bit is not set + Listraw = new List(); + byte val = br.ReadByte(); raw.Add(val); + while ((val >> 7) > 0) + { + val = br.ReadByte(); raw.Add(val); + } + + byte[] compressedBytes = raw.ToArray(); + Array.Reverse(compressedBytes); + uint cmd = val; + + // Assemble the bits into the final uint + for (int i = 0; i < compressedBytes.Length; i++) + cmd |= (uint)(compressedBytes[i] & 0x7F) << (7 * i); + + bw.Write(cmd); + } + return mn.ToArray(); + } + } + internal static byte[] compressScript(byte[] data) + { + using (BinaryReader br = new BinaryReader(new MemoryStream(data))) + using (MemoryStream mn = new MemoryStream()) + using (BinaryWriter bw = new BinaryWriter(mn)) + { + // Compress away! + while (br.BaseStream.Position < br.BaseStream.Length) + { + // Read the command to compress + uint val = br.ReadUInt32(); + + // Write byte if more bits are needed + while ((val >> 7) > 0) // 8th+ bit is set + { + bw.Write((byte)((val & 0x7F) | 0x80)); + val >>= 7; + } + // Write the remaining bits + bw.Write((byte)(val & 0x7F)); + } + return mn.ToArray(); + } + } + } +} diff --git a/pk3DS/pk3DS.csproj b/pk3DS/pk3DS.csproj index 8cc1bff..9860a80 100644 --- a/pk3DS/pk3DS.csproj +++ b/pk3DS/pk3DS.csproj @@ -186,6 +186,7 @@ XYWE.cs + Form