Skip to content
This repository has been archived by the owner on Nov 26, 2023. It is now read-only.

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
depthbomb committed Jul 28, 2023
1 parent 6ddf4b4 commit c4f74aa
Show file tree
Hide file tree
Showing 19 changed files with 84 additions and 100 deletions.
19 changes: 9 additions & 10 deletions Scraps/Args.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ private record Arg
public bool Active { get; set; }
}

private static readonly IList<Arg> _args = new List<Arg>();
private static readonly Regex _argPrefixRegex = new("(/|--?)", RegexOptions.Compiled);
private static readonly List<Arg> RegisteredArgs = new();
private static readonly Regex ArgPrefixRegex = new("(/|--?)", RegexOptions.Compiled);

/// <summary>
/// Parses program args and sets registered args active if applicable.
/// </summary>
/// <param name="args">Args array from program entry</param>
public static void Parse(IEnumerable<string> args)
{
foreach (string a in args)
foreach (var a in args)
{
var arg = GetArg(a);
if (arg != null && !arg.Active)
Expand All @@ -47,27 +47,27 @@ public static void Parse(IEnumerable<string> args)
}

/// <summary>
/// Registers a arg that the program accepts as valid.
/// Registers an arg that the program accepts as valid.
/// </summary>
/// <param name="name">The full name of the arg</param>
/// <param name="shortName">The short name or alias of the arg</param>
/// <exception cref="Exception">Arg is already registered</exception>
public static void Register(string name, string shortName)
{
if (_args.Any(f => f.Name == name || f.ShortName == shortName))
if (RegisteredArgs.Any(f => f.Name == name || f.ShortName == shortName))
{
throw new Exception($"Arg {name}|{shortName} is already registered");
}

_args.Add(new Arg
RegisteredArgs.Add(new Arg
{
Name = name.ToLower(),
ShortName = shortName.ToLower(),
});
}

/// <summary>
/// Returns true if a arg from the program args is found and active in the arg registry.
/// Returns true if an arg from the program args is found and active in the arg registry.
/// </summary>
/// <param name="arg">The arg name or short name to check the registry for</param>
/// <returns>`true` if the arg is found in the registry and is active</returns>
Expand All @@ -80,9 +80,8 @@ public static bool Has(string arg)

private static Arg GetArg(string input)
{
string arg = _argPrefixRegex.Replace(input.ToLower(), "");
var registeredArg = _args.FirstOrDefault(f => f.Name == arg || f.ShortName == arg);
var arg = ArgPrefixRegex.Replace(input.ToLower(), "");

return registeredArg;
return RegisteredArgs.FirstOrDefault(f => f.Name == arg || f.ShortName == arg);
}
}
3 changes: 1 addition & 2 deletions Scraps/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#endregion

using Microsoft.Extensions.DependencyInjection;

using Scraps.Forms;
using Scraps.Controls;
using Scraps.Services;
using Scraps.Resources;
using Microsoft.Extensions.DependencyInjection;

namespace Scraps;

Expand Down
6 changes: 2 additions & 4 deletions Scraps/Controls/AboutControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#endregion

using System.Reflection;
using System.Diagnostics;

using Scraps.Services;
using Scraps.Resources;
using Scraps.Extensions;
using System.Reflection;

namespace Scraps.Controls;

Expand All @@ -44,7 +42,7 @@ public AboutControl(UpdateService update)
private string GetAssemblyBuildDate()
{
#pragma warning disable IL3000
string assLocation = Assembly.GetExecutingAssembly().Location;
var assLocation = Assembly.GetExecutingAssembly().Location;
#pragma warning restore IL3000
if (assLocation.IsNullOrEmpty())
{
Expand Down
3 changes: 1 addition & 2 deletions Scraps/Controls/DebugControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#endregion

using Microsoft.Win32;

using Scraps.Services;

namespace Scraps.Controls
Expand All @@ -31,7 +30,7 @@ public DebugControl()

private async void _OpenSettingsFolder_Click(object sender, EventArgs e)
{
string path = Path.GetDirectoryName(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath);
var path = Path.GetDirectoryName(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath);

await Utils.OpenDirectory(path);
}
Expand Down
24 changes: 12 additions & 12 deletions Scraps/Controls/SettingsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ private void _CookieInput_MouseLeave(object sender, EventArgs e)

private void _SaveSettingsButton_Click(object sender, EventArgs e)
{
string cookie = _CookieInput.Text.Trim();
int scanDelay = (int)_ScanDelayInput.Value;
int paginateDelay = (int)_PaginateDelayInput.Value;
int joinDelay = (int)_JoinDelayInput.Value;
int joinJitter = (int)_JoinJitterInput.Value;
int scanJitter = (int)_ScanJitterInput.Value;
bool sortByNew = _RaffleSortByNewToggle.Checked;
bool incrementScanDelay = _AutoIncrementScanDelayToggle.Checked;
bool paranoid = _ParanoidModeToggle.Checked;
bool alwaysOnTop = _TopmostToggle.Checked;
bool checkUpdates = _CheckUpdatesToggle.Checked;
bool fetchAnnouncements = _FetchAnnouncementsToggle.Checked;
var cookie = _CookieInput.Text.Trim();
var scanDelay = (int)_ScanDelayInput.Value;
var paginateDelay = (int)_PaginateDelayInput.Value;
var joinDelay = (int)_JoinDelayInput.Value;
var joinJitter = (int)_JoinJitterInput.Value;
var scanJitter = (int)_ScanJitterInput.Value;
var sortByNew = _RaffleSortByNewToggle.Checked;
var incrementScanDelay = _AutoIncrementScanDelayToggle.Checked;
var paranoid = _ParanoidModeToggle.Checked;
var alwaysOnTop = _TopmostToggle.Checked;
var checkUpdates = _CheckUpdatesToggle.Checked;
var fetchAnnouncements = _FetchAnnouncementsToggle.Checked;

if (cookie.Contains("scr_session"))
{
Expand Down
12 changes: 5 additions & 7 deletions Scraps/Controls/WonRafflesControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#endregion

using Microsoft.Web.WebView2.Core;

using Scraps.Events;
using Scraps.Services;
using Microsoft.Web.WebView2.Core;

namespace Scraps.Controls;

Expand Down Expand Up @@ -53,9 +52,9 @@ private async Task InitializeWebViewAsync()
{
try
{
var environment = await CoreWebView2Environment.CreateAsync(userDataFolder: GlobalShared.DataPath);

await _WebView.EnsureCoreWebView2Async(environment);
await _WebView.EnsureCoreWebView2Async(
await CoreWebView2Environment.CreateAsync(userDataFolder: GlobalShared.DataPath)
);

#if RELEASE
_WebView.CoreWebView2.Settings.AreDevToolsEnabled = false;
Expand Down Expand Up @@ -132,8 +131,7 @@ private void WebViewOnNavigationStarting(object sender, CoreWebView2NavigationSt
{
if (_hasCookie)
{
string url = e.Uri;
if (url != "https://scrap.tf/raffles/won")
if (e.Uri != "https://scrap.tf/raffles/won")
{
e.Cancel = true;
}
Expand Down
7 changes: 3 additions & 4 deletions Scraps/Dialogs/UpdateTaskDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#endregion

using System.Diagnostics;

using Scraps.Models;
using System.Diagnostics;

namespace Scraps.Dialogs;

Expand Down Expand Up @@ -75,8 +74,8 @@ public UpdateTaskDialog(LatestReleaseResponse latestRelease)

private async void DownloadButtonOnClick(object sender, EventArgs e)
{
string downloadPath = Path.GetTempFileName();
string downloadUrl = _latestRelease.Assets.First(a => a.Name.StartsWith("scraps_setup")).BrowserDownloadUrl;
var downloadPath = Path.GetTempFileName();
var downloadUrl = _latestRelease.Assets.First(a => a.Name.StartsWith("scraps_setup")).BrowserDownloadUrl;

_updatePage.Navigate(_progressPage);

Expand Down
4 changes: 1 addition & 3 deletions Scraps/Events/SettingsServiceResetArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@

namespace Scraps.Events;

public class SettingsServiceResetArgs : EventArgs
{
}
public class SettingsServiceResetArgs : EventArgs { }
4 changes: 1 addition & 3 deletions Scraps/Events/SettingsServiceSavedArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@

namespace Scraps.Events;

public class SettingsServiceSavedArgs : EventArgs
{
}
public class SettingsServiceSavedArgs : EventArgs { }
3 changes: 1 addition & 2 deletions Scraps/Events/UpdateServiceCheckingUpdatesArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@

namespace Scraps.Events;

public class UpdateServiceCheckingUpdatesArgs : EventArgs
{ }
public class UpdateServiceCheckingUpdatesArgs : EventArgs { }
3 changes: 1 addition & 2 deletions Scraps/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#endregion

using Windows.Win32.Foundation;

using Scraps.Events;
using Scraps.Controls;
using Scraps.Services;
using Scraps.Resources;
using Windows.Win32.Foundation;

namespace Scraps.Forms;

Expand Down
6 changes: 3 additions & 3 deletions Scraps/RtbTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public RtbTarget(RichTextBox rtb)

protected override void Write(LogEventInfo logEvent)
{
string levelName = logEvent.Level.Name;
string prefix = $"[{DateTime.Now:HH:mm:ss}]";
string message = RenderLogEvent(Layout, logEvent);
var levelName = logEvent.Level.Name;
var prefix = $"[{DateTime.Now:HH:mm:ss}]";
var message = RenderLogEvent(Layout, logEvent);
_rtb.AppendText($"{prefix} ", _prefixColor);
_rtb.AppendLine(message, GetLevelColor(levelName));
_rtb.ScrollToCaret();
Expand Down
4 changes: 2 additions & 2 deletions Scraps/Scraps.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

<ItemGroup>
<PackageReference Include="AngleSharp" Version="1.0.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1905-prerelease" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-preview.6.23329.7" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1988-prerelease" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.18-beta">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
5 changes: 2 additions & 3 deletions Scraps/Services/AnnouncementService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ public async Task FetchAnnouncementsAsync()
var res = await _http.GetAsync(GlobalShared.AnnouncementFileUrl);
if (res.IsSuccessStatusCode)
{
string contents = await res.Content.ReadAsStringAsync();

var contents = await res.Content.ReadAsStringAsync();
var announcementLines = contents.Split("\n").ToList();

// Iterate through the announcements instead of adding them as a range so we can raise an event per announcement.
foreach (string announcement in announcementLines.Where(announcement => !_announcements.Contains(announcement)))
foreach (var announcement in announcementLines.Where(announcement => !_announcements.Contains(announcement)))
{
if (!announcement.IsNullOrEmpty())
{
Expand Down

0 comments on commit c4f74aa

Please sign in to comment.