Skip to content

Commit

Permalink
performance optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
laolarou726 committed Nov 15, 2023
1 parent 06f02f9 commit 2ba95be
Show file tree
Hide file tree
Showing 23 changed files with 86 additions and 231 deletions.
4 changes: 2 additions & 2 deletions ProjBobcat/ProjBobcat/Class/Helper/DownloadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public static async Task MultiPartDownloadTaskAsync(
headRes.EnsureSuccessStatusCode();

var responseLength = headRes.Content.Headers.ContentLength ?? 0;
var hasAcceptRanges = headRes.Headers.AcceptRanges.Any();
var hasAcceptRanges = headRes.Headers.AcceptRanges.Count != 0;

using var rangeGetMessage = new HttpRequestMessage(HttpMethod.Get, downloadFile.DownloadUri);
rangeGetMessage.Headers.Range = new RangeHeaderValue(0, 0);
Expand Down Expand Up @@ -484,7 +484,7 @@ public static async Task MultiPartDownloadTaskAsync(
}
}

if (exceptions.Any())
if (exceptions.Count > 0)
{
downloadFile.OnCompleted(false, new AggregateException(exceptions), 0);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static async IAsyncEnumerable<GameModResolvedInfo> ResolveModListAsync(IE
break;
}

if (!(model?.Any() ?? false))
if (model == null || model.Count == 0)
return null;

var authors = new HashSet<string>();
Expand Down Expand Up @@ -134,7 +134,7 @@ async Task<GameModResolvedInfo> GetFabricModInfo(IArchiveEntry entry)
var tempModel = await JsonSerializer.DeserializeAsync(stream,
FabricModInfoModelContext.Default.FabricModInfoModel, ct);

var author = tempModel?.Authors?.Any() ?? false
var author = tempModel?.Authors is { Length: > 0 }
? string.Join(',', tempModel.Authors)
: null;
var modList = tempModel?.Depends?.Select(d => d.Key)?.ToImmutableList();
Expand Down
16 changes: 9 additions & 7 deletions ProjBobcat/ProjBobcat/Class/Helper/RandomHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;

namespace ProjBobcat.Class.Helper;

Expand All @@ -17,13 +16,12 @@ public static class RandomHelper
/// <see cref="OutOfMemoryException" /> 。
/// </summary>
/// <typeparam name="T">数据的类型。</typeparam>
/// <param name="enumerable">一个有限的 <see cref="IEnumerable{T}" /> 。</param>
/// <returns> <paramref name="enumerable" /> 中的随机一项。</returns>
/// <param name="list">一个有限的 <see cref="IList{T}" /> 。</param>
/// <returns> <paramref name="list" /> 中的随机一项。</returns>
/// <exception cref="ArgumentNullException"></exception>
public static T? RandomSample<T>(this IEnumerable<T> enumerable)
public static T? RandomSample<T>(this IList<T> list)
{
var arr = enumerable.ToArray();
return arr.Length == 0 ? default : arr[RandomInteger(0, arr.Length - 1)];
return list.Count == 0 ? default : list[RandomInteger(0, list.Count - 1)];
}

/// <summary>
Expand All @@ -34,7 +32,11 @@ public static class RandomHelper
/// <returns></returns>
public static int RandomInteger(int min, int max)
{
return RandomNumberGenerator.GetInt32(min, max + 1);
#if NET8_0_OR_GREATER
return Random.Shared.Next(min, max + 1);
#else
return System.Security.Cryptography.RandomNumberGenerator.GetInt32(min, max + 1);
#endif
}

/// <summary>
Expand Down
154 changes: 0 additions & 154 deletions ProjBobcat/ProjBobcat/Class/Helper/RandomStringHelper.cs

This file was deleted.

2 changes: 1 addition & 1 deletion ProjBobcat/ProjBobcat/Class/Helper/RulesHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static bool CheckAllow(this IEnumerable<JvmRules?>? rules)

var jvmRules = rules.Where(r => r != null).Select(r => r!).ToList();

if (!jvmRules.Any()) return false;
if (jvmRules.Count == 0) return false;

var ruleFlag = false;
var orderedRules = new List<JvmRules>();
Expand Down
2 changes: 1 addition & 1 deletion ProjBobcat/ProjBobcat/Class/Model/Mojang/UserProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class UserProfile

[JsonPropertyName("id")] public string Id { get; set; }
[JsonPropertyName("name")] public string Name { get; set; }
[JsonPropertyName("properties")] public UserProfileProperty[] Properties { get; set; }
[JsonPropertyName("properties")] public UserProfileProperty[]? Properties { get; set; }
}

[JsonSerializable(typeof(UserProfile))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PingPayload

[JsonPropertyName("description")] public JsonElement Description { get; set; }

[JsonPropertyName("modinfo")] public ServerPingModInfo ModInfo { get; set; }
[JsonPropertyName("modinfo")] public ServerPingModInfo? ModInfo { get; set; }

[JsonPropertyName("favicon")] public string Icon { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public class PlayersPayload

[JsonPropertyName("online")] public int Online { get; set; }

[JsonPropertyName("sample")] public Player[] Sample { get; set; }
[JsonPropertyName("sample")] public Player[]? Sample { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void ParseVersion(string version)

if (version.Length > startIndex) list.Add(ParseItem(isDigit, version[startIndex..]));

while (stack.Any())
while (stack.Count > 0)
{
list = (ListItem)stack.Pop();
list.Normalize();
Expand Down
4 changes: 2 additions & 2 deletions ProjBobcat/ProjBobcat/Class/Model/VersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class VersionInfo
public string? MainClass { get; set; }
public string Assets { get; set; }
public Asset? AssetInfo { get; set; }
public List<FileInfo> Libraries { get; set; }
public List<NativeFileInfo> Natives { get; set; }
public List<FileInfo>? Libraries { get; set; }
public List<NativeFileInfo>? Natives { get; set; }
public Logging Logging { get; set; }
public IEnumerable<string> JvmArguments { get; set; }
public IEnumerable<string> GameArguments { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class AuthResponseModel
[JsonPropertyName("clientToken")] public string ClientToken { get; set; }

[JsonPropertyName("availableProfiles")]
public ProfileInfoModel[] AvailableProfiles { get; set; }
public ProfileInfoModel[]? AvailableProfiles { get; set; }

[JsonPropertyName("selectedProfile")] public ProfileInfoModel? SelectedProfile { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ await SendRequest(MSAuthXBLUrl, AuthXBLRequestModel.Get(accessToken),
await ownResRes.Content.ReadFromJsonAsync(MojangOwnershipResponseModelContext.Default
.MojangOwnershipResponseModel);

if (!(ownRes?.Items?.Any() ?? false))
if (ownRes?.Items == null || ownRes.Items.Length == 0)
return new MicrosoftAuthResult
{
AuthStatus = AuthStatus.Failed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public async Task<AuthResultBase> AuthTaskAsync(bool userField = false)
};
}

if (result.SelectedProfile == null && !(result.AvailableProfiles?.Any() ?? false))
if (result.SelectedProfile == null && (result.AvailableProfiles == null || result.AvailableProfiles.Length == 0))
return new YggdrasilAuthResult
{
AuthStatus = AuthStatus.Failed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@ string ResolveVariableRegex(string val)

foreach (var proc in ipModel.Processors)
{
if (proc.Sides != null &&
proc.Sides.Any() &&
if (proc.Sides is { Length: > 0 } &&
!proc.Sides.Any(s => s.Equals("client", StringComparison.OrdinalIgnoreCase))
)
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void LogReceivedEvent(object sender, DataReceivedEventArgs args)
await p.WaitForExitAsync();
InvokeStatusChangedEvent("安装即将完成", 90);

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

InvokeStatusChangedEvent("Optifine 安装完成", 100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public IEnumerable<string> ParseGameLoggingArguments()
/// <returns></returns>
public IEnumerable<string> ParseAdditionalArguments()
{
if (!VersionInfo.AvailableGameArguments.Any()) yield break;
if (VersionInfo.AvailableGameArguments.Count == 0) yield break;
if (!VersionInfo.AvailableGameArguments.ContainsKey("has_custom_resolution")) yield break;

if (!(LaunchSettings.GameArguments?.Resolution?.IsDefault() ?? true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override IEnumerable<string> ParseJvmArguments(IEnumerable<JsonElement>?
case JsonValueKind.Array:
var values = value.Deserialize(StringContext.Default.StringArray);

if (!(values?.Any() ?? false)) continue;
if (values == null || values.Length == 0) continue;

foreach (var val in values)
yield return StringHelper.FixArgument(val);
Expand Down Expand Up @@ -139,12 +139,12 @@ private protected override (IEnumerable<string>, Dictionary<string, string>) Par

var rulesArr = rules.Deserialize(GameRulesContext.Default.GameRulesArray);

if (!(rulesArr?.Any() ?? false)) continue;
if (rulesArr == null || rulesArr.Length == 0) continue;

foreach (var rule in rulesArr)
{
if (!rule.Action.Equals("allow", StringComparison.Ordinal)) continue;
if (!rule.Features.Any()) continue;
if (rule.Features.Count == 0) continue;
if (!rule.Features.First().Value) continue;

ruleKey = rule.Features.First().Key;
Expand Down Expand Up @@ -381,7 +381,7 @@ public override (List<NativeFileInfo>, List<FileInfo>) GetNatives(IEnumerable<Li

// 检查游戏是否存在继承关系。
// Check if there is inheritance.
if (inherits?.Any() ?? false)
if (inherits is { Count: > 0 })
{
// 存在继承关系。
// Inheritance exists.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using ProjBobcat.Interface;

namespace ProjBobcat.DefaultComponent.LogAnalysis.AnalysisReport;
Expand All @@ -8,5 +7,5 @@ public record AnalysisReport(CrashCauses Cause) : IAnalysisReport
{
public string? From { get; set; }
public IReadOnlyCollection<string>? Details { get; set; }
public bool HasDetails => Details?.Any() ?? false;
public bool HasDetails => Details is { Count: > 0 };
}
Loading

0 comments on commit 2ba95be

Please sign in to comment.