11// This is a modified version of `VRC.Udon.Compiler.dll`
22
33using System ;
4+ using System . Collections . Generic ;
45using System . IO ;
56using System . Linq ;
7+ using VRC . Udon ;
68using VRC . Udon . Common . Interfaces ;
79using 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