|
| 1 | +# SimConnect.NET AI Coding Agent Instructions |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +SimConnect.NET is a managed C# wrapper for Microsoft Flight Simulator's SimConnect SDK, providing async/await patterns and high-level abstractions for flight simulation data access. |
| 6 | + |
| 7 | +## Architecture & Core Components |
| 8 | + |
| 9 | +### Three-Layer Design Pattern |
| 10 | + |
| 11 | +- **SimConnectClient**: Main entry point, manages connection lifecycle and background message processing |
| 12 | +- **Manager Layer**: Domain-specific managers (`SimVarManager`, `AircraftDataManager`, `SimObjectManager`) |
| 13 | +- **Native Layer**: P/Invoke bindings in `SimConnectNative.cs` with ANSI string marshaling |
| 14 | + |
| 15 | +### Key Manager Responsibilities |
| 16 | + |
| 17 | +- **SimVarManager**: Dynamic SimVar operations with automatic data definition caching and async request handling |
| 18 | +- **AircraftDataManager**: High-level aircraft data aggregation (uses concurrent requests for efficiency) |
| 19 | +- **SimObjectManager**: AI object creation and lifecycle management |
| 20 | + |
| 21 | +### Message Processing Pattern |
| 22 | + |
| 23 | +The client runs a background message loop (`StartMessageProcessingLoopAsync`) with adaptive polling delays that routes messages based on `SimConnectRecvId` enum values. Each manager processes relevant messages via their own handlers. |
| 24 | + |
| 25 | +## Development Conventions |
| 26 | + |
| 27 | +### Code Style (StyleCop Enforced) |
| 28 | + |
| 29 | +- All public members require XML documentation with company copyright headers |
| 30 | +- Using directives outside namespace, newline at EOF required |
| 31 | +- `TreatWarningsAsErrors=true` in all projects except tests |
| 32 | +- File headers: `// <copyright file="FileName.cs" company="AussieScorcher">` |
| 33 | + |
| 34 | +### Async Patterns |
| 35 | + |
| 36 | +```csharp |
| 37 | +// Concurrent requests for efficiency |
| 38 | +var tasks = new[] { |
| 39 | + simVarManager.GetAsync<double>("VAR1", "unit"), |
| 40 | + simVarManager.GetAsync<double>("VAR2", "unit") |
| 41 | +}; |
| 42 | +await Task.WhenAll(tasks); |
| 43 | +``` |
| 44 | + |
| 45 | +### Error Handling |
| 46 | + |
| 47 | +- Use `SimConnectException` with `SimConnectError` enum for SimConnect-specific failures |
| 48 | +- `ObjectDisposedException.ThrowIf(disposed, nameof(Class))` for disposal checks |
| 49 | +- Debug.WriteLine for non-critical errors to avoid breaking message loops |
| 50 | + |
| 51 | +## Build & Test Workflow |
| 52 | + |
| 53 | +### Multi-targeting & Dependencies |
| 54 | + |
| 55 | +- Projects target both .NET 8.0 and 9.0 (`<TargetFrameworks>net8.0;net9.0</TargetFrameworks>`) |
| 56 | +- `SimConnect.dll` is bundled in `lib/` and copied to output (`CopyToOutputDirectory>PreserveNewest`) |
| 57 | +- NuGet package generation enabled with `GeneratePackageOnBuild=true` |
| 58 | + |
| 59 | +### Test Structure |
| 60 | + |
| 61 | +- Tests implement `ISimConnectTest` interface with Name/Description/Category properties |
| 62 | +- Tests assume client is already connected and focus on specific functionality |
| 63 | +- No traditional test framework - custom test runner in `TestRunner.cs` |
| 64 | +- StyleCop disabled for test projects (`RunAnalyzersDuringBuild>false`) |
| 65 | + |
| 66 | +### Build Commands |
| 67 | + |
| 68 | +```bash |
| 69 | +dotnet restore && dotnet build --configuration Release |
| 70 | +dotnet pack src/SimConnect.NET/SimConnect.NET.csproj --output ./artifacts |
| 71 | +``` |
| 72 | + |
| 73 | +## SimVar Integration Patterns |
| 74 | + |
| 75 | +### Registry vs Dynamic Definitions |
| 76 | + |
| 77 | +- `SimVarRegistry.Get(name)` for predefined SimVars with type validation |
| 78 | +- Dynamic definitions created automatically for unregistered SimVars |
| 79 | +- Data definitions cached by `(Name, Unit)` tuple to avoid recreating |
| 80 | + |
| 81 | +### Type Inference & Marshaling |
| 82 | + |
| 83 | +- Automatic `SimConnectDataType` inference from .NET types (int→Integer32, double→FloatDouble) |
| 84 | +- Boolean conversion: SimConnect int values (0/1) ↔ .NET bool |
| 85 | +- String handling with ANSI marshaling and configurable buffer sizes |
| 86 | + |
| 87 | +## Common Integration Points |
| 88 | + |
| 89 | +### P/Invoke Conventions |
| 90 | + |
| 91 | +- All SimConnect functions use ANSI string marshaling (`[MarshalAs(UnmanagedType.LPStr)]`) |
| 92 | +- Suppress CA2101 warning for ANSI marshaling requirement |
| 93 | +- IntPtr handle management with proper disposal patterns |
| 94 | + |
| 95 | +### Concurrent Request Handling |
| 96 | + |
| 97 | +- Request/Response correlation via unique IDs (`nextRequestId`, `nextDefinitionId`) |
| 98 | +- `ConcurrentDictionary<uint, object>` for pending request tracking |
| 99 | +- TaskCompletionSource pattern for async request completion |
| 100 | + |
| 101 | +When implementing new features, follow the established manager pattern, maintain StyleCop compliance, and use concurrent async patterns for SimVar operations. |
0 commit comments