Skip to content
This repository was archived by the owner on Jul 27, 2022. It is now read-only.

Commit e2ecd1d

Browse files
committed
add instruction labels to output
1 parent 0c5a685 commit e2ecd1d

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

AstralUdonViewer.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using MelonLoader;
22
using System;
33
using System.IO;
4+
using System.Linq;
45
using UnityEngine.SceneManagement;
56
using VRC.Udon;
67

7-
[assembly: MelonInfo(typeof(Astrum.AstralUdonViewer), nameof(Astrum.AstralUdonViewer), "0.2.1", downloadLink: "github.com/Astrum-Project/" + nameof(Astrum.AstralUdonViewer))]
8+
[assembly: MelonInfo(typeof(Astrum.AstralUdonViewer), nameof(Astrum.AstralUdonViewer), "0.3.0", downloadLink: "github.com/Astrum-Project/" + nameof(Astrum.AstralUdonViewer))]
89
[assembly: MelonColor(ConsoleColor.DarkMagenta)]
910

1011
namespace Astrum
@@ -36,21 +37,26 @@ public static System.Collections.IEnumerator DissassembleAll()
3637
yield return null;
3738

3839
// todo: change this to All
39-
// todo: dedup
40-
UdonBehaviour[] behaviours = UnityEngine.Object.FindObjectsOfType<UdonBehaviour>();
41-
40+
UdonBehaviour[] behaviours = UnityEngine.Object.FindObjectsOfType<UdonBehaviour>()
41+
.GroupBy(x => x.Pointer)
42+
.Select(x => x.FirstOrDefault())
43+
.ToArray();
44+
45+
if (behaviours.Length == 0) yield break;
46+
4247
AstralCore.Logger.Notif($"[UdonViewer] Disassembling {behaviours.Length} UdonBehaviours");
4348

4449
foreach (UdonBehaviour behaviour in behaviours)
4550
{
46-
if (behaviour._program is null) continue;
51+
if (behaviour?._program is null) continue;
4752

4853
AstralCore.Logger.Trace($"Disassembling {behaviour.name}");
49-
MelonCoroutines.Start(Disassembler.DisassembleProgram($"{path}/{RemoveInvalid(behaviour.name)}.uasm", behaviour._program));
54+
MelonCoroutines.Start(Disassembler.DisassembleProgram($"{path}/{RemoveInvalid(behaviour.name)}.uasm", behaviour));
5055

5156
yield return null;
5257
}
5358

59+
// this is a lie! it just started the last behaviour, not finished at all
5460
AstralCore.Logger.Notif("[UdonViewer] Finished!");
5561
}
5662

AstralUdonViewer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<PropertyGroup>
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7-
<ProjectGuid>{3F023BCB-4CB9-4631-A790-D49C4E910007}</ProjectGuid>
7+
<ProjectGuid>{494A3959-C330-41C6-8142-C7E8951AE991}</ProjectGuid>
88
<OutputType>Library</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>Astrum</RootNamespace>

Disassembler.cs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// This is a modified version of `VRC.Udon.Compiler.dll`
22

33
using System;
4+
using System.Collections.Generic;
45
using System.IO;
56
using System.Linq;
7+
using VRC.Udon;
68
using VRC.Udon.Common.Interfaces;
79
using VRC.Udon.VM.Common;
810

@@ -12,15 +14,33 @@ partial class AstralUdonViewer
1214
{
1315
public static class Disassembler
1416
{
15-
public static System.Collections.IEnumerator DisassembleProgram(string path, IUdonProgram program)
17+
public static System.Collections.IEnumerator DisassembleProgram(string path, UdonBehaviour udonBehaviour)
1618
{
17-
string[] lines = new string[program.ByteCode.Length];
19+
IUdonProgram program = udonBehaviour._program;
20+
var events = udonBehaviour._eventTable;
21+
22+
var label = GetNextFunction(udonBehaviour, null);
23+
24+
List<string> lines = new List<string>(program.ByteCode.Length / 4);
1825
for (uint i = 0; i < program.ByteCode.Length;)
1926
{
20-
lines[i] = DisassembleInstruction(program, ref i);
27+
if (i == label.Item2)
28+
{
29+
lines.Add($"{label.Item1}:");
30+
31+
string c = DisassembleInstruction(program, ref i);
32+
lines.Add(c);
33+
34+
label = GetNextFunction(udonBehaviour, i);
35+
} else
36+
{
37+
string c = DisassembleInstruction(program, ref i);
38+
lines.Add(c);
39+
}
40+
2141
yield return null;
2242
}
23-
File.WriteAllLines(path, lines.Where(x => !string.IsNullOrEmpty(x)));
43+
File.WriteAllLines(path, lines.ToArray().Where(x => !string.IsNullOrEmpty(x)));
2444
}
2545

2646
public static string DisassembleInstruction(IUdonProgram program, ref uint offset)
@@ -47,6 +67,27 @@ public static string DisassembleInstruction(IUdonProgram program, ref uint offse
4767
else return $"0x{(offset += 4) - 4:X}: INVALID (0x{opCode:X})";
4868
}
4969

70+
private static (string, uint) GetNextFunction(UdonBehaviour ub, uint? cur)
71+
{
72+
uint delta = unchecked((uint)-1);
73+
string key = null;
74+
foreach (var kvp in ub._eventTable) {
75+
if ((kvp.Value?.Count ?? 0) == 0) continue;
76+
77+
uint val = kvp.Value[0];
78+
79+
if (cur != null && val <= (uint)cur) continue;
80+
81+
if (val < delta) {
82+
delta = val;
83+
key = kvp.Key;
84+
85+
}
86+
}
87+
88+
return (key, delta);
89+
}
90+
5091
private static string SimpleInstruction(ref uint offset, string name)
5192
=> string.Format("0x{0:X8}: {1}", (offset += 4) - 4, name);
5293
private static string DirectInstruction(ref uint offset, string name, IUdonProgram program)

0 commit comments

Comments
 (0)