diff --git a/CommonHelpers/BaseSettings.cs b/CommonHelpers/BaseSettings.cs index 1ec9454..6fd0cbe 100644 --- a/CommonHelpers/BaseSettings.cs +++ b/CommonHelpers/BaseSettings.cs @@ -16,6 +16,12 @@ namespace CommonHelpers [Browsable(false)] public bool TouchSettings { get; set; } + [Browsable(false)] + public bool UseMemoryCache { get; set; } = true; + + [Browsable(false)] + public bool UseConfigFile { get; set; } = true; + [Browsable(false)] public String SettingsKey { get; protected set; } @@ -34,7 +40,7 @@ namespace CommonHelpers [Browsable(false)] public bool Exists { - get { return File.Exists(this.ConfigFile); } + get { return !this.UseConfigFile || File.Exists(this.ConfigFile); } } public override string ToString() @@ -61,7 +67,7 @@ namespace CommonHelpers return false; SettingChanging(key); - if (!SetProfileString(key, valueString)) + if (this.UseConfigFile && !SetProfileString(key, valueString)) return false; cachedValues[key] = value; @@ -71,8 +77,11 @@ namespace CommonHelpers protected T Get(string key, T defaultValue, bool touchSettings = false) { - if (cachedValues.TryGetValue(key, out var cachedValue)) - return ((T?)cachedValue) ?? defaultValue; + if (this.UseMemoryCache) + { + if (cachedValues.TryGetValue(key, out var cachedValue)) + return ((T?)cachedValue) ?? defaultValue; + } var typeConverter = TypeDescriptor.GetConverter(typeof(T)); var defaultString = typeConverter.ConvertToString(defaultValue); @@ -82,6 +91,12 @@ namespace CommonHelpers return defaultValue; } + if (!this.UseConfigFile) + { + cachedValues[key] = defaultValue; + return defaultValue; + } + try { var valueString = GetProfileString(key, defaultString); @@ -131,7 +146,12 @@ namespace CommonHelpers lock (this) { cachedValues.Remove(key); - return WritePrivateProfileString(SettingsKey, key, null, ConfigFile); + + if (this.UseConfigFile) + { + return WritePrivateProfileString(SettingsKey, key, null, ConfigFile); + } + return true; } } @@ -140,7 +160,12 @@ namespace CommonHelpers lock (this) { cachedValues.Clear(); - return WritePrivateProfileString(SettingsKey, null, null, ConfigFile); + + if (this.UseConfigFile) + { + return WritePrivateProfileString(SettingsKey, null, null, ConfigFile); + } + return true; } } @@ -148,10 +173,10 @@ namespace CommonHelpers { lock (this) { - if (Exists) - return; - - using (File.Create(ConfigFile)) { } + if (this.UseConfigFile && Exists) + { + using (File.Create(ConfigFile)) { } + } } } @@ -160,8 +185,12 @@ namespace CommonHelpers lock (this) { cachedValues.Clear(); - try { File.Delete(ConfigFile); } - catch (DirectoryNotFoundException) { } + + if (this.UseConfigFile) + { + try { File.Delete(ConfigFile); } + catch (DirectoryNotFoundException) { } + } } } diff --git a/PowerControl/Options/Profiles.cs b/PowerControl/Options/Profiles.cs index 545dde0..dfc3d42 100644 --- a/PowerControl/Options/Profiles.cs +++ b/PowerControl/Options/Profiles.cs @@ -10,7 +10,7 @@ namespace PowerControl.Options ApplyDelay = 500, OptionsValues = delegate () { - var currentProfileSettings = Controller?.CurrentProfileSettings; + var currentProfileSettings = Controller?.GameProfileSettings; if (currentProfileSettings == null) return null; @@ -34,7 +34,7 @@ namespace PowerControl.Options CycleOptions = false, CurrentValue = delegate () { - var currentProfileSettings = Controller?.CurrentProfileSettings; + var currentProfileSettings = Controller?.GameProfileSettings; if (currentProfileSettings == null) return null; @@ -53,11 +53,11 @@ namespace PowerControl.Options case "Create New": Controller?.CreateProfile(false); - return Controller?.CurrentProfileSettings?.ProfileName; + return Controller?.GameProfileSettings?.ProfileName; case "Save All": Controller?.CreateProfile(true); - return Controller?.CurrentProfileSettings?.ProfileName; + return Controller?.GameProfileSettings?.ProfileName; default: return selected; diff --git a/PowerControl/ProfilesController.cs b/PowerControl/ProfilesController.cs index 4d5a63a..32efe8a 100644 --- a/PowerControl/ProfilesController.cs +++ b/PowerControl/ProfilesController.cs @@ -10,10 +10,8 @@ namespace PowerControl { public const bool AutoCreateProfiles = true; public const int ApplyProfileDelayMs = 500; - public const int ResetProfileDelayMs = 500; private Dictionary watchedProcesses = new Dictionary(); - private Dictionary? changedSettings; private CancellationTokenSource? changeTask; private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer() @@ -30,7 +28,9 @@ namespace PowerControl } } - public ProfileSettings? CurrentProfileSettings { get; private set; } + public ProfileSettings? GameProfileSettings { get; private set; } + + public ProfileSettings? SessionProfileSettings { get; private set; } public ProfilesController() { @@ -92,12 +92,12 @@ namespace PowerControl if (!watchedProcesses.TryGetValue(processId, out var profileSettings)) return false; - if (CurrentProfileSettings != profileSettings) + if (GameProfileSettings != profileSettings) { Log.TraceLine("ProfilesController: Foreground changed: {0} => {1}", - CurrentProfileSettings?.ProfileName, profileSettings.ProfileName); - CurrentProfileSettings = profileSettings; - ProfileChanged(); + GameProfileSettings?.ProfileName, profileSettings.ProfileName); + GameProfileSettings = profileSettings; + ProfileChanged(profileSettings); } return true; } @@ -106,12 +106,18 @@ namespace PowerControl { Log.TraceLine("ProfilesController: New Process: {0}/{1}", processId, processName); - if (changedSettings == null) - changedSettings = new Dictionary(); - var profileSettings = new ProfileSettings(processName); watchedProcesses.Add(processId, profileSettings); + // Create memory only SessionProfileSettings + if (SessionProfileSettings is null) + { + SessionProfileSettings = new ProfileSettings("Session:" + processName) { UseConfigFile = false }; + SaveProfile(SessionProfileSettings); + } + + GameProfileSettings = profileSettings; + ProfileChanged(profileSettings); ApplyProfile(profileSettings); } @@ -120,8 +126,8 @@ namespace PowerControl if (!watchedProcesses.Remove(processId, out var profileSettings)) return; - if (CurrentProfileSettings == profileSettings) - CurrentProfileSettings = null; + if (GameProfileSettings == profileSettings) + GameProfileSettings = null; Log.TraceLine("ProfilesController: Removed Process: {0}", processId); @@ -136,79 +142,77 @@ namespace PowerControl if (options.PersistentKey is null) return; - if (oldValue is not null) - { - if (changedSettings?.TryAdd(options, oldValue) == true) - { - Log.TraceLine("ProfilesController: Saved change: {0} from {1}", options.PersistentKey, oldValue); - } - } + // No active profile, cannot persist + if (GameProfileSettings is null) + return; - // If profile exists persist value - if (CurrentProfileSettings != null && (CurrentProfileSettings.Exists || AutoCreateProfiles)) - { - CurrentProfileSettings.SetValue(options.PersistentKey, newValue); - options.ProfileOption = newValue; + // Do not auto-create profile unless requested + if (!GameProfileSettings.Exists && !AutoCreateProfiles) + return; - Log.TraceLine("ProfilesController: Stored: {0} {1} = {2}", - CurrentProfileSettings.ProfileName, options.PersistentKey, newValue); - } + GameProfileSettings.SetValue(options.PersistentKey, newValue); + options.ProfileOption = newValue; + + Log.TraceLine("ProfilesController: Stored: {0} {1} = {2}", + GameProfileSettings.ProfileName, options.PersistentKey, newValue); } - private void ProfileChanged() + private void ProfileChanged(ProfileSettings? profileSettings) { foreach (var menuItem in PersistableOptions()) { - menuItem.ProfileOption = CurrentProfileSettings?.GetValue(menuItem.PersistentKey ?? ""); + menuItem.ProfileOption = profileSettings?.GetValue(menuItem.PersistentKey ?? ""); } } public void CreateProfile(bool saveAll = true) { - var profileSettings = CurrentProfileSettings; - - profileSettings?.TouchFile(); - - Log.TraceLine("ProfilesController: Created Profile: {0}, SaveAll={1}", - profileSettings?.ProfileName, saveAll); - - if (!saveAll) + var profileSettings = GameProfileSettings; + if (profileSettings is null) return; - foreach (var menuItem in PersistableOptions()) - { - if (menuItem.ActiveOption is null || !menuItem.PersistOnCreate) - continue; - profileSettings?.SetValue(menuItem.PersistentKey ?? "", menuItem.ActiveOption); - } + profileSettings.TouchFile(); - ProfileChanged(); + Log.TraceLine("ProfilesController: Created Profile: {0}, SaveAll={1}", + profileSettings.ProfileName, saveAll); + + if (saveAll) + SaveProfile(profileSettings); + + ProfileChanged(profileSettings); } public void DeleteProfile() { - CurrentProfileSettings?.DeleteFile(); - ProfileChanged(); + GameProfileSettings?.DeleteFile(); + ProfileChanged(GameProfileSettings); - Log.TraceLine("ProfilesController: Deleted Profile: {0}", CurrentProfileSettings?.ProfileName); + Log.TraceLine("ProfilesController: Deleted Profile: {0}", GameProfileSettings?.ProfileName); + } + + private void SaveProfile(ProfileSettings profileSettings, bool force = false) + { + foreach (var menuItem in PersistableOptions()) + { + if (menuItem.ActiveOption is null || !menuItem.PersistOnCreate && !force) + continue; + profileSettings?.SetValue(menuItem.PersistentKey ?? "", menuItem.ActiveOption); + } } private void ApplyProfile(ProfileSettings profileSettings) { - CurrentProfileSettings = profileSettings; - ProfileChanged(); - - if (CurrentProfileSettings is null || CurrentProfileSettings?.Exists != true) + if (profileSettings is null || profileSettings?.Exists != true) return; - int delay = CurrentProfileSettings.GetInt("ApplyDelay", ApplyProfileDelayMs); + int delay = profileSettings.GetInt("ApplyDelay", ApplyProfileDelayMs); changeTask?.Cancel(); changeTask = Dispatcher.RunWithDelay(delay, () => { foreach (var menuItem in PersistableOptions()) { - var persistedValue = CurrentProfileSettings.GetValue(menuItem.PersistentKey ?? ""); + var persistedValue = profileSettings.GetValue(menuItem.PersistentKey ?? ""); if (persistedValue is null) continue; @@ -217,14 +221,14 @@ namespace PowerControl menuItem.Set(persistedValue, true, false); Log.TraceLine("ProfilesController: Applied from Profile: {0}: {1} = {2}", - CurrentProfileSettings.ProfileName, menuItem.PersistentKey, persistedValue); + profileSettings.ProfileName, menuItem.PersistentKey, persistedValue); } catch (Exception e) { Log.TraceLine("ProfilesController: Exception Profile: {0}: {1} = {2} => {3}", - CurrentProfileSettings.ProfileName, menuItem.PersistentKey, persistedValue, e); + profileSettings.ProfileName, menuItem.PersistentKey, persistedValue, e); - CurrentProfileSettings.DeleteKey(menuItem.PersistentKey ?? ""); + profileSettings.DeleteKey(menuItem.PersistentKey ?? ""); menuItem.ProfileOption = null; } } @@ -233,38 +237,14 @@ namespace PowerControl private void ResetProfile() { - CurrentProfileSettings = null; - ProfileChanged(); + GameProfileSettings = null; + ProfileChanged(null); - if (changedSettings is null) - return; - - // Revert all changes made to original value - var appliedSettings = changedSettings; - changedSettings = null; - - changeTask?.Cancel(); - changeTask = Dispatcher.RunWithDelay(ResetProfileDelayMs, () => + if (SessionProfileSettings is not null) { - foreach (var menuItem in PersistableOptions()) - { - if (!appliedSettings.TryGetValue(menuItem, out var setting)) - continue; - - try - { - menuItem.Set(setting, true, true); - - Log.TraceLine("ProfilesController: Reset: {0} = {1}", - menuItem.PersistentKey, setting); - } - catch (Exception e) - { - Log.TraceLine("ProfilesController: Reset Exception: {0} = {1} => {2}", - menuItem.PersistentKey, setting, e); - } - } - }); + ApplyProfile(SessionProfileSettings); + SessionProfileSettings = null; + } } private IEnumerable PersistableOptions() diff --git a/RELEASE.md b/RELEASE.md index 8fff653..9527bd7 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -31,6 +31,7 @@ ## 0.6.16 +- PowerControl: Improve handling of restoring DesktopProfile - All: Support [unofficial APU drivers](https://sourceforge.net/projects/amernimezone/files/Release%20Polaris-Vega-Navi/AMD%20SOC%20Driver%20Variant/) that present themselves as `AMD Radeon 670M` - PowerControl: Show Game Profiles menu item