Skip to content

Commit

Permalink
Fix script patches for RDR
Browse files Browse the repository at this point in the history
  • Loading branch information
maybegreat48 committed Jul 14, 2024
1 parent 6583818 commit f4139cc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
10 changes: 9 additions & 1 deletion GTA V Script Decompiler/Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,14 @@ public static Opcode MapOpcode(byte opcode)
throw new InvalidOperationException("Invalid RDR2 opcode");
}

public static byte UnmapOpcode(Opcode opcode)
{
if (!Properties.Settings.Default.IsRDR2)
return (byte)opcode;

return (byte)ShuffledOpcodes.First(x => x.Value == opcode).Key;
}

public Opcode Opcode { get; private set; }
public Opcode OriginalOpcode { get; private set; }
public byte[] Operands { get; private set; }
Expand All @@ -345,7 +353,7 @@ public void NopInstruction()
Opcode = Opcode.NOP;
}

public int UnmappedOpcode { get { return ShuffledOpcodes.First(x => x.Value == OriginalOpcode).Key; } }
public int UnmappedOpcode { get { return UnmapOpcode(OriginalOpcode); } }

public int Offset { get; }

Expand Down
10 changes: 5 additions & 5 deletions GTA V Script Decompiler/Patches/ChangeConstantValuePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ public override byte[] GetPatch(int start, int end)
List<byte> bytes = new();

if (constantType == ConstantType.SHORTHAND)
bytes.Add((byte)(((byte)Opcode.PUSH_CONST_0) + Value));
bytes.Add(Instruction.UnmapOpcode((Opcode)(((byte)Opcode.PUSH_CONST_0) + Value)));
else if (constantType == ConstantType.U8)
{
bytes.Add((byte)Opcode.PUSH_CONST_U8);
bytes.Add(Instruction.UnmapOpcode(Opcode.PUSH_CONST_U8));
bytes.Add((byte)Value);
}
else if (constantType == ConstantType.U16)
{
bytes.Add((byte)Opcode.PUSH_CONST_S16);
bytes.Add(Instruction.UnmapOpcode(Opcode.PUSH_CONST_S16));
bytes.AddRange(BitConverter.GetBytes((short)Value));
}
else if (constantType == ConstantType.U24)
{
bytes.Add((byte)Opcode.PUSH_CONST_U24);
bytes.Add(Instruction.UnmapOpcode(Opcode.PUSH_CONST_U24));
bytes.AddRange(BitConverter.GetBytes(Value).Skip(1)); // todo does this work?
}
else if (constantType == ConstantType.U32)
{
bytes.Add((byte)Opcode.PUSH_CONST_U32);
bytes.Add(Instruction.UnmapOpcode(Opcode.PUSH_CONST_U32));
bytes.AddRange(BitConverter.GetBytes(Value));
}

Expand Down
4 changes: 2 additions & 2 deletions GTA V Script Decompiler/Patches/NopInstructionPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public override byte[] GetPatch(int start, int end)

for (var i = start; i < end; i++)
{
bytes.Add(0);
bytes.Add(Instruction.UnmapOpcode(Opcode.NOP));

foreach (var _ in Function.Instructions[i].Operands)
{
bytes.Add(0);
bytes.Add(Instruction.UnmapOpcode(Opcode.NOP));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public override byte[] GetPatch(int start, int end)

for (var i = start; i < end; i++)
{
bytes.Add((byte)Opcode.DROP);
bytes.Add(Instruction.UnmapOpcode(Opcode.DROP));

foreach (var _ in Function.Instructions[i].Operands)
{
bytes.Add(0);
bytes.Add(Instruction.UnmapOpcode(Opcode.NOP));
}
}

Expand Down
8 changes: 4 additions & 4 deletions GTA V Script Decompiler/Patches/ReturnFunctionPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ public override byte[] GetPatch(int start, int end)
if (Function.NumReturns != 0)
{
if (ReturnValue <= 7)
bytes.Add((byte)(((byte)Opcode.PUSH_CONST_0) + ReturnValue));
bytes.Add(Instruction.UnmapOpcode((Opcode)(((byte)Opcode.PUSH_CONST_0) + ReturnValue)));
else if (ReturnValue <= 65535)
{
bytes.Add((byte)Opcode.PUSH_CONST_S16);
bytes.Add(Instruction.UnmapOpcode(Opcode.PUSH_CONST_S16));
bytes.AddRange(BitConverter.GetBytes((short)ReturnValue));
}
else
{
bytes.Add((byte)Opcode.PUSH_CONST_U32);
bytes.Add(Instruction.UnmapOpcode(Opcode.PUSH_CONST_U32));
bytes.AddRange(BitConverter.GetBytes(ReturnValue));
}
}

bytes.Add((byte)Opcode.LEAVE);
bytes.Add(Instruction.UnmapOpcode(Opcode.LEAVE));
bytes.Add((byte)Function.NumParams);
bytes.Add((byte)Function.NumReturns);

Expand Down

0 comments on commit f4139cc

Please sign in to comment.