PowerControl: Improve support for SessionProfile

This commit is contained in:
Kamil Trzciński 2023-04-01 13:11:12 +02:00 committed by Kamil Trzcinski
parent 160c4762f0
commit 30ce97a35d
4 changed files with 112 additions and 102 deletions

View file

@ -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<T>(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) { }
}
}
}

View file

@ -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;

View file

@ -10,10 +10,8 @@ namespace PowerControl
{
public const bool AutoCreateProfiles = true;
public const int ApplyProfileDelayMs = 500;
public const int ResetProfileDelayMs = 500;
private Dictionary<int, PowerControl.Helper.ProfileSettings> watchedProcesses = new Dictionary<int, PowerControl.Helper.ProfileSettings>();
private Dictionary<MenuItemWithOptions, String>? 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<MenuItemWithOptions, string>();
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<MenuItemWithOptions> PersistableOptions()

View file

@ -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