MessageSystem.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using UnityEngine.Assertions;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using UnityEditor.IMGUI.Controls;
  8. namespace AssetBundleBrowser
  9. {
  10. internal class MessageSystem
  11. {
  12. private static Texture2D m_ErrorIcon = null;
  13. private static Texture2D m_WarningIcon = null;
  14. private static Texture2D m_InfoIcon = null;
  15. private static Dictionary<MessageFlag, Message> m_MessageLookup = null;
  16. [Flags]
  17. internal enum MessageFlag
  18. {
  19. None = 0x0,
  20. Info = 0x80, //this flag is only used to check bits, not set.
  21. EmptyBundle = 0x81,
  22. EmptyFolder = 0x82,
  23. Warning = 0x8000, //this flag is only used to check bits, not set.
  24. WarningInChildren = 0x8100,
  25. AssetsDuplicatedInMultBundles = 0x8200,
  26. VariantBundleMismatch = 0x8400,
  27. Error = 0x800000, //this flag is only used to check bits, not set.
  28. ErrorInChildren = 0x810000,
  29. SceneBundleConflict = 0x820000,
  30. DependencySceneConflict = 0x840000,
  31. }
  32. internal class MessageState
  33. {
  34. //I have an enum and a set of enums to make some logic cleaner.
  35. // The enum has masks for Error/Warning/Info that won't ever be in the set
  36. // this allows for easy checking of IsSet for error rather than specific errors.
  37. private MessageFlag m_MessageFlags;
  38. private HashSet<MessageFlag> m_MessageSet;
  39. internal MessageState()
  40. {
  41. m_MessageFlags = MessageFlag.None;
  42. m_MessageSet = new HashSet<MessageFlag>();
  43. }
  44. internal void Clear()
  45. {
  46. m_MessageFlags = MessageFlag.None;
  47. m_MessageSet.Clear();
  48. }
  49. internal void SetFlag(MessageFlag flag, bool on)
  50. {
  51. if (flag == MessageFlag.Info || flag == MessageFlag.Warning || flag == MessageFlag.Error)
  52. return;
  53. if (on)
  54. {
  55. m_MessageFlags |= flag;
  56. m_MessageSet.Add(flag);
  57. }
  58. else
  59. {
  60. m_MessageFlags &= ~flag;
  61. m_MessageSet.Remove(flag);
  62. }
  63. }
  64. internal bool IsSet(MessageFlag flag)
  65. {
  66. return (m_MessageFlags & flag) == flag;
  67. }
  68. internal bool HasMessages()
  69. {
  70. return (m_MessageFlags != MessageFlag.None);
  71. }
  72. internal MessageType HighestMessageLevel()
  73. {
  74. if (IsSet(MessageFlag.Error))
  75. return MessageType.Error;
  76. if (IsSet(MessageFlag.Warning))
  77. return MessageType.Warning;
  78. if (IsSet(MessageFlag.Info))
  79. return MessageType.Info;
  80. return MessageType.None;
  81. }
  82. internal MessageFlag HighestMessageFlag()
  83. {
  84. MessageFlag high = MessageFlag.None;
  85. foreach(var f in m_MessageSet)
  86. {
  87. if (f > high)
  88. high = f;
  89. }
  90. return high;
  91. }
  92. internal List<Message> GetMessages()
  93. {
  94. var msgs = new List<Message>();
  95. foreach(var f in m_MessageSet)
  96. {
  97. msgs.Add(GetMessage(f));
  98. }
  99. return msgs;
  100. }
  101. }
  102. internal static Texture2D GetIcon(MessageType sev)
  103. {
  104. if (sev == MessageType.Error)
  105. return GetErrorIcon();
  106. else if (sev == MessageType.Warning)
  107. return GetWarningIcon();
  108. else if (sev == MessageType.Info)
  109. return GetInfoIcon();
  110. else
  111. return null;
  112. }
  113. private static Texture2D GetErrorIcon()
  114. {
  115. if (m_ErrorIcon == null)
  116. FindMessageIcons();
  117. return m_ErrorIcon;
  118. }
  119. private static Texture2D GetWarningIcon()
  120. {
  121. if (m_WarningIcon == null)
  122. FindMessageIcons();
  123. return m_WarningIcon;
  124. }
  125. private static Texture2D GetInfoIcon()
  126. {
  127. if (m_InfoIcon == null)
  128. FindMessageIcons();
  129. return m_InfoIcon;
  130. }
  131. private static void FindMessageIcons()
  132. {
  133. m_ErrorIcon = EditorGUIUtility.FindTexture("console.errorIcon");
  134. m_WarningIcon = EditorGUIUtility.FindTexture("console.warnicon");
  135. m_InfoIcon = EditorGUIUtility.FindTexture("console.infoIcon");
  136. }
  137. internal class Message
  138. {
  139. internal Message(string msg, MessageType sev)
  140. {
  141. message = msg;
  142. severity = sev;
  143. }
  144. internal MessageType severity;
  145. internal string message;
  146. internal Texture2D icon
  147. {
  148. get
  149. {
  150. return GetIcon(severity);
  151. }
  152. }
  153. }
  154. internal static Message GetMessage(MessageFlag lookup)
  155. {
  156. if (m_MessageLookup == null)
  157. InitMessages();
  158. Message msg = null;
  159. m_MessageLookup.TryGetValue(lookup, out msg);
  160. if (msg == null)
  161. msg = m_MessageLookup[MessageFlag.None];
  162. return msg;
  163. }
  164. private static void InitMessages()
  165. {
  166. m_MessageLookup = new Dictionary<MessageFlag, Message>();
  167. m_MessageLookup.Add(MessageFlag.None, new Message(string.Empty, MessageType.None));
  168. m_MessageLookup.Add(MessageFlag.EmptyBundle, new Message("This bundle is empty. Empty bundles cannot get saved with the scene and will disappear from this list if Unity restarts or if various other bundle rename or move events occur.", MessageType.Info));
  169. m_MessageLookup.Add(MessageFlag.EmptyFolder, new Message("This folder is either empty or contains only empty children. Empty bundles cannot get saved with the scene and will disappear from this list if Unity restarts or if various other bundle rename or move events occur.", MessageType.Info));
  170. m_MessageLookup.Add(MessageFlag.WarningInChildren, new Message("Warning in child(ren)", MessageType.Warning));
  171. m_MessageLookup.Add(MessageFlag.AssetsDuplicatedInMultBundles, new Message("Assets being pulled into this bundle due to dependencies are also being pulled into another bundle. This will cause duplication in memory", MessageType.Warning));
  172. m_MessageLookup.Add(MessageFlag.VariantBundleMismatch, new Message("Variants of a given bundle must have exactly the same assets between them based on a Name.Extension (without Path) comparison. These bundle variants fail that check.", MessageType.Warning));
  173. m_MessageLookup.Add(MessageFlag.ErrorInChildren, new Message("Error in child(ren)", MessageType.Error));
  174. m_MessageLookup.Add(MessageFlag.SceneBundleConflict, new Message("A bundle with one or more scenes must only contain scenes. This bundle has scenes and non-scene assets.", MessageType.Error));
  175. m_MessageLookup.Add(MessageFlag.DependencySceneConflict, new Message("The folder added to this bundle has pulled in scenes and non-scene assets. A bundle must only have one type or the other.", MessageType.Error));
  176. }
  177. }
  178. }