Skip to content

Commit

Permalink
Optimisations, reduced dependency size, bug fix
Browse files Browse the repository at this point in the history
- Persist "Service not responding" message in config editor

- Replace Markdig with Marked.NET for changelog parsing

- Remove custom System.Runtime.CompilerServices.Unsafe assembly loading code since it's unnecessary after removing Markdig

- Optimise Strings helper classes

- Suppress warning related to a class with an underscored name that can't be changed without breaking YAMDCC configs

- Shorten even more variable/method names
  • Loading branch information
Sparronator9999 committed Jan 13, 2025
1 parent d20d915 commit e30e82d
Show file tree
Hide file tree
Showing 20 changed files with 166 additions and 425 deletions.
32 changes: 7 additions & 25 deletions YAMDCC.Common/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,15 @@ internal static class Strings
{
private static ResourceManager resMan;

/// <summary>
/// Gets a string from the underlying resource file.
/// </summary>
/// <remarks>
/// This function internally calls
/// <see cref="ResourceManager.GetString(string)"/> to retrieve the string.
/// </remarks>
/// <param name="name">
/// The name of the string to find.
/// </param>
/// <returns>
/// <para>The value of the specified string name, if found.</para>
/// <para><c>null</c> if the string couldn't be found.</para>
/// </returns>
public static string GetString(string name)
{
resMan ??= new ResourceManager(typeof(Strings));
return resMan.GetString(name, CultureInfo.InvariantCulture);
}

/// <summary>
/// Gets a string from the underlying resource file, and
/// replaces format objects with their string representation.
/// </summary>
/// <param name="name">
/// The name of the string to find.
/// </param>
/// <param name="arg0">
/// The object to format the string with.
/// <param name="args">
/// The objects to format the string with.
/// </param>
/// <returns>
/// <para>
Expand All @@ -63,11 +43,13 @@ public static string GetString(string name)
/// </para>
/// <para><c>null</c> if the string couldn't be found.</para>
/// </returns>
public static string GetString(string name, object arg0)
public static string GetString(string name, params object[] args)
{
string temp = GetString(name);
resMan ??= new ResourceManager(typeof(Strings));
string temp = resMan.GetString(name, CultureInfo.InvariantCulture);

return temp is null
? null
: string.Format(CultureInfo.InvariantCulture, temp, arg0);
: string.Format(CultureInfo.InvariantCulture, temp, args);
}
}
23 changes: 15 additions & 8 deletions YAMDCC.Config/YAMDCC_Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@

namespace YAMDCC.Config;

// IDE0079: Remove unnecessary suppression (even though it *is* necessary)
#pragma warning disable IDE0079
// CA1707: Identifiers should not contain underscores
#pragma warning disable CA1707
/// <summary>
/// Represents a YAMDCC configuration.
/// </summary>

