unit decompile; interface uses SysUtils, labelsvars, opcodefile, Dialogs; type TOpcode = record Opcode: Word; PType: Array of Byte; P: Array of String; end; function GetParam(PType: Byte; raw: TRawCode; var pos: Integer): String; function GetInt8(raw: TRawCode; var pos: Integer): String; function GetInt16(raw: TRawCode; var pos: Integer): String; function GetInt32(raw: TRawCode; var pos: Integer): String; function GetGlobal(raw: TRawCode; var pos: Integer): String; function GetLocal(raw: TRawCode; var pos: Integer): String; function GetFloat(raw: TRawCode; var pos: Integer): String; function GetGlobalArray(raw: TRawCode; var pos: Integer): String; function GetLocalArray(raw: TRawCode; var pos: Integer): String; function GetName(raw: TRawCode; var pos: Integer): String; function GetGlobalName(raw: TRawCode; var pos: Integer): String; function GetLocalName(raw: TRawCode; var pos: Integer): String; function GetGlobalNameArray(raw: TRawCode; var pos: Integer): String; function GetLocalNameArray(raw: TRawCode; var pos: Integer): String; function GetString(raw: TRawCode; var pos: Integer): String; function GetGlobalString(raw: TRawCode; var pos: Integer): String; function GetLocalString(raw: TRawCode; var pos: Integer): String; function GetGlobalStringArray(raw: TRawCode; var pos: Integer): String; function GetLocalStringArray(raw: TRawCode; var pos: Integer): String; function DecompileOpcode(raw: TRawCode; var pos: Integer): String; implementation function GetParam(PType: Byte; raw: TRawCode; var pos: Integer): String; begin Result:= '!error!'; case PType of $01: Result:= GetInt32(raw,pos); $02: Result:= GetGlobal(raw,pos); $03: Result:= GetLocal(raw,pos); $04: Result:= GetInt8(raw,pos); $05: Result:= GetInt16(raw,pos); $06: Result:= GetFloat(raw,pos); $07: Result:= GetGlobalArray(raw,pos); $08: Result:= GetLocalArray(raw,pos); $09: Result:= GetName(raw,pos); $0A: Result:= GetGlobalName(raw,pos); $0B: Result:= GetLocalName(raw,pos); $0C: Result:= GetGlobalArray(raw,pos); $0D: Result:= GetLocalArray(raw,pos); $0E: Result:= GetString(raw,pos); $10: Result:= GetGlobalString(raw,pos); $11: Result:= GetLocalString(raw,pos); $12: Result:= GetGlobalArray(raw,pos); $13: Result:= GetLocalArray(raw,pos); end; end; function GetInt8(raw: TRawCode; var pos: Integer): String; var i: Byte; begin Move(raw[pos],i,1); pos:= pos + 1; Result:= inttostr(i); end; function GetInt16(raw: TRawCode; var pos: Integer): String; var i: Word; begin Move(raw[pos],i,2); pos:= pos + 2; Result:= inttostr(i); end; function GetInt32(raw: TRawCode; var pos: Integer): String; var i: Longword; begin Move(raw[pos],i,4); pos:= pos + 4; Result:= inttohex(i,8); end; function GetGlobal(raw: TRawCode; var pos: Integer): String; var i: Word; begin Move(raw[pos],i,2); pos:= pos + 2; Result:= '$' + inttohex(i,4); end; function GetLocal(raw: TRawCode; var pos: Integer): String; var i: Word; begin Move(raw[pos],i,2); pos:= pos + 2; Result:= '&' + inttostr(i); end; function GetFloat(raw: TRawCode; var pos: Integer): String; var i: Single; begin Move(raw[pos],i,4); pos:= pos + 4; Result:= floattostr(i); end; function GetGlobalArray(raw: TRawCode; var pos: Integer): String; var arrayvar: Word; indexvar: Word; items: Byte; indextype: Byte; begin Move(raw[pos],arrayvar,2); pos:= pos + 2; Move(raw[pos],indexvar,2); pos:= pos + 2; Move(raw[pos],items,1); pos:= pos + 1; Move(raw[pos],indextype,1); pos:= pos + 1; //Result:= 'global array '+'$'+inttohex(arrayvar,4)+'['+inttostr(items)+'] of '; Result:= '$'+inttohex(arrayvar,4)+'['; case indextype of 00: Result:= Result + '&' + inttostr(indexvar)+'] //array[0..'+inttostr(items)+'] of Integer'; 01: Result:= Result + '&' + inttostr(indexvar)+'] //array[0..'+inttostr(items)+'] of Float'; 02: Result:= Result + '&' + inttostr(indexvar)+'] //array[0..'+inttostr(items)+'] of Name'; 03: Result:= Result + '&' + inttostr(indexvar)+'] //array[0..'+inttostr(items)+'] of String'; 80: Result:= Result + '$' + inttohex(indexvar,4)+'] //array[0..'+inttostr(items)+'] of Integer'; 81: Result:= Result + '$' + inttohex(indexvar,4)+'] //array[0..'+inttostr(items)+'] of Float'; 82: Result:= Result + '$' + inttohex(indexvar,4)+'] //array[0..'+inttostr(items)+'] of Name'; 83: Result:= Result + '$' + inttohex(indexvar,4)+'] //array[0..'+inttostr(items)+'] of String'; end; end; function GetLocalArray(raw: TRawCode; var pos: Integer): String; var arrayvar: Word; indexvar: Word; items: Byte; indextype: Byte; begin Move(raw[pos],arrayvar,2); pos:= pos + 2; Move(raw[pos],indexvar,2); pos:= pos + 2; Move(raw[pos],items,1); pos:= pos + 1; Move(raw[pos],indextype,1); pos:= pos + 1; //Result:= 'local array '+'&'+inttostr(arrayvar)+'['+inttostr(items)+'] of '; Result:= '&'+inttostr(arrayvar)+'['; case indextype of 00: Result:= Result + '&' + inttostr(indexvar)+'] //array[0..'+inttostr(items)+'] of Integer'; 01: Result:= Result + '&' + inttostr(indexvar)+'] //array[0..'+inttostr(items)+'] of Float'; 02: Result:= Result + '&' + inttostr(indexvar)+'] //array[0..'+inttostr(items)+'] of Name'; 03: Result:= Result + '&' + inttostr(indexvar)+'] //array[0..'+inttostr(items)+'] of String'; 80: Result:= Result + '$' + inttohex(indexvar,4)+'] //array[0..'+inttostr(items)+'] of Integer'; 81: Result:= Result + '$' + inttohex(indexvar,4)+'] //array[0..'+inttostr(items)+'] of Float'; 82: Result:= Result + '$' + inttohex(indexvar,4)+'] //array[0..'+inttostr(items)+'] of Name'; 83: Result:= Result + '$' + inttohex(indexvar,4)+'] //array[0..'+inttostr(items)+'] of String'; end; end; function GetName(raw: TRawCode; var pos: Integer): String; var j: Integer; begin j:= pos; Result:= '"'; while raw[j] > $20 do begin Result:= Result + Chr(raw[j]); Inc(j); end; Result:= Result + '"'; pos:= pos + 8; end; function GetGlobalName(raw: TRawCode; var pos: Integer): String; var i: Word; begin Move(raw[pos],i,2); pos:= pos + 2; Result:= '$' + inttohex(i,4); end; function GetLocalName(raw: TRawCode; var pos: Integer): String; var i: Word; begin Move(raw[pos],i,2); pos:= pos + 2; Result:= '&' + inttostr(i); end; function GetGlobalNameArray(raw: TRawCode; var pos: Integer): String; begin end; function GetLocalNameArray(raw: TRawCode; var pos: Integer): String; begin end; function GetString(raw: TRawCode; var pos: Integer): String; var Size: Byte; i: Integer; begin Move(raw[pos],Size,1); pos:= pos + 1; i:= 0; Result:= '"'; while i < Size do begin Result:= Result + Chr(raw[pos]); Inc(pos); Inc(i); end; Result:= Result + '"'; end; function GetGlobalString(raw: TRawCode; var pos: Integer): String; var i: Word; begin Move(raw[pos],i,2); pos:= pos + 2; Result:= '$' + inttohex(i,4); end; function GetLocalString(raw: TRawCode; var pos: Integer): String; var i: Word; begin Move(raw[pos],i,2); pos:= pos + 2; Result:= '&' + inttostr(i); end; function GetGlobalStringArray(raw: TRawCode; var pos: Integer): String; begin end; function GetLocalStringArray(raw: TRawCode; var pos: Integer): String; begin end; function DecompileOpcode(raw: TRawCode; var pos: Integer): String; var Opcode: TOpcode; i,x,j: Integer; begin Move(raw[pos],Opcode.Opcode,2); pos:= pos + 2; try x:= opcodefile.OpcodeDefs.GetParamNum(Opcode.Opcode); j:= x; if x = -1 then begin i:= Length(Opcode.PType); SetLength(Opcode.PType,i+1); Move(raw[pos],Opcode.PType[i],1); pos:= pos + 1; SetLength(Opcode.P,i+1); Opcode.P[i]:= GetParam(Opcode.PType[i],raw,pos); while (raw[pos] <> $00) do begin i:= Length(Opcode.PType); SetLength(Opcode.PType,i+1); Move(raw[pos],Opcode.PType[i],1); pos:= pos + 1; SetLength(Opcode.P,i+1); Opcode.P[i]:= GetParam(Opcode.PType[i],raw,pos); end; Inc(pos); {pos:= pos - 1; SetLength(Opcode.PType,i); SetLength(Opcode.P,i);} x:= i+1; end else begin while x > 0 do begin i:= Length(Opcode.PType); SetLength(Opcode.PType,i+1); Move(raw[pos],Opcode.PType[i],1); pos:= pos + 1; SetLength(Opcode.P,i+1); Opcode.P[i]:= GetParam(Opcode.PType[i],raw,pos); x:= x - 1; end; x:= j; end; Result:= opcodefile.OpcodeDefs.GetName(Opcode.Opcode) + ' '; i:= 0; while i < x do begin Result:= Result + Opcode.P[i] + ', '; Inc(i); end; if x = 0 then Delete(Result,Length(Result),1) else Delete(Result,Length(Result)-1,2); except Result:= ''; end; end; end.