From a2f0d0bb1d3296c7efcdada19dcb1abe1d74e0d0 Mon Sep 17 00:00:00 2001 From: Koseng <26673978+Koseng@users.noreply.github.com> Date: Tue, 9 Jan 2024 20:52:12 +0100 Subject: [PATCH 1/2] Hash hostname for WASM registration --- SimConnectMSFS/SimConnectCache.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/SimConnectMSFS/SimConnectCache.cs b/SimConnectMSFS/SimConnectCache.cs index c4b5737c7..be912810b 100644 --- a/SimConnectMSFS/SimConnectCache.cs +++ b/SimConnectMSFS/SimConnectCache.cs @@ -72,10 +72,11 @@ public SimConnectCache() DATA_DEFINITION_ID = SIMCONNECT_DEFINE_ID.INIT_CLIENT, RESPONSE_OFFSET = 0 }; - + + string hashMachineName = GenerateSimpleHash(Environment.MachineName, int.MaxValue).ToString(); WasmRuntimeClientData = new WasmModuleClientData() { - NAME = $"Client_{Environment.MachineName}", + NAME = $"Client_{hashMachineName}", AREA_SIMVAR_ID = SIMCONNECT_CLIENT_DATA_ID.RUNTIME_LVARS, AREA_COMMAND_ID = SIMCONNECT_CLIENT_DATA_ID.RUNTIME_CMD, AREA_RESPONSE_ID = SIMCONNECT_CLIENT_DATA_ID.RUNTIME_RESPONSE, @@ -105,6 +106,18 @@ public void ReceiveSimConnectMessage() Disconnect(); } } + + private int GenerateSimpleHash(string s, int maxint) + { + // Simple hash algorithm, folding on a string, summed 4 bytes at a time + long sum = 0, mul = 1; + for (int i = 0; i < s.Length; i++) + { + mul = (i % 4 == 0) ? 1 : mul * 256; + sum += (long)s[i] * mul; + } + return (int)(Math.Abs(sum) % maxint); + } private void loadEventPresets() { From 32d2300d3d58c6a69810d56ce43500dfa386e24d Mon Sep 17 00:00:00 2001 From: Koseng <26673978+Koseng@users.noreply.github.com> Date: Tue, 9 Jan 2024 22:17:47 +0100 Subject: [PATCH 2/2] Generate hash as string extension method --- Base/Extensions.cs | 12 ++++++++++++ MobiFlight/MidiBoard/MidiBoard.cs | 19 ++++--------------- SimConnectMSFS/SimConnectCache.cs | 17 +++-------------- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/Base/Extensions.cs b/Base/Extensions.cs index cf627eada..ba7fe93ca 100644 --- a/Base/Extensions.cs +++ b/Base/Extensions.cs @@ -70,5 +70,17 @@ public static string GetLastFolderName(this string path) DirectoryInfo directoryInfo = new DirectoryInfo(path); return directoryInfo.Name; } + + public static int GenerateSimpleHash(this string s, int maxint) + { + // Simple hash algorithm, folding on a string, summed 4 bytes at a time + long sum = 0, mul = 1; + for (int i = 0; i < s.Length; i++) + { + mul = (i % 4 == 0) ? 1 : mul * 256; + sum += (long)s[i] * mul; + } + return (int)(Math.Abs(sum) % maxint); + } } } diff --git a/MobiFlight/MidiBoard/MidiBoard.cs b/MobiFlight/MidiBoard/MidiBoard.cs index 827395e78..7b969f126 100644 --- a/MobiFlight/MidiBoard/MidiBoard.cs +++ b/MobiFlight/MidiBoard/MidiBoard.cs @@ -1,4 +1,5 @@ using M; +using MobiFlight.Base; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -61,33 +62,21 @@ public bool HasMidiOutput public MidiBoard(MidiInputDevice midiInput, MidiOutputDevice midiOutput, string name, MidiBoardDefinition def) - { + { this.MidiInput = midiInput; this.MidiOutput = midiOutput; this.name = name; // Two boards of the same type get a different postfix to their name in windows - this.serial = GenerateSerial(name, int.MaxValue).ToString(); + this.serial = name.GenerateSimpleHash(int.MaxValue).ToString(); this.Definition = def; if (def != null) { EncoderNeutral = def.EncoderNeutralPosition; if (!string.IsNullOrEmpty(def.InitialLayer)) ActiveLayer = def.InitialLayer; - } + } EncoderRightFast = EncoderNeutral + 3; EncoderLeftFast = EncoderNeutral - 3; - } - - private int GenerateSerial(string s, int maxint) - { - // Simple hash algorithm, folding on a string, summed 4 bytes at a time - long sum = 0, mul = 1; - for (int i = 0; i < s.Length; i++) - { - mul = (i % 4 == 0) ? 1 : mul * 256; - sum += (long)s[i] * mul; - } - return (int)(Math.Abs(sum) % maxint); } private void SendInputEvent(InputEventArgs inputEventArgs) diff --git a/SimConnectMSFS/SimConnectCache.cs b/SimConnectMSFS/SimConnectCache.cs index be912810b..a9c51e1bd 100644 --- a/SimConnectMSFS/SimConnectCache.cs +++ b/SimConnectMSFS/SimConnectCache.cs @@ -3,6 +3,7 @@ using System.Runtime.InteropServices; using HidSharp.Utility; using Microsoft.FlightSimulator.SimConnect; +using MobiFlight.Base; namespace MobiFlight.SimConnectMSFS { @@ -72,8 +73,8 @@ public SimConnectCache() DATA_DEFINITION_ID = SIMCONNECT_DEFINE_ID.INIT_CLIENT, RESPONSE_OFFSET = 0 }; - - string hashMachineName = GenerateSimpleHash(Environment.MachineName, int.MaxValue).ToString(); + + string hashMachineName = Environment.MachineName.GenerateSimpleHash(int.MaxValue).ToString(); WasmRuntimeClientData = new WasmModuleClientData() { NAME = $"Client_{hashMachineName}", @@ -106,18 +107,6 @@ public void ReceiveSimConnectMessage() Disconnect(); } } - - private int GenerateSimpleHash(string s, int maxint) - { - // Simple hash algorithm, folding on a string, summed 4 bytes at a time - long sum = 0, mul = 1; - for (int i = 0; i < s.Length; i++) - { - mul = (i % 4 == 0) ? 1 : mul * 256; - sum += (long)s[i] * mul; - } - return (int)(Math.Abs(sum) % maxint); - } private void loadEventPresets() {