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

Add support for new bytecode weirdness #1751

Merged
Merged
Show file tree
Hide file tree
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
15 changes: 13 additions & 2 deletions UndertaleModLib/Decompiler/Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,19 @@ public static UndertaleInstruction AssembleOne(string source, IList<UndertaleFun
instr.Value = breakId;
if (breakId == -11) // pushref
{
// parse additional int argument
instr.IntArgument = Int32.Parse(line);
// Parse additional int argument
if (Int32.TryParse(line, out int intArgument))
{
instr.IntArgument = intArgument;
}
else
{
// Or alternatively parse function!
var f = data.Functions.ByName(line);
if (f == null)
throw new Exception("Function in pushref not found: " + line);
instr.Function = new UndertaleInstruction.Reference<UndertaleFunction>(f);
}
}
}
else
Expand Down
5 changes: 5 additions & 0 deletions UndertaleModLib/Decompiler/Decompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,11 @@ assign.Value is FunctionDefinition funcDef &&
// Note that this operator peeks from the stack, it does not pop directly.
break;
case -11: // GM 2023.8+, pushref
if (instr.Function != null)
{
stack.Push(new ExpressionConstant(UndertaleInstruction.DataType.Int32, instr.Function));
break;
}
stack.Push(new ExpressionAssetRef(instr.IntArgument));
break;
}
Expand Down
2 changes: 1 addition & 1 deletion UndertaleModLib/Decompiler/Disassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class Disassembler
{
private static void AppendLocalVarDefinitionsToStringBuilder(StringBuilder sb, UndertaleCode code, IList<UndertaleVariable> vars, UndertaleCodeLocals locals)
{
if (code.WeirdLocalFlag)
if (code.WeirdLocalFlag && locals is null)
{
return;
}
Expand Down
27 changes: 22 additions & 5 deletions UndertaleModLib/Models/UndertaleCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,18 @@ public static void ParseReferenceChain(UndertaleReader reader, T obj)
public Reference<T> GetReference<T>(bool allowResolve = false) where T : class, UndertaleObject, ReferencedObject
{
Reference<T> res = (Destination as Reference<T>) ?? (Function as Reference<T>) ?? (Value as Reference<T>);
if (allowResolve && res == null && Value is int val)
if (allowResolve && res == null)
{
Value = new Reference<T>(val);
return (Reference<T>)Value;
if (Kind == Opcode.Break && Value is short breakType && breakType == -11 /* pushref */)
{
Function = new Reference<UndertaleFunction>(IntArgument);
return Function as Reference<T>;
}
if (Value is int val)
{
Value = new Reference<T>(val);
return (Reference<T>)Value;
}
}
return res;
}
Expand Down Expand Up @@ -563,7 +571,13 @@ public void Serialize(UndertaleWriter writer)
writer.Write((short)Value);
writer.Write((byte)Type1);
writer.Write((byte)Kind);
if (Type1 == DataType.Int32) writer.Write(IntArgument);
if (Type1 == DataType.Int32)
{
if (Function != null)
writer.WriteUndertaleObject(Function);
else
writer.Write(IntArgument);
}
}
break;

Expand Down Expand Up @@ -1008,7 +1022,10 @@ public void ToString(StringBuilder stringBuilder, UndertaleCode code, List<uint>
if (Type1 == DataType.Int32)
{
sbh.Append(stringBuilder, ' ');
sbh.Append(stringBuilder, IntArgument);
if (Function != null)
sbh.Append(stringBuilder, Function);
else
sbh.Append(stringBuilder, IntArgument);
}
break;
}
Expand Down
Loading