diff --git a/App/CustomVarible.cs b/App/CustomVarible.cs new file mode 100644 index 00000000..cbd05d10 --- /dev/null +++ b/App/CustomVarible.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text.Json.Nodes; +using PCL.Core.IO; + +namespace PCL.Core.App; + +public class CustomVarible +{ + /// + /// 自定义主页变量保存路径。 + /// + public static string VaribleJsonPath { get; } = Path.Combine(FileService.SharedDataPath, "varibles.json"); + + /// + /// 存放所有自定义主页变量的 JSON 对象。 + /// + public static JsonNode? VaribleJson; + + public static Dictionary VaribleDict = new(); + + public static void Set(string key, string value) + { + + } + + public static void Get(string key, string value) + { + + } + + public static void Init() + { + if (!File.Exists(VaribleJsonPath)) + { + File.Create(VaribleJsonPath).Close(); + } + else + { + try + { + VaribleJson = JsonNode.Parse(File.ReadAllText(VaribleJsonPath)); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + } + } +} \ No newline at end of file diff --git a/Utils/Exts/StringExtension.cs b/Utils/Exts/StringExtension.cs index f971117f..df705034 100644 --- a/Utils/Exts/StringExtension.cs +++ b/Utils/Exts/StringExtension.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Numerics; using System.Reflection; +using System.Runtime.CompilerServices; using System.Text.RegularExpressions; namespace PCL.Core.Utils.Exts; @@ -200,8 +201,22 @@ public static List RegexSearch(this string str, Regex regex) /// // ReSharper disable once InconsistentNaming public static bool IsASCII(this string str) + => str.All(c => c < 128); + + public static T ParseToEnum(this string str) where T : struct, Enum { - return str.All(c => c < 128); + if (String.IsNullOrWhiteSpace(str)) + { + return (T)(object)0; + } + else if (int.TryParse(str, out int numericValue)) + { + return (T)(object)numericValue; + } + else + { + return Enum.Parse(str, true); + } } public static bool StartsWithF(this string str, string prefix, bool ignoreCase = false)