Skip to content

Commit

Permalink
Move every persistent setting (last config, EC-to-config state) to gl…
Browse files Browse the repository at this point in the history
…obal config

- Pre-release updates are now always enabled by default for now and is persisted in GlobalConfig

- The legacy `LastConf` file will automatically be upgraded on YAMDCC config editor launch until the v1.0 release at the latest.
  • Loading branch information
Sparronator9999 committed Jan 14, 2025
1 parent cbbec6f commit bc3ccc0
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 184 deletions.
103 changes: 99 additions & 4 deletions YAMDCC.Common/CommonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,110 @@ public class CommonConfig
[XmlAttribute]
public int Ver { get; set; } = 1;

/// <summary>
/// The path to the last YAMDCC config loaded by the config editor.
/// </summary>
[XmlElement]
public string LastConf { get; set; }

/// <summary>
/// The current progress of the EC-to-config feature.
/// </summary>
/// <remarks>
/// 0 = EC-to-config not in progress<br/>
/// 1 = EC-to-config pending, reboot required<br/>
/// 2 = EC-to-config pending, post-reboot<br/>
/// 3 = EC-to-config successful<br/>
/// 4 = EC-to-config failed
/// </remarks>
[XmlElement]
public ECtoConfState ECtoConfState { get; set; }

/// <summary>
/// How verbose logs should be.
/// </summary>
[XmlElement]
public LogLevel LogLevel { get; set; } = LogLevel.Debug;

/// <summary>
/// <c>true</c> if we've already asked to enable auto-updating,
/// otherwise <c>false</c>.
/// <see langword="true"/> if we've already asked to enable auto-updating,
/// otherwise <see langword="false"/>.
/// </summary>
[XmlElement]
public bool AutoUpdateAsked { get; set; }

/// <summary>
/// <see langword="true"/> if the YAMDCC updater should update
/// to pre-releases, otherwise <see langword="false"/>.
/// </summary>
[XmlElement]
public bool PreRelease { get; set; } = true;

public static string GetLastConf()
{
return Load().LastConf;
}

public static ECtoConfState GetECtoConfState()
{
return Load().ECtoConfState;
}

public static LogLevel GetLogLevel()
{
return Load().LogLevel;
}

public static bool GetAutoUpdateAsked()
{
return Load().AutoUpdateAsked;
}

public static bool GetPreRelease()
{
return Load().PreRelease;
}

public static void SetLastConf(string path)
{
CommonConfig cfg = Load();
cfg.LastConf = path;
cfg.Save();
}

public static void SetECtoConfState(ECtoConfState state)
{
CommonConfig cfg = Load();
cfg.ECtoConfState = state;
cfg.Save();
}

public static void SetLogLevel(LogLevel level)
{
CommonConfig cfg = Load();
cfg.LogLevel = level;
cfg.Save();
}

public static void SetAutoUpdateAsked(bool value)
{
CommonConfig cfg = Load();
cfg.AutoUpdateAsked = value;
cfg.Save();
}

public static void SetPreRelease(bool value)
{
CommonConfig cfg = Load();
cfg.PreRelease = value;
cfg.Save();
}

/// <summary>
/// Loads the global app config XML and returns a
/// <see cref="CommonConfig"/> object.
/// </summary>
public static CommonConfig Load()
private static CommonConfig Load()
{
XmlSerializer serialiser = new(typeof(CommonConfig));
try
Expand Down Expand Up @@ -86,7 +172,7 @@ or InvalidOperationException
/// Saves the global app config XML.
/// </summary>
/// <exception cref="InvalidOperationException"/>
public void Save()
private void Save()
{
XmlSerializer serializer = new(typeof(CommonConfig));
XmlWriterSettings settings = new()
Expand All @@ -101,3 +187,12 @@ public void Save()
}
}
}

public enum ECtoConfState
{
None,
PendingReboot,
PostReboot,
Success,
Fail,
}
7 changes: 2 additions & 5 deletions YAMDCC.Common/Paths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,8 @@ public static class Paths
/// The path where the path to the last saved YAMDCC config is saved.
/// </summary>
/// <remarks>
/// (C:\ProgramData\Sparronator9999\YAMDCC\CurrentConfig.xml on Windows)
/// (C:\ProgramData\Sparronator9999\YAMDCC\LastConfig on Windows)
/// </remarks>
// TODO: remove legacy LastConfig after upgrade to CommonConfig
public static readonly string LastConf = Path.Combine(Data, "LastConfig");

