-
-
Notifications
You must be signed in to change notification settings - Fork 3
Improve SimConnect struct writing and allow empty unit with SetAsync #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve SimConnect struct writing and allow empty unit with SetAsync #12
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].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
* 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 <[email protected]>
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: