AUMSSProgramCheck.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using UdonSharp;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace AUMSS.EditorTools
  5. {
  6. /// <summary>
  7. /// Makes sure AUMSS's U# programs are compiled. The package ships them uncompiled, and U# only compiles when a
  8. /// script's source differs from what it last compiled: after an update that changes no script, the programs
  9. /// would stay uncompiled, and editor code (U#'s own, or a world's tools) couldn't write to AUMSS components.
  10. /// So they're checked whenever the editor loads, and whenever the package's program assets are reimported (an
  11. /// update while the editor is open, which reloads nothing if no script changed).
  12. /// </summary>
  13. [InitializeOnLoad]
  14. public static class AUMSSProgramCheck
  15. {
  16. const string PackagePath = "Packages/dev.av2x.aumss/";
  17. static AUMSSProgramCheck()
  18. {
  19. EditorApplication.delayCall += CheckOnLoad;
  20. }
  21. class ImportCheck : AssetPostprocessor
  22. {
  23. static void OnPostprocessAllAssets(string[] imported, string[] deleted, string[] moved, string[] movedFrom)
  24. {
  25. foreach (string path in imported)
  26. {
  27. if (!path.StartsWith(PackagePath) || !path.EndsWith(".asset")) continue;
  28. // U#'s own compile saves these too; by then they're compiled, so the check does nothing
  29. EditorApplication.delayCall -= CheckOnLoad;
  30. EditorApplication.delayCall += CheckOnLoad;
  31. return;
  32. }
  33. }
  34. }
  35. static void CheckOnLoad()
  36. {
  37. if (EditorApplication.isPlayingOrWillChangePlaymode) return;
  38. if (EditorApplication.isCompiling || EditorApplication.isUpdating)
  39. {
  40. EditorApplication.delayCall += CheckOnLoad;
  41. return;
  42. }
  43. // U# can't compile while C# has errors; it gets compiled once they're fixed (a script change, so U# does)
  44. if (EditorUtility.scriptCompilationFailed || !NeedsCompile()) return;
  45. Debug.Log("[AUMSS] Compiling UdonSharp programs: the package's arrived uncompiled.");
  46. UdonSharpProgramAsset.CompileAllCsPrograms(forceCompile: true);
  47. }
  48. /// <summary>True if any of the package's U# programs hasn't been compiled yet.</summary>
  49. public static bool NeedsCompile()
  50. {
  51. foreach (UdonSharpProgramAsset program in UdonSharpProgramAsset.GetAllUdonSharpPrograms())
  52. {
  53. if (program == null || program.sourceCsScript == null) continue;
  54. if (!AssetDatabase.GetAssetPath(program).StartsWith(PackagePath)) continue;
  55. if (program.fieldDefinitions == null) return true;
  56. }
  57. return false;
  58. }
  59. }
  60. }