public sealed class YAMDCC_Config
#pragma warning restore CA1707
#pragma warning restore IDE0079
{
/// <summary>
/// The config version expected when loading a config.
Expand Down Expand Up @@ -131,17 +138,17 @@ public sealed class YAMDCC_Config
/// Parses a YAMDCC config XML and returns a
/// <see cref="YAMDCC_Config"/> object.
/// </summary>
/// <param name="xmlFile">
/// <param name="path">
/// The path to an XML config file.
/// </param>
/// <exception cref="InvalidConfigException"/>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="FileNotFoundException"/>
/// <exception cref="InvalidOperationException"/>
public static YAMDCC_Config Load(string xmlFile)
public static YAMDCC_Config Load(string path)
{
XmlSerializer serialiser = new(typeof(YAMDCC_Config));
using (XmlReader reader = XmlReader.Create(xmlFile))
using (XmlReader reader = XmlReader.Create(path))
{
YAMDCC_Config cfg = (YAMDCC_Config)serialiser.Deserialize(reader);
return cfg.IsValid() ? cfg : throw new InvalidConfigException();
Expand All @@ -151,23 +158,23 @@ public static YAMDCC_Config Load(string xmlFile)
/// <summary>
/// Saves a YAMDCC config to the specified location.
/// </summary>
/// <param name="xmlFile">
/// <param name="path">
/// The XML file to write to.
/// </param>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="InvalidOperationException"/>
public void Save(string xmlFile)
public void Save(string path)
{
XmlSerializer serializer = new(typeof(YAMDCC_Config));
XmlSerializer serialiser = new(typeof(YAMDCC_Config));
XmlWriterSettings settings = new()
{
Indent = true,
IndentChars = "\t",
};

using (XmlWriter writer = XmlWriter.Create(xmlFile, settings))
using (XmlWriter writer = XmlWriter.Create(path, settings))
{
serializer.Serialize(writer, this);
serialiser.Serialize(writer, this);
}
}

Expand Down
11 changes: 6 additions & 5 deletions YAMDCC.ConfigEditor/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1444,12 +1444,12 @@ private void UpdateStatus(StatusCode status, int data = 0)
{
if (AppStatus.Code == status)
{
AppStatus.RepeatCount++;
AppStatus.Repeats++;
}
else
{
AppStatus.Code = status;
AppStatus.RepeatCount = 0;
AppStatus.Repeats = 0;
}

// set status text
Expand All @@ -1464,6 +1464,7 @@ private void UpdateStatus(StatusCode status, int data = 0)
lblStatus.Text = Strings.GetString("statResponseEmpty");
break;
case StatusCode.ServiceTimeout:
persist = true;
lblStatus.Text = Strings.GetString("statSvcTimeout");
break;
case StatusCode.NoConfig:
Expand All @@ -1481,14 +1482,14 @@ private void UpdateStatus(StatusCode status, int data = 0)
break;
default:
persist = true;
AppStatus.RepeatCount = 0;
AppStatus.Repeats = 0;
lblStatus.Text = "Ready";
break;
}

if (AppStatus.RepeatCount > 0)
if (AppStatus.Repeats > 0)
{
lblStatus.Text += $" (x{AppStatus.RepeatCount + 1})";
lblStatus.Text += $" (x{AppStatus.Repeats + 1})";
}

tmrStatusReset.Stop();
Expand Down
4 changes: 2 additions & 2 deletions YAMDCC.ConfigEditor/Status.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ namespace YAMDCC.ConfigEditor;
internal sealed class Status
{
internal StatusCode Code;
internal int RepeatCount;
internal int Repeats;

internal Status()
{
Code = StatusCode.None;
RepeatCount = 0;
Repeats = 0;
}
}

Expand Down
32 changes: 7 additions & 25 deletions YAMDCC.ConfigEditor/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,15 @@ internal static class Strings
{
private static ResourceManager resMan;

/// <summary>
/// Gets a string from the underlying resource file.
/// </summary>
/// <remarks>
/// This function internally calls
/// <see cref="ResourceManager.GetString(string)"/> to retrieve the string.
/// </remarks>
/// <param name="name">
/// The name of the string to find.
/// </param>
/// <returns>
/// <para>The value of the specified string name, if found.</para>
/// <para><c>null</c> if the string couldn't be found.</para>
/// </returns>
public static string GetString(string name)
{
resMan ??= new ResourceManager(typeof(Strings));
return resMan.GetString(name, CultureInfo.InvariantCulture);
}

/// <summary>
/// Gets a string from the underlying resource file, and
/// replaces format objects with their string representation.
/// </summary>
/// <param name="name">
/// The name of the string to find.
/// </param>
/// <param name="arg0">
/// The object to format the string with.
/// <param name="args">
/// The objects to format the string with.
/// </param>
/// <returns>
/// <para>
Expand All @@ -63,11 +43,13 @@ public static string GetString(string name)
/// </para>
/// <para><c>null</c> if the string couldn't be found.</para>
/// </returns>
public static string GetString(string name, object arg0)
public static string GetString(string name, params object[] args)
{
string temp = GetString(name);
resMan ??= new ResourceManager(typeof(Strings));
string temp = resMan.GetString(name, CultureInfo.InvariantCulture);

return temp is null
? null
: string.Format(CultureInfo.InvariantCulture, temp, arg0);
: string.Format(CultureInfo.InvariantCulture, temp, args);
}
}
11 changes: 4 additions & 7 deletions YAMDCC.ConfigEditor/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
"version": 1,
"dependencies": {
".NETFramework,Version=v4.8": {
"Markdig": {
"MarkedNet": {
"type": "Transitive",
"resolved": "0.40.0",
"contentHash": "4ve14zs+gt1irldTQE3y5FLAHuzmhW7T99lAAvVipe/q2LWT/nUCO0iICb9TXGvMX6n7Z1OZroFXkdSy91rO8w==",
"dependencies": {
"System.Memory": "4.5.5"
}
"resolved": "2.1.4",
"contentHash": "7JSHAIeNM1Zsmza046u6OIBx36GzzGDLjKCSxroAeYKYiAFQ+34dGfbx06nX1KR1RhxBucGyZxlDD1fv/33FIg=="
},
"MessagePack": {
"type": "Transitive",
Expand Down Expand Up @@ -114,7 +111,7 @@
"Updater": {
"type": "Project",
"dependencies": {
"Markdig": "[0.40.0, )",
"MarkedNet": "[2.1.4, )",
"Newtonsoft.Json": "[13.0.3, )",
"TaskScheduler": "[2.11.0, )",
"YAMDCC.Common": "[1.0.0-dev, )"
Expand Down
Loading

0 comments on commit e30e82d

Please sign in to comment.