using System; using System.IO; using System.Linq; using System.Reflection; using System.Security.Cryptography; using Newtonsoft.Json.Linq; using UnityEditor; using UnityEngine; // Run only in the disposable projects created by Tools~/Validate-iOS.ps1. public static class AppRuntimesSettingsValidation { private static readonly string[] Names = { "AppSettings", "FacebookSettings", "settings" }; private const string UniWebViewPath = "Assets/Editor/UniWebView/settings.asset"; public static void VerifyStartup() { var package = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(AppRuntimes).Assembly); var baseline = JArray.Parse(File.ReadAllText("Library/AppRuntimesSettingsBaseline.json")); foreach (var entry in baseline) { string path = (string)entry["path"]; Require(Hash(path) == (string)entry["hash"] && Hash(path + ".meta") == (string)entry["metaHash"], "Startup preserves existing file and GUID: " + path); } foreach (string name in Names) { // Deliberately do not call EnsureDefaultSettings first: test automatic startup creation. var asset = Load(name); Require(asset != null, "Settings available automatically at startup: " + name); string path = AssetDatabase.GetAssetPath(asset); string templatePath = package.assetPath + "/Editor/DefaultSettings/" + name + ".asset"; var template = AssetDatabase.LoadAssetAtPath(templatePath); Require(template != null && template.GetType() == asset.GetType(), "Default template type: " + name); Require(path.StartsWith("Assets/", StringComparison.Ordinal), "Settings are project-owned: " + name); Require(AssetDatabase.AssetPathToGUID(path) != AssetDatabase.AssetPathToGUID(templatePath), "Template and project use independent GUIDs: " + name); if (!baseline.Any(entry => (string)entry["path"] == path)) Require(Json(asset) == Json(template), "Missing settings use package defaults: " + name); var bytes = File.ReadAllBytes(path); string guid = AssetDatabase.AssetPathToGUID(path); EnsureDefaults(); Require(bytes.SequenceEqual(File.ReadAllBytes(path)) && guid == AssetDatabase.AssetPathToGUID(path), "Repeated initialization leaves existing settings unchanged: " + name); } Require(Resources.LoadAll("AppSettings").Length == 1, "Exactly one runtime AppSettings"); Require(Resources.LoadAll("FacebookSettings").Length == 1, "Exactly one runtime FacebookSettings"); } public static void Run() { VerifyStartup(); var package = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(AppRuntimes).Assembly); var templateHashes = Names.ToDictionary(name => name, name => Hash(Path.Combine(package.resolvedPath, "Editor/DefaultSettings/" + name + ".asset"))); // Make every project asset differ from its template, then simulate another startup. foreach (string name in Names) { var asset = Load(name); var serialized = new SerializedObject(asset); if (name == "AppSettings") serialized.FindProperty("AppChannel").stringValue = "validation-custom-channel"; else if (name == "FacebookSettings") { var labels = serialized.FindProperty("appLabels"); labels.arraySize = Math.Max(1, labels.arraySize); labels.GetArrayElementAtIndex(0).stringValue = "validation-custom-label"; } else { var field = serialized.FindProperty("usesCleartextTraffic"); field.boolValue = !field.boolValue; } serialized.ApplyModifiedPropertiesWithoutUndo(); } AssetDatabase.SaveAssets(); var customValues = Names.ToDictionary(name => name, name => Json(Load(name))); var customGuids = Names.ToDictionary(name => name, name => AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(Load(name)))); EnsureDefaults(); foreach (string name in Names) Require(Json(Load(name)) == customValues[name] && AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(Load(name))) == customGuids[name], "Customized settings survive initialization: " + name); // Restore just one missing file at a time without changing the other two. foreach (string missing in Names) { var survivors = Names.Where(name => name != missing).ToDictionary(name => name, name => Json(Load(name))); var survivorGuids = survivors.Keys.ToDictionary(name => name, name => AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(Load(name)))); Require(AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(Load(missing))), "Remove isolated test asset: " + missing); EnsureDefaults(); var template = AssetDatabase.LoadAssetAtPath(package.assetPath + "/Editor/DefaultSettings/" + missing + ".asset"); Require(Json(Load(missing)) == Json(template), "Only missing settings restored from defaults: " + missing); foreach (string survivor in survivors.Keys) Require(Json(Load(survivor)) == survivors[survivor] && AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(Load(survivor))) == survivorGuids[survivor], "Other settings untouched while restoring " + missing + ": " + survivor); } // Respect configurations installed at the previous Resources location. if (!AssetDatabase.IsValidFolder("Assets/AppHelp")) AssetDatabase.CreateFolder("Assets", "AppHelp"); if (!AssetDatabase.IsValidFolder("Assets/AppHelp/Resources")) AssetDatabase.CreateFolder("Assets/AppHelp", "Resources"); const string legacyPath = "Assets/AppHelp/Resources/AppSettings.asset"; string originalPath = AssetDatabase.GetAssetPath(Load("AppSettings")); Require(AssetDatabase.MoveAsset(originalPath, legacyPath) == string.Empty, "Relocate test settings to legacy Resources"); string legacyGuid = AssetDatabase.AssetPathToGUID(legacyPath); EnsureDefaults(); Require(!File.Exists(originalPath) && AssetDatabase.GetAssetPath(Load("AppSettings")) == legacyPath && AssetDatabase.AssetPathToGUID(legacyPath) == legacyGuid, "Existing Resources locations are respected"); Require(AssetDatabase.MoveAsset(legacyPath, originalPath) == string.Empty, "Restore relocated test settings"); foreach (string name in Names) Require(Hash(Path.Combine(package.resolvedPath, "Editor/DefaultSettings/" + name + ".asset")) == templateHashes[name], "Project edits leave package template unchanged: " + name); Debug.Log("APP_RUNTIMES_SETTINGS_VALIDATION_PASS"); } private static ScriptableObject Load(string name) => name == "settings" ? AssetDatabase.LoadAssetAtPath(UniWebViewPath) : Resources.Load(name); private static string Json(ScriptableObject asset) => EditorJsonUtility.ToJson(asset); private static string Hash(string path) { using (var sha = SHA256.Create()) return BitConverter.ToString(sha.ComputeHash(File.ReadAllBytes(path))).Replace("-", string.Empty); } private static void EnsureDefaults() { typeof(AppSettingsWindow).Assembly.GetType("AppHelpProjectSettings", true) .GetMethod("EnsureDefaultSettings", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, null); } private static void Require(bool condition, string check) { if (!condition) throw new Exception("AppRuntimes settings validation failed: " + check); Debug.Log("APP_RUNTIMES_SETTINGS_CHECK_PASS " + check); } }