| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using System;
- using System.IO;
- using System.Linq;
- using GUPS.Obfuscator;
- using Newtonsoft.Json.Linq;
- using UnityEditor;
- using UnityEngine;
- public static class AppRuntimesGupsValidation
- {
- private static byte[] templateBytes;
- private static string template;
- public static void BeforeBuild()
- {
- var package = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(GupsProjectSettings).Assembly);
- template = Path.Combine(package.resolvedPath, "ThirdParty/GUPS/Obfuscator/Settings/Obfuscator_Settings.json");
- templateBytes = File.ReadAllBytes(template);
- Require(File.Exists(GupsProjectSettings.SettingsPath), "GUPS settings initialized on Editor startup");
- foreach (string folder in new[] { "", "/Obfuscator", "/Obfuscator/Settings", "/Obfuscator/Cache", "/Obfuscator/Log", "/Obfuscator/Temp" })
- Require(AssetDatabase.IsValidFolder("Assets/GUPS" + folder) && File.Exists("Assets/GUPS" + folder + ".meta"),
- "GUPS folder and metadata exist before build: " + folder);
- var before = File.ReadAllBytes(GupsProjectSettings.SettingsPath);
- string guid = AssetDatabase.AssetPathToGUID(GupsProjectSettings.SettingsPath);
- GupsProjectSettings.EnsureInitialized();
- Require(before.SequenceEqual(File.ReadAllBytes(GupsProjectSettings.SettingsPath))
- && guid == AssetDatabase.AssetPathToGUID(GupsProjectSettings.SettingsPath), "Existing GUPS settings and GUID preserved");
- if (!Environment.GetCommandLineArgs().Contains("-appRuntimesPackageOnly")) return;
- Require(templateBytes.SequenceEqual(before), "Fresh project uses bundled GUPS defaults");
- Require(Enabled(), "Obfuscation enabled for clean-project regression build");
- // Verify custom project values survive initialization before restoring the test defaults.
- var custom = JObject.Parse(File.ReadAllText(GupsProjectSettings.SettingsPath));
- custom["SettingElement_Array"].Single(item => (string)item["Key"] == "Global_Enable_Obfuscation")["Value"] = "False";
- File.WriteAllText(GupsProjectSettings.SettingsPath, custom.ToString());
- GupsProjectSettings.EnsureInitialized();
- Require(!Enabled() && guid == AssetDatabase.AssetPathToGUID(GupsProjectSettings.SettingsPath), "Customized GUPS settings are not overwritten");
- File.WriteAllBytes(GupsProjectSettings.SettingsPath, before);
- // Reproduce a first build with no legacy GUPS folders; the early build callback must repair it.
- Require(AssetDatabase.DeleteAsset("Assets/GUPS"), "Remove GUPS workspace in isolated test project");
- Require(!Directory.Exists("Assets/GUPS"), "No GUPS parent directory before build callbacks");
- }
- public static void AfterBuild()
- {
- Require(File.Exists(GupsProjectSettings.SettingsPath + ".meta"), "GUPS settings metadata restored before SDK write");
- Require(File.ReadAllBytes(template).SequenceEqual(templateBytes), "Package GUPS template unchanged by build");
- if (Environment.GetCommandLineArgs().Contains("-appRuntimesPackageOnly"))
- {
- Require(Enabled(), "GUPS obfuscation remains enabled after build");
- Require(Directory.GetFiles("Assets/GUPS/Obfuscator/Log", "*", SearchOption.AllDirectories)
- .Any(path => !path.EndsWith(".meta", StringComparison.OrdinalIgnoreCase)), "GUPS produced a build report");
- }
- }
- private static bool Enabled() => string.Equals((string)JObject.Parse(File.ReadAllText(GupsProjectSettings.SettingsPath))
- ["SettingElement_Array"].Single(item => (string)item["Key"] == "Global_Enable_Obfuscation")["Value"], "True", StringComparison.OrdinalIgnoreCase);
- private static void Require(bool condition, string check)
- {
- if (!condition) throw new Exception("AppRuntimes GUPS validation failed: " + check);
- Debug.Log("APP_RUNTIMES_GUPS_CHECK_PASS " + check);
- }
- }
|