Fix Decompression length not matching

Still have to fix signs.
This commit is contained in:
Kurt
2015-05-21 20:40:18 -07:00
parent 1dc2227ee2
commit c8dc7b6e6f
2 changed files with 30 additions and 20 deletions

View File

@@ -123,15 +123,15 @@ private void getScriptData()
if (data.Length > 4)
{
uint length = BitConverter.ToUInt32(data, 0);
byte[] ScriptData = data.Skip(4).Take(data.Length - 4).ToArray();
byte[] ScriptData = data;
RTB_MS.Text = Util.getHexString(ScriptData);
RTB_MS.Width = RTB_MS.Text.Length < 25 * 3 * 16 ? 245 : 260;
int start = BitConverter.ToInt32(ScriptData, 0x8);
int declen = BitConverter.ToInt32(ScriptData, 0xC);
int junkoff = BitConverter.ToInt32(ScriptData, 0x10);
int reserved = BitConverter.ToInt32(ScriptData, 0x14);
int start = BitConverter.ToInt32(ScriptData, 0xC);
int declen = BitConverter.ToInt32(ScriptData, 0x10);
int junkoff = BitConverter.ToInt32(ScriptData, 0x14);
int reserved = BitConverter.ToInt32(ScriptData, 0x18);
L_MS08.Text = "Data Start :0x" + start.ToString("X4");
L_MS0C.Text = "Decmp Length: 0x" + declen.ToString("X4");
@@ -140,8 +140,11 @@ private void getScriptData()
byte[] compressed = ScriptData.Skip(start).ToArray();
string c = Util.getHexString(compressed);
compressed = File.ReadAllBytes("raw.bin");
byte[] decompressed = Scripts.decompressScript(compressed);
string d = Util.getHexString(decompressed);
RTB_MS.Text = Scripts.getu32line(decompressed);
}
}
private void getOWSData()
@@ -185,15 +188,15 @@ private void getOWSData()
OWScriptData = data.Skip(4 + owData.Length).Take(data.Length - 4 - owData.Length).ToArray();
if (OWScriptData.Length > 4)
{
byte[] ScriptData = OWScriptData.Skip(4).Take(OWScriptData.Length - 4).ToArray();
byte[] ScriptData = OWScriptData;
RTB_S.Text = Util.getHexString(ScriptData);
RTB_S.Width = RTB_S.Text.Length < 25 * 3 * 16 ? 245 : 260;
int start = BitConverter.ToInt32(ScriptData, 0x8);
int declen = BitConverter.ToInt32(ScriptData, 0xC);
int junkoff = BitConverter.ToInt32(ScriptData, 0x10);
int reserved = BitConverter.ToInt32(ScriptData, 0x14);
int start = BitConverter.ToInt32(ScriptData, 0xC);
int declen = BitConverter.ToInt32(ScriptData, 0x10);
int junkoff = BitConverter.ToInt32(ScriptData, 0x14);
int reserved = BitConverter.ToInt32(ScriptData, 0x18);
L_SL08.Text = "Data Start :0x" + start.ToString("X4");
L_SL0C.Text = "Decmp Length: 0x" + declen.ToString("X4");
@@ -204,6 +207,8 @@ private void getOWSData()
string c = Util.getHexString(compressed);
byte[] decompressed = Scripts.decompressScript(compressed);
string d = Util.getHexString(decompressed);
RTB_S.Text = Scripts.getu32line(decompressed);
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace pk3DS
{
@@ -19,13 +20,10 @@ internal static byte[] decompressScript(byte[] data)
// Read until the top bit is not set
List<byte>raw = new List<byte>();
byte val = br.ReadByte(); raw.Add(val);
if ((val >> 6) > 0)
while ((val >> 7) > 0)
{
if (br.BaseStream.Position == br.BaseStream.Length) return mn.ToArray();
val = br.ReadByte(); raw.Add(val);
while ((val >> 7) > 0)
{
val = br.ReadByte(); raw.Add(val);
}
}
byte[] compressedBytes = raw.ToArray();
@@ -35,8 +33,7 @@ internal static byte[] decompressScript(byte[] data)
if ((val & 0x40) > 0) // Signed Parameter
{
// Check the next bytecode
byte second = compressedBytes[1];
if ((second >> 7) > 0) // Many-bits-required command
if (compressedBytes.Length > 1 && (compressedBytes[1] >> 7) > 0) // Many-bits-required command
{
// 2 Byte Signed Parameter
// Process Many-bits instructional
@@ -44,7 +41,7 @@ internal static byte[] decompressScript(byte[] data)
int cmd = (compressedBytes[1] & 0x7F << 7) | compressedBytes[2];
bw.Write(BitConverter.GetBytes((ushort)cmd), 0, 2);
int deviation = ((val >> 1) & 0x3F) - 0x40;
int deviation = -((val & 0x3F) >> 1);
bw.Write(BitConverter.GetBytes(deviation), 0, 2);
}
else if ((val >> 7) > 0)
@@ -53,13 +50,13 @@ internal static byte[] decompressScript(byte[] data)
// Process Many-bits instructional
bw.Write((byte)(((val & 0x1) << 7) | compressedBytes[1])); // bottom bit is sent to low byte as the 8th bit
int deviation = ((val >> 1) & 0x3F) - 0x40;
int deviation = ((val & 0x3F) >> 1) - 0x40;
bw.Write(BitConverter.GetBytes(deviation), 0, 3);
}
else
{
// 4 Byte Signed Parameter
int deviation = ((val >> 1) & 0x3F) - 0x40;
int deviation = ((val & 0x3F) >> 1) - 0x40;
bw.Write(BitConverter.GetBytes(deviation), 0, 4);
}
}
@@ -109,5 +106,13 @@ internal static byte[] compressScript(byte[] data)
return mn.ToArray();
}
}
internal static string getu32line(byte[] data)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i+= 4)
sb.Append(BitConverter.ToString(data, i, 4).Replace('-', ' ') + "\r\n");
return sb.ToString();
}
}
}