Seven is the number.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
5.5 KiB

3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Photon.Bolt.Editor.Utils;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace Photon.Bolt.Utils
  8. {
  9. public static class MenuUtililies
  10. {
  11. private const string DLL_SUFIX_DEBUG = ".debug";
  12. private const string DLL_SUFIX_RELEASE = ".release";
  13. // ======= PUBLIC METHODS =====================================================================================
  14. [MenuItem("Photon Bolt/Utils/Find Missing Scripts", priority = 125)]
  15. public static void FindMissingScriptsMenu()
  16. {
  17. BoltLog.Info("Searching for Missing Scripts");
  18. if (FindMissingComponents() == 0)
  19. {
  20. BoltLog.Info("Not found any prefab with missing scripts");
  21. }
  22. }
  23. [MenuItem("Photon Bolt/Utils/Remove Missing Scripts", priority = 126)]
  24. public static void RemoveMissingScriptMenu()
  25. {
  26. BoltLog.Info("Removing Missing Scripts");
  27. RemoveMissingScripts();
  28. }
  29. [MenuItem("Photon Bolt/Utils/Change DLL Mode", priority = 127)]
  30. public static void ChangeDllModeMenu()
  31. {
  32. var current = BoltNetwork.IsDebugMode ? "Debug" : "Release";
  33. var target = !BoltNetwork.IsDebugMode ? "Debug" : "Release";
  34. var msg = string.Format("Bolt is in {0} mode, want to change to {1}?", current, target);
  35. if (EditorUtility.DisplayDialog("Change Bolt DLL Mode", msg, "Yes", "Cancel"))
  36. {
  37. if (ChangeDllMode())
  38. {
  39. Debug.LogFormat("Bolt Mode swiched to {0}.", target);
  40. }
  41. else
  42. {
  43. Debug.LogError("Error while swithing Bolt Mode, changes were reverted.");
  44. }
  45. }
  46. }
  47. public static bool ChangeDllMode()
  48. {
  49. return SwitchDebugReleaseMode(BoltNetwork.IsDebugMode);
  50. }
  51. // ======= PRIVATE METHODS =====================================================================================
  52. public static int FindMissingComponents()
  53. {
  54. var missingScriptsCount = 0;
  55. var components = new List<Component>();
  56. var folders = new string[] { "Assets" };
  57. var iter = AssetDatabase.FindAssets("t:Prefab", folders).GetEnumerator();
  58. while (iter.MoveNext())
  59. {
  60. var guid = (string)iter.Current;
  61. var path = AssetDatabase.GUIDToAssetPath(guid);
  62. var go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
  63. go.GetComponentsInChildren(true, components);
  64. for (var j = 0; j < components.Count; ++j)
  65. {
  66. if (components[j] == null)
  67. {
  68. ++missingScriptsCount;
  69. BoltLog.Error("Missing script: " + path);
  70. }
  71. }
  72. components.Clear();
  73. }
  74. if (missingScriptsCount != 0)
  75. {
  76. BoltLog.Info("Found {0} Missing Scripts", missingScriptsCount);
  77. }
  78. return missingScriptsCount;
  79. }
  80. public static void RemoveMissingScripts()
  81. {
  82. var folders = new string[] { "Assets" };
  83. var iter = AssetDatabase.FindAssets("t:Prefab", folders).GetEnumerator();
  84. while (iter.MoveNext())
  85. {
  86. var guid = (string)iter.Current;
  87. var path = AssetDatabase.GUIDToAssetPath(guid);
  88. var go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
  89. var result = RemoveFromGO(go);
  90. if (result > 0)
  91. {
  92. BoltLog.Info("Removed scripts from {0}", path);
  93. }
  94. }
  95. }
  96. private static int RemoveFromGO(GameObject go)
  97. {
  98. var result = GameObjectUtility.RemoveMonoBehavioursWithMissingScript(go);
  99. for (int i = 0; i < go.transform.childCount; i++)
  100. {
  101. var child = go.transform.GetChild(i);
  102. result += RemoveFromGO(child.gameObject);
  103. }
  104. return result;
  105. }
  106. private static bool SwitchDebugReleaseMode(bool debug)
  107. {
  108. var from = debug ? DLL_SUFIX_DEBUG : DLL_SUFIX_RELEASE;
  109. var to = debug ? DLL_SUFIX_RELEASE : DLL_SUFIX_DEBUG;
  110. var paths = new string[]
  111. {
  112. BoltPathUtility.BoltDllPath,
  113. BoltPathUtility.BoltCompilerDLLPath,
  114. BoltPathUtility.BoltEditorDLLPath
  115. };
  116. var abort = false;
  117. var backup = "";
  118. foreach (var path in paths)
  119. {
  120. if (abort == true) { break; }
  121. try
  122. {
  123. backup = FileUtils.BackupFile(path);
  124. FileUtils.ExchangeFile(path, from, to);
  125. }
  126. catch (IOException ex)
  127. {
  128. Debug.LogError("Aborting...");
  129. Debug.LogException(ex);
  130. abort = true;
  131. try
  132. {
  133. FileUtils.BackupFile(path, true);
  134. }
  135. catch (Exception ex2)
  136. {
  137. Debug.LogException(ex2);
  138. }
  139. }
  140. finally
  141. {
  142. try
  143. {
  144. FileUtils.DeleteFile(backup);
  145. }
  146. catch (Exception ex)
  147. {
  148. Debug.LogException(ex);
  149. }
  150. backup = "";
  151. }
  152. }
  153. if (abort == false)
  154. {
  155. AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  156. }
  157. return abort == false;
  158. }
  159. private class FileUtils
  160. {
  161. public static void ExchangeFile(string basePath, string fromSuffix, string toSuffix)
  162. {
  163. MoveFile(basePath, basePath + fromSuffix);
  164. MoveFile(basePath + toSuffix, basePath);
  165. }
  166. public static void MoveFile(string from, string to)
  167. {
  168. if (File.Exists(to)) { return; }
  169. if (from.EndsWith(DLL_SUFIX_DEBUG) || from.EndsWith(DLL_SUFIX_RELEASE))
  170. {
  171. DeleteFile(string.Format("{0}.meta", from));
  172. }
  173. Debug.LogFormat("Moving file from {0} to {1}", from, to);
  174. File.Move(from, to);
  175. }
  176. public static string BackupFile(string path, bool restore = false)
  177. {
  178. var backup = string.Format("{0}.backup", path);
  179. if (restore)
  180. {
  181. Debug.LogFormat("Restore backup from file {0}", backup);
  182. File.Copy(backup, path, true);
  183. }
  184. else
  185. {
  186. Debug.LogFormat("Creating backup from file {0}", path);
  187. File.Copy(path, backup, true);
  188. }
  189. return backup;
  190. }
  191. public static void DeleteFile(string path)
  192. {
  193. if (string.IsNullOrEmpty(path) || File.Exists(path) == false) { return; }
  194. Debug.LogFormat("Removing file {0}", path);
  195. File.Delete(path);
  196. }
  197. }
  198. }
  199. }