Skip to content

Conversation

@bstudtma
Copy link
Contributor

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:

  • Added a cache (defToWriter) in SimVarManager to store delegates and layout sizes for struct writers keyed by definition ID, ensuring efficient and consistent struct writing.
  • Refactored SetStructAsync to use the cached writer delegate and layout size from defToWriter, improving reliability and preventing redundant layout computation. Throws a clear error if the writer is missing.

String encoding improvements:

  • Updated string field writing logic in SimVarFieldWriter to 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:

  • Standardized error messages to refer to [SimConnect] attributes instead of [SimVar] in factories, clarifying attribute usage in exception messages.
  • Improved parameter validation in SetAsync by switching to ArgumentNullException for the unit parameter, 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:

  • Added support for inferring SimConnectDataType.InitPosition from the SimConnectDataInitPosition type, expanding the range of supported struct types.

Author Information

Discord Username: bstudtma


Checklist:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

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].
Copilot AI review requested due to automatic review settings October 21, 2025 03:01
Copy link
Contributor

Copilot AI left a 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 defToWriter cache in SimVarManager to store struct writer delegates and layout sizes, eliminating redundant layout computations in SetStructAsync
  • Fixed string field encoding to use Latin1 with explicit null-termination and avoid unnecessary allocations
  • Relaxed unit parameter validation in SetAsync to 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

@stopbars stopbars deleted a comment from Copilot AI Oct 21, 2025
@stopbars stopbars deleted a comment from Copilot AI Oct 21, 2025
@AussieScorcher AussieScorcher changed the base branch from main to 0.1.17-beta October 21, 2025 10:27
@AussieScorcher AussieScorcher merged commit cec12cd into stopbars:0.1.17-beta Oct 21, 2025
2 checks passed
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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants