public static string Disassemble(this UndertaleCode code, IList <UndertaleVariable> vars, UndertaleCodeLocals locals)
        {
            StringBuilder sb = new StringBuilder();

            if (locals == null && !code.WeirdLocalFlag)
            {
                sb.Append("; WARNING: Missing code locals, possibly due to unsupported bytecode version or a brand new code entry.\n");
            }
            else
            {
                sb.Append(code.GenerateLocalVarDefinitions(vars, locals));
            }

            Dictionary <uint, string> fragments = new Dictionary <uint, string>();

            foreach (var dup in code.Duplicates)
            {
                fragments.Add(dup.Offset / 4, (dup.Name?.Content ?? "<null>") + $" (locals={dup.LocalsCount}, argc={dup.ArgumentsCount})");
            }
            List <uint> blocks = FindBlockAddresses(code);

            foreach (var inst in code.Instructions)
            {
                bool doNewline = true;
                if (fragments.TryGetValue(inst.Address, out string entry))
                {
                    sb.AppendLine();
                    sb.AppendLine($"> {entry}");
                    doNewline = false;
                }

                int ind = blocks.IndexOf(inst.Address);
                if (ind != -1)
                {
                    if (doNewline)
                    {
                        sb.AppendLine();
                    }
                    sb.AppendLine($":[{ind}]");
                }

                sb.AppendLine(inst.ToString(code, blocks));
            }

            sb.AppendLine();
            sb.Append(":[end]");

            return(sb.ToString());
        }
Exemple #2
0
        private void DisassembleCode(UndertaleCode code)
        {
            code.UpdateAddresses();

            FlowDocument document = new FlowDocument();

            document.PagePadding = new Thickness(0);
            document.PageWidth   = 2048; // Speed-up.
            document.FontFamily  = new FontFamily("Lucida Console");
            Paragraph par = new Paragraph();

            par.Margin = new Thickness(0);

            if (code.Instructions.Count > 5000)
            {
                // Disable syntax highlighting. Loading it can take a few MINUTES on large scripts.
                var      data  = (Application.Current.MainWindow as MainWindow).Data;
                string[] split = code.Disassemble(data.Variables, data.CodeLocals.For(code)).Split('\n');

                for (var i = 0; i < split.Length; i++)
                { // Makes it possible to select text.
                    if (i > 0 && (i % 100) == 0)
                    {
                        document.Blocks.Add(par);
                        par        = new Paragraph();
                        par.Margin = new Thickness(0);
                    }

                    par.Inlines.Add(split[i] + (split.Length > i + 1 && ((i + 1) % 100) != 0 ? "\n" : ""));
                }
            }
            else
            {
                Brush addressBrush = new SolidColorBrush(Color.FromRgb(50, 50, 50));
                Brush opcodeBrush  = new SolidColorBrush(Color.FromRgb(0, 100, 0));
                Brush argBrush     = new SolidColorBrush(Color.FromRgb(0, 0, 150));
                Brush typeBrush    = new SolidColorBrush(Color.FromRgb(0, 0, 50));
                var   data         = (Application.Current.MainWindow as MainWindow).Data;
                par.Inlines.Add(new Run(code.GenerateLocalVarDefinitions(data.Variables, data.CodeLocals.For(code)))
                {
                    Foreground = addressBrush
                });
                foreach (var instr in code.Instructions)
                {
                    par.Inlines.Add(new Run(instr.Address.ToString("D5") + ": ")
                    {
                        Foreground = addressBrush
                    });
                    par.Inlines.Add(new Run(instr.Kind.ToString().ToLower())
                    {
                        Foreground = opcodeBrush, FontWeight = FontWeights.Bold
                    });

                    switch (UndertaleInstruction.GetInstructionType(instr.Kind))
                    {
                    case UndertaleInstruction.InstructionType.SingleTypeInstruction:
                        par.Inlines.Add(new Run("." + instr.Type1.ToOpcodeParam())
                        {
                            Foreground = typeBrush
                        });

                        if (instr.Kind == UndertaleInstruction.Opcode.Dup || instr.Kind == UndertaleInstruction.Opcode.CallV)
                        {
                            par.Inlines.Add(new Run(" "));
                            par.Inlines.Add(new Run(instr.Extra.ToString())
                            {
                                Foreground = argBrush
                            });
                        }
                        break;

                    case UndertaleInstruction.InstructionType.DoubleTypeInstruction:
                        par.Inlines.Add(new Run("." + instr.Type1.ToOpcodeParam())
                        {
                            Foreground = typeBrush
                        });
                        par.Inlines.Add(new Run("." + instr.Type2.ToOpcodeParam())
                        {
                            Foreground = typeBrush
                        });
                        break;

                    case UndertaleInstruction.InstructionType.ComparisonInstruction:
                        par.Inlines.Add(new Run("." + instr.Type1.ToOpcodeParam())
                        {
                            Foreground = typeBrush
                        });
                        par.Inlines.Add(new Run("." + instr.Type2.ToOpcodeParam())
                        {
                            Foreground = typeBrush
                        });
                        par.Inlines.Add(new Run(" "));
                        par.Inlines.Add(new Run(instr.ComparisonKind.ToString())
                        {
                            Foreground = opcodeBrush
                        });
                        break;

                    case UndertaleInstruction.InstructionType.GotoInstruction:
                        par.Inlines.Add(new Run(" "));
                        string tgt = (instr.Address + instr.JumpOffset).ToString("D5");
                        if (instr.Address + instr.JumpOffset == code.Length / 4)
                        {
                            tgt = "func_end";
                        }
                        if (instr.JumpOffsetPopenvExitMagic)
                        {
                            tgt = "[drop]";
                        }
                        par.Inlines.Add(new Run(tgt)
                        {
                            Foreground = argBrush, ToolTip = "$" + instr.JumpOffset.ToString("+#;-#;0")
                        });
                        break;

                    case UndertaleInstruction.InstructionType.PopInstruction:
                        par.Inlines.Add(new Run("." + instr.Type1.ToOpcodeParam())
                        {
                            Foreground = typeBrush
                        });
                        par.Inlines.Add(new Run("." + instr.Type2.ToOpcodeParam())
                        {
                            Foreground = typeBrush
                        });
                        par.Inlines.Add(new Run(" "));
                        if (instr.Type1 == UndertaleInstruction.DataType.Int16)
                        {
                            // Special scenario - the swap instruction
                            // TODO: Figure out the proper syntax, see #129
                            Run runType = new Run(instr.SwapExtra.ToString().ToLower())
                            {
                                Foreground = argBrush
                            };
                            par.Inlines.Add(runType);
                        }
                        else
                        {
                            if (instr.Type1 == UndertaleInstruction.DataType.Variable && instr.TypeInst != UndertaleInstruction.InstanceType.Undefined)
                            {
                                par.Inlines.Add(new Run(instr.TypeInst.ToString().ToLower())
                                {
                                    Foreground = typeBrush
                                });
                                par.Inlines.Add(new Run("."));
                            }

                            Run runDest = new Run(instr.Destination.ToString())
                            {
                                Foreground = argBrush, Cursor = Cursors.Hand
                            };
                            runDest.MouseDown += (sender, e) =>
                            {
                                (Application.Current.MainWindow as MainWindow).ChangeSelection(instr.Destination);
                            };
                            par.Inlines.Add(runDest);
                        }
                        break;

                    case UndertaleInstruction.InstructionType.PushInstruction:
                        par.Inlines.Add(new Run("." + instr.Type1.ToOpcodeParam())
                        {
                            Foreground = typeBrush
                        });
                        par.Inlines.Add(new Run(" "));
                        if (instr.Type1 == UndertaleInstruction.DataType.Variable && instr.TypeInst != UndertaleInstruction.InstanceType.Undefined)
                        {
                            par.Inlines.Add(new Run(instr.TypeInst.ToString().ToLower())
                            {
                                Foreground = typeBrush
                            });
                            par.Inlines.Add(new Run("."));
                        }
                        Run valueRun = new Run((instr.Value as IFormattable)?.ToString(null, CultureInfo.InvariantCulture) ?? instr.Value.ToString())
                        {
                            Foreground = argBrush, Cursor = (instr.Value is UndertaleObject || instr.Value is UndertaleResourceRef) ? Cursors.Hand : Cursors.Arrow
                        };
                        if (instr.Value is UndertaleResourceRef)
                        {
                            valueRun.MouseDown += (sender, e) =>
                            {
                                (Application.Current.MainWindow as MainWindow).ChangeSelection((instr.Value as UndertaleResourceRef).Resource);
                            };
                        }
                        else if (instr.Value is UndertaleObject)
                        {
                            valueRun.MouseDown += (sender, e) =>
                            {
                                (Application.Current.MainWindow as MainWindow).ChangeSelection(instr.Value);
                            };
                        }
                        par.Inlines.Add(valueRun);
                        break;

                    case UndertaleInstruction.InstructionType.CallInstruction:
                        par.Inlines.Add(new Run("." + instr.Type1.ToOpcodeParam())
                        {
                            Foreground = typeBrush
                        });
                        par.Inlines.Add(new Run(" "));
                        par.Inlines.Add(new Run(instr.Function.ToString())
                        {
                            Foreground = argBrush
                        });
                        par.Inlines.Add(new Run("(argc="));
                        par.Inlines.Add(new Run(instr.ArgumentsCount.ToString())
                        {
                            Foreground = argBrush
                        });
                        par.Inlines.Add(new Run(")"));
                        break;

                    case UndertaleInstruction.InstructionType.BreakInstruction:
                        par.Inlines.Add(new Run("." + instr.Type1.ToOpcodeParam())
                        {
                            Foreground = typeBrush
                        });
                        par.Inlines.Add(new Run(" "));
                        par.Inlines.Add(new Run(instr.Value.ToString())
                        {
                            Foreground = argBrush
                        });
                        break;
                    }

                    if (par.Inlines.Count >= 250)
                    { // Makes selecting text possible.
                        document.Blocks.Add(par);
                        par        = new Paragraph();
                        par.Margin = new Thickness(0);
                    }
                    else
                    {
                        par.Inlines.Add(new Run("\n"));
                    }
                }
            }
            document.Blocks.Add(par);

            DisassemblyView.Document = document;

            CurrentDisassembled = code;
        }