Beginnings of script decompression

Very WIP; it's a bit more complicated than just a bit 'continue' flag.

Top bit is continue; next highest is quite likely a sign flag.
This commit is contained in:
Kurt
2015-05-20 22:23:50 -07:00
parent b1a36a985c
commit dde2d6ce1b
3 changed files with 75 additions and 0 deletions

View File

@@ -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()

65
pk3DS/Tools/Scripts.cs Normal file
View File

@@ -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
List<byte>raw = new List<byte>();
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();
}
}
}
}

View File

@@ -186,6 +186,7 @@
<Compile Include="Subforms\XYWE.designer.cs">
<DependentUpon>XYWE.cs</DependentUpon>
</Compile>
<Compile Include="Tools\Scripts.cs" />
<Compile Include="Tools\ToolsUI.cs">
<SubType>Form</SubType>
</Compile>