SRDebugEditor.Compiler.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace SRDebugger.Editor
  8. {
  9. public partial class SRDebugEditor
  10. {
  11. /// <summary>
  12. /// Sets compiler define <paramref name="define"/> to be enabled/disabled on all build targets.
  13. /// </summary>
  14. static void SetCompileDefine(string define, bool enabled)
  15. {
  16. foreach (BuildTargetGroup targetGroup in GetAllBuildTargetGroups())
  17. {
  18. // Use hash set to remove duplicates.
  19. List<string> defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup).Split(';').ToList();
  20. bool alreadyExists = false;
  21. for (var i = 0; i < defines.Count; i++)
  22. {
  23. if (string.Equals(define, defines[i], StringComparison.InvariantCultureIgnoreCase))
  24. {
  25. alreadyExists = true;
  26. if (!enabled)
  27. {
  28. defines.RemoveAt(i);
  29. }
  30. }
  31. }
  32. if (!alreadyExists && enabled)
  33. {
  34. defines.Add(define);
  35. }
  36. PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, string.Join(";", defines.ToArray()));
  37. }
  38. }
  39. static void ForceRecompile()
  40. {
  41. AssetDatabase.ImportAsset(SRInternalEditorUtil.GetAssetPath("StompyRobot.SRDebugger.asmdef"), ImportAssetOptions.ForceUpdate);
  42. }
  43. static IEnumerable<BuildTargetGroup> GetAllBuildTargetGroups()
  44. {
  45. Type enumType = typeof(BuildTargetGroup);
  46. string[] names = Enum.GetNames(enumType);
  47. Array values = Enum.GetValues(enumType);
  48. for (var i = 0; i < names.Length; i++)
  49. {
  50. string name = names[i];
  51. BuildTargetGroup value = (BuildTargetGroup)values.GetValue(i);
  52. if (value == BuildTargetGroup.Unknown) continue;
  53. MemberInfo[] member = enumType.GetMember(name);
  54. MemberInfo entry = member.FirstOrDefault(p => p.DeclaringType == enumType);
  55. if (entry == null)
  56. {
  57. Debug.LogErrorFormat(
  58. "[SRDebugger] Unhandled build target: {0}. SRDebugger disabled state may not be applied correctly to this platform.",
  59. name);
  60. continue;
  61. }
  62. if (entry.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length != 0)
  63. {
  64. // obsolete, ignore.
  65. continue;
  66. }
  67. yield return value;
  68. }
  69. }
  70. }
  71. }