Skip to content

Commit

Permalink
Add UndertaleCode.Insert() and UndertaleInstruction.Clone() (by @…
Browse files Browse the repository at this point in the history
  • Loading branch information
VladiStep committed Dec 28, 2023
1 parent 34bebb6 commit 15f416f
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions UndertaleModLib/Models/UndertaleCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,42 @@ public static uint UnserializeChildObjectCount(UndertaleReader reader)
return 0;
}

public UndertaleInstruction Clone()
{
return new UndertaleInstruction()
{
Kind = this.Kind,
ComparisonKind = this.ComparisonKind,
Type1 = this.Type1,
Type2 = this.Type2,
TypeInst = this.TypeInst,
Value = (
this.Value != null
? (
this.Value.GetType() == typeof(Reference<UndertaleVariable>)
? new Reference<UndertaleVariable>(((Reference<UndertaleVariable>)(this.Value)).Target, ((Reference<UndertaleVariable>)(this.Value)).Type)
: (
this.Value.GetType() == typeof(Reference<UndertaleFunction>)
? new Reference<UndertaleFunction>(((Reference<UndertaleFunction>)(this.Value)).Target)
: (
this.Value.GetType() == typeof(UndertaleResourceById<UndertaleString, UndertaleChunkSTRG>)
? new UndertaleResourceById<UndertaleString, UndertaleChunkSTRG>(((UndertaleResourceById<UndertaleString, UndertaleChunkSTRG>)(this.Value)).Resource, ((UndertaleResourceById<UndertaleString, UndertaleChunkSTRG>)(this.Value)).CachedId)
: this.Value
)
)
)
: null
),
Destination = (this.Destination != null ? new Reference<UndertaleVariable>(this.Destination.Target, this.Destination.Type) : null),
Function = (this.Function != null ? new Reference<UndertaleFunction>(this.Function.Target, this.Function.Type) : null),
JumpOffset = this.JumpOffset,
JumpOffsetPopenvExitMagic = this.JumpOffsetPopenvExitMagic,
ArgumentsCount = this.ArgumentsCount,
Extra = this.Extra,
SwapExtra = this.SwapExtra,
};
}

/// <inheritdoc />
public override string ToString()
{
Expand Down Expand Up @@ -1343,6 +1379,35 @@ public void Replace(IList<UndertaleInstruction> instructions)
Append(instructions);
}

public void Insert(IList<UndertaleInstruction> instructions, int index)
{
uint offsetU = 0;
uint address = Instructions[index].Address;
foreach (UndertaleInstruction instr in instructions)
offsetU += instr.CalculateInstructionSize();

int offset = (int)offsetU;

for (int i = 0; i < Instructions.Count; i++)
{
if (UndertaleInstruction.GetInstructionType(Instructions[i].Kind) == UndertaleInstruction.InstructionType.GotoInstruction)
{
if (i < index && Instructions[i].Address + Instructions[i].JumpOffset > address)
{
Instructions[i] = Instructions[i].Clone();
Instructions[i].JumpOffset += offset;
}
else if (i > index && Instructions[i].Address + Instructions[i].JumpOffset <= address)
{
Instructions[i] = Instructions[i].Clone();
Instructions[i].JumpOffset -= offset;
}
}
}

Instructions.InsertRange(index + 1, instructions);
}

/// <summary>
/// Append GML instructions at the end of this code entry.
/// </summary>
Expand Down

0 comments on commit 15f416f

Please sign in to comment.