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.

169 lines
3.9 KiB

4 years ago
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using Bolt.Editor;
  7. using UnityEditor;
  8. using UnityEngine;
  9. namespace Bolt.Utils
  10. {
  11. public static class MenuUtililies
  12. {
  13. // ======= PUBLIC METHODS =====================================================================================
  14. [MenuItem("Bolt/Utils/Find Missing Scripts", priority = 25)]
  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("Bolt/Utils/Change DLL Mode", priority = 26)]
  24. public static void ChangeDllModeMenu()
  25. {
  26. var current = BoltNetwork.IsDebugMode ? "Debug" : "Release";
  27. var target = !BoltNetwork.IsDebugMode ? "Debug" : "Release";
  28. var msg = string.Format("Bolt is in {0} mode, want to change to {1}?", current, target);
  29. if (EditorUtility.DisplayDialog("Change Bolt DLL Mode", msg, "Yes", "Cancel"))
  30. {
  31. if (ChangeDllMode())
  32. {
  33. UnityEngine.Debug.LogFormat("Bolt Mode swiched to {0}.", target);
  34. }
  35. else
  36. {
  37. UnityEngine.Debug.LogError("Error while swithing Bolt Mode, changes were reverted.");
  38. }
  39. }
  40. }
  41. public static bool ChangeDllMode()
  42. {
  43. return SwitchDebugReleaseMode(BoltNetwork.IsDebugMode);
  44. }
  45. // ======= PRIVATE METHODS =====================================================================================
  46. public static int FindMissingComponents()
  47. {
  48. int missingScriptsCount = 0;
  49. List<Component> components = new List<Component>();
  50. var folders = new string[] { "Assets" };
  51. var iter = AssetDatabase.FindAssets("t:Prefab", folders).GetEnumerator();
  52. while (iter.MoveNext())
  53. {
  54. var guid = (string) iter.Current;
  55. var path = AssetDatabase.GUIDToAssetPath(guid);
  56. var go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
  57. go.GetComponentsInChildren(true, components);
  58. for (int j = 0; j < components.Count; ++j)
  59. {
  60. if (components[j] == null)
  61. {
  62. ++missingScriptsCount;
  63. BoltLog.Error("Missing script: " + path);
  64. }
  65. }
  66. components.Clear();
  67. }
  68. if (missingScriptsCount != 0)
  69. {
  70. BoltLog.Info("Found {0} Missing Scripts", missingScriptsCount);
  71. }
  72. return missingScriptsCount;
  73. }
  74. private static bool SwitchDebugReleaseMode(bool debug)
  75. {
  76. var from = debug ? ".debug" : ".release";
  77. var to = debug ? ".release" : ".debug";
  78. var paths = new string[]
  79. {
  80. BoltPathUtility.BoltDllPath,
  81. BoltPathUtility.BoltCompilerDLLPath,
  82. BoltPathUtility.BoltEditorDLLPath
  83. };
  84. var abort = false;
  85. var backup = "";
  86. foreach (var path in paths)
  87. {
  88. if (abort == true) { break; }
  89. try
  90. {
  91. backup = FileUtils.BackupFile(path);
  92. FileUtils.ExchangeFile(path, from, to);
  93. }
  94. catch (IOException)
  95. {
  96. FileUtils.BackupFile(path, true);
  97. abort = true;
  98. }
  99. finally
  100. {
  101. FileUtils.DeleteFile(backup);
  102. }
  103. }
  104. if (abort == false)
  105. {
  106. AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  107. }
  108. return abort == false;
  109. }
  110. private class FileUtils
  111. {
  112. public static void ExchangeFile(string basePath, string fromSuffix, string toSuffix)
  113. {
  114. MoveFile(basePath, basePath + fromSuffix);
  115. MoveFile(basePath + toSuffix, basePath);
  116. }
  117. public static void MoveFile(string from, string to)
  118. {
  119. if (File.Exists(to)) { return; }
  120. UnityEngine.Debug.LogFormat("Moving file from {0} to {1}", from, to);
  121. File.Move(from, to);
  122. }
  123. public static string BackupFile(string path, bool restore = false)
  124. {
  125. var backup = path + ".backup";
  126. if (restore)
  127. {
  128. File.Copy(backup, path, true);
  129. }
  130. else
  131. {
  132. File.Copy(path, backup, true);
  133. }
  134. return backup;
  135. }
  136. public static void DeleteFile(string path)
  137. {
  138. if (string.IsNullOrEmpty(path) || File.Exists(path) == false) { return; }
  139. File.Delete(path);
  140. }
  141. }
  142. }
  143. }