OptimizationHandler.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor;
  6. namespace Pathfinding {
  7. /**
  8. * Helper for enabling or disabling compiler directives.
  9. * Used only in the editor.
  10. * \astarpro
  11. */
  12. public static class OptimizationHandler {
  13. public class DefineDefinition {
  14. public string name;
  15. public string description;
  16. public bool enabled;
  17. public bool consistent;
  18. }
  19. /** Various build targets that Unity have deprecated.
  20. * There is apparently no way to figute out which these are without hard coding them.
  21. */
  22. static readonly BuildTargetGroup[] deprecatedBuildTargets = new BuildTargetGroup[] {
  23. BuildTargetGroup.Unknown,
  24. #if UNITY_5_4_OR_NEWER
  25. (BuildTargetGroup)16, /* BlackBerry */
  26. #endif
  27. #if UNITY_5_5_OR_NEWER
  28. (BuildTargetGroup)5, /* PS3 */
  29. (BuildTargetGroup)6, /* XBox360 */
  30. (BuildTargetGroup)15, /* WP8 */
  31. #endif
  32. };
  33. static string GetPackageRootDirectory () {
  34. var paths = Directory.GetDirectories($"./Assets/Model/Module/Pathfinding/", "AstarPathfindingProject", SearchOption.AllDirectories);
  35. if (paths.Length > 0) {
  36. return paths[0];
  37. }
  38. Debug.LogError("Could not find AstarPathfindingProject root folder");
  39. return Application.dataPath + "/AstarPathfindingProject";
  40. }
  41. static Dictionary<BuildTargetGroup, List<string> > GetDefineSymbols () {
  42. var result = new Dictionary<BuildTargetGroup, List<string> >();
  43. var buildTypes = System.Enum.GetValues(typeof(BuildTargetGroup)) as int[];
  44. for (int i = 0; i < buildTypes.Length; i++) {
  45. if (deprecatedBuildTargets.Contains((BuildTargetGroup)buildTypes[i])) continue;
  46. string defineString = PlayerSettings.GetScriptingDefineSymbolsForGroup((BuildTargetGroup)buildTypes[i]);
  47. if (defineString == null) continue;
  48. var defines = defineString.Split(';').Select(s => s.Trim()).ToList();
  49. result[(BuildTargetGroup)buildTypes[i]] = defines;
  50. }
  51. return result;
  52. }
  53. static void SetDefineSymbols (Dictionary<BuildTargetGroup, List<string> > symbols) {
  54. foreach (var pair in symbols) {
  55. var defineString = string.Join(";", pair.Value.Distinct().ToArray());
  56. PlayerSettings.SetScriptingDefineSymbolsForGroup(pair.Key, defineString);
  57. }
  58. }
  59. public static void EnableDefine (string name) {
  60. name = name.Trim();
  61. var newSymbols = GetDefineSymbols().ToDictionary(pair => pair.Key, pair => {
  62. pair.Value.Add(name);
  63. return pair.Value;
  64. });
  65. SetDefineSymbols(newSymbols);
  66. }
  67. public static void DisableDefine (string name) {
  68. name = name.Trim();
  69. var newSymbols = GetDefineSymbols().ToDictionary(pair => pair.Key, pair => {
  70. pair.Value.Remove(name);
  71. return pair.Value;
  72. });
  73. SetDefineSymbols(newSymbols);
  74. }
  75. public static void IsDefineEnabled (string name, out bool enabled, out bool consistent) {
  76. name = name.Trim();
  77. int foundEnabled = 0;
  78. int foundDisabled = 0;
  79. foreach (var pair in GetDefineSymbols()) {
  80. if (pair.Value.Contains(name)) {
  81. foundEnabled++;
  82. } else {
  83. foundDisabled++;
  84. }
  85. }
  86. enabled = foundEnabled > foundDisabled;
  87. consistent = (foundEnabled > 0) != (foundDisabled > 0);
  88. }
  89. public static List<DefineDefinition> FindDefines () {
  90. var path = GetPackageRootDirectory()+"/defines.csv";
  91. if (File.Exists(path)) {
  92. // Read a file consisting of lines with the format
  93. // NAME;Description
  94. // Ignore empty lines and lines which do not contain exactly 1 ';'
  95. var definePairs = File.ReadAllLines(path)
  96. .Select(line => line.Trim())
  97. .Where(line => line.Length > 0)
  98. .Select(line => line.Split(';'))
  99. .Where(opts => opts.Length == 2);
  100. return definePairs.Select(opts => {
  101. var def = new DefineDefinition { name = opts[0].Trim(), description = opts[1].Trim() };
  102. IsDefineEnabled(def.name, out def.enabled, out def.consistent);
  103. return def;
  104. }).ToList();
  105. }
  106. Debug.LogError("Could not find file '"+path+"'");
  107. return new List<DefineDefinition>();
  108. }
  109. public static void ApplyDefines (List<DefineDefinition> defines) {
  110. foreach (var define in defines) {
  111. if (define.enabled) {
  112. EnableDefine(define.name);
  113. } else {
  114. DisableDefine(define.name);
  115. }
  116. }
  117. }
  118. }
  119. }