|
| 1 | +# NativeCodeGen - Claude Context |
| 2 | + |
| 3 | +Always use /tmp/nativegen for putting your generated builds |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +NativeCodeGen is a code generator for RDR3 (Red Dead Redemption 3) and FiveM/RedM native functions. It parses MDX files containing native function definitions and generates TypeScript, Lua, JSON, and Protobuf outputs. |
| 8 | + |
| 9 | +## Project Structure |
| 10 | + |
| 11 | +``` |
| 12 | +NativeCodeGen/ |
| 13 | +├── src/ |
| 14 | +│ ├── NativeCodeGen.Cli/ # CLI application |
| 15 | +│ │ ├── Program.cs # Entry point, command parsing |
| 16 | +│ │ └── TrimmerRoots.xml # Types to preserve for AOT/trimming |
| 17 | +│ ├── NativeCodeGen.Core/ # Core library |
| 18 | +│ │ ├── Export/ # Exporters for different formats |
| 19 | +│ │ │ ├── IExporter.cs # Exporter interface + ExportOptions |
| 20 | +│ │ │ ├── ICodeGenerator.cs # Code generator interface + GeneratorOptions |
| 21 | +│ │ │ ├── BaseExporter.cs # Base class for code exporters |
| 22 | +│ │ │ ├── JsonExporter.cs # JSON output |
| 23 | +│ │ │ ├── ProtobufExporter.cs # Protobuf binary output |
| 24 | +│ │ │ ├── DatabaseConverter.cs # Converts parsed models to export models |
| 25 | +│ │ │ ├── ExportModels.cs # Export DTOs with protobuf attributes |
| 26 | +│ │ │ ├── JsonContext.cs # JSON source generator context |
| 27 | +│ │ │ └── natives.proto # Protobuf schema definition |
| 28 | +│ │ ├── Generation/ # Code generation utilities |
| 29 | +│ │ │ ├── CodeBuilder.cs # String builder with indentation |
| 30 | +│ │ │ ├── RawNativeBuilder.cs # Raw function generation (TS/Lua) |
| 31 | +│ │ │ ├── ArgumentBuilder.cs # Native invoke argument building |
| 32 | +│ │ │ ├── NativeClassifier.cs # Classifies natives into handle classes |
| 33 | +│ │ │ ├── SharedClassGenerator.cs # Generates handle/namespace classes |
| 34 | +│ │ │ ├── SharedStructGenerator.cs # Generates struct classes |
| 35 | +│ │ │ └── SharedEnumGenerator.cs # Generates enum definitions |
| 36 | +│ │ ├── Models/ # Data models |
| 37 | +│ │ │ ├── NativeDefinition.cs # Native function definition |
| 38 | +│ │ │ ├── NativeParameter.cs # Function parameter with flags |
| 39 | +│ │ │ ├── TypeInfo.cs # Type information + categorization |
| 40 | +│ │ │ ├── EnumDefinition.cs # Enum with members |
| 41 | +│ │ │ ├── StructDefinition.cs # Struct with fields |
| 42 | +│ │ │ ├── SharedExample.cs # Shared code examples |
| 43 | +│ │ │ ├── Callout.cs # Note/warning callouts |
| 44 | +│ │ │ └── Flags.cs # ParamFlags, FieldFlags enums |
| 45 | +│ │ ├── Parsing/ # MDX/C parsing |
| 46 | +│ │ │ ├── MdxParser.cs # Main MDX file parser |
| 47 | +│ │ │ ├── MdxComponentParser.cs # Bracket syntax parser [enum: X] |
| 48 | +│ │ │ ├── FrontmatterParser.cs # YAML frontmatter parser |
| 49 | +│ │ │ ├── SignatureParser.cs # C-style signature parser |
| 50 | +│ │ │ ├── EnumParser.cs # C-style enum parser |
| 51 | +│ │ │ ├── StructParser.cs # C-style struct parser |
| 52 | +│ │ │ └── CLexer.cs # C tokenizer |
| 53 | +│ │ ├── Registry/ # Type registries |
| 54 | +│ │ │ ├── EnumRegistry.cs # Enum definitions lookup |
| 55 | +│ │ │ ├── StructRegistry.cs # Struct definitions lookup |
| 56 | +│ │ │ └── SharedExampleRegistry.cs # Shared examples lookup |
| 57 | +│ │ ├── TypeSystem/ # Type mapping |
| 58 | +│ │ │ ├── ITypeMapper.cs # Type mapper interface |
| 59 | +│ │ │ ├── TypeMapperBase.cs # Base implementation |
| 60 | +│ │ │ └── LanguageConfig.cs # Language-specific config |
| 61 | +│ │ └── Utilities/ # Helpers |
| 62 | +│ │ ├── NameConverter.cs # Name transformations |
| 63 | +│ │ └── NameDeduplicator.cs # Deduplication logic |
| 64 | +│ ├── NativeCodeGen.TypeScript/ # TypeScript generator |
| 65 | +│ │ ├── TypeScriptExporter.cs # Main exporter |
| 66 | +│ │ ├── Generation/ |
| 67 | +│ │ │ ├── TypeScriptGenerator.cs # Generates TS files |
| 68 | +│ │ │ ├── TypeScriptEmitter.cs # Language-specific emission |
| 69 | +│ │ │ └── TypeScriptTypeMapper.cs # TS type mapping |
| 70 | +│ └── NativeCodeGen.Lua/ # Lua generator (similar structure) |
| 71 | +├── tests/ |
| 72 | +│ └── NativeCodeGen.Tests/ # Unit tests |
| 73 | +├── packages/ # Generated npm packages (WIP) |
| 74 | +└── esbuild-plugin/ # esbuild tree-shaking plugin |
| 75 | +``` |
| 76 | + |
| 77 | +## Key Concepts |
| 78 | + |
| 79 | +### MDX File Format |
| 80 | +Native definitions are in MDX files with: |
| 81 | +- YAML frontmatter: `ns` (namespace), `aliases`, `apiset` |
| 82 | +- Heading with native name: `## NATIVE_NAME` |
| 83 | +- C-style signature in code block |
| 84 | +- Description, parameters, return value sections |
| 85 | + |
| 86 | +### Bracket Syntax |
| 87 | +- `[enum: EnumName]` - Reference to enum |
| 88 | +- `[struct: StructName]` - Reference to struct |
| 89 | +- `[native: NativeName]` or `[native: NativeName | gta5]` - Reference to native (with optional game) |
| 90 | +- `[example: ExampleName]` - Reference to shared example |
| 91 | +- `[note: Description]` or `[note: Title | Description]` - Note callout |
| 92 | +- `[warning: ...]`, `[info: ...]`, `[danger: ...]` - Other callout types |
| 93 | + |
| 94 | +### Type Categories (TypeInfo.cs) |
| 95 | +- `Void`, `Primitive`, `Handle`, `Hash`, `String`, `Vector3`, `Any`, `Struct`, `Enum` |
| 96 | + |
| 97 | +### Parameter Flags (ParamFlags) |
| 98 | +- `None`, `Output`, `This`, `NotNull`, `In` |
| 99 | +- `Output` = pointer output (int*, float*, Vector3*) |
| 100 | +- `In` = input+output pointer (@in attribute) |
| 101 | +- `This` = instance method receiver (@this attribute) |
| 102 | + |
| 103 | +### Binding Styles (RawNativeBuilder) |
| 104 | +- `Global` - `globalThis.X = function()` (no tree-shaking) |
| 105 | +- `Export` - `export function X()` (tree-shakeable) |
| 106 | +- `Module` - `ModuleName.X = function()` |
| 107 | + |
| 108 | +### Generation Modes |
| 109 | +1. **Class-based** (default): Generates handle classes (Entity, Ped, Vehicle) and namespace utilities |
| 110 | +2. **Raw**: Generates plain functions per namespace file |
| 111 | +3. **Single-file**: All natives in one file (requires --raw) |
| 112 | +4. **Package**: Complete npm package with esbuild plugin (WIP) |
| 113 | + |
| 114 | +## CLI Commands |
| 115 | + |
| 116 | +```bash |
| 117 | +# Generate TypeScript (class-based) |
| 118 | +nativegen generate -i <input> -o <output> -f typescript |
| 119 | + |
| 120 | +# Generate raw functions |
| 121 | +nativegen generate -i <input> -o <output> -f typescript --raw |
| 122 | + |
| 123 | +# Generate single file with exports (tree-shakeable) |
| 124 | +nativegen generate -i <input> -o <output> -f typescript --raw --single-file --exports |
| 125 | + |
| 126 | +# Generate JSON database |
| 127 | +nativegen generate -i <input> -o <output>/natives.json -f json |
| 128 | + |
| 129 | +# Generate Protobuf binary |
| 130 | +nativegen generate -i <input> -o <output>/natives.bin -f proto |
| 131 | + |
| 132 | +# Validate only |
| 133 | +nativegen validate -i <input> |
| 134 | +``` |
| 135 | + |
| 136 | +## Important Implementation Details |
| 137 | + |
| 138 | +### Trimmer/AOT Compatibility |
| 139 | +- Uses `TrimmerRoots.xml` to preserve reflection-based types |
| 140 | +- Must add any YamlDotNet-deserialized classes (Frontmatter, SharedExampleFrontmatter) |
| 141 | +- Must add protobuf-net serialized classes (Export* classes) |
| 142 | + |
| 143 | +### Enum Value Generation |
| 144 | +- C-style: unassigned members get `previous + 1` |
| 145 | +- Handles negative start values (e.g., eVehicleSeat starts at -2) |
| 146 | +- Parses hex values for incrementing |
| 147 | + |
| 148 | +### JSON Output Structure |
| 149 | +- Flat `natives` array (each with `ns` field) |
| 150 | +- `enums`, `structs`, `sharedExamples` as dictionaries keyed by name |
| 151 | +- `types` for type definitions |
| 152 | + |
| 153 | +### Protobuf Output |
| 154 | +- Uses protobuf-net for serialization |
| 155 | +- Schema in `natives.proto` |
| 156 | +- Binary output for efficient parsing |
| 157 | + |
| 158 | +## Current Work in Progress |
| 159 | + |
| 160 | +1. **--package flag**: Generate complete npm package with: |
| 161 | + - package.json |
| 162 | + - esbuild plugin for tree-shaking |
| 163 | + - Build scripts |
| 164 | + - Generated natives with `export function` style |
| 165 | + |
| 166 | +2. **esbuild plugin**: Auto-import used natives for tree-shaking without explicit imports |
| 167 | + |
| 168 | +## Testing |
| 169 | + |
| 170 | +```bash |
| 171 | +dotnet test # Run all tests |
| 172 | +dotnet test --filter "ClassName" # Run specific test class |
| 173 | +``` |
| 174 | + |
| 175 | +## Building |
| 176 | + |
| 177 | +```bash |
| 178 | +dotnet build # Debug build |
| 179 | +dotnet publish src/NativeCodeGen.Cli -c Release -r linux-x64 --self-contained # Release |
| 180 | +``` |
| 181 | + |
| 182 | +## Related Repositories |
| 183 | + |
| 184 | +- `rdr3-natives`: MDX native definitions for RDR3 |
| 185 | +- Input expected at: `code/enums/`, `code/structs/`, `code/shared-examples/`, and namespace directories |
0 commit comments