Improve SimConnect struct writing and allow empty unit with SetAsync#12
Merged
AussieScorcher merged 3 commits intostopbars:0.1.17-betafrom Oct 21, 2025
Merged
Conversation
Introduces IFieldWriter, SimVarFieldWriter, and SimVarFieldWriterFactory to enable writing SimVar-annotated structs into unmanaged buffers. Adds SimVarManager.SetAsync<T> and SetStructAsync for setting multiple SimVars in one call using a struct. Updates tests to verify struct-based SimVar setting and restoration.
Refactored string field writing to ensure explicit null-termination and use Latin1 encoding for SimConnect compatibility. Added caching of struct writers and total size in SimVarManager for efficient SetDataOnSimObject calls. Improved error handling and updated attribute references from [SimVar] to [SimConnect].
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR improves SimConnect struct writing reliability and flexibility by introducing caching for struct writers, fixing string encoding to match SimConnect's ANSI expectations, and allowing empty units in SetAsync. The changes enhance performance through cached delegates, improve correctness of string marshaling, and fix consistency issues in error messages.
Key Changes:
- Introduced
defToWritercache inSimVarManagerto store struct writer delegates and layout sizes, eliminating redundant layout computations inSetStructAsync - Fixed string field encoding to use Latin1 with explicit null-termination and avoid unnecessary allocations
- Relaxed unit parameter validation in
SetAsyncto allow null values for SimVars that don't require units
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/SimConnect.NET/SimVar/SimVarManager.cs | Added writer caching mechanism, changed unit validation to allow null, added InitPosition type inference |
| src/SimConnect.NET/SimVar/Internal/SimVarFieldWriterFactory.cs | Updated error message to reference correct attribute name |
| src/SimConnect.NET/SimVar/Internal/SimVarFieldWriter.cs | Rewrote string encoding to use Latin1 with explicit null-termination and eliminate array allocation |
| src/SimConnect.NET/SimVar/Internal/SimVarFieldReaderFactory.cs | Updated error messages to reference correct attribute name |
AussieScorcher
approved these changes
Oct 21, 2025
AussieScorcher
added a commit
that referenced
this pull request
Nov 14, 2025
* Improve SimConnect struct writing and allow empty unit with SetAsync (#12) * Add struct-based SimVar set functionality Introduces IFieldWriter, SimVarFieldWriter, and SimVarFieldWriterFactory to enable writing SimVar-annotated structs into unmanaged buffers. Adds SimVarManager.SetAsync<T> and SetStructAsync for setting multiple SimVars in one call using a struct. Updates tests to verify struct-based SimVar setting and restoration. * Improve SimConnect struct writing and error handling Refactored string field writing to ensure explicit null-termination and use Latin1 encoding for SimConnect compatibility. Added caching of struct writers and total size in SimVarManager for efficient SetDataOnSimObject calls. Improved error handling and updated attribute references from [SimVar] to [SimConnect]. * chore: release version 0.1.17 with enhancements and fixes - Added ExceptionHelper for improved exception handling in various components. - Enhanced SimVar type inference for automatic detection of SimConnectDataInitPosition. - Improved struct-based SimVars.SetAsync<TStruct> for better performance and error handling. - Updated logging mechanisms to use ExceptionHelper for critical exception filtering. - Refined input event processing with defensive coding practices. - Maintained integration tests for better coverage and reliability. - Fixed issues with struct writers and SimVarManager logging. * Add debug logging for type conversion errors in TryGetDoubleValue --------- Co-authored-by: bstudtma <bstudtma@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This pull request introduces several minor improvements and fixes to the SimConnect.NET library, focusing on more robust handling of SimConnect data definitions and struct writing, as well as improved string encoding for SimConnect interoperability. The changes primarily address consistency, caching, and encoding issues, making struct operations safer and more efficient. This mainly cleans up a few issues detected by GitHub Copilot during the previous commit.
Changes Made
SimConnect struct writing and definition handling:
defToWriter) inSimVarManagerto store delegates and layout sizes for struct writers keyed by definition ID, ensuring efficient and consistent struct writing.SetStructAsyncto use the cached writer delegate and layout size fromdefToWriter, improving reliability and preventing redundant layout computation. Throws a clear error if the writer is missing.String encoding improvements:
SimVarFieldWriterto encode strings as fixed-size, null-terminated ANSI (Latin1) strings, reserving the last byte for the terminator and avoiding unnecessary allocations. This aligns with SimConnect's expectations for string fields.Consistency and error handling:
[SimConnect]attributes instead of[SimVar]in factories, clarifying attribute usage in exception messages.SetAsyncby switching toArgumentNullExceptionfor theunitparameter, ensuring correct exception semantics. For example, this allows setting SimVar "Initial Position" which doesn't require a unit type. SimConnect uses a default unit if one isn't provided.SimConnect data type inference:
SimConnectDataType.InitPositionfrom theSimConnectDataInitPositiontype, expanding the range of supported struct types.Author Information
Discord Username: bstudtma
Checklist: