Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed disappearing returns #1680

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 21 additions & 33 deletions UndertaleModLib/Decompiler/Decompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,38 +199,18 @@ internal static void DecompileFromBlock(DecompileContext context, Dictionary<uin

case UndertaleInstruction.Opcode.Ret:
case UndertaleInstruction.Opcode.Exit:
// 2.3 scripts add exits to every script, even those that lack a return
// This detects that type of exit using the next block.
Block nextBlock = null;
if (DecompileContext.GMS2_3 && instr.Kind == UndertaleInstruction.Opcode.Exit)
{
uint[] blockAddresses = blocks.Keys.ToArray();
Array.Sort(blockAddresses);
int nextBlockIndex = Array.IndexOf(blockAddresses, block.Address ?? 0) + 1;
if (blockAddresses.Length > nextBlockIndex)
{
uint nextBlockAddress = blockAddresses[nextBlockIndex];
nextBlock = blocks[nextBlockAddress];
}
}
ReturnStatement stmt = new ReturnStatement(instr.Kind == UndertaleInstruction.Opcode.Ret ? stack.Pop() : null);
/*
This shouldn't be necessary: all unused things on the stack get converted to tempvars at the end anyway, and this fixes decompilation of repeat()
See #85

foreach (var expr in stack.Reverse())
if (!(expr is ExpressionTempVar))
statements.Add(expr);
stack.Clear();
*/
statements.Add(stmt);

if (!(nextBlock is not null
&& nextBlock.Instructions.Count > 0
&& nextBlock.Instructions[0].Kind == UndertaleInstruction.Opcode.Push
&& nextBlock.Instructions[0].Value.GetType() != typeof(int)))
{
ReturnStatement stmt = new ReturnStatement(instr.Kind == UndertaleInstruction.Opcode.Ret ? stack.Pop() : null);
/*
This shouldn't be necessary: all unused things on the stack get converted to tempvars at the end anyway, and this fixes decompilation of repeat()
See #85

foreach (var expr in stack.Reverse())
if (!(expr is ExpressionTempVar))
statements.Add(expr);
stack.Clear();
*/
statements.Add(stmt);
}
end = true;
returned = true;
break;
Expand Down Expand Up @@ -1438,8 +1418,16 @@ public static string Decompile(UndertaleCode code, GlobalDecompileContext global
DoTypePropagation(context, blocks);
context.Statements = new Dictionary<uint, List<Statement>>();
context.Statements.Add(0, HLDecompile(context, blocks, blocks[0], blocks[code.Length / 4]));
foreach (UndertaleCode duplicate in code.ChildEntries)
context.Statements.Add(duplicate.Offset / 4, HLDecompile(context, blocks, blocks[duplicate.Offset / 4], blocks[code.Length / 4]));
foreach (UndertaleCode duplicate in code.ChildEntries) {
List<Statement> statements = HLDecompile(context, blocks, blocks[duplicate.Offset / 4], blocks[code.Length / 4]);

// 2.3 scripts add exits to every script, even those that lack a return
// This removes that
if (DecompileContext.GMS2_3 && statements.Last() is ReturnStatement)
statements.RemoveAt(statements.Count - 1);

context.Statements.Add(duplicate.Offset / 4, statements);
}

// Write code.
context.IndentationLevel = 0;
Expand Down