ref: 升级到 C# 语言版本 12
This commit is contained in:
1 parent
08b17e71bb
commit
e8b7ac33e0
10 files changed
+200
-211
No files matched your search
@@ -4,8 +4,8 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace MeloongCore.Extensions {
|
namespace MeloongCore.Extensions;
|
||||||
public static class EnumerableExtensions {
|
public static class EnumerableExtensions {
|
||||||
|
|
||||||
#region Distinct
|
#region Distinct
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ namespace MeloongCore.Extensions {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static List<T> MaxByAll<T, C>(this IEnumerable<T> source, Func<T, C> selector) where C : IComparable<C> {
|
public static List<T> MaxByAll<T, C>(this IEnumerable<T> source, Func<T, C> selector) where C : IComparable<C> {
|
||||||
var results = new List<T>();
|
var results = new List<T>();
|
||||||
using (var enumerator = source.GetEnumerator()) {
|
using var enumerator = source.GetEnumerator();
|
||||||
if (!enumerator.MoveNext()) return results;
|
if (!enumerator.MoveNext()) return results;
|
||||||
T maxItem = enumerator.Current;
|
T maxItem = enumerator.Current;
|
||||||
C maxValue = selector(maxItem);
|
C maxValue = selector(maxItem);
|
||||||
@@ -55,7 +55,6 @@ namespace MeloongCore.Extensions {
|
|||||||
results.Add(currentItem);
|
results.Add(currentItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -64,7 +63,7 @@ namespace MeloongCore.Extensions {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static List<T> MinByAll<T, C>(this IEnumerable<T> source, Func<T, C> selector) where C : IComparable<C> {
|
public static List<T> MinByAll<T, C>(this IEnumerable<T> source, Func<T, C> selector) where C : IComparable<C> {
|
||||||
var results = new List<T>();
|
var results = new List<T>();
|
||||||
using (var enumerator = source.GetEnumerator()) {
|
using var enumerator = source.GetEnumerator();
|
||||||
if (!enumerator.MoveNext()) return results;
|
if (!enumerator.MoveNext()) return results;
|
||||||
T minItem = enumerator.Current;
|
T minItem = enumerator.Current;
|
||||||
C minValue = selector(minItem);
|
C minValue = selector(minItem);
|
||||||
@@ -81,7 +80,6 @@ namespace MeloongCore.Extensions {
|
|||||||
results.Add(currentItem);
|
results.Add(currentItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +88,7 @@ namespace MeloongCore.Extensions {
|
|||||||
/// 若没有元素则返回 Nothing。
|
/// 若没有元素则返回 Nothing。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static T MaxBy<T, C>(this IEnumerable<T> source, Func<T, C> selector) where C : IComparable<C> {
|
public static T MaxBy<T, C>(this IEnumerable<T> source, Func<T, C> selector) where C : IComparable<C> {
|
||||||
using (var enumerator = source.GetEnumerator()) {
|
using var enumerator = source.GetEnumerator();
|
||||||
if (!enumerator.MoveNext()) return default;
|
if (!enumerator.MoveNext()) return default;
|
||||||
T maxItem = enumerator.Current;
|
T maxItem = enumerator.Current;
|
||||||
C maxValue = selector(maxItem);
|
C maxValue = selector(maxItem);
|
||||||
@@ -102,13 +100,12 @@ namespace MeloongCore.Extensions {
|
|||||||
}
|
}
|
||||||
return maxItem;
|
return maxItem;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 选择最小值对应的对象。
|
/// 选择最小值对应的对象。
|
||||||
/// 若没有元素则返回 Nothing。
|
/// 若没有元素则返回 Nothing。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static T MinBy<T, C>(this IEnumerable<T> List, Func<T, C> Selector) where C : IComparable<C> {
|
public static T MinBy<T, C>(this IEnumerable<T> List, Func<T, C> Selector) where C : IComparable<C> {
|
||||||
using (var enumerator = List.GetEnumerator()) {
|
using var enumerator = List.GetEnumerator();
|
||||||
if (!enumerator.MoveNext()) { return default; }
|
if (!enumerator.MoveNext()) { return default; }
|
||||||
T minItem = enumerator.Current;
|
T minItem = enumerator.Current;
|
||||||
C minValue = Selector(minItem);
|
C minValue = Selector(minItem);
|
||||||
@@ -120,7 +117,6 @@ namespace MeloongCore.Extensions {
|
|||||||
}
|
}
|
||||||
return minItem;
|
return minItem;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -130,7 +126,7 @@ namespace MeloongCore.Extensions {
|
|||||||
/// 用指定的分割符将集合连接为字符串。
|
/// 用指定的分割符将集合连接为字符串。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string Join(this IEnumerable list, char split) {
|
public static string Join(this IEnumerable list, char split) {
|
||||||
StringBuilder builder = new StringBuilder();
|
var builder = new StringBuilder();
|
||||||
bool isFirst = true;
|
bool isFirst = true;
|
||||||
foreach (var element in list) {
|
foreach (var element in list) {
|
||||||
if (isFirst)
|
if (isFirst)
|
||||||
@@ -146,7 +142,7 @@ namespace MeloongCore.Extensions {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static string Join(this IEnumerable list, string split) {
|
public static string Join(this IEnumerable list, string split) {
|
||||||
if (split.IsSingle()) return list.Join(split[0]);
|
if (split.IsSingle()) return list.Join(split[0]);
|
||||||
StringBuilder builder = new StringBuilder();
|
var builder = new StringBuilder();
|
||||||
bool isFirst = true;
|
bool isFirst = true;
|
||||||
foreach (var element in list) {
|
foreach (var element in list) {
|
||||||
if (isFirst)
|
if (isFirst)
|
||||||
@@ -166,12 +162,11 @@ namespace MeloongCore.Extensions {
|
|||||||
public static bool IsSingle<T>(this IEnumerable<T> source) {
|
public static bool IsSingle<T>(this IEnumerable<T> source) {
|
||||||
if (source == null) return false;
|
if (source == null) return false;
|
||||||
if (source is IList<T> list) return list.Count == 1;
|
if (source is IList<T> list) return list.Count == 1;
|
||||||
using (var enumerator = source.GetEnumerator()) {
|
using var enumerator = source.GetEnumerator();
|
||||||
if (!enumerator.MoveNext()) return false;
|
if (!enumerator.MoveNext()) return false;
|
||||||
if (enumerator.MoveNext()) return false;
|
if (enumerator.MoveNext()) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 对集合的每个元素执行指定操作。
|
/// 对集合的每个元素执行指定操作。
|
||||||
@@ -181,5 +176,4 @@ namespace MeloongCore.Extensions {
|
|||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -2,8 +2,8 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace MeloongCore.Extensions {
|
namespace MeloongCore.Extensions;
|
||||||
public static class StringExtensions {
|
public static class StringExtensions {
|
||||||
|
|
||||||
#region 区域性 / 大小写 简化
|
#region 区域性 / 大小写 简化
|
||||||
|
|
||||||
@@ -162,8 +162,14 @@ namespace MeloongCore.Extensions {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static string Capitalize(this string word) {
|
public static string Capitalize(this string word) {
|
||||||
if (string.IsNullOrEmpty(word)) return word;
|
if (string.IsNullOrEmpty(word)) return word;
|
||||||
return word.Substring(0, 1).Upper() + word.Substring(1).Lower();
|
return $"{word.Substring(0, 1).Upper()}{word.Substring(1).Lower()}";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
/// <summary>
|
||||||
|
/// 替换字符串中的内容。
|
||||||
|
/// 仅当需要替换时,才调用 <paramref name="newValue"/> 获取结果字符串。
|
||||||
|
/// </summary>
|
||||||
|
public static string ReplaceOnDemand(this string str, string oldValue, Func<string> newValue) =>
|
||||||
|
str.Contains(oldValue) ? str.Replace(oldValue, newValue()) : str;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
using MeloongCore.Extensions;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace MeloongCore {
|
|
||||||
public static class DirectoryUtils {
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 确保路径以 / 或 \ 结尾。
|
|
||||||
/// </summary>
|
|
||||||
public static string ToDirectoryFormat(string folder) => IsDirectoryFormat(folder) ? folder : folder + Path.DirectorySeparatorChar;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 检查路径是否以 / 或 \ 结尾。
|
|
||||||
/// </summary>
|
|
||||||
public static bool IsDirectoryFormat(string path) => path.EndsWithF("/") || path.EndsWithF("\\");
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 创建文件夹,或文件所在的文件夹。
|
|
||||||
/// 支持长路径,文件夹已存在时不会抛出异常。
|
|
||||||
/// </summary>
|
|
||||||
public static void Create(string path, bool isFilePath = false) {
|
|
||||||
if (isFilePath) path = Path.GetDirectoryName(path);
|
|
||||||
Directory.CreateDirectory(PathUtils.Shorten(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,109 +0,0 @@
|
|||||||
using MeloongCore.Extensions;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Numerics;
|
|
||||||
|
|
||||||
namespace MeloongCore {
|
|
||||||
public static class MathUtils {
|
|
||||||
|
|
||||||
#region 进制转换
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 将字符串作为数字,转换为指定进制的形式。
|
|
||||||
/// 进制数需在 2 到 86 之间,且输入字符串中的每个字符都必须在该进制的字符集中。
|
|
||||||
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
|
|
||||||
/// </summary>
|
|
||||||
public static string RadixConvert(this string input, int fromRadix, int toRadix) {
|
|
||||||
const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=!?@#$%^&*()[]{}<>;:',";
|
|
||||||
if (fromRadix < 2 || fromRadix > digits.Length) throw new ArgumentOutOfRangeException(nameof(fromRadix), $"{nameof(fromRadix)} must be between 2 and 86.");
|
|
||||||
// 零与负数的预处理
|
|
||||||
if (string.IsNullOrEmpty(input)) return "0";
|
|
||||||
bool isNegative = input.StartsWithF("-");
|
|
||||||
if (isNegative) input = input.TrimStart('-');
|
|
||||||
// 转换为十进制
|
|
||||||
BigInteger realNum = 0;
|
|
||||||
foreach (char c in input) {
|
|
||||||
int digit = digits.IndexOf(c);
|
|
||||||
if (digit == -1 || digit >= fromRadix) throw new ArgumentException($"Character '{c}' in input '{input}' is not a valid digit for radix {fromRadix}.");
|
|
||||||
realNum = realNum * fromRadix + digit;
|
|
||||||
}
|
|
||||||
return RadixConvert(realNum * (isNegative ? -1 : 1), toRadix);
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// 转换为 2 到 86 进制的字符串。
|
|
||||||
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
|
|
||||||
/// </summary>
|
|
||||||
public static string RadixConvert(this long input, int toRadix) {
|
|
||||||
return RadixConvert((BigInteger) input, toRadix);
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// 转换为 2 到 86 进制的字符串。
|
|
||||||
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
|
|
||||||
/// </summary>
|
|
||||||
public static string RadixConvert(this int input, int toRadix) {
|
|
||||||
return RadixConvert((BigInteger) input, toRadix);
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// 转换为 2 到 86 进制的字符串。
|
|
||||||
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
|
|
||||||
/// </summary>
|
|
||||||
public static string RadixConvert(this byte input, int toRadix) {
|
|
||||||
return RadixConvert((BigInteger) input, toRadix);
|
|
||||||
}
|
|
||||||
private static string RadixConvert(this BigInteger input, int toRadix) {
|
|
||||||
const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=!?@#$%^&*()[]{}<>;:',";
|
|
||||||
if (toRadix < 2 || toRadix > digits.Length) throw new ArgumentOutOfRangeException(nameof(toRadix), $"{nameof(toRadix)} must be between 2 and 86.");
|
|
||||||
// 零与负数的预处理
|
|
||||||
if (input == 0) return "0";
|
|
||||||
bool isNegative = input < 0;
|
|
||||||
// 转换为指定进制
|
|
||||||
var results = new List<char>();
|
|
||||||
BigInteger remaining = BigInteger.Abs(input);
|
|
||||||
BigInteger bigRadix = toRadix;
|
|
||||||
while (remaining > 0) {
|
|
||||||
remaining = BigInteger.DivRem(remaining, bigRadix, out BigInteger remainder);
|
|
||||||
results.Add(digits[(int) remainder]);
|
|
||||||
}
|
|
||||||
// 负数的结束处理与返回
|
|
||||||
if (isNegative) results.Add('-');
|
|
||||||
results.Reverse();
|
|
||||||
return new string(results.ToArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 将值限制在指定范围内。
|
|
||||||
/// </summary>
|
|
||||||
public static T Clamp<T>(this T value, T min, T max) where T : IComparable<T> {
|
|
||||||
if (value.CompareTo(min) < 0) return min;
|
|
||||||
if (value.CompareTo(max) > 0) return max;
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 在一个范围中进行线性插值。
|
|
||||||
/// 返回值将被舍入到小数点后 7 位,以解决浮点数误差问题。
|
|
||||||
/// </summary>
|
|
||||||
public static double Lerp(double start, double end, double percentage) {
|
|
||||||
return Math.Round(start + (end - start) * percentage, 7);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 计算二阶贝塞尔曲线。
|
|
||||||
/// </summary>
|
|
||||||
public static double Bezier(double t, double x1, double y1, double x2, double y2, double accuracy = 0.01) {
|
|
||||||
if (t < 0.0000001 || double.IsNaN(t)) return 0;
|
|
||||||
if (t > 0.9999999) return 1;
|
|
||||||
double a = t;
|
|
||||||
double b;
|
|
||||||
do {
|
|
||||||
b = 3 * a * ((0.33333333 + x1 - x2) * a * a + (x2 - 2 * x1) * a + x1);
|
|
||||||
a += (t - b) * 0.5;
|
|
||||||
} while (!(Math.Abs(b - t) < accuracy)); // 精度
|
|
||||||
return 3 * a * ((0.33333333 + y1 - y2) * a * a + (y2 - 2 * y1) * a + y1);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+5
-7
@@ -3,12 +3,12 @@ using System.ComponentModel;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace MeloongCore {
|
namespace MeloongCore;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 使用 Windows MsDelta API 生成、应用增量补丁文件。
|
/// 使用 Windows MsDelta API 生成、应用增量补丁文件。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class MsDelta {
|
public static class MsDelta {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 生成增量补丁文件:比较 <paramref name="oldFilePath"/> 和 <paramref name="newFilePath"/>,将补丁写入 <paramref name="deltaFilePath"/>。
|
/// 生成增量补丁文件:比较 <paramref name="oldFilePath"/> 和 <paramref name="newFilePath"/>,将补丁写入 <paramref name="deltaFilePath"/>。
|
||||||
@@ -58,6 +58,4 @@ namespace MeloongCore {
|
|||||||
[return: MarshalAs(UnmanagedType.Bool)]
|
[return: MarshalAs(UnmanagedType.Bool)]
|
||||||
private static extern bool ApplyDelta(long applyFlags, string sourceName, string deltaName, string targetName);
|
private static extern bool ApplyDelta(long applyFlags, string sourceName, string deltaName, string targetName);
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
using MeloongCore.Extensions;
|
|
||||||
using System.IO;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace MeloongCore {
|
|
||||||
public static class PathUtils {
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 若路径较长,则尽量将其转换为短路径。
|
|
||||||
/// 若输入的是文件夹路径,不保证其结尾是否有 \ 。
|
|
||||||
/// </summary>
|
|
||||||
public static string Shorten(string fullName, bool keepFileName = false) {
|
|
||||||
if (string.IsNullOrEmpty(fullName) || fullName.Length <= 200) return fullName;
|
|
||||||
fullName = fullName.Replace('/', '\\');
|
|
||||||
|
|
||||||
// 保留文件名
|
|
||||||
string pathToKeep = "";
|
|
||||||
string pathToShorten = fullName;
|
|
||||||
if (fullName.EndsWithF(".jar", true)) keepFileName = true; // jar 文件的文件名需要保留原样,否则会导致 Forge 1.20.1 无法通过文件名识别模块名
|
|
||||||
if (keepFileName && File.Exists(fullName)) {
|
|
||||||
pathToKeep = Path.GetFileName(fullName);
|
|
||||||
pathToShorten = Path.GetDirectoryName(fullName);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 逐级向上寻找已存在的文件夹,将不存在的部分挪到 suffix,不再缩短
|
|
||||||
while (!Directory.Exists(pathToShorten) && !File.Exists(pathToShorten)) { // 如果路径不存在
|
|
||||||
string parentPath = Path.GetDirectoryName(pathToShorten);
|
|
||||||
if (string.IsNullOrEmpty(parentPath) || parentPath == pathToShorten) return fullName; // 已经到达根目录,全都不存在,直接返回
|
|
||||||
pathToKeep = Path.Combine(Path.GetFileName(pathToShorten), pathToKeep);
|
|
||||||
pathToShorten = parentPath;
|
|
||||||
}
|
|
||||||
if (pathToShorten.Length <= 10) return fullName;
|
|
||||||
|
|
||||||
// 缩短路径
|
|
||||||
StringBuilder buffer = new StringBuilder(260);
|
|
||||||
if (GetShortPathNameW(pathToShorten, buffer, buffer.Capacity) == 0) return fullName;
|
|
||||||
return Path.Combine(buffer.ToString(), pathToKeep);
|
|
||||||
}
|
|
||||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
|
|
||||||
private static extern int GetShortPathNameW(string lpszLongPath, StringBuilder lpszShortPath, int cchBuffer);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
|
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
|
||||||
<Configurations>Debug;Release;Snapshot</Configurations>
|
<Configurations>Debug;Release;Snapshot</Configurations>
|
||||||
<Deterministic>true</Deterministic>
|
<Deterministic>true</Deterministic>
|
||||||
|
<LangVersion>12.0</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
using MeloongCore.Extensions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MeloongCore;
|
||||||
|
public static class DirectoryUtils {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 确保路径以 / 或 \ 结尾。
|
||||||
|
/// </summary>
|
||||||
|
public static string ToDirectoryFormat(string folder) => IsDirectoryFormat(folder) ? folder : folder + Path.DirectorySeparatorChar;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查路径是否以 / 或 \ 结尾。
|
||||||
|
/// </summary>
|
||||||
|
public static bool IsDirectoryFormat(string path) => path.EndsWithF("/") || path.EndsWithF("\\");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建文件夹,或文件所在的文件夹。
|
||||||
|
/// 支持长路径,文件夹已存在时不会抛出异常。
|
||||||
|
/// </summary>
|
||||||
|
public static void Create(string path, bool isFilePath = false) {
|
||||||
|
if (isFilePath) path = Path.GetDirectoryName(path);
|
||||||
|
Directory.CreateDirectory(PathUtils.Shorten(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
using MeloongCore.Extensions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Numerics;
|
||||||
|
namespace MeloongCore;
|
||||||
|
|
||||||
|
public static class MathUtils {
|
||||||
|
|
||||||
|
#region 进制转换
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 将字符串作为数字,转换为指定进制的形式。
|
||||||
|
/// 进制数需在 2 到 86 之间,且输入字符串中的每个字符都必须在该进制的字符集中。
|
||||||
|
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
|
||||||
|
/// </summary>
|
||||||
|
public static string RadixConvert(this string input, int fromRadix, int toRadix) {
|
||||||
|
const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=!?@#$%^&*()[]{}<>;:',";
|
||||||
|
if (fromRadix < 2 || fromRadix > digits.Length) throw new ArgumentOutOfRangeException(nameof(fromRadix), $"{nameof(fromRadix)} must be between 2 and 86.");
|
||||||
|
// 零与负数的预处理
|
||||||
|
if (string.IsNullOrEmpty(input)) return "0";
|
||||||
|
bool isNegative = input.StartsWithF("-");
|
||||||
|
if (isNegative) input = input.TrimStart('-');
|
||||||
|
// 转换为十进制
|
||||||
|
BigInteger realNum = 0;
|
||||||
|
foreach (char c in input) {
|
||||||
|
int digit = digits.IndexOf(c);
|
||||||
|
if (digit == -1 || digit >= fromRadix) throw new ArgumentException($"Character '{c}' in input '{input}' is not a valid digit for radix {fromRadix}.");
|
||||||
|
realNum = realNum * fromRadix + digit;
|
||||||
|
}
|
||||||
|
return RadixConvert(realNum * (isNegative ? -1 : 1), toRadix);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 转换为 2 到 86 进制的字符串。
|
||||||
|
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
|
||||||
|
/// </summary>
|
||||||
|
public static string RadixConvert(this long input, int toRadix) => RadixConvert((BigInteger) input, toRadix);
|
||||||
|
/// <summary>
|
||||||
|
/// 转换为 2 到 86 进制的字符串。
|
||||||
|
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
|
||||||
|
/// </summary>
|
||||||
|
public static string RadixConvert(this int input, int toRadix) => RadixConvert((BigInteger) input, toRadix);
|
||||||
|
/// <summary>
|
||||||
|
/// 转换为 2 到 86 进制的字符串。
|
||||||
|
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
|
||||||
|
/// </summary>
|
||||||
|
public static string RadixConvert(this byte input, int toRadix) => RadixConvert((BigInteger) input, toRadix);
|
||||||
|
private static string RadixConvert(this BigInteger input, int toRadix) {
|
||||||
|
const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=!?@#$%^&*()[]{}<>;:',";
|
||||||
|
if (toRadix < 2 || toRadix > digits.Length) throw new ArgumentOutOfRangeException(nameof(toRadix), $"{nameof(toRadix)} must be between 2 and 86.");
|
||||||
|
// 零与负数的预处理
|
||||||
|
if (input == 0) return "0";
|
||||||
|
bool isNegative = input < 0;
|
||||||
|
// 转换为指定进制
|
||||||
|
var results = new List<char>();
|
||||||
|
BigInteger remaining = BigInteger.Abs(input);
|
||||||
|
BigInteger bigRadix = toRadix;
|
||||||
|
while (remaining > 0) {
|
||||||
|
remaining = BigInteger.DivRem(remaining, bigRadix, out BigInteger remainder);
|
||||||
|
results.Add(digits[(int) remainder]);
|
||||||
|
}
|
||||||
|
// 负数的结束处理与返回
|
||||||
|
if (isNegative) results.Add('-');
|
||||||
|
results.Reverse();
|
||||||
|
return new string(results.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 将值限制在指定范围内。
|
||||||
|
/// </summary>
|
||||||
|
public static T Clamp<T>(this T value, T min, T max) where T : IComparable<T> {
|
||||||
|
if (value.CompareTo(min) < 0) return min;
|
||||||
|
if (value.CompareTo(max) > 0) return max;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 在一个范围中进行线性插值。
|
||||||
|
/// 返回值将被舍入到小数点后 7 位,以解决浮点数误差问题。
|
||||||
|
/// </summary>
|
||||||
|
public static double Lerp(double start, double end, double percentage) =>
|
||||||
|
Math.Round(start + (end - start) * percentage, 7);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 计算二阶贝塞尔曲线。
|
||||||
|
/// </summary>
|
||||||
|
public static double Bezier(double t, double x1, double y1, double x2, double y2, double accuracy = 0.01) {
|
||||||
|
if (t < 0.0000001 || double.IsNaN(t)) return 0;
|
||||||
|
if (t > 0.9999999) return 1;
|
||||||
|
double a = t;
|
||||||
|
double b;
|
||||||
|
do {
|
||||||
|
b = 3 * a * ((0.33333333 + x1 - x2) * a * a + (x2 - 2 * x1) * a + x1);
|
||||||
|
a += (t - b) * 0.5;
|
||||||
|
} while (!(Math.Abs(b - t) < accuracy)); // 精度
|
||||||
|
return 3 * a * ((0.33333333 + y1 - y2) * a * a + (y2 - 2 * y1) * a + y1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
using MeloongCore.Extensions;
|
||||||
|
using System.IO;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text;
|
||||||
|
namespace MeloongCore;
|
||||||
|
|
||||||
|
public static class PathUtils {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 若路径较长,则尽量将其转换为短路径。
|
||||||
|
/// 若输入的是文件夹路径,不保证其结尾是否有 \ 。
|
||||||
|
/// </summary>
|
||||||
|
public static string Shorten(string fullName, bool keepFileName = false) {
|
||||||
|
if (string.IsNullOrEmpty(fullName) || fullName.Length <= 200) return fullName;
|
||||||
|
fullName = fullName.Replace('/', '\\');
|
||||||
|
|
||||||
|
// 保留文件名
|
||||||
|
string pathToKeep = "";
|
||||||
|
string pathToShorten = fullName;
|
||||||
|
if (fullName.EndsWithF(".jar", true)) keepFileName = true; // jar 文件的文件名需要保留原样,否则会导致 Forge 1.20.1 无法通过文件名识别模块名
|
||||||
|
if (keepFileName && File.Exists(fullName)) {
|
||||||
|
pathToKeep = Path.GetFileName(fullName);
|
||||||
|
pathToShorten = Path.GetDirectoryName(fullName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 逐级向上寻找已存在的文件夹,将不存在的部分挪到 suffix,不再缩短
|
||||||
|
while (!Directory.Exists(pathToShorten) && !File.Exists(pathToShorten)) { // 如果路径不存在
|
||||||
|
string parentPath = Path.GetDirectoryName(pathToShorten);
|
||||||
|
if (string.IsNullOrEmpty(parentPath) || parentPath == pathToShorten) return fullName; // 已经到达根目录,全都不存在,直接返回
|
||||||
|
pathToKeep = Path.Combine(Path.GetFileName(pathToShorten), pathToKeep);
|
||||||
|
pathToShorten = parentPath;
|
||||||
|
}
|
||||||
|
if (pathToShorten.Length <= 10) return fullName;
|
||||||
|
|
||||||
|
// 缩短路径
|
||||||
|
var buffer = new StringBuilder(260);
|
||||||
|
if (GetShortPathNameW(pathToShorten, buffer, buffer.Capacity) == 0) return fullName;
|
||||||
|
return Path.Combine(buffer.ToString(), pathToKeep);
|
||||||
|
}
|
||||||
|
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
|
||||||
|
private static extern int GetShortPathNameW(string lpszLongPath, StringBuilder lpszShortPath, int cchBuffer);
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user