From 19e7ed7012ff0748fe2feb23d9ed688433a72f83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Fri, 2 Dec 2022 11:29:06 +0100 Subject: [PATCH] Allow to lock steam controller locking files - This locks `controller_neptune` configs when adding steam detection - This overwrites default desktop/chord template - This enables a desktop template --- RELEASE.md | 3 +- SteamController/Controller.cs | 5 +- .../Devices/SteamControllerHaptic.cs | 6 +- SteamController/Helpers/SteamConfiguration.cs | 77 +++++++++++++++++++ .../Managers/SteamConfigsManager.cs | 77 +++++++++++++++++++ SteamController/Profiles/SteamProfile.cs | 2 +- SteamController/Profiles/X360HapticProfile.cs | 4 +- SteamController/Profiles/X360RumbleProfile.cs | 12 --- .../ProfilesSettings/X360HapticSettings.cs | 4 +- SteamController/Resources.Designer.cs | 20 +++++ SteamController/Resources.resx | 6 ++ .../controller_base/chord_neptune.vdf | 35 +++++++++ .../controller_base/empty_neptune.vdf | 35 +++++++++ SteamController/Settings.cs | 16 ++++ 14 files changed, 279 insertions(+), 23 deletions(-) create mode 100644 SteamController/Managers/SteamConfigsManager.cs create mode 100644 SteamController/Resources/controller_base/chord_neptune.vdf create mode 100644 SteamController/Resources/controller_base/empty_neptune.vdf diff --git a/RELEASE.md b/RELEASE.md index dc88ed4..393216c 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -17,4 +17,5 @@ It does help this project on being supported. - Fix using Playnite to launch Steam game where on exit Desktop was activated - Build DEBUG that has all experimental features - Introduce X360 Haptic profile to improve vibration (in DEBUG) -- Re-open Neptune controller every 10 failures \ No newline at end of file +- Re-open Neptune controller every 10 failures +- Manage Steam default controller configs to prevent double inputs (in DEBUG, change Settings) diff --git a/SteamController/Controller.cs b/SteamController/Controller.cs index 5512ddb..900725b 100644 --- a/SteamController/Controller.cs +++ b/SteamController/Controller.cs @@ -31,6 +31,7 @@ namespace SteamController new Managers.ProcessManager(), new Managers.SteamManager(), new Managers.ProfileSwitcher(), + new Managers.SteamConfigsManager(), new Managers.SharedDataManager() } }; @@ -231,8 +232,8 @@ namespace SteamController ignoreSteamItem.Visible = blacklistedSteamController is not null; useX360WithSteamItem.Visible = blacklistedSteamController is not null; - steamSeparatorItem.Visible = blacklistedSteamController is not null; useSteamInputItem.Visible = blacklistedSteamController is not null; + steamSeparatorItem.Visible = blacklistedSteamController is not null; ignoreSteamItem.Checked = !Settings.Default.EnableSteamDetection || blacklistedSteamController == null; useX360WithSteamItem.Checked = Settings.Default.EnableSteamDetection && blacklistedSteamController == true; @@ -260,6 +261,7 @@ namespace SteamController Helpers.SteamConfiguration.KillSteam(); Helpers.SteamConfiguration.WaitForSteamClose(5000); Helpers.SteamConfiguration.BackupSteamConfig(); + var steamControllerUpdate = Helpers.SteamConfiguration.UpdateControllerBlacklist( Devices.SteamController.VendorID, Devices.SteamController.ProductID, @@ -271,7 +273,6 @@ namespace SteamController blacklistX360Controller ); Settings.Default.EnableSteamDetection = steamDetection; - Settings.Default.Save(); if (steamControllerUpdate && x360ControllerUpdate) { diff --git a/SteamController/Devices/SteamControllerHaptic.cs b/SteamController/Devices/SteamControllerHaptic.cs index 34ef6c5..ce4f34b 100644 --- a/SteamController/Devices/SteamControllerHaptic.cs +++ b/SteamController/Devices/SteamControllerHaptic.cs @@ -36,7 +36,7 @@ namespace SteamController.Devices try { Marshal.StructureToPtr(haptic, handle.AddrOfPinnedObject(), false); - neptuneDevice.RequestFeatureReport(bytes); + neptuneDevice.RequestFeatureReportAsync(bytes); return true; } catch (Exception e) @@ -69,7 +69,7 @@ namespace SteamController.Devices var haptic = new SDCHapticPacket2() { position = position, - intensity = intensity, + intensity = (sbyte)(intensity - 5), // convert from dB to values tsA = ts, tsB = ts }; @@ -82,7 +82,7 @@ namespace SteamController.Devices try { Marshal.StructureToPtr(haptic, handle.AddrOfPinnedObject(), false); - neptuneDevice.RequestFeatureReport(bytes); + neptuneDevice.RequestFeatureReportAsync(bytes); return true; } catch (Exception e) diff --git a/SteamController/Helpers/SteamConfiguration.cs b/SteamController/Helpers/SteamConfiguration.cs index 6a8396c..63e9bba 100644 --- a/SteamController/Helpers/SteamConfiguration.cs +++ b/SteamController/Helpers/SteamConfiguration.cs @@ -262,6 +262,83 @@ namespace SteamController.Helpers } } + public static bool? IsConfigFileOverwritten(String path, byte[] content) + { + try + { + var configPath = GetConfigPath(path); + if (configPath is null) + return null; + + byte[] diskContent = File.ReadAllBytes(configPath); + return content.SequenceEqual(diskContent); + } + catch (IOException) + { + return null; + } + } + + public static bool? ResetConfigFile(String path) + { + try + { + var configPath = GetConfigPath(path); + if (configPath is null) + return null; + + File.Copy(configPath + ".orig", configPath, true); + return true; + } + catch (UnauthorizedAccessException) + { + return false; + } + catch (System.Security.SecurityException) + { + return false; + } + catch (IOException) + { + return null; + } + } + + public static bool? OverwriteConfigFile(String path, byte[] content, bool backup) + { + try + { + var configPath = GetConfigPath(path); + if (configPath is null) + return null; + + try + { + byte[] diskContent = File.ReadAllBytes(configPath); + if (content.Equals(diskContent)) + return false; + } + catch (IOException) { } + + if (backup) + File.Copy(configPath, configPath + ".orig", true); + File.WriteAllBytes(configPath, content); + return true; + } + catch (UnauthorizedAccessException) + { + return false; + } + catch (System.Security.SecurityException) + { + return false; + } + catch (IOException) + { + return null; + } + } + private static T? GetValue(string key, string value) where T : struct { try diff --git a/SteamController/Managers/SteamConfigsManager.cs b/SteamController/Managers/SteamConfigsManager.cs new file mode 100644 index 0000000..5083c19 --- /dev/null +++ b/SteamController/Managers/SteamConfigsManager.cs @@ -0,0 +1,77 @@ +using System.Diagnostics; +using SteamController.Helpers; + +namespace SteamController.Managers +{ + public sealed class SteamConfigsManager : Manager + { + static readonly Dictionary lockedSteamControllerFiles = new Dictionary + { + // Use existing defaults in BasicUI and BigPicture + // { "controller_base/basicui_neptune.vdf", Resources.basicui_neptune }, + // { "controller_base/bigpicture_neptune.vdf", Resources.bigpicture_neptune }, + { "controller_base/desktop_neptune.vdf", Resources.empty_neptune }, + { "controller_base/chord_neptune.vdf", Resources.chord_neptune } + }; + static readonly Dictionary installedSteamControllerFiles = new Dictionary { + { "controller_base/templates/controller_neptune_steamcontroller.vdf", Resources.empty_neptune }, + }; + + private bool? filesLocked; + + public SteamConfigsManager() + { + // always unlock configs when changed + Settings.Default.SettingChanging += UnlockControllerFiles; + SetSteamControllerFilesLock(false); + } + + private bool IsActive + { + get { return Settings.Default.ManageSteamControllerConfigs && Settings.Default.EnableSteamDetection; } + } + + public override void Dispose() + { + SetSteamControllerFilesLock(false); + Settings.Default.SettingChanging -= UnlockControllerFiles; + } + + private void UnlockControllerFiles(object sender, System.Configuration.SettingChangingEventArgs e) + { + SetSteamControllerFilesLock(false); + } + + public override void Tick(Context context) + { + if (!IsActive) + return; + + bool running = Helpers.SteamConfiguration.IsRunning; + if (running == filesLocked) + return; + + SetSteamControllerFilesLock(running); + } + + private void SetSteamControllerFilesLock(bool lockConfigs) + { + if (!IsActive) + return; + + if (lockConfigs) + { + foreach (var config in lockedSteamControllerFiles) + Helpers.SteamConfiguration.OverwriteConfigFile(config.Key, config.Value, true); + foreach (var config in installedSteamControllerFiles) + Helpers.SteamConfiguration.OverwriteConfigFile(config.Key, config.Value, false); + } + else + { + foreach (var config in lockedSteamControllerFiles) + Helpers.SteamConfiguration.ResetConfigFile(config.Key); + } + filesLocked = lockConfigs; + } + } +} diff --git a/SteamController/Profiles/SteamProfile.cs b/SteamController/Profiles/SteamProfile.cs index 2f8b1be..e325d86 100644 --- a/SteamController/Profiles/SteamProfile.cs +++ b/SteamController/Profiles/SteamProfile.cs @@ -10,7 +10,7 @@ namespace SteamController.Profiles public override bool Selected(Context context) { - return context.Enabled && context.State.SteamUsesSteamInput; + return context.Enabled && context.State.SteamUsesSteamInput && !Settings.Default.ManageSteamControllerConfigs; } public override Status Run(Context context) diff --git a/SteamController/Profiles/X360HapticProfile.cs b/SteamController/Profiles/X360HapticProfile.cs index c68f1a3..0c2d337 100644 --- a/SteamController/Profiles/X360HapticProfile.cs +++ b/SteamController/Profiles/X360HapticProfile.cs @@ -19,12 +19,12 @@ namespace SteamController.Profiles if (context.X360.FeedbackLargeMotor.GetValueOrDefault() > 0) { - context.Steam.SendHaptic(1, 0); + context.Steam.SendHaptic(1, HapticSettings.LeftIntensity); } if (context.X360.FeedbackSmallMotor.GetValueOrDefault() > 0) { - context.Steam.SendHaptic(0, 0); + context.Steam.SendHaptic(0, HapticSettings.RightIntensity); } context.X360.ResetFeedback(); diff --git a/SteamController/Profiles/X360RumbleProfile.cs b/SteamController/Profiles/X360RumbleProfile.cs index afa2a4c..72a6d83 100644 --- a/SteamController/Profiles/X360RumbleProfile.cs +++ b/SteamController/Profiles/X360RumbleProfile.cs @@ -21,17 +21,6 @@ namespace SteamController.Profiles return Status.Done; } -#if false - if (context.X360.FeedbackLargeMotor.GetValueOrDefault() > 0) - { - context.Steam.SetHaptic2(1, 0); - } - - if (context.X360.FeedbackSmallMotor.GetValueOrDefault() > 0) - { - context.Steam.SetHaptic2(0, 0); - } -#else if (context.X360.FeedbackLargeMotor.HasValue) { context.Steam.SetFeedback( @@ -43,7 +32,6 @@ namespace SteamController.Profiles context.Steam.SetFeedback( 0, GetHapticAmplitude(context.X360.FeedbackSmallMotor), RumbleSettings.Period, FeedbackCount); } -#endif context.X360.ResetFeedback(); diff --git a/SteamController/ProfilesSettings/X360HapticSettings.cs b/SteamController/ProfilesSettings/X360HapticSettings.cs index 552df56..fb0160c 100644 --- a/SteamController/ProfilesSettings/X360HapticSettings.cs +++ b/SteamController/ProfilesSettings/X360HapticSettings.cs @@ -18,7 +18,7 @@ namespace SteamController.ProfilesSettings } [UserScopedSettingAttribute()] - [DefaultSettingValueAttribute("0")] + [DefaultSettingValueAttribute("5")] [Description("Haptic intensity between -2dB and 10dB")] public sbyte LeftIntensity { @@ -27,7 +27,7 @@ namespace SteamController.ProfilesSettings } [UserScopedSettingAttribute()] - [DefaultSettingValueAttribute("0")] + [DefaultSettingValueAttribute("5")] [Description("Haptic intensity between -2dB and 10dB")] public sbyte RightIntensity { diff --git a/SteamController/Resources.Designer.cs b/SteamController/Resources.Designer.cs index 8a5d6e9..8d2fa24 100644 --- a/SteamController/Resources.Designer.cs +++ b/SteamController/Resources.Designer.cs @@ -60,6 +60,26 @@ namespace SteamController { } } + /// + /// Looks up a localized resource of type System.Byte[]. + /// + internal static byte[] chord_neptune { + get { + object obj = ResourceManager.GetObject("chord_neptune", resourceCulture); + return ((byte[])(obj)); + } + } + + /// + /// Looks up a localized resource of type System.Byte[]. + /// + internal static byte[] empty_neptune { + get { + object obj = ResourceManager.GetObject("empty_neptune", resourceCulture); + return ((byte[])(obj)); + } + } + /// /// Looks up a localized resource of type System.Drawing.Icon similar to (Icon). /// diff --git a/SteamController/Resources.resx b/SteamController/Resources.resx index e66ba11..306ebee 100644 --- a/SteamController/Resources.resx +++ b/SteamController/Resources.resx @@ -118,6 +118,12 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + Resources\controller_base\chord_neptune.vdf;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Resources\controller_base\empty_neptune.vdf;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + Resources\laptop.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a diff --git a/SteamController/Resources/controller_base/chord_neptune.vdf b/SteamController/Resources/controller_base/chord_neptune.vdf new file mode 100644 index 0000000..1fcf527 --- /dev/null +++ b/SteamController/Resources/controller_base/chord_neptune.vdf @@ -0,0 +1,35 @@ +"controller_mappings" +{ + "version" "3" + "title" "SteamController replaced Chord Configuration" + "description" "Template provided by SteamController to disable default mappings of Steam to prevent double inputs" + "controller_type" "controller_neptune" + "major_revision" "0" + "minor_revision" "0" + "actions" + { + "Preset_1000001" + { + "title" "Empty" + "legacy_set" "1" + } + } + "preset" + { + "id" "0" + "name" "Preset_1000001" + "group_source_bindings" + { + } + } + "action_layers" + { + } + "settings" + { + "left_trackpad_mode" "0" + "right_trackpad_mode" "0" + "action_set_trigger_cursor_show" "0" + "action_set_trigger_cursor_hide" "0" + } +} diff --git a/SteamController/Resources/controller_base/empty_neptune.vdf b/SteamController/Resources/controller_base/empty_neptune.vdf new file mode 100644 index 0000000..8733a66 --- /dev/null +++ b/SteamController/Resources/controller_base/empty_neptune.vdf @@ -0,0 +1,35 @@ +"controller_mappings" +{ + "version" "3" + "title" "SteamController provided empty configuration" + "description" "Template provided by SteamController to disable default mappings of Steam to prevent double inputs" + "controller_type" "controller_neptune" + "major_revision" "0" + "minor_revision" "0" + "actions" + { + "Preset_1000001" + { + "title" "Empty" + "legacy_set" "1" + } + } + "preset" + { + "id" "0" + "name" "Preset_1000001" + "group_source_bindings" + { + } + } + "action_layers" + { + } + "settings" + { + "left_trackpad_mode" "0" + "right_trackpad_mode" "0" + "action_set_trigger_cursor_show" "0" + "action_set_trigger_cursor_hide" "0" + } +} diff --git a/SteamController/Settings.cs b/SteamController/Settings.cs index 6aecc8d..0aabb09 100644 --- a/SteamController/Settings.cs +++ b/SteamController/Settings.cs @@ -35,6 +35,22 @@ namespace SteamController set { this["StartupProfile"] = value; } } +#if DEBUG + [UserScopedSetting] + [BrowsableAttribute(true)] +#else + [ApplicationScopedSetting] + [BrowsableAttribute(false)] +#endif + [DefaultSettingValue("False")] + [DisplayName("Manage Steam Controller Configs")] + [Description("This does replace Steam configuration for controllers to prevent double inputs")] + public bool ManageSteamControllerConfigs + { + get { return ((bool)(this["ManageSteamControllerConfigs"])); } + set { this["ManageSteamControllerConfigs"] = value; } + } + public override string ToString() { return "";