Skip to content

Commit

Permalink
Lots of optimisations + regression fix
Browse files Browse the repository at this point in the history
- Use List<T> instead of array for most YAMDCC config elements

- Remove unnecessary string copies from FanCurveConf's Copy method

- Combine profile add/delete, config apply/revert, and fan speed change methods

- Shorten some config editor method names

- Fix config editor not exiting after starting EC-to-config

- Fix apply buttons not getting disabled when applying configs using tool strip menu or keyboard shortcut (which triggers the tool strip menu item)

- Fix buggy curve editor while adding/removing curve points
  • Loading branch information
Sparronator9999 committed Jan 15, 2025
1 parent bc3ccc0 commit f0b98f9
Show file tree
Hide file tree
Showing 15 changed files with 413 additions and 585 deletions.
9 changes: 1 addition & 8 deletions YAMDCC.Common/Dialogs/CrashDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ private void btnReportIssue_Click(object sender, EventArgs e)
private void btnCopy_Click(object sender, EventArgs e)
{
Clipboard.SetText(txtReport.Text);

// should never fail, but better safe than sorry
// (this is the crash handling dialog after all)
if (sender is Button b)
{
// give confirmation that the crash report has been copied
b.Text = "Copied!";
}
((Button)sender).Text = "Copied!";
}

private void btnExit_Click(object sender, EventArgs e)
Expand Down
8 changes: 4 additions & 4 deletions YAMDCC.Common/Dialogs/ProgressDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ public ProgressDialog(
SetTitle(Caption);

// event setup
Worker.DoWork += Worker_DoWork;
Worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
Worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
if (reportsProgress)
{
Worker.ProgressChanged += Worker_ProgressChanged;
Worker.ProgressChanged += new ProgressChangedEventHandler(Worker_ProgressChanged);
}
else
{
Expand All @@ -127,7 +127,7 @@ public ProgressDialog(
}

DisplayTimer.Interval = 1000;
DisplayTimer.Tick += DisplayTimer_Tick;
DisplayTimer.Tick += new EventHandler(DisplayTimer_Tick);
}

private void ProgressDialog_Load(object sender, EventArgs e)
Expand Down
7 changes: 1 addition & 6 deletions YAMDCC.Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,10 @@ public static string GetPCManufacturer()

private static string GetBIOSRegValue(string name)
{
RegistryKey biosKey = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\BIOS");
try
using (RegistryKey biosKey = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\BIOS"))
{
return (string)biosKey?.GetValue(name, null);
}
finally
{
biosKey?.Close();
}
}

}
3 changes: 2 additions & 1 deletion YAMDCC.Config/FanConf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License along with
// YAMDCC. If not, see <https://www.gnu.org/licenses/>.

using System.Collections.Generic;
using System.Xml.Serialization;

namespace YAMDCC.Config;
Expand Down Expand Up @@ -95,5 +96,5 @@ public sealed class FanConf
/// otherwise at least one fan curve (the "default" curve) must exist.
/// </remarks>
[XmlArray]
public FanCurveConf[] FanCurveConfs { get; set; }
public List<FanCurveConf> FanCurveConfs { get; set; }
}
11 changes: 5 additions & 6 deletions YAMDCC.Config/FanCurveConf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License along with
// YAMDCC. If not, see <https://www.gnu.org/licenses/>.

using System.Collections.Generic;
using System.Xml.Serialization;

namespace YAMDCC.Config;
Expand All @@ -39,7 +40,7 @@ public sealed class FanCurveConf
/// The fan speeds and associated up and down thresholds.
/// </summary>
[XmlArray]
public TempThreshold[] TempThresholds { get; set; }
public List<TempThreshold> TempThresholds { get; set; }

/// <summary>
/// Creates a deep copy of this <see cref="FanCurveConf"/>.
Expand All @@ -53,12 +54,10 @@ public FanCurveConf Copy()
FanCurveConf newCfg = (FanCurveConf)MemberwiseClone();

// create a copy of everything that didn't get copied by the above
newCfg.Name = string.Copy(Name);
newCfg.Desc = string.Copy(Desc);
newCfg.TempThresholds = new TempThreshold[TempThresholds.Length];
for (int i = 0; i < newCfg.TempThresholds.Length; i++)
newCfg.TempThresholds = new List<TempThreshold>(TempThresholds.Count);
for (int i = 0; i < TempThresholds.Count; i++)
{
newCfg.TempThresholds[i] = TempThresholds[i].Copy();
newCfg.TempThresholds.Add(TempThresholds[i].Copy());
}
return newCfg;
}
Expand Down
3 changes: 2 additions & 1 deletion YAMDCC.Config/FanModeConf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License along with
// YAMDCC. If not, see <https://www.gnu.org/licenses/>.

using System.Collections.Generic;
using System.Xml.Serialization;

namespace YAMDCC.Config;
Expand Down Expand Up @@ -41,5 +42,5 @@ public sealed class FanModeConf
/// An array of possible fan modes for the laptop.
/// </summary>
[XmlArray]
public FanMode[] FanModes { get; set; }
public List<FanMode> FanModes { get; set; }
}
3 changes: 2 additions & 1 deletion YAMDCC.Config/PerfModeConf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License along with
// YAMDCC. If not, see <https://www.gnu.org/licenses/>.

using System.Collections.Generic;
using System.Xml.Serialization;

