AppRuntimesGupsValidation.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using GUPS.Obfuscator;
  5. using Newtonsoft.Json.Linq;
  6. using UnityEditor;
  7. using UnityEngine;
  8. public static class AppRuntimesGupsValidation
  9. {
  10. private static byte[] templateBytes;
  11. private static string template;
  12. public static void BeforeBuild()
  13. {
  14. var package = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(GupsProjectSettings).Assembly);
  15. template = Path.Combine(package.resolvedPath, "ThirdParty/GUPS/Obfuscator/Settings/Obfuscator_Settings.json");
  16. templateBytes = File.ReadAllBytes(template);
  17. Require(File.Exists(GupsProjectSettings.SettingsPath), "GUPS settings initialized on Editor startup");
  18. foreach (string folder in new[] { "", "/Obfuscator", "/Obfuscator/Settings", "/Obfuscator/Cache", "/Obfuscator/Log", "/Obfuscator/Temp" })
  19. Require(AssetDatabase.IsValidFolder("Assets/GUPS" + folder) && File.Exists("Assets/GUPS" + folder + ".meta"),
  20. "GUPS folder and metadata exist before build: " + folder);
  21. var before = File.ReadAllBytes(GupsProjectSettings.SettingsPath);
  22. string guid = AssetDatabase.AssetPathToGUID(GupsProjectSettings.SettingsPath);
  23. GupsProjectSettings.EnsureInitialized();
  24. Require(before.SequenceEqual(File.ReadAllBytes(GupsProjectSettings.SettingsPath))
  25. && guid == AssetDatabase.AssetPathToGUID(GupsProjectSettings.SettingsPath), "Existing GUPS settings and GUID preserved");
  26. if (!Environment.GetCommandLineArgs().Contains("-appRuntimesPackageOnly")) return;
  27. Require(templateBytes.SequenceEqual(before), "Fresh project uses bundled GUPS defaults");
  28. Require(Enabled(), "Obfuscation enabled for clean-project regression build");
  29. // Verify custom project values survive initialization before restoring the test defaults.
  30. var custom = JObject.Parse(File.ReadAllText(GupsProjectSettings.SettingsPath));
  31. custom["SettingElement_Array"].Single(item => (string)item["Key"] == "Global_Enable_Obfuscation")["Value"] = "False";
  32. File.WriteAllText(GupsProjectSettings.SettingsPath, custom.ToString());
  33. GupsProjectSettings.EnsureInitialized();
  34. Require(!Enabled() && guid == AssetDatabase.AssetPathToGUID(GupsProjectSettings.SettingsPath), "Customized GUPS settings are not overwritten");
  35. File.WriteAllBytes(GupsProjectSettings.SettingsPath, before);
  36. // Reproduce a first build with no legacy GUPS folders; the early build callback must repair it.
  37. Require(AssetDatabase.DeleteAsset("Assets/GUPS"), "Remove GUPS workspace in isolated test project");
  38. Require(!Directory.Exists("Assets/GUPS"), "No GUPS parent directory before build callbacks");
  39. }
  40. public static void AfterBuild()
  41. {
  42. Require(File.Exists(GupsProjectSettings.SettingsPath + ".meta"), "GUPS settings metadata restored before SDK write");
  43. Require(File.ReadAllBytes(template).SequenceEqual(templateBytes), "Package GUPS template unchanged by build");
  44. if (Environment.GetCommandLineArgs().Contains("-appRuntimesPackageOnly"))
  45. {
  46. Require(Enabled(), "GUPS obfuscation remains enabled after build");
  47. Require(Directory.GetFiles("Assets/GUPS/Obfuscator/Log", "*", SearchOption.AllDirectories)
  48. .Any(path => !path.EndsWith(".meta", StringComparison.OrdinalIgnoreCase)), "GUPS produced a build report");
  49. }
  50. }
  51. private static bool Enabled() => string.Equals((string)JObject.Parse(File.ReadAllText(GupsProjectSettings.SettingsPath))
  52. ["SettingElement_Array"].Single(item => (string)item["Key"] == "Global_Enable_Obfuscation")["Value"], "True", StringComparison.OrdinalIgnoreCase);
  53. private static void Require(bool condition, string check)
  54. {
  55. if (!condition) throw new Exception("AppRuntimes GUPS validation failed: " + check);
  56. Debug.Log("APP_RUNTIMES_GUPS_CHECK_PASS " + check);
  57. }
  58. }