11using System . Text . Json ;
22using System . Text . Json . Serialization ;
33using NativeCodeGen . Core . Parsing ;
4+ using ProtoBuf ;
45
56namespace NativeCodeGen . Core . Export ;
67
@@ -13,6 +14,7 @@ public void Export(NativeDatabase db, string outputPath, ExportOptions options)
1314 // For JSON, we want dictionaries keyed by name for enums, structs, and examples
1415 var output = new JsonOutput
1516 {
17+ Types = TypeRegistry . GetTypeDefinitions ( ) ,
1618 Natives = exportDb . Namespaces . SelectMany ( ns => ns . Natives ) . ToList ( ) ,
1719 Enums = exportDb . Enums . ToDictionary ( e => e . Name , e => e ) ,
1820 Structs = exportDb . Structs . ToDictionary ( s => s . Name , s => s ) ,
@@ -31,8 +33,127 @@ public void Export(NativeDatabase db, string outputPath, ExportOptions options)
3133/// </summary>
3234public class JsonOutput
3335{
36+ public Dictionary < string , ExportTypeInfo > Types { get ; set ; } = new ( ) ;
3437 public List < ExportNative > Natives { get ; set ; } = new ( ) ;
3538 public Dictionary < string , ExportEnum > Enums { get ; set ; } = new ( ) ;
3639 public Dictionary < string , ExportStruct > Structs { get ; set ; } = new ( ) ;
3740 public Dictionary < string , ExportSharedExample > SharedExamples { get ; set ; } = new ( ) ;
3841}
42+
43+ /// <summary>
44+ /// Type information for JSON/Protobuf export.
45+ /// </summary>
46+ [ ProtoContract ]
47+ public class ExportTypeInfo
48+ {
49+ [ ProtoMember ( 1 ) ]
50+ [ JsonConverter ( typeof ( JsonStringEnumConverter < ExportTypeCategory > ) ) ]
51+ public ExportTypeCategory Category { get ; set ; }
52+
53+ [ ProtoMember ( 2 ) ]
54+ [ JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingNull ) ]
55+ public string ? NativeType { get ; set ; }
56+
57+ [ ProtoMember ( 3 ) ]
58+ [ JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingNull ) ]
59+ public string ? Description { get ; set ; }
60+ }
61+
62+ /// <summary>
63+ /// Type categories for the type registry.
64+ /// </summary>
65+ [ ProtoContract ]
66+ public enum ExportTypeCategory
67+ {
68+ Primitive = 0 ,
69+ Handle = 1 ,
70+ Hash = 2 ,
71+ Vector3 = 3 ,
72+ String = 4 ,
73+ Void = 5 ,
74+ Any = 6
75+ }
76+
77+ /// <summary>
78+ /// Type entry for protobuf serialization (since protobuf doesn't support dictionaries directly).
79+ /// </summary>
80+ [ ProtoContract ]
81+ public class ExportTypeEntry
82+ {
83+ [ ProtoMember ( 1 ) ]
84+ public string Name { get ; set ; } = string . Empty ;
85+
86+ [ ProtoMember ( 2 ) ]
87+ public ExportTypeInfo Type { get ; set ; } = new ( ) ;
88+ }
89+
90+ /// <summary>
91+ /// Registry of all supported types and their mappings.
92+ /// </summary>
93+ public static class TypeRegistry
94+ {
95+ public static Dictionary < string , ExportTypeInfo > GetTypeDefinitions ( )
96+ {
97+ var types = new Dictionary < string , ExportTypeInfo > ( ) ;
98+
99+ // Primitives - integers
100+ types [ "int" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "int" , Description = "32-bit signed integer" } ;
101+ types [ "uint" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "int" , Description = "32-bit unsigned integer" } ;
102+ types [ "i8" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "int" , Description = "8-bit signed integer" } ;
103+ types [ "i16" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "int" , Description = "16-bit signed integer" } ;
104+ types [ "i32" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "int" , Description = "32-bit signed integer" } ;
105+ types [ "i64" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "int" , Description = "64-bit signed integer" } ;
106+ types [ "u8" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "int" , Description = "8-bit unsigned integer" } ;
107+ types [ "u16" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "int" , Description = "16-bit unsigned integer" } ;
108+ types [ "u32" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "int" , Description = "32-bit unsigned integer" } ;
109+ types [ "u64" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "int" , Description = "64-bit unsigned integer" } ;
110+
111+ // Primitives - floats
112+ types [ "float" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "float" , Description = "32-bit floating point" } ;
113+ types [ "double" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "float" , Description = "64-bit floating point" } ;
114+ types [ "f32" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "float" , Description = "32-bit floating point" } ;
115+ types [ "f64" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "float" , Description = "64-bit floating point" } ;
116+
117+ // Primitives - boolean
118+ types [ "bool" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "int" , Description = "Boolean (0 or 1)" } ;
119+ types [ "BOOL" ] = new ExportTypeInfo { Category = ExportTypeCategory . Primitive , NativeType = "int" , Description = "Boolean (0 or 1)" } ;
120+
121+ // Special types
122+ types [ "void" ] = new ExportTypeInfo { Category = ExportTypeCategory . Void , Description = "No return value" } ;
123+ types [ "Any" ] = new ExportTypeInfo { Category = ExportTypeCategory . Any , NativeType = "int" , Description = "Any type (context-dependent)" } ;
124+ types [ "Hash" ] = new ExportTypeInfo { Category = ExportTypeCategory . Hash , NativeType = "int" , Description = "32-bit hash value (joaat)" } ;
125+ types [ "Vector3" ] = new ExportTypeInfo { Category = ExportTypeCategory . Vector3 , Description = "3D vector (x, y, z floats)" } ;
126+ types [ "string" ] = new ExportTypeInfo { Category = ExportTypeCategory . String , Description = "Null-terminated string pointer" } ;
127+ types [ "char*" ] = new ExportTypeInfo { Category = ExportTypeCategory . String , Description = "Null-terminated string pointer" } ;
128+
129+ // Handle types - all map to int at native level
130+ var handles = new [ ]
131+ {
132+ ( "Entity" , "Base type for all world entities" ) ,
133+ ( "Ped" , "Pedestrian/character handle (extends Entity)" ) ,
134+ ( "Vehicle" , "Vehicle handle (extends Entity)" ) ,
135+ ( "Object" , "Object/prop handle (extends Entity)" ) ,
136+ ( "Pickup" , "Pickup handle" ) ,
137+ ( "Player" , "Player handle" ) ,
138+ ( "Cam" , "Camera handle" ) ,
139+ ( "Blip" , "Map blip handle" ) ,
140+ ( "Interior" , "Interior handle" ) ,
141+ ( "FireId" , "Fire instance handle" ) ,
142+ ( "AnimScene" , "Animation scene handle" ) ,
143+ ( "ItemSet" , "Item set handle" ) ,
144+ ( "PersChar" , "Persistent character handle" ) ,
145+ ( "PopZone" , "Population zone handle" ) ,
146+ ( "PropSet" , "Prop set handle" ) ,
147+ ( "Volume" , "Volume handle" ) ,
148+ ( "ScrHandle" , "Generic script handle" ) ,
149+ ( "PedGroup" , "Ped group handle" )
150+ } ;
151+
152+ foreach ( var ( name , desc ) in handles )
153+ {
154+ types [ name ] = new ExportTypeInfo { Category = ExportTypeCategory . Handle , NativeType = "int" , Description = desc } ;
155+ }
156+
157+ return types ;
158+ }
159+ }
0 commit comments