namespace YAMDCC.Config;
Expand Down Expand Up @@ -41,5 +42,5 @@ public sealed class PerfModeConf
/// An array of possible performance modes for the laptop.
/// </summary>
[XmlArray]
public PerfMode[] PerfModes { get; set; }
public List<PerfMode> PerfModes { get; set; }
}
35 changes: 18 additions & 17 deletions YAMDCC.Config/YAMDCC_Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// YAMDCC. If not, see <https://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
Expand Down Expand Up @@ -69,7 +70,7 @@ public sealed class YAMDCC_Config
/// The list of <see cref="FanConf"/>s associated with the laptop.
/// </summary>
[XmlArray]
public FanConf[] FanConfs { get; set; }
public List<FanConf> FanConfs { get; set; }

/// <summary>
/// The laptop's Full Blast config.
Expand Down Expand Up @@ -132,7 +133,7 @@ public sealed class YAMDCC_Config
/// May be <c>null</c> or empty if not needed.
/// </remarks>
[XmlArray]
public RegConf[] RegConfs { get; set; }
public List<RegConf> RegConfs { get; set; }

/// <summary>
/// Parses a YAMDCC config XML and returns a
Expand Down Expand Up @@ -209,12 +210,12 @@ private bool IsValid()

// 1. Check if FanConfigs is not null
// 2. Check if there's at least 1 FanConfig
if (FanConfs?.Length < 1)
if (FanConfs?.Count < 1)
{
return false;
}

for (int i = 0; i < FanConfs.Length; i++)
for (int i = 0; i < FanConfs.Count; i++)
{
FanConf cfg = FanConfs[i];

Expand All @@ -232,7 +233,7 @@ private bool IsValid()

// the selected fan curve shouldn't be higher than
// the number of fan curves in the config.
if (cfg.CurveSel >= FanConfs[i].FanCurveConfs.Length ||
if (cfg.CurveSel >= FanConfs[i].FanCurveConfs.Count ||
cfg.CurveSel < 0)
{
// if the fan profile selection is out of range,
Expand All @@ -251,24 +252,24 @@ private bool IsValid()
if (cfg.UpThresholdRegs?.Length < 1 ||
cfg.UpThresholdRegs?.Length != cfg.DownThresholdRegs?.Length ||
cfg.FanCurveRegs?.Length != cfg.UpThresholdRegs?.Length + 1 ||
cfg.FanCurveConfs?.Length < 1)
cfg.FanCurveConfs?.Count < 1)
{
return false;
}

for (int j = 0; j < cfg.FanCurveConfs.Length; j++)
for (int j = 0; j < cfg.FanCurveConfs.Count; j++)
{
FanCurveConf curveCfg = cfg.FanCurveConfs[j];
if (string.IsNullOrEmpty(curveCfg.Name) ||
string.IsNullOrEmpty(curveCfg.Desc) ||
// there should be exactly one temperature threshold
// per fan curve register; if there isn't, return false
curveCfg.TempThresholds?.Length != cfg.FanCurveRegs.Length)
curveCfg.TempThresholds?.Count != cfg.FanCurveRegs.Length)
{
return false;
}

for (int k = 0; k < curveCfg.TempThresholds.Length; k++)
for (int k = 0; k < curveCfg.TempThresholds.Count; k++)
{
if (curveCfg.TempThresholds[k] is null)
{
Expand Down Expand Up @@ -311,22 +312,22 @@ private bool IsValid()

if (PerfModeConf is not null)
{
if (PerfModeConf.PerfModes?.Length < 1)
if (PerfModeConf.PerfModes?.Count < 1)
{
return false;
}

// the selected performance mode shouldn't be higher than
// the number of performance modes in the config
if (PerfModeConf.ModeSel >= PerfModeConf.PerfModes.Length ||
if (PerfModeConf.ModeSel >= PerfModeConf.PerfModes.Count ||
PerfModeConf.ModeSel < 0)
{
// same as fan profile selection, set the performance
// mode to the first available performance mode:
PerfModeConf.ModeSel = 0;
}

for (int i = 0; i < PerfModeConf.PerfModes.Length; i++)
for (int i = 0; i < PerfModeConf.PerfModes.Count; i++)
{
PerfMode perfMode = PerfModeConf.PerfModes[i];

Expand All @@ -340,19 +341,19 @@ private bool IsValid()

if (FanModeConf is not null)
{
if (FanModeConf.FanModes?.Length < 1)
if (FanModeConf.FanModes?.Count < 1)
{
return false;
}

// you know the drill by now
if (FanModeConf.ModeSel >= FanModeConf.FanModes.Length ||
if (FanModeConf.ModeSel >= FanModeConf.FanModes.Count ||
FanModeConf.ModeSel < 0)
{
FanModeConf.ModeSel = 0;
}

for (int i = 0; i < FanModeConf.FanModes.Length; i++)
for (int i = 0; i < FanModeConf.FanModes.Count; i++)
{
FanMode fanMode = FanModeConf.FanModes[i];

Expand All @@ -374,9 +375,9 @@ private bool IsValid()
return false;
}

if (RegConfs?.Length > 0)
if (RegConfs?.Count > 0)
{
for (int i = 0; i < RegConfs.Length; i++)
for (int i = 0; i < RegConfs.Count; i++)
{
if (string.IsNullOrEmpty(RegConfs[i].Name) ||
string.IsNullOrEmpty(RegConfs[i].Desc))
Expand Down
Loading

0 comments on commit f0b98f9

Please sign in to comment.