11using System . Text ;
22using ReCap . Parser ;
33
4- namespace ReCap . Parser . CLI . Commands ;
4+ namespace ReCap . Wiki ;
55
6- public static class WikiCatalog
6+ /// <summary>
7+ /// Wiki generator for ReCap.Parser asset catalog documentation.
8+ /// </summary>
9+ public static class Program
710{
811 private static readonly Dictionary < string , HashSet < string > > _usagesMap = new ( ) ;
912 private const string FolderStructures = "Structures" ;
1013 private const string FolderEnums = "Enums" ;
1114 private const string FolderCatalog = "Catalog" ;
1215
13- public static void Run ( string outputDir )
16+ public static int Main ( string [ ] args )
17+ {
18+ string outputDir = args . Length > 0 ? args [ 0 ] : "./wiki-output" ;
19+
20+ try
21+ {
22+ GenerateWiki ( outputDir ) ;
23+ return 0 ;
24+ }
25+ catch ( Exception ex )
26+ {
27+ Console . Error . WriteLine ( $ "Error: { ex . Message } ") ;
28+ Console . Error . WriteLine ( ex . StackTrace ) ;
29+ return 1 ;
30+ }
31+ }
32+
33+ private static void GenerateWiki ( string outputDir )
1434 {
1535 Console . WriteLine ( $ "[WikiGen] Generating Wiki in: { outputDir } ") ;
1636
@@ -30,10 +50,17 @@ public static void Run(string outputDir)
3050 var loadedCatalogs = new List < AssetCatalog > ( ) ;
3151 foreach ( var type in catalogTypes )
3252 {
33- try { loadedCatalogs . Add ( ( AssetCatalog ) Activator . CreateInstance ( type ) ! ) ; }
34- catch { Console . WriteLine ( $ "[WikiGen] Warning: Failed to instantiate { type . Name } ") ; }
53+ try
54+ {
55+ loadedCatalogs . Add ( ( AssetCatalog ) Activator . CreateInstance ( type ) ! ) ;
56+ }
57+ catch
58+ {
59+ Console . WriteLine ( $ "[WikiGen] Warning: Failed to instantiate { type . Name } ") ;
60+ }
3561 }
3662
63+ ResolveEnumReferences ( loadedCatalogs ) ;
3764 BuildDependencyGraph ( loadedCatalogs ) ;
3865
3966 var allStructs = new List < string > ( ) ;
@@ -64,6 +91,73 @@ public static void Run(string outputDir)
6491 Console . WriteLine ( $ "[WikiGen] Done! { allStructs . Count } Structures, { allEnums . Count } Enums.") ;
6592 }
6693
94+ private static void ResolveEnumReferences ( List < AssetCatalog > catalogs )
95+ {
96+ var globalEnumNames = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
97+ foreach ( var cat in catalogs )
98+ {
99+ foreach ( var e in cat . EnumNames ) globalEnumNames . Add ( e ) ;
100+ }
101+
102+ Console . WriteLine ( $ "[WikiGen] Resolving references for { globalEnumNames . Count } Enums...") ;
103+
104+ foreach ( var cat in catalogs )
105+ {
106+ foreach ( var structName in cat . StructNames )
107+ {
108+ var def = cat . GetStruct ( structName ) ;
109+ if ( def == null ) continue ;
110+
111+ var fieldsList = ( List < FieldDefinition > ) def . Fields ;
112+
113+ for ( int i = 0 ; i < fieldsList . Count ; i ++ )
114+ {
115+ var field = fieldsList [ i ] ;
116+ bool isEnum = field . Type == DataType . Enum ;
117+ bool isEnumArray = field . Type == DataType . Array && field . ElementType == "Enum" ;
118+
119+ if ( ( ! isEnum && ! isEnumArray ) || field . EnumType != null )
120+ continue ;
121+
122+ string ? resolvedEnum = null ;
123+ var specificName = $ "{ def . Name } .{ field . Name } ";
124+ if ( globalEnumNames . Contains ( specificName ) )
125+ {
126+ resolvedEnum = specificName ;
127+ }
128+ else
129+ {
130+ if ( globalEnumNames . Contains ( field . Name ) )
131+ {
132+ resolvedEnum = field . Name ;
133+ }
134+ else
135+ {
136+ var pascalName = ToPascalCase ( field . Name ) ;
137+ if ( globalEnumNames . Contains ( pascalName ) )
138+ {
139+ resolvedEnum = pascalName ;
140+ }
141+ }
142+ }
143+
144+ if ( resolvedEnum != null )
145+ {
146+ fieldsList [ i ] = field with { EnumType = resolvedEnum } ;
147+ Console . WriteLine ( $ " Linked: { def . Name } .{ field . Name } -> { resolvedEnum } ") ;
148+ }
149+ }
150+ }
151+ }
152+ }
153+
154+ private static string ToPascalCase ( string s )
155+ {
156+ if ( string . IsNullOrEmpty ( s ) ) return s ;
157+ if ( char . IsUpper ( s [ 0 ] ) ) return s ;
158+ return char . ToUpper ( s [ 0 ] ) + s . Substring ( 1 ) ;
159+ }
160+
67161 private static void GenerateSidebar ( string outputDir , List < string > structs , List < string > enums )
68162 {
69163 var sb = new StringBuilder ( ) ;
@@ -115,9 +209,10 @@ private static void BuildDependencyGraph(List<AssetCatalog> catalogs)
115209 if ( field . Type == DataType . Struct ) dep = field . ElementType ;
116210 else if ( field . Type == DataType . Nullable ) dep = field . ElementType ;
117211 else if ( field . Type == DataType . Enum ) dep = field . EnumType ;
118- else if ( field . Type == DataType . Array && field . ElementType != null )
212+ else if ( field . Type == DataType . Array )
119213 {
120- if ( ! Enum . TryParse < DataType > ( field . ElementType , true , out _ ) )
214+ if ( field . ElementType == "Enum" ) dep = field . EnumType ;
215+ else if ( field . ElementType != null && ! Enum . TryParse < DataType > ( field . ElementType , true , out _ ) )
121216 dep = field . ElementType ;
122217 }
123218
@@ -193,7 +288,7 @@ private static string FormatDataType(FieldDefinition field)
193288 var name = field . ElementType ?? "Unknown" ;
194289 return $ "[`({ name } )`]({ name } )";
195290 }
196-
291+
197292 if ( field . Type == DataType . Nullable )
198293 {
199294 var name = field . ElementType ?? "Unknown" ;
@@ -203,15 +298,22 @@ private static string FormatDataType(FieldDefinition field)
203298 if ( field . Type == DataType . Enum )
204299 {
205300 var name = field . EnumType ?? "Unknown" ;
206- return $ "`Enum` [`({ name } )`]({ name } )";
301+ return name != "Unknown" ? $ "`Enum` [`({ name } )`]({ name } )" : $ "`Enum` `( { name } )` ";
207302 }
208303
209304 if ( field . Type == DataType . Array )
210305 {
211306 var inner = field . ElementType ?? "Unknown" ;
212307 bool isComplexType = ! Enum . TryParse < DataType > ( inner , true , out _ ) ;
213308
214- if ( isComplexType )
309+ if ( inner == "Enum" )
310+ {
311+ var enumName = field . EnumType ?? "Unknown" ;
312+ return enumName != "Unknown"
313+ ? $ "`Array` `Enum` [`({ enumName } )`]({ enumName } )"
314+ : $ "`Array` `Enum`";
315+ }
316+ else if ( isComplexType )
215317 {
216318 return $ "`Array` [`({ inner } )`]({ inner } )";
217319 }
0 commit comments