Skip to content

Commit

Permalink
Code cleanup + use file-scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparronator9999 committed Jan 7, 2025
1 parent 18c695a commit 84899a1
Show file tree
Hide file tree
Showing 55 changed files with 6,248 additions and 6,303 deletions.
5 changes: 4 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ end_of_line = crlf
csharp_using_directive_placement = outside_namespace:suggestion
csharp_prefer_simple_using_statement = false:suggestion
csharp_prefer_braces = true:suggestion
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_namespace_declarations = file_scoped:suggestion
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = false:silent
csharp_style_expression_bodied_methods = true:silent
Expand Down Expand Up @@ -150,3 +150,6 @@ csharp_indent_case_contents_when_block = false
csharp_preserve_single_line_statements = true
csharp_style_prefer_primary_constructors = false:suggestion
csharp_style_prefer_readonly_struct_member = true:suggestion
csharp_prefer_system_threading_lock = true:suggestion
csharp_prefer_static_anonymous_function = true:suggestion
csharp_space_around_declaration_statements = ignore
128 changes: 63 additions & 65 deletions YAMDCC.Common/CommonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,85 +20,83 @@
using System.Xml.Serialization;
using YAMDCC.Logs;

namespace YAMDCC.Common
namespace YAMDCC.Common;

public class CommonConfig
{
public class CommonConfig
{
/// <summary>
/// The config version expected when loading a config.
/// </summary>
[XmlIgnore]
public const int ExpectedVer = 1;
/// <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 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; }
/// <summary>
/// The product this <see cref="CommonConfig"/> was made for.
/// </summary>
[XmlAttribute]
public string App { get; set; }

/// <summary>
/// How verbose logs should be.
/// </summary>
[XmlElement]
public LogLevel LogLevel { get; set; } = LogLevel.Debug;
/// <summary>
/// How verbose logs should be.
/// </summary>
[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()
/// <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
{
XmlSerializer serialiser = new(typeof(CommonConfig));
try
using (XmlReader reader = XmlReader.Create(Paths.GlobalConf))
{
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;
}
CommonConfig cfg = (CommonConfig)serialiser.Deserialize(reader);
return cfg.Ver == ExpectedVer
? cfg
: throw new InvalidConfigException();
}
}

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

using (XmlWriter writer = XmlWriter.Create(Paths.GlobalConf, settings))
return new CommonConfig();
}
else
{
serializer.Serialize(writer, this);
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);
}
}
}
51 changes: 25 additions & 26 deletions YAMDCC.Common/Dialogs/CrashDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,37 @@
using System.Diagnostics;
using System.Windows.Forms;

namespace YAMDCC.Common.Dialogs
namespace YAMDCC.Common.Dialogs;

public sealed partial class CrashDialog : Form
{
public sealed partial class CrashDialog : Form
public CrashDialog(Exception ex)
{
public CrashDialog(Exception ex)
{
InitializeComponent();
lblError.Text = Strings.GetString("Crash");
txtReport.Text = $"{ex.GetType()}: {ex.Message}\r\n{ex.StackTrace}";
}

private void btnReportIssue_Click(object sender, EventArgs e)
{
Process.Start($"{Paths.GitHubPage}/issues");
}
InitializeComponent();
lblError.Text = Strings.GetString("Crash");
txtReport.Text = $"{ex.GetType()}: {ex.Message}\r\n{ex.StackTrace}";
}

private void btnCopy_Click(object sender, EventArgs e)
{
Clipboard.SetText(txtReport.Text);
private void btnReportIssue_Click(object sender, EventArgs e)
{
Process.Start($"{Paths.GitHubPage}/issues");
}

// 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!";
}
}
private void btnCopy_Click(object sender, EventArgs e)
{
Clipboard.SetText(txtReport.Text);

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

private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
}
Loading

0 comments on commit 84899a1

Please sign in to comment.