Skip to content

Commit

Permalink
code smell
Browse files Browse the repository at this point in the history
  • Loading branch information
laolarou726 committed Jan 24, 2025
1 parent b51c318 commit 6b3c429
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 35 deletions.
6 changes: 3 additions & 3 deletions ProjBobcat/ProjBobcat/Class/Helper/CurseForgeAPIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Net.Mime;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -47,8 +48,7 @@ public static class CurseForgeAPIHelper

static HttpRequestMessage Req(HttpMethod method, string url)
{
if (string.IsNullOrEmpty(ApiKey))
throw new NullReferenceException("未设置 API KEY,请调用 SetApiKey(string apiKey) 来进行设置。");
ArgumentException.ThrowIfNullOrEmpty(ApiKey);

var req = new HttpRequestMessage(method, url);

Expand Down Expand Up @@ -92,7 +92,7 @@ public static void SetApiKey(string apiKey)
CurseForgeModelContext.Default.AddonInfoReqModel);

using var req = Req(HttpMethod.Post, reqUrl);
req.Content = new StringContent(data, Encoding.UTF8, "application/json");
req.Content = new StringContent(data, Encoding.UTF8, MediaTypeNames.Application.Json);

using var res = await Client.SendAsync(req);

Expand Down
2 changes: 1 addition & 1 deletion ProjBobcat/ProjBobcat/Class/Helper/SystemInfoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static string GetSystemArch()
Architecture.X86 => "x86",
Architecture.X64 => "x64",
Architecture.Arm64 => "arm64",
var arch => throw new Exception($"Unknown system arch: {arch}")
var arch => throw new ArgumentOutOfRangeException($"Unknown system arch: {arch}")
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public async Task<string> InstallTaskAsync()
{
this.InvokeStatusChangedEvent("开始安装", ProgressValue.Start);

if (string.IsNullOrEmpty(this.RootPath))
throw new NullReferenceException("RootPath 字段为空");
ArgumentException.ThrowIfNullOrEmpty(this.RootPath);

var mcVersion = this.LoaderArtifact.Intermediary.Version;
var fabricVersion = this.LoaderArtifact.Loader.Separator == "."
Expand Down Expand Up @@ -63,8 +62,7 @@ public async Task<string> InstallTaskAsync()
_ => string.Empty
};

if (string.IsNullOrEmpty(mainClass))
throw new NullReferenceException("MainClass 字段为空");
ArgumentException.ThrowIfNullOrEmpty(mainClass);

var inheritsFrom = string.IsNullOrEmpty(this.InheritsFrom) ? mcVersion : this.InheritsFrom;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ public string Install()

public async Task<string> InstallTaskAsync()
{
if (string.IsNullOrEmpty(this.RootPath))
throw new NullReferenceException("RootPath 不能为 null");
if (this.InheritVersion == null)
throw new NullReferenceException("InheritVersion 不能为 null");
if (this.VersionModel == null)
throw new NullReferenceException("VersionModel 不能为 null");
ArgumentException.ThrowIfNullOrEmpty(this.RootPath);
ArgumentNullException.ThrowIfNull(this.InheritVersion);
ArgumentNullException.ThrowIfNull(this.VersionModel);

this.InvokeStatusChangedEvent("开始安装 LiteLoader", ProgressValue.Start);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ public async Task InstallTaskAsync()
HashType = HashType.SHA1
});

if (!this.FailedFiles.IsEmpty)
throw new NullReferenceException("未能下载全部的 Mods");
ArgumentOutOfRangeException.ThrowIfEqual(this.FailedFiles.IsEmpty, false);

var modPackFullPath = Path.GetFullPath(this.ModPackPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ public string Install()

public async Task<string> InstallTaskAsync()
{
if (string.IsNullOrEmpty(this.JavaExecutablePath))
throw new NullReferenceException("未指定 Java 运行时");
if (string.IsNullOrEmpty(this.OptifineJarPath))
throw new NullReferenceException("未指定 Optifine 安装包路径");
if (this.OptifineDownloadVersion == null)
throw new NullReferenceException("未指定 Optifine 下载信息");
ArgumentException.ThrowIfNullOrEmpty(this.JavaExecutablePath);
ArgumentException.ThrowIfNullOrEmpty(this.OptifineJarPath);
ArgumentNullException.ThrowIfNull(this.OptifineDownloadVersion);

this.InvokeStatusChangedEvent("开始安装 Optifine", ProgressValue.Start);
var mcVersion = this.OptifineDownloadVersion.McVersion;
Expand Down Expand Up @@ -186,8 +183,7 @@ void LogReceivedEvent(object sender, DataReceivedEventArgs args)
await p.WaitForExitAsync();
this.InvokeStatusChangedEvent("安装即将完成", ProgressValue.FromDisplay(95));

if (errList.Count > 0)
throw new NullReferenceException(string.Join(Environment.NewLine, errList));
ArgumentOutOfRangeException.ThrowIfGreaterThan(errList.Count, 0);

this.InvokeStatusChangedEvent("Optifine 安装完成", ProgressValue.Finished);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ public async Task<string> InstallTaskAsync()

this.InvokeStatusChangedEvent("生成版本总成", ProgressValue.FromDisplay(50));

if (versionModel == null)
throw new NullReferenceException(nameof(versionModel));
ArgumentNullException.ThrowIfNull(versionModel);

var hashed = versionModel.Libraries.FirstOrDefault(l =>
l.Name.StartsWith("org.quiltmc:hashed", StringComparison.OrdinalIgnoreCase));

if (hashed != default)
if (hashed != null)
{
var index = Array.IndexOf(versionModel.Libraries, hashed);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ public partial class DefaultLogAnalyzer : ILogAnalyzer

public IEnumerable<IAnalysisReport> GenerateReport()
{
if (string.IsNullOrEmpty(this.RootPath))
throw new NullReferenceException("未提供 RootPath 参数");
if (string.IsNullOrEmpty(this.GameId))
throw new NullReferenceException("未提供 GameId 参数");
ArgumentException.ThrowIfNullOrEmpty(this.RootPath);
ArgumentException.ThrowIfNullOrEmpty(this.GameId);

var logs = new Dictionary<LogFileType, List<(string, string)>>();

Expand Down Expand Up @@ -104,10 +102,8 @@ static bool IsValidLogFile(FileInfo fi, double minutesAgo = 3)

IEnumerable<(LogFileType, (string, string))> GetAllLogs()
{
if (string.IsNullOrEmpty(this.RootPath))
throw new NullReferenceException("未提供 RootPath 参数");
if (string.IsNullOrEmpty(this.GameId))
throw new NullReferenceException("未提供 GameId 参数");
ArgumentException.ThrowIfNullOrEmpty(this.RootPath);
ArgumentException.ThrowIfNullOrEmpty(this.GameId);

var logFiles = new List<FileInfo>();
var fullRootPath = Path.GetFullPath(this.RootPath);
Expand Down

0 comments on commit 6b3c429

Please sign in to comment.