public static readonly string ECToConfSuccess = Path.Combine(Data, "ECToConfSuccess");
public static readonly string ECToConfFail = Path.Combine(Data, "ECToConfFail");
public static readonly string ECToConfPending = Path.Combine(Data, "ECToConfPending");
}
113 changes: 40 additions & 73 deletions YAMDCC.ConfigEditor/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ internal sealed partial class MainWindow : Form
#region Fields
private readonly Status AppStatus = new();

private readonly CommonConfig GlobalConfig;

/// <summary>
/// The YAMDCC config that is currently open for editing.
/// </summary>
Expand Down Expand Up @@ -117,8 +115,7 @@ public MainWindow()
};
tmrSvcTimeout.Tick += tmrSvcTimeout_Tick;

GlobalConfig = CommonConfig.Load();
switch (GlobalConfig.LogLevel)
switch (CommonConfig.GetLogLevel())
{
case LogLevel.None:
tsiLogNone.Checked = true;
Expand All @@ -140,7 +137,7 @@ public MainWindow()
break;
}

if (!GlobalConfig.AutoUpdateAsked)
if (!CommonConfig.GetAutoUpdateAsked())
{
if (Utils.ShowInfo(Strings.GetString("dlgAutoUpdate"),
"Check for updates?", MessageBoxButtons.YesNo) == DialogResult.Yes)
Expand All @@ -154,9 +151,27 @@ public MainWindow()
Utils.ShowError("Updater.exe not found!");
}
}
GlobalConfig.AutoUpdateAsked = true;
CommonConfig.SetAutoUpdateAsked(true);
}

try
{
string path;
StreamReader sr = new(Paths.LastConf, Encoding.UTF8);
try
{
path = sr.ReadLine();
CommonConfig.SetLastConf(path);
}
finally
{
sr.Close();
}
File.Delete(Paths.LastConf);
}
// legacy LastConf file doesn't exist
catch (FileNotFoundException) { }

DisableAll();
}

Expand Down Expand Up @@ -191,20 +206,17 @@ private void MainWindow_Load(object sender, EventArgs e)
}
}

if (File.Exists(Paths.ECToConfFail))
{
Utils.ShowError(Strings.GetString("dlgECtoConfErr", Paths.Logs));
}
else if (File.Exists(Paths.ECToConfSuccess))
{
Utils.ShowInfo(Strings.GetString("dlgECtoConfSuccess"), "Success");
}
try
switch (CommonConfig.GetECtoConfState())
{
File.Delete(Paths.ECToConfSuccess);
File.Delete(Paths.ECToConfFail);
case ECtoConfState.Fail:
Utils.ShowError(Strings.GetString("dlgECtoConfErr", Paths.Logs));
CommonConfig.SetECtoConfState(ECtoConfState.None);
break;
case ECtoConfState.Success:
Utils.ShowInfo(Strings.GetString("dlgECtoConfSuccess"), "Success");
CommonConfig.SetECtoConfState(ECtoConfState.None);
break;
}
catch (DirectoryNotFoundException) { }
}

private void MainWindow_Closing(object sender, FormClosingEventArgs e)
Expand All @@ -214,14 +226,6 @@ private void MainWindow_Closing(object sender, FormClosingEventArgs e)
{
SendServiceMessage(new ServiceCommand(Command.FullBlast, "0"));
}

try
{
GlobalConfig.Save();
}
// ignore DirectoryNotFoundException, since we probably closed the
// window due to uninstalling with data directory delete enabled
catch (DirectoryNotFoundException) { }
}

private void OnProcessExit(object sender, EventArgs e)
Expand Down Expand Up @@ -353,7 +357,7 @@ private void tsiLoadConf_Click(object sender, EventArgs e)
if (ofd.ShowDialog() == DialogResult.OK)
{
LoadConf(ofd.FileName);
SetLastConfPath(ofd.FileName);
CommonConfig.SetLastConf(ofd.FileName);
btnRevert.Enabled = tsiRevert.Enabled = false;
}
}
Expand All @@ -373,7 +377,7 @@ private void tsiSaveConf_Click(object sender, EventArgs e)
Config.ChargeLimitConf.CurVal = (byte)(chkChgLim.Checked
? numChgLim.Value : 0);
Config.Save(sfd.FileName);
SetLastConfPath(sfd.FileName);
CommonConfig.SetLastConf(sfd.FileName);
btnRevert.Enabled = tsiRevert.Enabled = false;
}
}
Expand Down Expand Up @@ -462,17 +466,7 @@ private void tsiECtoConf_Click(object sender, EventArgs e)
if (Utils.ShowInfo(Strings.GetString("dlgECtoConfStart"),
"Default fan profile from EC?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
StreamWriter sw = new(Paths.ECToConfPending, false);
try
{
sw.Write(1);
sw.Flush();
}
finally
{
sw.Close();
}
Application.Exit();
CommonConfig.SetECtoConfState(ECtoConfState.PendingReboot);
}
}

