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

Fix returns/multiple scripts in GMS2.3+ #1191

Merged
merged 8 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 1 addition & 6 deletions UndertaleModLib/Compiler/AssemblyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,12 +1016,7 @@ private static void AssembleStatement(CodeWriter cw, Parser.Statement s, int rem
else
{
// Returns nothing, basically the same as exit
// TODO: I'm pretty sure the "remaining" part is actually just because the decompiler keeps adding
// returns and it's necessary to preserve 1:1 compilation
// But this workaround causes issue https://github.com/krzys-h/UndertaleModTool/issues/900
// So it would be fixed by cutting the "remaining" check here and removing the extra from decompilation.
if (!(CompileContext.GMS2_3 && remaining == 1))
AssembleExit(cw);
AssembleExit(cw);
}
break;
case Parser.Statement.StatementKind.Exit:
Expand Down
43 changes: 32 additions & 11 deletions UndertaleModLib/Decompiler/Decompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2065,17 +2065,38 @@ internal static void DecompileFromBlock(DecompileContext context, Dictionary<uin

case UndertaleInstruction.Opcode.Ret:
case UndertaleInstruction.Opcode.Exit:
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);
// 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)
{
List<uint> blockAddresses = new(blocks.Keys);
blockAddresses.Sort();
int nextBlockIndex = blockAddresses.IndexOf(block.Address ?? 0) + 1;
Jacky720 marked this conversation as resolved.
Show resolved Hide resolved
if (blockAddresses.Count > nextBlockIndex)
{
uint nextBlockAddress = blockAddresses[nextBlockIndex];
nextBlock = blocks[nextBlockAddress];
}
}

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