Files
PCL-ISE/tests/Check-FixedTheme.cs
T
Coldsmile_7 e831d0dfe0
Release PCL-ISE / release (push) Canceled after 0s
feat: prepare PCL-ISE 0.1.2 update test release
2026-10-27 23:57:25 +08:00

79 lines
4.9 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Media;
[assembly: System.Runtime.Versioning.TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
// Run from a fresh ThemeTest_* directory. Only that directory's synthetic
// Setup.ini is migrated; no launcher window, registry writes or network calls.
internal static class CheckFixedTheme {
private static int passed;
private static void Assert(bool condition, string description) {
if (!condition) throw new Exception("FAIL: " + description);
passed++;
Console.WriteLine("PASS: " + description);
}
[STAThread]
private static int Main(string[] args) {
try {
string isolatedRoot = AppDomain.CurrentDomain.BaseDirectory;
if (!new DirectoryInfo(isolatedRoot).Name.StartsWith("ThemeTest_", StringComparison.Ordinal))
throw new InvalidOperationException("Run the checker from a fresh ThemeTest_* directory");
string configDirectory = Path.Combine(isolatedRoot, "PCL");
Directory.CreateDirectory(configDirectory);
string configPath = Path.Combine(configDirectory, "Setup.ini");
if (File.Exists(configPath)) throw new InvalidOperationException("Use a fresh test directory");
File.WriteAllText(configPath,
"UiLauncherTheme:14\r\nUiLauncherHue:345\r\nUiLauncherSat:25\r\nUiLauncherDelta:175\r\nUiLauncherLight:40\r\n" +
"UiMusicRandom:False\r\nUiBackgroundColorful:False\r\nUiLauncherTransparent:321\r\nSyntheticThemeTestMarker:keep\r\n");
Assembly launcher = Assembly.LoadFrom(Path.GetFullPath(args[0]));
Type appType = launcher.GetType("PCL.Application", true);
Application app = (Application)Activator.CreateInstance(appType, true);
Assembly core = Assembly.Load("MeloongCore");
core.GetType("MeloongCore.Main", true).GetMethod("Init").Invoke(null, new object[] { "PCL" });
string actualRoot = (string)core.GetType("MeloongCore.Paths", true).GetProperty("Base").GetValue(null, null);
if (!string.Equals(Path.GetFullPath(actualRoot), Path.GetFullPath(isolatedRoot), StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException("Launcher configuration directory is not isolated");
appType.GetMethod("InitializeComponent").Invoke(app, null);
launcher.GetType("PCL.ModBase", true).GetMethod("RunInUi", Type.EmptyTypes).Invoke(null, null);
Type settings = launcher.GetType("PCL.Settings", true);
MethodInfo get = settings.GetMethods().Single(m => m.Name == "Get" && !m.IsGenericMethodDefinition);
Func<string, object> read = key => get.Invoke(null, new object[] { key, null });
Assert((int)read("UiLauncherTheme") == 14, "load a real legacy custom-theme setting from the isolated configuration");
Type theme = launcher.GetType("PCL.ModSecret", true);
MethodInfo migrate = theme.GetMethod("ThemeCheckAll", BindingFlags.NonPublic | BindingFlags.Static);
migrate.Invoke(null, new object[] { false });
Assert((int)read("UiLauncherTheme") == 0, "legacy theme selection migrates to Cat Blue");
string afterMigration = File.ReadAllText(configPath);
string[] legacyKeys = { "UiLauncherTheme:", "UiLauncherHue:", "UiLauncherSat:", "UiLauncherDelta:", "UiLauncherLight:" };
Assert(legacyKeys.All(key => !afterMigration.Contains(key)), "remove only obsolete theme and tuning overrides from Setup.ini");
Assert(!(bool)read("UiMusicRandom") && !(bool)read("UiBackgroundColorful") && (int)read("UiLauncherTransparent") == 321 && afterMigration.Contains("SyntheticThemeTestMarker:keep"), "preserve background, music, opacity and unrelated configuration");
MethodInfo refresh = theme.GetMethod("ThemeRefresh");
refresh.Invoke(null, new object[] { 0 });
Color blue = ((SolidColorBrush)app.Resources["ColorBrush3"]).Color;
Assert(blue.B > blue.R && blue.B > blue.G, "render the default palette with a blue primary color");
foreach (int legacyTheme in new[] { 1, 5, 8, 12, 14 }) {
refresh.Invoke(null, new object[] { legacyTheme });
Assert(((SolidColorBrush)app.Resources["ColorBrush3"]).Color == blue, "legacy theme argument " + legacyTheme + " retains Cat Blue");
}
migrate.Invoke(null, new object[] { true });
Assert(File.ReadAllText(configPath) == afterMigration, "repeated initialization leaves migrated configuration unchanged");
Console.WriteLine(passed + " fixed-theme checks passed. Isolated configuration: " + configPath);
return 0;
} catch (Exception ex) {
Console.Error.WriteLine(ex);
return 1;
}
}
}