diff --git a/src/Export.cs b/src/Export.cs index 7f5194a..745545e 100644 --- a/src/Export.cs +++ b/src/Export.cs @@ -1,4 +1,4 @@ -using System.Diagnostics.CodeAnalysis; + using System.Net; using System.Text; using Microsoft.Win32; @@ -145,10 +145,11 @@ public static class Export { }; } - [SuppressMessage("Interoperability", "CA1416:验证平台兼容性")] + #pragma warning disable CA1416 private static bool CheckSnapScheme() { return (string?)Registry.ClassesRoot.OpenSubKey("snapgenshin")?.GetValue("") == "URL:snapgenshin"; } + #pragma warning restore CA1416 private static string JoinToString(this IEnumerable list, string separator) { return string.Join(separator, list); diff --git a/src/Extensions.cs b/src/Extensions.cs index 39fea95..31f11c1 100644 --- a/src/Extensions.cs +++ b/src/Extensions.cs @@ -1,40 +1,28 @@ -using System.Collections.Specialized; -using System.Diagnostics.CodeAnalysis; -using System.Net; -using System.Security.Cryptography; +using System.Security.Cryptography; using System.Text; -using System.Web; -using Newtonsoft.Json; namespace YaeAchievement; -[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] +// ReSharper disable MemberCanBePrivate.Global public static class Extensions { - // ReSharper disable once InconsistentNaming - private static readonly Lazy aes = new (Aes.Create); // ReSharper disable once InconsistentNaming private static readonly Lazy md5 = new (MD5.Create); // ReSharper disable once InconsistentNaming private static readonly Lazy sha1 = new (SHA1.Create); - // ReSharper disable once InconsistentNaming - private static readonly Lazy defaultClient = new (() => new HttpClient(new HttpClientHandler { - Proxy = GlobalVars.DebugProxy ? new WebProxy("http://127.0.0.1:8888") : null - }) { - DefaultRequestHeaders = {{ "User-Agent", "UnityPlayer/2017.4.30f1 (UnityWebRequest/1.0, libcurl/7.51.0-DEV)" }} - }); - + public static byte[] ToBytes(this string text) { return Encoding.UTF8.GetBytes(text); } - public static string DecodeToString(this byte[] bytes) { - return Encoding.UTF8.GetString(bytes); + // ReSharper disable once InconsistentNaming + public static string MD5Hash(this string text) { + return text.ToBytes().MD5Hash(); } // ReSharper disable once InconsistentNaming - public static string MD5Hash(this string text) { - return md5.Value.ComputeHash(text.ToBytes()).ToHex(); + public static string MD5Hash(this byte[] data) { + return md5.Value.ComputeHash(data).ToHex().ToLower(); } // ReSharper disable once InconsistentNaming @@ -43,55 +31,6 @@ public static class Extensions { return base64 ? bytes.ToBase64() : bytes.ToHex(); } - public static HttpResponseMessage Send(this HttpRequestMessage message, HttpClient? client = null) { - Logger.Trace($"{message.Method} {message.RequestUri?.GetFullPath()}"); - return (client ?? defaultClient.Value).Send(message); // dispose message? - } - - public static T? Send(this HttpRequestMessage message, HttpClient? client = null, Func? onPreDeserialize = null) { - Logger.Trace($"{message.Method} {message.RequestUri?.GetFullPath()}"); - using var response = (client ?? defaultClient.Value).Send(message); - var text = response.Content.ReadAsStringAsync().Result; - if (onPreDeserialize != null) { - text = onPreDeserialize.Invoke(text); - } - return JsonConvert.DeserializeObject(text); - } - - public static string ToQueryString(this NameValueCollection collection, bool escape = true) { - var items = collection.AllKeys - .Select(key => escape ? - $"{HttpUtility.UrlEncode(key)}={HttpUtility.UrlEncode(collection[key])}" : - $"{key}={collection[key]}"); - return string.Join("&", items); - } - - public static string ToQueryString(this IEnumerable> dict, bool escape = true) { - var items = dict - .Select(pair => escape ? - $"{HttpUtility.UrlEncode(pair.Key)}={HttpUtility.UrlEncode(pair.Value.ToString())}" : - $"{pair.Key}={pair.Value}"); - return string.Join("&", items); - } - - public static string ToJsonString(this Dictionary data) where TKey : notnull { - return JsonConvert.SerializeObject(data); - } - - public static string GetFullPath(this Uri uri) { - return $"{uri.Scheme}://{uri.Host}{uri.AbsolutePath}"; - } - - public static IEnumerable> ToKeyValuePairs(this NameValueCollection collection) { - return collection.AllKeys - .Select(key => new KeyValuePair(key!, collection[key] ?? string.Empty)) - .ToArray(); - } - - public static string UrlEncode(this string text) { - return HttpUtility.UrlEncode(text); - } - public static string ToHex(this byte[] bytes) { return Convert.ToHexString(bytes); } @@ -99,8 +38,4 @@ public static class Extensions { public static string ToBase64(this byte[] bytes) { return Convert.ToBase64String(bytes); } - - public static byte[] FromBase64(this string text) { - return Convert.FromBase64String(text); - } -} \ No newline at end of file +} diff --git a/src/GlobalVars.cs b/src/GlobalVars.cs index 2659317..ebcf4ff 100644 --- a/src/GlobalVars.cs +++ b/src/GlobalVars.cs @@ -1,20 +1,20 @@ -using System.Diagnostics.CodeAnalysis; -using System.Reflection; +using System.Reflection; namespace YaeAchievement; -[SuppressMessage("ReSharper", "ConvertToConstant.Global")] -[SuppressMessage("ReSharper", "FieldCanBeMadeReadOnly.Global")] - +// ReSharper disable InconsistentNaming +// ReSharper disable ConvertToConstant.Global +// ReSharper disable FieldCanBeMadeReadOnly.Global #pragma warning disable CA2211 + public static class GlobalVars { public static bool DebugProxy = false; public static bool CheckGamePath = true; public static bool UnexpectedExit = true; public static string GamePath = null!; - public static Logger.Level LogLevel = Logger.Level.Info; public static Version AppVersion = Assembly.GetEntryAssembly()!.GetName().Version!; + public static readonly string AppPath = AppDomain.CurrentDomain.BaseDirectory; public const uint AppVersionCode = 28; public const string AppVersionName = "2.0"; @@ -24,4 +24,3 @@ public static class GlobalVars { public const string ConfigFileName = "YaeAchievement.runtimeconfig.json"; } -#pragma warning restore CA2211 diff --git a/src/Logger.cs b/src/Logger.cs deleted file mode 100644 index 5ee8b90..0000000 --- a/src/Logger.cs +++ /dev/null @@ -1,40 +0,0 @@ -namespace YaeAchievement; - -public static class Logger { - - public enum Level { - Trace, Debug, Info, Warn, Error - } - - public static void Error(string msg) { - Log(msg, Level.Error); - } - - public static void Warn(string msg) { - Log(msg, Level.Warn); - } - - public static void Info(string msg) { - Log(msg, Level.Info); - } - - public static void Debug(string msg) { - Log(msg, Level.Debug); - } - - public static void Trace(string msg) { - Log(msg, Level.Trace); - } - - private static void Log(string msg, Level level) { - if (level >= GlobalVars.LogLevel) { - Console.WriteLine(msg); - } - } - - public static void WriteLog(string msg, Level level = Level.Info) { - if (level >= GlobalVars.LogLevel) { - Console.Write($"{DateTime.Now:MM/dd HH:mm:ss} {level.ToString().ToUpper().PadLeft(5)} : {msg}"); - } - } -} diff --git a/src/Win32/Extensions.cs b/src/Win32/Extensions.cs index 5e9da95..e67c212 100644 --- a/src/Win32/Extensions.cs +++ b/src/Win32/Extensions.cs @@ -6,7 +6,7 @@ namespace YaeAchievement.Win32; public static class Extensions { public static int PrintMsgAndReturnErrCode(this Win32Exception ex, string msg) { - Logger.Error($"{msg}: {ex.Message}"); + Console.WriteLine($"{msg}: {ex.Message}"); AppCenter.TrackCrash(ex, false); return ex.NativeErrorCode; } diff --git a/src/Win32/Native.cs b/src/Win32/Native.cs index 13be673..dd31c55 100644 --- a/src/Win32/Native.cs +++ b/src/Win32/Native.cs @@ -1,13 +1,13 @@ -using System.Diagnostics.CodeAnalysis; -using System.Runtime.InteropServices; +using System.Runtime.InteropServices; using System.Security; namespace YaeAchievement.Win32; -[SuppressMessage("Interoperability", "CA1401:P/Invokes 应该是不可见的")] +#pragma warning disable CA1401, CA2101 public static class Native { [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + public static extern bool CreateProcess( string lpApplicationName, string? lpCommandLine, @@ -38,7 +38,6 @@ public static class Native { public static extern IntPtr GetModuleHandle(string lpModuleName); [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)] - [SuppressMessage("Globalization", "CA2101:指定对 P/Invoke 字符串参数进行封送处理")] public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]