using System.Diagnostics; using System.IO; using System.IO.Compression; using System.Reflection; using System.Text.Json; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; using XFEToolBox.Core.Tools; namespace BilibiliDanmaku.Validation; internal static partial class Program { private static async Task Package() { GenerateIcon(); var service = typeof(XFEToolBox.Client.Utilities.QuickToolsBridge).Assembly.GetType("XFEToolBox.Client.Utilities.ToolProjectRunService", true)!; var options = new JsonSerializerOptions(JsonSerializerDefaults.Web); var manifest = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(Workspace, "manifest.json")), options)!; string package = Path.Combine(root, manifest.Id + "-" + manifest.Version + ".xfetool"); await Test("Production host compiles real tool workspace", async () => { var task = (Task)service.GetMethod("BuildAsync")!.Invoke(null, [Workspace, manifest, CancellationToken.None, null])!; await task.WaitAsync(TimeSpan.FromMinutes(4)); EnsureResult(task); }); await Test("Package ZIP validation and imported source compilation", async () => { ZipFile.CreateFromDirectory(Workspace, package, CompressionLevel.Optimal, false); string unpacked = Path.Combine(root, "unpacked"); Directory.CreateDirectory(unpacked); var extract = (Task)service.GetMethod("ExtractAndValidatePackageAsync", BindingFlags.NonPublic | BindingFlags.Static)!.Invoke(null, [package, unpacked, manifest.Id, manifest.Version, CancellationToken.None])!; var validated = await extract; Check(File.Exists(Path.Combine(unpacked, validated.Icon!)), "Icon not packaged"); var build = (Task)service.GetMethod("BuildAsync")!.Invoke(null, [unpacked, validated, CancellationToken.None, null])!; await build.WaitAsync(TimeSpan.FromMinutes(4)); EnsureResult(build); }); await Test("Independent tool runtime initializes, queries, filters and selects without binding errors", async () => { string fixture = Path.Combine(root, "runtime-workspace"); Directory.CreateDirectory(fixture); foreach (var path in Directory.GetFiles(Workspace, "*", SearchOption.AllDirectories)) { string target = Path.Combine(fixture, Path.GetRelativePath(Workspace, path)); Directory.CreateDirectory(Path.GetDirectoryName(target)!); File.Copy(path, target); } foreach (var path in Directory.GetFiles(Path.Combine(AppContext.BaseDirectory, "RuntimeFixture"))) File.Copy(path, Path.Combine(fixture, Path.GetFileName(path)), true); var runtimeManifest = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(fixture, "manifest.json")), options)!; string marker = Path.Combine(root, "runtime-result.json"); string? previous = Environment.GetEnvironmentVariable("XFE_DANMAKU_SMOKE_RESULT"); Process? process = null; try { Environment.SetEnvironmentVariable("XFE_DANMAKU_SMOKE_RESULT", marker); var task = (Task)service.GetMethod("BuildAndRunAsync")!.Invoke(null, [fixture, runtimeManifest, CancellationToken.None, null])!; await task.WaitAsync(TimeSpan.FromMinutes(4)); object result = EnsureResult(task); process = Process.GetProcessById((int)result.GetType().GetProperty("ProcessId")!.GetValue(result)!); await process.WaitForExitAsync().WaitAsync(TimeSpan.FromSeconds(30)); Equal(0, process.ExitCode); using var json = JsonDocument.Parse(File.ReadAllText(marker)); Check(json.RootElement.GetProperty("success").GetBoolean(), json.RootElement.ToString()); } finally { Environment.SetEnvironmentVariable("XFE_DANMAKU_SMOKE_RESULT", previous); if (process is not null) { if (!process.HasExited) { process.Kill(); await process.WaitForExitAsync(); } process.Dispose(); } } }); } private static object EnsureResult(Task task) { object result = task.GetType().GetProperty("Result")!.GetValue(task)!; Check((bool)result.GetType().GetProperty("Success")!.GetValue(result)!, (string)result.GetType().GetProperty("Message")!.GetValue(result)!); return result; } private static void GenerateIcon() { var visual = new DrawingVisual(); using (var dc = visual.RenderOpen()) { dc.DrawRoundedRectangle(new SolidColorBrush(Color.FromArgb(25, 30, 25, 80)), null, new Rect(26, 36, 200, 190), 42, 42); dc.DrawRoundedRectangle(new LinearGradientBrush(Color.FromRgb(254, 151, 186), Color.FromRgb(218, 81, 154), 70), new Pen(new SolidColorBrush(Color.FromArgb(110,255,255,255)),2), new Rect(20, 20, 204, 188), 42, 42); dc.DrawRoundedRectangle(new LinearGradientBrush(Color.FromArgb(120,255,255,255), Color.FromArgb(0,255,255,255), 90), null, new Rect(30, 28, 184, 92), 32,32); var white = new SolidColorBrush(Color.FromRgb(255,249,255)); dc.DrawRoundedRectangle(white, null, new Rect(52, 65, 141, 97), 20, 20); dc.DrawGeometry(white, null, Geometry.Parse("M 66,151 L 66,181 L 103,157 Z")); var pink = new Pen(new SolidColorBrush(Color.FromRgb(214,91,158)),8) { StartLineCap = PenLineCap.Round, EndLineCap = PenLineCap.Round }; dc.DrawLine(pink, new Point(76,95), new Point(165,95)); dc.DrawLine(pink, new Point(76,120), new Point(134,120)); var lens = new Pen(new LinearGradientBrush(Color.FromRgb(129,196,255), Color.FromRgb(55,87,203), 70), 13) { StartLineCap = PenLineCap.Round, EndLineCap = PenLineCap.Round }; dc.DrawEllipse(new SolidColorBrush(Color.FromArgb(235,243,250,255)), lens, new Point(183,171), 31,31); dc.DrawLine(lens, new Point(205,196), new Point(230,223)); } var bitmap = new RenderTargetBitmap(256,256,96,96,PixelFormats.Pbgra32); bitmap.Render(visual); var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); Directory.CreateDirectory(Path.Combine(Workspace,"Assets")); using var output = File.Create(Path.Combine(Workspace,"Assets","icon.png")); encoder.Save(output); } }