Commit e59f2a7a authored by Spencer Alves's avatar Spencer Alves

Code reassembly mostly complete, now working on reference tracking

parent 27f61783
......@@ -42,7 +42,7 @@ namespace Altar
public unsafe struct RefDefEntryWithOthers
{
public uint NameOffset;
public uint _pad0; // unknown, some flags?
public int InstanceType;
public uint _pad1; // unknown
public uint Occurrences;
public uint FirstAddress;
......
......@@ -259,6 +259,17 @@ namespace Altar
SetCAndWr(cl, ct, O_PAREN + (i + 1) + SLASH + f.Code.Length + C_PAREN);
File.WriteAllText(od + DIR_CODE + f.Code[i].Name + EXT_GML_ASM, Disassembler.DisplayInstructions(f, i, eo.AbsoluteAddresses));
/*BinBuffer bb = new BinBuffer();
for (uint j = 0; j < f.Code[i].Instructions.Length; j++)
{
var instr = f.Code[i].Instructions[j];
var isize = DisasmExt.Size(instr, f.General.BytecodeVersion)*4;
bb.Write((IntPtr)instr, (int)isize);
}
bb.Position = 0;
File.WriteAllBytes(od + DIR_CODE + f.Code[i].Name + EXT_BIN, bb.ReadBytes(bb.Size));*/
}
Console.WriteLine();
}
......@@ -561,6 +572,9 @@ namespace Altar
var texpChunk = new BBData(new BinBuffer(), new int[0]);
int[] texPagOffsets = SectionWriter.WriteTexturePages(texpChunk, f.TexturePages);
var codeChunk = new BBData(new BinBuffer(), new int[0]);
var codeChunkStringOffsetOffsets = SectionWriter.WriteCodes(codeChunk, f, stringOffsets);
var output = Path.GetFullPath(opt.OutputFile);
var offsets = new int[0];
......@@ -572,6 +586,8 @@ namespace Altar
int stringsChunkPosition = 0;
var texpOffsetOffsets = new List<int>();
int texpChunkPosition = 0;
var codeOffsetOffsets = new List<int>();
int codeChunkPosition = 0;
foreach (var chunkFile in projFile["chunks"])
{
......@@ -581,6 +597,7 @@ namespace Altar
BBData chunk = new BBData(new BinBuffer(), new int[0]);
int[] chunkStringOffsetOffsets = null;
int[] chunkTexpOffsetOffsets = null;
int[] chunkCodeOffsetOffsets = null;
switch (chunkId)
{
case SectionHeaders.General:
......@@ -621,16 +638,20 @@ namespace Altar
texpChunkPosition = writer.Buffer.Position + 8;
break;
case SectionHeaders.Code:
chunkStringOffsetOffsets = SectionWriter.WriteCodes(chunk, f, stringOffsets);
chunk = codeChunk;
chunkStringOffsetOffsets = codeChunkStringOffsetOffsets;
codeChunkPosition = writer.Buffer.Position + 8;
break;
case SectionHeaders.Variables:
if (f.VariableExtra != null)
foreach (var e in f.VariableExtra)
chunk.Buffer.Write(e);
chunkStringOffsetOffsets = SectionWriter.WriteRefDefs(chunk, f.RefData.Variables, stringOffsets, f.General.IsOldBCVersion, false);
SectionWriter.WriteRefDefs(chunk, f.RefData.Variables, stringOffsets, f.General.IsOldBCVersion, false,
out chunkStringOffsetOffsets, out chunkCodeOffsetOffsets);
break;
case SectionHeaders.Functions:
chunkStringOffsetOffsets = SectionWriter.WriteRefDefs(chunk, f.RefData.Functions, stringOffsets, f.General.IsOldBCVersion, true);
SectionWriter.WriteRefDefs(chunk, f.RefData.Functions, stringOffsets, f.General.IsOldBCVersion, true,
out chunkStringOffsetOffsets, out chunkCodeOffsetOffsets);
chunkStringOffsetOffsets = chunkStringOffsetOffsets.Concat(SectionWriter.WriteFunctionLocals(chunk, f.FunctionLocals, stringOffsets)).ToArray();
break;
case SectionHeaders.Strings:
......@@ -653,20 +674,28 @@ namespace Altar
}
if (chunkStringOffsetOffsets != null)
{
foreach (var stringOffset in chunkStringOffsetOffsets)
foreach (var offset in chunkStringOffsetOffsets)
{
stringOffsetOffsets.Add(stringOffset + writer.Buffer.Position);
stringOffsetOffsets.Add(offset + writer.Buffer.Position);
}
}
chunkStringOffsetOffsets = null;
if (chunkTexpOffsetOffsets != null)
{
foreach (var texpOffset in chunkTexpOffsetOffsets)
foreach (var offset in chunkTexpOffsetOffsets)
{
texpOffsetOffsets.Add(texpOffset + writer.Buffer.Position);
texpOffsetOffsets.Add(offset + writer.Buffer.Position);
}
}
chunkTexpOffsetOffsets = null;
if (chunkCodeOffsetOffsets != null)
{
foreach (var offset in chunkCodeOffsetOffsets)
{
codeOffsetOffsets.Add(offset + writer.Buffer.Position);
}
}
chunkCodeOffsetOffsets = null;
SectionWriter.WriteChunk(writer, chunkId, chunk);
}
......@@ -690,6 +719,14 @@ namespace Altar
writer.Buffer.Write(o + texpChunkPosition);
}
foreach (var codeOffset in codeOffsetOffsets)
{
writer.Buffer.Position = codeOffset;
var o = writer.Buffer.ReadInt32();
//bb.Position -= sizeof(int);
writer.Buffer.Write(o + codeChunkPosition);
}
writer.Buffer.Position = 0;
File.WriteAllBytes(output, writer.Buffer.ReadBytes(writer.Buffer.Size));
}
......
This diff is collapsed.
This diff is collapsed.
......@@ -13,7 +13,7 @@ namespace Altar
public uint Occurrences;
public uint FirstOffset;
public bool HasExtra;
public uint unknown1;
public InstanceType InstanceType;
public uint unknown2;
}
......@@ -187,6 +187,8 @@ namespace Altar
public int Size;
internal AnyInstruction[] InstructionsCopy; // I ain't dealin' with no pointers
// (TODO: don't use pointers)
internal IDictionary<Tuple<string, InstanceType>, IList<uint>> functionReferences;
internal IDictionary<Tuple<string, InstanceType>, IList<uint>> variableReferences;
}
[StructLayout(LayoutKind.Sequential)]
public struct TextureInfo
......
......@@ -728,7 +728,7 @@ namespace Altar.Unpack
ret.Occurrences = rde->Occurrences;
ret.FirstOffset = rde->FirstAddress;
ret.HasExtra = true;
ret.unknown1 = rde->_pad0;
ret.InstanceType = (Decomp.InstanceType)rde->InstanceType;
ret.unknown2 = rde->_pad1;
return ret;
......
......@@ -454,16 +454,20 @@ namespace Altar.Unpack
private static JsonData SerializeReferenceDef(ReferenceDef rd)
{
if (!rd.HasExtra)
{
return new JsonData(rd.Name);
}
var r = CreateObj();
r["name"] = rd.Name;
r["occurrences"] = rd.Occurrences;
if (rd.FirstOffset != 0xFFFFFFFF)
r["firstoffset"] = rd.FirstOffset;
//r["occurrences"] = rd.Occurrences;
//if (rd.FirstOffset != 0xFFFFFFFF)
// r["firstoffset"] = rd.FirstOffset;
if (rd.HasExtra)
{
r["unknown1"] = rd.unknown1;
r["unknown2"] = rd.unknown2;
r["instancetype"] = (int)rd.InstanceType;
r["unknown"] = rd.unknown2;
}
return r;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment