Skip to content

Commit

Permalink
fully migrated to NET8
Browse files Browse the repository at this point in the history
  • Loading branch information
laolarou726 committed Nov 29, 2023
1 parent 26805ab commit f81eee1
Show file tree
Hide file tree
Showing 173 changed files with 1,163 additions and 1,387 deletions.
12 changes: 8 additions & 4 deletions ProjBobcat/ProjBobcat/Class/Helper/AuthPropertyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public static string ResolveUserProperties(this IEnumerable<PropertyModel>? prop
/// <param name="model">PropertyModel</param>
/// <param name="profiles">Profile集合</param>
/// <returns>转换好的UserProperty</returns>
public static AuthPropertyModel ToAuthProperty(this PropertyModel model,
public static AuthPropertyModel? ToAuthProperty(
this PropertyModel? model,
IReadOnlyDictionary<PlayerUUID, AuthProfileModel> profiles)
{
return model is null
Expand All @@ -61,11 +62,14 @@ public static AuthPropertyModel ToAuthProperty(this PropertyModel model,
/// <param name="models">PropertyModel集合</param>
/// <param name="profiles">Profile集合</param>
/// <returns>转换好的UserProperty</returns>
public static IEnumerable<AuthPropertyModel> ToAuthProperties(this IEnumerable<PropertyModel> models,
public static IEnumerable<AuthPropertyModel> ToAuthProperties(
this IEnumerable<PropertyModel>? models,
IReadOnlyDictionary<PlayerUUID, AuthProfileModel> profiles)
{
return models == null
? new List<AuthPropertyModel>()
: models.Select(model => model.ToAuthProperty(profiles));
? []
: models
.Select(model => model.ToAuthProperty(profiles))
.OfType<AuthPropertyModel>();
}
}
9 changes: 6 additions & 3 deletions ProjBobcat/ProjBobcat/Class/Helper/CurseForgeAPIHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
Expand All @@ -14,7 +15,6 @@ namespace ProjBobcat.Class.Helper;
#region Temp Models

record AddonInfoReqModel(IEnumerable<long> modIds);

record FileInfoReqModel(IEnumerable<long> fileIds);

[JsonSerializable(typeof(AddonInfoReqModel))]
Expand All @@ -37,11 +37,14 @@ public static class CurseForgeAPIHelper
{
const string BaseUrl = "https://api.curseforge.com/v1";

static string ApiKey { get; set; }
static string ApiKey { get; set; } = null!;
static HttpClient Client => HttpClientHelper.DefaultClient;

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

var req = new HttpRequestMessage(method, url);

req.Headers.Add("x-api-key", ApiKey);
Expand Down
12 changes: 1 addition & 11 deletions ProjBobcat/ProjBobcat/Class/Helper/GameRegexHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,10 @@ public static partial class GameRegexHelper

const string ForgeLegacyJarRegexStr =
"forge-[0-9]{1,}.[0-9]{1,}[.]?[0-9]{0,}-[0-9]{1,}.[0-9]{1,}.[0-9]{1,}.[0-9]{4}-[0-9]{1,}.[0-9]{1,}[.]?[0-9]{0,}-universal.jar";


#if NET8_0_OR_GREATER

[GeneratedRegex("[0-9]{1,}.[0-9]{1,}[.]?[0-9]{0,}")]
public static partial Regex GameVersionRegex();

[GeneratedRegex("forge-[0-9]{1,}.[0-9]{1,}[.]?[0-9]{0,}-[0-9]{1,}.[0-9]{1,}.[0-9]{1,}.[0-9]{4}-[0-9]{1,}.[0-9]{1,}[.]?[0-9]{0,}-universal.jar")]
public static partial Regex ForgeLegacyJarRegex();

#else

public static readonly Regex GameVersionRegex = new(GameVersionRegexStr, RegexOptions.Compiled);

public static readonly Regex ForgeLegacyJarRegex = new(ForgeLegacyJarRegexStr, RegexOptions.Compiled);

#endif
}
54 changes: 28 additions & 26 deletions ProjBobcat/ProjBobcat/Class/Helper/HttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,18 @@ public static partial class HttpHelper
{
const string UriRegexStr =
"((([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[-;:&=\\+$,\\w]+@)?[A-Za-z0-9.-]+(:[0-9]+)?|(?:ww‌​w.|[-;:&=\\+$,\\w]+@)[A-Za-z0-9.-]+)((?:\\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%@.\\w_]*)#?‌​(?:[\\w]*))?)";

#if NET8_0_OR_GREATER

[GeneratedRegex(UriRegexStr)]
private static partial Regex UriRegex();

#else

static readonly Regex UriRegex = new(UriRegexStr, RegexOptions.Compiled);

#endif

static HttpClient Client => HttpClientHelper.DefaultClient;

/// <summary>
/// 正则匹配Uri
/// </summary>
/// <param name="uri">待处理Uri</param>
/// <returns>匹配的Uri</returns>
public static string RegexMatchUri(string uri)
{
#if NET8_0_OR_GREATER
return UriRegex().Match(uri).Value;
#else
return UriRegex.Match(uri).Value;
#endif
}
public static string RegexMatchUri(string uri) => UriRegex().Match(uri).Value;

/// <summary>
/// Http Delete方法
Expand All @@ -51,8 +37,11 @@ public static string RegexMatchUri(string uri)
/// <param name="contentType">ContentType</param>
/// <param name="auth">Auth 字段</param>
/// <returns></returns>
public static async Task<HttpResponseMessage> Delete(string address, string data,
string contentType = "application/json", Tuple<string, string> auth = default)
public static async Task<HttpResponseMessage> Delete(
string address,
string data,
string contentType = "application/json",
ValueTuple<string, string> auth = default)
{
using var req = new HttpRequestMessage(HttpMethod.Delete, new Uri(address))
{
Expand All @@ -61,7 +50,9 @@ public static async Task<HttpResponseMessage> Delete(string address, string data

req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));

if (!(auth?.Equals(default) ?? true))
if (auth != default &&
!string.IsNullOrEmpty(auth.Item1) &&
!string.IsNullOrEmpty(auth.Item2))
req.Headers.Authorization = new AuthenticationHeaderValue(auth.Item1, auth.Item2);

var acceptLanguage = new StringWithQualityHeaderValue(CultureInfo.CurrentCulture.Name);
Expand All @@ -78,14 +69,18 @@ public static async Task<HttpResponseMessage> Delete(string address, string data
/// <param name="address">Get地址</param>
/// <param name="auth">Auth 字段</param>
/// <returns>获取到的字符串</returns>
public static async Task<HttpResponseMessage> Get(string address, Tuple<string, string> auth = default)
public static async Task<HttpResponseMessage> Get(
string address,
ValueTuple<string, string> auth = default)
{
using var req = new HttpRequestMessage(HttpMethod.Get, new Uri(address));

var acceptLanguage = new StringWithQualityHeaderValue(CultureInfo.CurrentCulture.Name);
req.Headers.AcceptLanguage.Add(acceptLanguage);

if (!(auth?.Equals(default) ?? true))
if (auth != default &&
!string.IsNullOrEmpty(auth.Item1) &&
!string.IsNullOrEmpty(auth.Item2))
req.Headers.Authorization = new AuthenticationHeaderValue(auth.Item1, auth.Item2);

var res = await Client.SendAsync(req);
Expand Down Expand Up @@ -118,8 +113,11 @@ public static async Task<HttpResponseMessage> PostFormData(string address,
/// <param name="contentType">ContentType</param>
/// <param name="auth">Auth 字段</param>
/// <returns></returns>
public static async Task<HttpResponseMessage> Post(string address, string data,
string contentType = "application/json", Tuple<string, string> auth = default)
public static async Task<HttpResponseMessage> Post(
string address,
string data,
string contentType = "application/json",
ValueTuple<string, string> auth = default)
{
using var req = new HttpRequestMessage(HttpMethod.Post, new Uri(address))
{
Expand All @@ -128,7 +126,9 @@ public static async Task<HttpResponseMessage> Post(string address, string data,

req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));

if (!(auth?.Equals(default) ?? true))
if (auth != default &&
!string.IsNullOrEmpty(auth.Item1) &&
!string.IsNullOrEmpty(auth.Item2))
req.Headers.Authorization = new AuthenticationHeaderValue(auth.Item1, auth.Item2);

var acceptLanguage = new StringWithQualityHeaderValue(CultureInfo.CurrentCulture.Name);
Expand All @@ -146,8 +146,10 @@ public static async Task<HttpResponseMessage> Post(string address, string data,
/// <param name="param">参数</param>
/// <param name="contentType">ContentType</param>
/// <returns></returns>
public static async Task<HttpResponseMessage> PostWithParams(string address,
IEnumerable<KeyValuePair<string, string>> param, string contentType = "application/json")
public static async Task<HttpResponseMessage> PostWithParams(
string address,
IEnumerable<KeyValuePair<string, string>> param,
string contentType = "application/json")
{
using var content = new FormUrlEncodedContent(param);
content.Headers.ContentType = new MediaTypeWithQualityHeaderValue(contentType);
Expand Down
13 changes: 1 addition & 12 deletions ProjBobcat/ProjBobcat/Class/Helper/MavenHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ namespace ProjBobcat.Class.Helper;
/// </summary>
public static partial class MavenHelper
{
#if NET8_0_OR_GREATER
[GeneratedRegex("\\.")]
private static partial Regex GroupPathRegex();
#else
static readonly Regex GroupPathRegex = new("\\.", RegexOptions.Compiled);
#endif

/// <summary>
/// 使用名字来解析Maven包信息。
Expand Down Expand Up @@ -69,14 +65,7 @@ public static partial class MavenHelper
/// </summary>
/// <param name="artifactId">Maven Id</param>
/// <returns>处理好的Group Path</returns>
public static string GetGroupPath(this string artifactId)
{
#if NET8_0_OR_GREATER
return GroupPathRegex().Replace(artifactId, "/");
#else
return GroupPathRegex.Replace(artifactId, "/");
#endif
}
public static string GetGroupPath(this string artifactId) => GroupPathRegex().Replace(artifactId, "/");

/// <summary>
/// 获取Maven全名
Expand Down
9 changes: 1 addition & 8 deletions ProjBobcat/ProjBobcat/Class/Helper/RandomHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ public static class RandomHelper
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public static int RandomInteger(int min, int max)
{
#if NET8_0_OR_GREATER
return Random.Shared.Next(min, max + 1);
#else
return System.Security.Cryptography.RandomNumberGenerator.GetInt32(min, max + 1);
#endif
}
public static int RandomInteger(int min, int max) => Random.Shared.Next(min, max + 1);

/// <summary>
/// 随机打乱集合当中的元素
Expand Down
3 changes: 2 additions & 1 deletion ProjBobcat/ProjBobcat/Class/Helper/StringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ namespace ProjBobcat.Class.Helper;
/// </summary>
public static class StringHelper
{
public static string FixPathArgument(string arg)
public static string? FixPathArgument(string? arg)
{
if (string.IsNullOrEmpty(arg)) return null;
if (!arg.Contains(' ')) return arg;

return $"\"{arg}\"";
Expand Down
49 changes: 4 additions & 45 deletions ProjBobcat/ProjBobcat/Class/Helper/TOMLParser/TomlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1899,33 +1899,15 @@ public static bool IsNaN(string s)
return s is NAN_VALUE or POS_NAN_VALUE or NEG_NAN_VALUE;
}

public static bool IsInteger(string s)
{
#if NET8_0_OR_GREATER
return IntegerPattern().IsMatch(s);
#else
return IntegerPattern.IsMatch(s);
#endif
}
public static bool IsInteger(string s) => IntegerPattern().IsMatch(s);

public static bool IsFloat(string s)
{
#if NET8_0_OR_GREATER
return FloatPattern().IsMatch(s);
#else
return FloatPattern.IsMatch(s);
#endif
}
public static bool IsFloat(string s) => FloatPattern().IsMatch(s);

public static bool IsIntegerWithBase(string s, out int numberBase)
{
numberBase = 10;

#if NET8_0_OR_GREATER

var match = BasedIntegerPattern().Match(s);
#else
var match = BasedIntegerPattern.Match(s);
#endif

if (!match.Success) return false;
IntegerBases.TryGetValue(match.Groups["base"].Value, out numberBase);
Expand Down Expand Up @@ -2051,8 +2033,7 @@ public static bool IsValueSeparator(char c)
{
return c is ITEM_SEPARATOR or ARRAY_END_SYMBOL or INLINE_TABLE_END_SYMBOL;
}

#if NET8_0_OR_GREATER

[GeneratedRegex("^(\\+|-)?(?!_)(0|(?!0)(_?\\d)*)$")]
private static partial Regex IntegerPattern();

Expand All @@ -2062,28 +2043,6 @@ public static bool IsValueSeparator(char c)
[GeneratedRegex("^(\\+|-)?0(?<base>x|b|o)(?!_)(_?[0-9A-F])*$", RegexOptions.IgnoreCase)]
private static partial Regex BasedIntegerPattern();

#else

/**
* A pattern to verify the integer value according to the TOML specification.
*/
static readonly Regex IntegerPattern = new("^(\\+|-)?(?!_)(0|(?!0)(_?\\d)*)$", RegexOptions.Compiled);

/**
* A pattern to verify the float value according to the TOML specification.
*/
static readonly Regex FloatPattern =
new("^(\\+|-)?(?!_)(0|(?!0)(_?\\d)+)(((e(\\+|-)?(?!_)(_?\\d)+)?)|(\\.(?!_)(_?\\d)+(e(\\+|-)?(?!_)(_?\\d)+)?))$",
RegexOptions.IgnoreCase | RegexOptions.Compiled);

/**
* A pattern to verify a special 0x, 0o and 0b forms of an integer according to the TOML specification.
*/
static readonly Regex BasedIntegerPattern = new("^(\\+|-)?0(?<base>x|b|o)(?!_)(_?[0-9A-F])*$",
RegexOptions.IgnoreCase | RegexOptions.Compiled);

#endif

#endregion
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.IO;
using System.Text;

#nullable disable

namespace ProjBobcat.Class.Helper.TOMLParser;

/// <summary>
Expand Down Expand Up @@ -225,4 +227,6 @@ public static TomlNode MergeWith(this TomlNode self, TomlNode with, bool mergeNe

return self;
}
}
}

#nullable restore
Loading

0 comments on commit f81eee1

Please sign in to comment.