|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using UnityEditor; |
| 5 | +using UnityEditor.PackageManager.UI; |
| 6 | + |
| 7 | +[InitializeOnLoad] |
| 8 | +internal static class FixedMathSharpSampleImportSmokeBootstrap |
| 9 | +{ |
| 10 | + private const string ConfigAssetPath = "FixedMathSharpSampleImportSmokeConfig.json"; |
| 11 | + private const string SampleDisplayName = "Demo Scene"; |
| 12 | + private const string ImportedPathKey = "FixedMathSharp.SampleImportSmoke.ImportedPath"; |
| 13 | + private const string ImportAttemptedKey = "FixedMathSharp.SampleImportSmoke.ImportAttempted"; |
| 14 | + |
| 15 | + static FixedMathSharpSampleImportSmokeBootstrap() |
| 16 | + { |
| 17 | + if (!ImportSampleOnce()) |
| 18 | + EditorApplication.delayCall += () => ImportSampleOnce(); |
| 19 | + } |
| 20 | + |
| 21 | + private static bool ImportSampleOnce() |
| 22 | + { |
| 23 | + if (SessionState.GetBool(ImportAttemptedKey, false)) |
| 24 | + return true; |
| 25 | + |
| 26 | + if (!TryLoadConfig(out var config)) |
| 27 | + { |
| 28 | + SessionState.SetBool(ImportAttemptedKey, true); |
| 29 | + return true; |
| 30 | + } |
| 31 | + |
| 32 | + var packageInfo = UnityEditor.PackageManager.PackageInfo.FindForAssetPath($"Packages/{config.packageName}/package.json"); |
| 33 | + if (packageInfo == null) |
| 34 | + { |
| 35 | + UnityEngine.Debug.Log($"FMS_SAMPLE_SMOKE package not ready yet: {config.packageName}"); |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + var sample = Sample.FindByPackage(packageInfo.name, packageInfo.version) |
| 40 | + .FirstOrDefault(candidate => string.Equals(candidate.displayName, SampleDisplayName, StringComparison.Ordinal)); |
| 41 | + if (string.IsNullOrEmpty(sample.displayName)) |
| 42 | + { |
| 43 | + UnityEngine.Debug.LogError($"FMS_SAMPLE_SMOKE sample was not found: {SampleDisplayName}"); |
| 44 | + SessionState.SetBool(ImportAttemptedKey, true); |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + UnityEngine.Debug.Log($"FMS_SAMPLE_SMOKE importing {sample.displayName} from {sample.resolvedPath}"); |
| 49 | + sample.Import(Sample.ImportOptions.OverridePreviousImports); |
| 50 | + SessionState.SetString(ImportedPathKey, sample.importPath); |
| 51 | + SessionState.SetBool(ImportAttemptedKey, true); |
| 52 | + AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); |
| 53 | + UnityEngine.Debug.Log($"FMS_SAMPLE_SMOKE imported sample to {sample.importPath}"); |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + private static bool TryLoadConfig(out SmokeConfig config) |
| 58 | + { |
| 59 | + config = null; |
| 60 | + |
| 61 | + var configPath = Path.Combine(UnityEngine.Application.dataPath, ConfigAssetPath); |
| 62 | + if (!File.Exists(configPath)) |
| 63 | + { |
| 64 | + UnityEngine.Debug.LogError($"FMS_SAMPLE_SMOKE config was not found: {configPath}"); |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + config = UnityEngine.JsonUtility.FromJson<SmokeConfig>(File.ReadAllText(configPath)); |
| 69 | + if (config == null || string.IsNullOrWhiteSpace(config.packageName)) |
| 70 | + { |
| 71 | + UnityEngine.Debug.LogError($"FMS_SAMPLE_SMOKE config must define packageName: {configPath}"); |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + [Serializable] |
| 79 | + private sealed class SmokeConfig |
| 80 | + { |
| 81 | + public string packageName; |
| 82 | + } |
| 83 | +} |
0 commit comments