GupsProjectSettings.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEditor.Build;
  4. using UnityEditor.Build.Reporting;
  5. using PackageInfo = UnityEditor.PackageManager.PackageInfo;
  6. namespace GUPS.Obfuscator
  7. {
  8. // The vendor DLL uses fixed project paths for writable settings and build data.
  9. public static class GupsProjectSettings
  10. {
  11. public const string SettingsPath = "Assets/GUPS/Obfuscator/Settings/Obfuscator_Settings.json";
  12. private const string TemplatePath = "ThirdParty/GUPS/Obfuscator/Settings/Obfuscator_Settings.json";
  13. public static void EnsureInitialized()
  14. {
  15. foreach (string folder in new[] { "Settings", "Cache", "Log", "Temp" })
  16. EnsureFolder("Assets/GUPS/Obfuscator/" + folder);
  17. // Preserve project-specific changes, including the obfuscation enable flag.
  18. if (File.Exists(SettingsPath))
  19. {
  20. if (!File.Exists(SettingsPath + ".meta"))
  21. AssetDatabase.ImportAsset(SettingsPath, ImportAssetOptions.ForceSynchronousImport);
  22. return;
  23. }
  24. var package = PackageInfo.FindForAssembly(typeof(GupsProjectSettings).Assembly);
  25. string template = package?.assetPath + "/" + TemplatePath;
  26. if (package == null || AssetDatabase.LoadAssetAtPath<UnityEngine.TextAsset>(template) == null)
  27. throw new BuildFailedException("AppRuntimes GUPS default settings could not be loaded: " + template);
  28. if (!AssetDatabase.CopyAsset(template, SettingsPath))
  29. throw new BuildFailedException("Could not initialize GUPS project settings: " + SettingsPath);
  30. }
  31. private static void EnsureFolder(string path)
  32. {
  33. if (path == "Assets") return;
  34. EnsureFolder(Path.GetDirectoryName(path).Replace('\\', '/'));
  35. if (AssetDatabase.IsValidFolder(path) && File.Exists(path + ".meta")) return;
  36. // A previous failed build may have left a directory without its metadata.
  37. if (Directory.Exists(path))
  38. AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport);
  39. else
  40. AssetDatabase.CreateFolder(Path.GetDirectoryName(path).Replace('\\', '/'), Path.GetFileName(path));
  41. if (!AssetDatabase.IsValidFolder(path) || !File.Exists(path + ".meta"))
  42. throw new BuildFailedException("Could not initialize GUPS project folder and metadata: " + path);
  43. }
  44. }
  45. internal sealed class GupsSettingsInitializer : AssetPostprocessor
  46. {
  47. private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets,
  48. string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload)
  49. {
  50. if (didDomainReload) GupsProjectSettings.EnsureInitialized();
  51. }
  52. }
  53. internal sealed class GupsSettingsBuildPreparation : IPreprocessBuildWithReport
  54. {
  55. public int callbackOrder => int.MinValue;
  56. public void OnPreprocessBuild(BuildReport report) => GupsProjectSettings.EnsureInitialized();
  57. }
  58. }