Skip to content

Commit

Permalink
Add service log level selection
Browse files Browse the repository at this point in the history
- Create new Common library, with a shared global config and common methods

- Reword a fan profile recommendation
  • Loading branch information
Sparronator9999 committed Jan 4, 2025
1 parent 72401d6 commit 43f99f9
Show file tree
Hide file tree
Showing 36 changed files with 640 additions and 118 deletions.
101 changes: 101 additions & 0 deletions YAMDCC.Common/CommonConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// This file is part of YAMDCC (Yet Another MSI Dragon Center Clone).
// Copyright © Sparronator9999 and Contributors 2023-2025.
//
// YAMDCC is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// YAMDCC is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// 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;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using YAMDCC.Logs;

namespace YAMDCC.Common
{
public class CommonConfig
{
/// <summary>
/// The config version expected when loading a config.
/// </summary>
[XmlIgnore]
public const int ExpectedVer = 1;

/// <summary>
/// The config version. Should be the same as <see cref="ExpectedVer"/>
/// unless the config is newer or invalid.
/// </summary>
[XmlAttribute]
public int Ver { get; set; }

/// <summary>
/// The product this <see cref="CommonConfig"/> was made for.
/// </summary>
[XmlAttribute]
public string App { get; set; }

[XmlElement]
public LogLevel LogLevel { get; set; } = LogLevel.Debug;

/// <summary>
/// Loads the global app config XML and returns a
/// <see cref="CommonConfig"/> object.
/// </summary>
public static CommonConfig Load()
{
XmlSerializer serialiser = new(typeof(CommonConfig));
try
{
using (XmlReader reader = XmlReader.Create(Paths.GlobalConf))
{
CommonConfig cfg = (CommonConfig)serialiser.Deserialize(reader);
return cfg.Ver == ExpectedVer
? cfg
: throw new InvalidConfigException();
}
}
catch (Exception ex)
{
if (ex is FileNotFoundException
or InvalidOperationException
or InvalidConfigException)
{
return new CommonConfig();
}
else
{
throw;
}
}
}

/// <summary>
/// Saves the global app config XML.
/// </summary>
/// <exception cref="InvalidOperationException"/>
public void Save()
{
XmlSerializer serializer = new(typeof(CommonConfig));
XmlWriterSettings settings = new()
{
Indent = true,
IndentChars = "\t",
};

using (XmlWriter writer = XmlWriter.Create(Paths.GlobalConf, settings))
{
serializer.Serialize(writer, this);
}
}

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
using System.Diagnostics;
using System.Windows.Forms;

namespace YAMDCC.ConfigEditor.Dialogs
namespace YAMDCC.Common.Dialogs
{
internal sealed partial class CrashDialog : Form
public sealed partial class CrashDialog : Form
{
public CrashDialog(Exception ex)
{
Expand Down
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
using System.Globalization;
using System.Windows.Forms;

namespace YAMDCC.ConfigEditor.Dialogs
namespace YAMDCC.Common.Dialogs
{
internal sealed partial class ProgressDialog : Form
public sealed partial class ProgressDialog : Form
{
#region Disable close button
private const int CP_NOCLOSE_BUTTON = 0x200;
Expand All @@ -37,8 +37,8 @@ protected override CreateParams CreateParams
}
#endregion

public bool Cancelled;
public object Result;
public bool Cancelled { get; set; }
public object Result { get; set; }

private readonly object Argument;
private readonly string Caption;
Expand Down
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
using System;
using System.Windows.Forms;

namespace YAMDCC.ConfigEditor.Dialogs
namespace YAMDCC.Common.Dialogs
{
internal sealed partial class TextInputDialog : Form
public sealed partial class TextInputDialog : Form
{
/// <summary>
/// The text that the user entered in this dialog.
/// </summary>
public string Result;
public string Result { get; set; }

public TextInputDialog(string caption, string title, string text, bool multiline = false)
{
Expand Down
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
using System.Diagnostics;
using System.Windows.Forms;

namespace YAMDCC.ConfigEditor.Dialogs
namespace YAMDCC.Common.Dialogs
{
internal sealed partial class VersionDialog : Form
public sealed partial class VersionDialog : Form
{
public VersionDialog()
{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

using System;

namespace YAMDCC.Config
namespace YAMDCC.Common
{
/// <summary>
/// The exception thrown when an invalid <see cref="YAMDCC_Config"/> is loaded.
Expand All @@ -27,6 +27,6 @@ public sealed class InvalidConfigException : Exception
/// Initializes a new instance of the <see cref="InvalidConfigException" /> class.
/// </summary>
public InvalidConfigException()
: base("The YAMDCC config was not in the expected format.") { }
: base("The config was not in the expected format.") { }
}
}
7 changes: 5 additions & 2 deletions YAMDCC.ConfigEditor/Paths.cs → YAMDCC.Common/Paths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
using System;
using System.IO;

namespace YAMDCC.ConfigEditor
namespace YAMDCC.Common
{
internal static class Paths
public static class Paths
{
/// <summary>
/// The URL to this project's GitHub page.
Expand All @@ -44,6 +44,9 @@ internal static class Paths
/// </remarks>
public static readonly string Logs = Path.Combine(Data, "Logs");

public static readonly string GlobalConf = Path.Combine(Data, "GlobalConfig.xml");


/// <summary>
/// The path where the currently applied YAMDCC config is saved.
/// </summary>
Expand Down
74 changes: 74 additions & 0 deletions YAMDCC.Common/Strings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// This file is part of YAMDCC (Yet Another MSI Dragon Center Clone).
// Copyright © Sparronator9999 and Contributors 2023-2025.
//
// YAMDCC is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// YAMDCC is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// 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.Globalization;
using System.Resources;

namespace YAMDCC.Common
{
/// <summary>
/// A resource class for retrieving strings.
/// </summary>
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>
/// <returns>
/// <para>
/// The formatted string corresponding to
/// 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, object arg0)
{
string temp = GetString(name);
return temp is null
? null
: string.Format(CultureInfo.InvariantCulture, temp, arg0);
}
}
}
Loading

0 comments on commit 43f99f9

Please sign in to comment.