AddonDetector.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace Chronos
  8. {
  9. class AddonDetector : AssetPostprocessor
  10. {
  11. const string PluginName = "Chronos";
  12. const string DefinePrefix = "CHRONOS_";
  13. static readonly Addon[] addons =
  14. {
  15. new Addon()
  16. {
  17. name = "PlayMaker",
  18. define = "PLAYMAKER",
  19. filePattern = "PlayMakerMainMenu.cs"
  20. }
  21. };
  22. static void CheckForAddons(bool display)
  23. {
  24. int foundCount = 0;
  25. foreach (Addon addon in addons)
  26. {
  27. if (addon.Check(display))
  28. {
  29. foundCount++;
  30. }
  31. }
  32. if (display)
  33. {
  34. Debug.LogFormat("{0}: Addon check complete. {1} / {2} addons found.\n", PluginName, foundCount, addons.Length);
  35. }
  36. }
  37. // Automatic check for addons
  38. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
  39. {
  40. CheckForAddons(false);
  41. }
  42. [MenuItem("Assets/" + PluginName + "/Check for addons...")]
  43. public static void ManualCheckForAddons()
  44. {
  45. CheckForAddons(true);
  46. }
  47. public class Addon
  48. {
  49. public string name { get; set; }
  50. public string define { get; set; }
  51. public string filePattern { get; set; }
  52. public bool Check(bool display)
  53. {
  54. if (Directory.GetFiles(Application.dataPath, filePattern, SearchOption.AllDirectories).Any())
  55. {
  56. bool added = AddDefine();
  57. if (added)
  58. {
  59. Debug.LogFormat("{0}: Enabled {1} addon.\n", PluginName, name);
  60. }
  61. else if (display)
  62. {
  63. Debug.LogFormat("{0}: {1} addon is enabled.\n", PluginName, name);
  64. }
  65. return true;
  66. }
  67. else
  68. {
  69. bool removed = RemoveDefine();
  70. if (removed)
  71. {
  72. Debug.LogFormat("{0}: Disabled {1} addon.\n", PluginName, name);
  73. }
  74. else if (display)
  75. {
  76. Debug.LogFormat("{0}: {1} addon is disabled.\n", PluginName, name);
  77. }
  78. return false;
  79. }
  80. }
  81. // Adapted from Demigiant's awesome voodoo.
  82. // He makes some rad stuff, have a look! http://demigiant.com/
  83. public bool AddDefine()
  84. {
  85. bool added = false;
  86. string define = DefinePrefix + this.define;
  87. foreach (BuildTargetGroup group in Enum.GetValues(typeof(BuildTargetGroup)))
  88. {
  89. if (group == BuildTargetGroup.Unknown)
  90. {
  91. continue;
  92. }
  93. if (typeof(BuildTargetGroup).GetField(group.ToString()).IsDefined(typeof(ObsoleteAttribute), true))
  94. {
  95. continue;
  96. }
  97. List<string> defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';').Select(d => d.Trim()).ToList();
  98. if (!defines.Contains(define))
  99. {
  100. added = true;
  101. defines.Add(define);
  102. PlayerSettings.SetScriptingDefineSymbolsForGroup(group, string.Join(";", defines.ToArray()));
  103. }
  104. }
  105. return added;
  106. }
  107. public bool RemoveDefine()
  108. {
  109. bool removed = false;
  110. string define = DefinePrefix + this.define;
  111. foreach (BuildTargetGroup group in Enum.GetValues(typeof(BuildTargetGroup)))
  112. {
  113. if (group == BuildTargetGroup.Unknown)
  114. {
  115. continue;
  116. }
  117. if (typeof(BuildTargetGroup).GetField(group.ToString()).IsDefined(typeof(ObsoleteAttribute), true))
  118. {
  119. continue;
  120. }
  121. List<string> defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';').Select(d => d.Trim()).ToList();
  122. if (defines.Contains(define))
  123. {
  124. removed = true;
  125. defines.Remove(define);
  126. PlayerSettings.SetScriptingDefineSymbolsForGroup(group, string.Join(";", defines.ToArray()));
  127. }
  128. }
  129. return removed;
  130. }
  131. }
  132. }
  133. }