Expand Down Expand Up @@ -512,7 +506,7 @@ private void tsiAdvanced_Click(object sender, EventArgs e)

private void tsiLogNone_Click(object sender, EventArgs e)
{
GlobalConfig.LogLevel = LogLevel.None;
CommonConfig.SetLogLevel(LogLevel.None);
tsiLogNone.Checked = true;
tsiLogDebug.Checked = false;
tsiLogInfo.Checked = false;
Expand All @@ -523,7 +517,7 @@ private void tsiLogNone_Click(object sender, EventArgs e)

private void tsiLogDebug_Click(object sender, EventArgs e)
{
GlobalConfig.LogLevel = LogLevel.Debug;
CommonConfig.SetLogLevel(LogLevel.Debug);
tsiLogNone.Checked = false;
tsiLogDebug.Checked = true;
tsiLogInfo.Checked = false;
Expand All @@ -534,7 +528,7 @@ private void tsiLogDebug_Click(object sender, EventArgs e)

private void tsiLogInfo_Click(object sender, EventArgs e)
{
GlobalConfig.LogLevel = LogLevel.Info;
CommonConfig.SetLogLevel(LogLevel.Info);
tsiLogNone.Checked = false;
tsiLogDebug.Checked = false;
tsiLogInfo.Checked = true;
Expand All @@ -545,7 +539,7 @@ private void tsiLogInfo_Click(object sender, EventArgs e)

private void tsiLogWarn_Click(object sender, EventArgs e)
{
GlobalConfig.LogLevel = LogLevel.Warn;
CommonConfig.SetLogLevel(LogLevel.Warn);
tsiLogNone.Checked = false;
tsiLogDebug.Checked = false;
tsiLogInfo.Checked = false;
Expand All @@ -556,7 +550,7 @@ private void tsiLogWarn_Click(object sender, EventArgs e)

private void tsiLogError_Click(object sender, EventArgs e)
{
GlobalConfig.LogLevel = LogLevel.Error;
CommonConfig.SetLogLevel(LogLevel.Error);
tsiLogNone.Checked = false;
tsiLogDebug.Checked = false;
tsiLogInfo.Checked = false;
Expand All @@ -567,7 +561,7 @@ private void tsiLogError_Click(object sender, EventArgs e)

private void tsiLogFatal_Click(object sender, EventArgs e)
{
GlobalConfig.LogLevel = LogLevel.Fatal;
CommonConfig.SetLogLevel(LogLevel.Fatal);
tsiLogNone.Checked = false;
tsiLogDebug.Checked = false;
tsiLogInfo.Checked = false;
Expand Down Expand Up @@ -1125,7 +1119,7 @@ private void RevertConf()
{
try
{
YAMDCC_Config tempConf = YAMDCC_Config.Load(GetLastConfPath());
YAMDCC_Config tempConf = YAMDCC_Config.Load(CommonConfig.GetLastConf());
LoadConf(tempConf);
Config = tempConf;
UpdateFanCurveDisplay();
Expand Down Expand Up @@ -1262,33 +1256,6 @@ private void DelFanProfImpl(int start, int end)
}
}

private static string GetLastConfPath()
{
StreamReader sr = new(Paths.LastConf, Encoding.UTF8);
try
{
string path = sr.ReadLine();
return path;
}
finally
{
sr.Close();
}
}

private static void SetLastConfPath(string path)
{
StreamWriter sw = new(Paths.LastConf, false, Encoding.UTF8);
try
{
sw.WriteLine(path);
}
finally
{
sw.Close();
}
}

private void UpdateFanCurveDisplay()
{
FanConf cfg = Config.FanConfs[cboFanSel.SelectedIndex];
Expand Down
Loading

0 comments on commit bc3ccc0

Please sign in to comment.