mirror of
https://github.com/Maassoft/ColorControl.git
synced 2026-10-22 00:22:18 -05:00
made loading presets code more robust
This commit is contained in:
1 parent
3667243fc7
commit
dd9ce932fc
10 files changed
+111
-71
No files matched your search
@@ -13,16 +13,11 @@ namespace ColorControl
|
||||
|
||||
private ADLDisplayInfo _currentDisplay;
|
||||
|
||||
public AmdService(string dataPath) : base(dataPath)
|
||||
public AmdService(string dataPath) : base(dataPath, "AmdPresets.json")
|
||||
{
|
||||
LoadPresets();
|
||||
}
|
||||
|
||||
public AmdPreset GetLastAppliedPreset()
|
||||
{
|
||||
return _lastAppliedPreset;
|
||||
}
|
||||
|
||||
public static bool ExecutePresetAsync(string idOrName)
|
||||
{
|
||||
try
|
||||
@@ -45,20 +40,9 @@ namespace ColorControl
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadPresets()
|
||||
protected override List<AmdPreset> GetDefaultPresets()
|
||||
{
|
||||
_presetsFilename = Path.Combine(_dataPath, "AmdPresets.json");
|
||||
|
||||
if (File.Exists(_presetsFilename))
|
||||
{
|
||||
var json = File.ReadAllText(_presetsFilename);
|
||||
|
||||
_presets = _JsonDeserializer.Deserialize<List<AmdPreset>>(json);
|
||||
}
|
||||
else
|
||||
{
|
||||
_presets = AmdPreset.GetDefaultPresets();
|
||||
}
|
||||
return AmdPreset.GetDefaultPresets();
|
||||
}
|
||||
|
||||
private void SavePresets()
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
<ProductName>ColorControl</ProductName>
|
||||
<PublisherName>Maassoft</PublisherName>
|
||||
<ApplicationRevision>0</ApplicationRevision>
|
||||
<ApplicationVersion>4.1.0.0</ApplicationVersion>
|
||||
<ApplicationVersion>4.2.0.0</ApplicationVersion>
|
||||
<UseApplicationTrust>false</UseApplicationTrust>
|
||||
<PublishWizardCompleted>true</PublishWizardCompleted>
|
||||
<BootstrapperEnabled>true</BootstrapperEnabled>
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace ColorControl
|
||||
{
|
||||
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
public GraphicsService(string dataPath) : base(dataPath)
|
||||
public GraphicsService(string dataPath, string presetsBaseFilename) : base(dataPath, presetsBaseFilename)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
@@ -227,11 +227,11 @@ namespace ColorControl
|
||||
|
||||
if (settings.backlight != null)
|
||||
{
|
||||
PictureSettings.Backlight = Utils.ParseInt(settings.backlight.Value, PictureSettings.Backlight);
|
||||
PictureSettings.Backlight = Utils.ParseDynamicAsInt(settings.backlight, PictureSettings.Backlight);
|
||||
}
|
||||
if (settings.contrast != null)
|
||||
{
|
||||
PictureSettings.Contrast = Utils.ParseInt(settings.contrast.Value, PictureSettings.Contrast);
|
||||
PictureSettings.Contrast = Utils.ParseDynamicAsInt(settings.contrast, PictureSettings.Contrast);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+10
-24
@@ -65,7 +65,7 @@ namespace ColorControl
|
||||
private Task _monitorTask;
|
||||
private int _monitorTaskCounter;
|
||||
|
||||
public LgService(string dataPath, bool allowPowerOn) : base(dataPath)
|
||||
public LgService(string dataPath, bool allowPowerOn) : base(dataPath, "LgPresets.json")
|
||||
{
|
||||
_allowPowerOn = allowPowerOn;
|
||||
|
||||
@@ -127,33 +127,19 @@ namespace ColorControl
|
||||
|
||||
return preset;
|
||||
}
|
||||
|
||||
private void LoadPresets()
|
||||
protected override List<LgPreset> GetDefaultPresets()
|
||||
{
|
||||
_presetsFilename = Path.Combine(_dataPath, "LgPresets.json");
|
||||
var toCopy = Path.Combine(Directory.GetCurrentDirectory(), "LgPresets.json");
|
||||
if (!File.Exists(_presetsFilename) && File.Exists(toCopy))
|
||||
List<LgPreset> presets = null;
|
||||
|
||||
var defaultPresetsFileName = Path.Combine(Directory.GetCurrentDirectory(), "LgPresets.json");
|
||||
if (File.Exists(defaultPresetsFileName))
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Copy(toCopy, _presetsFilename);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Error($"Error while copying {toCopy} to {_presetsFilename}: {e.Message}");
|
||||
}
|
||||
var json = File.ReadAllText(defaultPresetsFileName);
|
||||
|
||||
presets = _JsonDeserializer.Deserialize<List<LgPreset>>(json);
|
||||
}
|
||||
|
||||
if (File.Exists(_presetsFilename))
|
||||
{
|
||||
var json = File.ReadAllText(_presetsFilename);
|
||||
|
||||
_presets = _JsonSerializer.Deserialize<List<LgPreset>>(json);
|
||||
}
|
||||
else
|
||||
{
|
||||
_presets = new List<LgPreset>();
|
||||
}
|
||||
return presets;
|
||||
}
|
||||
|
||||
private void LoadConfig()
|
||||
|
||||
@@ -126,10 +126,10 @@ namespace ColorControl
|
||||
|
||||
_nvService.AfterApplyPreset += NvServiceAfterApplyPreset;
|
||||
}
|
||||
catch (Exception)
|
||||
catch (Exception ex)
|
||||
{
|
||||
//Logger.Error("Error initializing NvService: " + e.ToLogString());
|
||||
Logger.Debug("No NVIDIA device detected");
|
||||
Logger.Debug($"No NVIDIA device detected: {ex.Message}");
|
||||
tcMain.TabPages.Remove(tabNVIDIA);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ namespace ColorControl
|
||||
|
||||
private Display _currentDisplay;
|
||||
|
||||
public NvService(string dataPath) : base(dataPath)
|
||||
public NvService(string dataPath) : base(dataPath, "NvPresets.json")
|
||||
{
|
||||
var converter = new ColorDataConverter();
|
||||
_JsonDeserializer.RegisterConverters(new[] { converter });
|
||||
@@ -126,20 +126,9 @@ namespace ColorControl
|
||||
SavePresets();
|
||||
}
|
||||
|
||||
private void LoadPresets()
|
||||
protected override List<NvPreset> GetDefaultPresets()
|
||||
{
|
||||
_presetsFilename = Path.Combine(_dataPath, "NvPresets.json");
|
||||
|
||||
if (File.Exists(_presetsFilename))
|
||||
{
|
||||
var json = File.ReadAllText(_presetsFilename);
|
||||
|
||||
_presets = _JsonDeserializer.Deserialize<List<NvPreset>>(json);
|
||||
}
|
||||
else
|
||||
{
|
||||
_presets = NvPreset.GetDefaultPresets();
|
||||
}
|
||||
return NvPreset.GetDefaultPresets();
|
||||
}
|
||||
|
||||
private void SavePresets()
|
||||
@@ -493,11 +482,6 @@ namespace ColorControl
|
||||
return list;
|
||||
}
|
||||
|
||||
public NvPreset GetLastAppliedPreset()
|
||||
{
|
||||
return _lastAppliedPreset;
|
||||
}
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
NVIDIA.Initialize();
|
||||
|
||||
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("4.1.0.0")]
|
||||
[assembly: AssemblyFileVersion("4.1.0.0")]
|
||||
[assembly: AssemblyVersion("4.2.0.0")]
|
||||
[assembly: AssemblyFileVersion("4.2.0.0")]
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Web.Script.Serialization;
|
||||
|
||||
@@ -7,19 +8,25 @@ namespace ColorControl
|
||||
{
|
||||
abstract class ServiceBase<T> where T : PresetBase
|
||||
{
|
||||
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
public event EventHandler<T> AfterApplyPreset;
|
||||
|
||||
protected string _dataPath;
|
||||
protected string _presetsBaseFilename;
|
||||
protected string _presetsFilename;
|
||||
protected string _presetsBackupFilename;
|
||||
protected JavaScriptSerializer _JsonSerializer;
|
||||
protected JavaScriptSerializer _JsonDeserializer;
|
||||
protected bool _initialized = false;
|
||||
protected List<T> _presets;
|
||||
protected T _lastAppliedPreset;
|
||||
protected string _loadPresetsError;
|
||||
|
||||
public ServiceBase(string dataPath)
|
||||
public ServiceBase(string dataPath, string presetsBaseFilename)
|
||||
{
|
||||
_dataPath = dataPath;
|
||||
_presetsBaseFilename = presetsBaseFilename;
|
||||
|
||||
_JsonSerializer = new JavaScriptSerializer();
|
||||
_JsonDeserializer = new JavaScriptSerializer();
|
||||
@@ -48,6 +55,11 @@ namespace ColorControl
|
||||
return preset;
|
||||
}
|
||||
|
||||
public T GetLastAppliedPreset()
|
||||
{
|
||||
return _lastAppliedPreset;
|
||||
}
|
||||
|
||||
protected virtual void Initialize()
|
||||
{
|
||||
|
||||
@@ -62,5 +74,65 @@ namespace ColorControl
|
||||
{
|
||||
AfterApplyPreset?.Invoke(this, _lastAppliedPreset);
|
||||
}
|
||||
|
||||
protected void LoadPresets()
|
||||
{
|
||||
_presetsFilename = Path.Combine(_dataPath, _presetsBaseFilename);
|
||||
_presetsBackupFilename = Path.Combine(_dataPath, $"{_presetsBaseFilename}.backup");
|
||||
|
||||
try
|
||||
{
|
||||
var presetsExists = File.Exists(_presetsFilename);
|
||||
if (presetsExists)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(_presetsFilename);
|
||||
|
||||
// Hack to convert incorrect triggers
|
||||
json = json.Replace(@"""Triggers"":0", @"""Triggers"":[]");
|
||||
|
||||
_presets = _JsonDeserializer.Deserialize<List<T>>(json);
|
||||
|
||||
if (_presets != null)
|
||||
{
|
||||
if (File.Exists(_presetsBackupFilename))
|
||||
{
|
||||
File.Delete(_presetsBackupFilename);
|
||||
}
|
||||
File.Copy(_presetsFilename, _presetsBackupFilename);
|
||||
}
|
||||
}
|
||||
catch (Exception ex1)
|
||||
{
|
||||
Logger.Error($"Error while loading presets, reverting to default presets: {ex1.Message}");
|
||||
_loadPresetsError = ex1.Message;
|
||||
}
|
||||
}
|
||||
if (!presetsExists || _presets == null)
|
||||
{
|
||||
Logger.Debug("Reverting to default presets");
|
||||
try
|
||||
{
|
||||
_presets = GetDefaultPresets();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Error($"Error loading default presets, reverting to empty list: {e.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error($"General error while loading presets, reverting to empty list: {ex.Message}");
|
||||
_loadPresetsError = ex.Message;
|
||||
}
|
||||
if (_presets == null)
|
||||
{
|
||||
_presets = new List<T>();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract List<T> GetDefaultPresets();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Win32;
|
||||
using Microsoft.Win32.TaskScheduler;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NStandard;
|
||||
using NWin32;
|
||||
using NWin32.NativeTypes;
|
||||
@@ -942,6 +943,19 @@ namespace ColorControl
|
||||
return def;
|
||||
}
|
||||
|
||||
public static int ParseDynamicAsInt(dynamic value, int def = 0)
|
||||
{
|
||||
if (value.Type == JTokenType.Integer)
|
||||
{
|
||||
return (int)value.Value;
|
||||
}
|
||||
if (value.Type == JTokenType.String)
|
||||
{
|
||||
return ParseInt(value.Value, def);
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
public static async Task<dynamic> GetRestJsonAsync(string url, Action<dynamic> callBack = null)
|
||||
{
|
||||
var httpClient = new HttpClient();
|
||||
|
||||
Reference in new issue
Block a user