using System.Text.Json;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Runtime.InteropServices.WindowsRuntime;
using LogiBoardToolBox.ViewModels;
using LogiBoardToolBox.Views;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Windows.Graphics.Imaging;
using Windows.Storage.Streams;
namespace LogiBoardToolBox.Utilities;
/// Opt-in, read-only hardware UI verification. Captures this app's visual tree, never the desktop.
internal static partial class UiVerification
{
private static readonly string[] args = Environment.GetCommandLineArgs();
public static bool IsInputEditRequested => args.Contains("--input-verify");
public static bool IsCatalogRequested => args.Contains("--catalog-verify");
public static bool IsRequested => args.Contains("--ui-verify") || IsInputEditRequested || IsCatalogRequested;
public static bool HardwareWritesEnabled { get; private set; }
public static string OutputDirectory { get { var index = Array.IndexOf(args, IsCatalogRequested ? "--catalog-verify" : IsInputEditRequested ? "--input-verify" : "--ui-verify"); return Path.GetFullPath(index >= 0 && index + 1 < args.Length ? args[index + 1] : "ui-verification"); } }
public static string SettingsDirectory => Path.Combine(OutputDirectory, "isolated-settings", Environment.ProcessId.ToString());
public static int ViewportWidth => args.Contains("--compact-viewport") ? 1280 : args.Contains("--workspace-review") ? 1600 : 3840;
public static int ViewportHeight => args.Contains("--compact-viewport") ? 720 : args.Contains("--workspace-review") ? 900 : 2088;
private static bool running;
private static async Task GetBuildHashAsync()
{
var assemblyPath = RuntimeFeature.IsDynamicCodeSupported ? typeof(UiVerification).Assembly.Location : "";
var path = assemblyPath.Length > 0 ? assemblyPath : Environment.ProcessPath ?? throw new IOException("找不到程序文件。");
await using var file = File.OpenRead(path);
return Convert.ToHexString(await SHA256.HashDataAsync(file));
}
public static async Task RunAsync(MainPage page, Controls.WorkspaceHost tabs)
{
if (running) return; running = true;
var at = Array.IndexOf(args, "--ui-verify");
var folder = Path.GetFullPath(at + 1 < args.Length ? args[at + 1] : "ui-verification"); Directory.CreateDirectory(folder);
var checks = new List