Skip to content

Commit

Permalink
Merge pull request UnderminersTeam#1751 from colinator27/2024_bytecod…
Browse files Browse the repository at this point in the history
…e_weirdness

Add support for new bytecode weirdness
  • Loading branch information
colinator27 authored May 3, 2024
2 parents f038c18 + 48830d7 commit 21ecfe5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
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

0 comments on commit 21ecfe5

Please sign in to comment.