using System.IO; using UnityEditor; using UnityEditor.Build; using UnityEditor.Build.Reporting; using PackageInfo = UnityEditor.PackageManager.PackageInfo; namespace GUPS.Obfuscator { // The vendor DLL uses fixed project paths for writable settings and build data. public static class GupsProjectSettings { public const string SettingsPath = "Assets/GUPS/Obfuscator/Settings/Obfuscator_Settings.json"; private const string TemplatePath = "ThirdParty/GUPS/Obfuscator/Settings/Obfuscator_Settings.json"; public static void EnsureInitialized() { foreach (string folder in new[] { "Settings", "Cache", "Log", "Temp" }) EnsureFolder("Assets/GUPS/Obfuscator/" + folder); // Preserve project-specific changes, including the obfuscation enable flag. if (File.Exists(SettingsPath)) { if (!File.Exists(SettingsPath + ".meta")) AssetDatabase.ImportAsset(SettingsPath, ImportAssetOptions.ForceSynchronousImport); return; } var package = PackageInfo.FindForAssembly(typeof(GupsProjectSettings).Assembly); string template = package?.assetPath + "/" + TemplatePath; if (package == null || AssetDatabase.LoadAssetAtPath(template) == null) throw new BuildFailedException("AppRuntimes GUPS default settings could not be loaded: " + template); if (!AssetDatabase.CopyAsset(template, SettingsPath)) throw new BuildFailedException("Could not initialize GUPS project settings: " + SettingsPath); } private static void EnsureFolder(string path) { if (path == "Assets") return; EnsureFolder(Path.GetDirectoryName(path).Replace('\\', '/')); if (AssetDatabase.IsValidFolder(path) && File.Exists(path + ".meta")) return; // A previous failed build may have left a directory without its metadata. if (Directory.Exists(path)) AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport); else AssetDatabase.CreateFolder(Path.GetDirectoryName(path).Replace('\\', '/'), Path.GetFileName(path)); if (!AssetDatabase.IsValidFolder(path) || !File.Exists(path + ".meta")) throw new BuildFailedException("Could not initialize GUPS project folder and metadata: " + path); } } internal sealed class GupsSettingsInitializer : AssetPostprocessor { private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload) { if (didDomainReload) GupsProjectSettings.EnsureInitialized(); } } internal sealed class GupsSettingsBuildPreparation : IPreprocessBuildWithReport { public int callbackOrder => int.MinValue; public void OnPreprocessBuild(BuildReport report) => GupsProjectSettings.EnsureInitialized(); } }