using MessagePack; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Text.RegularExpressions; namespace System.Application.Models { [MessagePackObject(keyAsPropertyName: true)] public class ProxyScript { /// /// 脚本文件路径 /// public string? FilePath { get; set; } /// /// 脚本文件名称 /// public string? FileName { get; set; } /// /// name /// public string? Name { get; set; } /// /// namespace /// public string[]? NameSpace { get; set; } /// /// Version /// public string? Version { get; set; } public string? HomepageURL { get; set; } public string? SupportURL { get; set; } public string? DownloadURL { get; set; } public string? UpdateURL { get; set; } /// /// description /// public string? Description { get; set; } /// /// author /// public string? Author { get; set; } /// /// author /// public string[]? Match { get; set; } /// /// include /// public string[]? Include { get; set; } /// /// exclude /// public string[]? Exclude { get; set; } public string[]? Grant { get; set; } public string[]? Require { get; set; } public bool Enable { get; set; } public string? Content { get; set; } const string DescRegex = @"(?<={0})[\s\S]*?(?=\n)"; public static bool TryParse(string path, [NotNullWhen(true)] out ProxyScript? proxyScript) { var content = File.ReadAllText(path); if (!string.IsNullOrEmpty(content)) { var userScript = content.Substring("==UserScript==", "==/UserScript=="); if (!string.IsNullOrEmpty(userScript)) { var script = new ProxyScript { FilePath = path, Content = RemoveComment(content.Replace("", "<\\/script>")).Replace("\t", ""), //Content = content.Replace("", "<\\/script>").Replace(" ", "").Replace("\r", "").Replace("\n", "").Replace("\t", ""), FileName = Path.GetFileName(path), Name = Regex.Match(userScript, string.Format(DescRegex, $"@{nameof(Name)}"), RegexOptions.IgnoreCase).GetValue(s => s.Success == true), NameSpace = Regex.Matches(userScript, string.Format(DescRegex, $"@{nameof(NameSpace)}"), RegexOptions.IgnoreCase).GetValues(s => s.Success == true).ToArray(), Version = Regex.Match(userScript, string.Format(DescRegex, $"@{nameof(Version)}"), RegexOptions.IgnoreCase).GetValue(s => s.Success == true), Description = Regex.Match(userScript, string.Format(DescRegex, $"@{nameof(Description)}"), RegexOptions.IgnoreCase).GetValue(s => s.Success == true), Author = Regex.Match(userScript, string.Format(DescRegex, $"@{nameof(Author)}"), RegexOptions.IgnoreCase).GetValue(s => s.Success == true), HomepageURL = Regex.Match(userScript, string.Format(DescRegex, $"@{nameof(HomepageURL)}"), RegexOptions.IgnoreCase).GetValue(s => s.Success == true), SupportURL = Regex.Match(userScript, string.Format(DescRegex, $"@{nameof(SupportURL)}"), RegexOptions.IgnoreCase).GetValue(s => s.Success == true), DownloadURL = Regex.Match(userScript, string.Format(DescRegex, $"@{nameof(DownloadURL)}"), RegexOptions.IgnoreCase).GetValue(s => s.Success == true), UpdateURL = Regex.Match(userScript, string.Format(DescRegex, $"@{nameof(UpdateURL)}"), RegexOptions.IgnoreCase).GetValue(s => s.Success == true), Exclude = Regex.Matches(userScript, string.Format(DescRegex, $"@{nameof(Exclude)}"), RegexOptions.IgnoreCase).GetValues(s => s.Success == true).ToArray(), Grant = Regex.Matches(userScript, string.Format(DescRegex, $"@{nameof(Grant)}"), RegexOptions.IgnoreCase).GetValues(s => s.Success == true).ToArray(), Require = Regex.Matches(userScript, string.Format(DescRegex, $"@{nameof(Require)}"), RegexOptions.IgnoreCase).GetValues(s => s.Success == true).ToArray() }; var matchs = Regex.Matches(userScript, string.Format(DescRegex, $"@{nameof(Match)}"), RegexOptions.IgnoreCase).GetValues(s => s.Success == true).ToArray(); var Includes = Regex.Matches(userScript, string.Format(DescRegex, $"@{nameof(Include)}"), RegexOptions.IgnoreCase).GetValues(s => s.Success == true).ToArray(); script.Match = matchs.Length == 0 ? Includes : matchs; var enable = Regex.Match(userScript, string.Format(DescRegex, $"@{nameof(Enable)}"), RegexOptions.IgnoreCase).GetValue(s => s.Success == true); script.Enable = bool.TryParse(enable, out var e) && e; proxyScript = script; return true; } } proxyScript = null; return false; } static string RemoveComment(string str) { var r = Regex.Replace(str, "^//.*", "", RegexOptions.Multiline); return Regex.Replace(r, @"^\s*\n", "", RegexOptions.Multiline); } } }