暫無描述
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.

PlasticApp.cs 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using Codice.Client.BaseCommands;
  7. using Codice.Client.BaseCommands.EventTracking;
  8. using Codice.Client.Common;
  9. using Codice.Client.Common.Connection;
  10. using Codice.Client.Common.Encryption;
  11. using Codice.Client.Common.EventTracking;
  12. using Codice.Client.Common.FsNodeReaders;
  13. using Codice.Client.Common.FsNodeReaders.Watcher;
  14. using Codice.Client.Common.Threading;
  15. using Codice.CM.Common;
  16. using Codice.CM.ConfigureHelper;
  17. using Codice.LogWrapper;
  18. using Codice.Utils;
  19. using CodiceApp.EventTracking;
  20. using MacUI;
  21. using PlasticGui;
  22. using PlasticGui.EventTracking;
  23. using PlasticGui.WebApi;
  24. using PlasticPipe.Certificates;
  25. using Unity.PlasticSCM.Editor.AssetUtils;
  26. using Unity.PlasticSCM.Editor.Configuration;
  27. using Unity.PlasticSCM.Editor.UI;
  28. namespace Unity.PlasticSCM.Editor
  29. {
  30. internal static class PlasticApp
  31. {
  32. internal static bool IsUnitTesting { get; set; }
  33. internal static void InitializeIfNeeded()
  34. {
  35. if (mIsInitialized)
  36. return;
  37. mIsInitialized = true;
  38. ConfigureLogging();
  39. if (!IsUnitTesting)
  40. GuiMessage.Initialize(new UnityPlasticGuiMessage());
  41. RegisterExceptionHandlers();
  42. InitLocalization();
  43. if (!IsUnitTesting)
  44. ThreadWaiter.Initialize(new UnityThreadWaiterBuilder());
  45. ServicePointConfigurator.ConfigureServicePoint();
  46. CertificateUi.RegisterHandler(new ChannelCertificateUiImpl());
  47. SetupFsWatcher();
  48. EditionManager.Get().DisableCapability(
  49. EnumEditionCapabilities.Extensions);
  50. ClientHandlers.Register();
  51. PlasticGuiConfig.SetConfigFile(
  52. PlasticGuiConfig.UNITY_GUI_CONFIG_FILE);
  53. if (!IsUnitTesting)
  54. {
  55. sEventSenderScheduler = EventTracking.Configure(
  56. (PlasticWebRestApi)PlasticGui.Plastic.WebRestAPI,
  57. ApplicationIdentifier.UnityPackage,
  58. IdentifyEventPlatform.Get());
  59. }
  60. if (sEventSenderScheduler != null)
  61. {
  62. sPingEventLoop = new PingEventLoop();
  63. sPingEventLoop.Start();
  64. sPingEventLoop.SetUnityVersion(Application.unityVersion);
  65. CollabPlugin.GetVersion(pluginVersion => sPingEventLoop.SetPluginVersion(pluginVersion));
  66. }
  67. PlasticMethodExceptionHandling.InitializeAskCredentialsUi(
  68. new CredentialsUiImpl());
  69. ClientEncryptionServiceProvider.SetEncryptionPasswordProvider(
  70. new MissingEncryptionPasswordPromptHandler());
  71. DiffMergeToolConfigFactory.Get().SetDiffMergeToolConfig(
  72. GetDiffMergeToolConfig.ForPlatform());
  73. }
  74. internal static void SetWorkspace(WorkspaceInfo wkInfo)
  75. {
  76. RegisterApplicationFocusHandlers();
  77. RegisterAssemblyReloadHandlers();
  78. if (sEventSenderScheduler == null)
  79. return;
  80. sPingEventLoop.SetWorkspace(wkInfo);
  81. PlasticGui.Plastic.WebRestAPI.SetToken(
  82. CmConnection.Get().BuildWebApiTokenForCloudEditionDefaultUser());
  83. }
  84. internal static void Dispose()
  85. {
  86. mIsInitialized = false;
  87. UnRegisterExceptionHandlers();
  88. UnRegisterApplicationFocusHandlers();
  89. UnRegisterAssemblyReloadHandlers();
  90. if (sEventSenderScheduler != null)
  91. {
  92. sPingEventLoop.Stop();
  93. sEventSenderScheduler.End();
  94. }
  95. WorkspaceInfo wkInfo = FindWorkspace.InfoForApplicationPath(
  96. ApplicationDataPath.Get(), PlasticGui.Plastic.API);
  97. if (wkInfo == null)
  98. return;
  99. WorkspaceFsNodeReaderCachesCleaner.CleanWorkspaceFsNodeReader(wkInfo);
  100. }
  101. static void InitLocalization()
  102. {
  103. string language = null;
  104. try
  105. {
  106. language = ClientConfig.Get().GetLanguage();
  107. }
  108. catch
  109. {
  110. language = string.Empty;
  111. }
  112. Localization.Init(language);
  113. PlasticLocalization.SetLanguage(language);
  114. }
  115. static void ConfigureLogging()
  116. {
  117. try
  118. {
  119. string log4netpath = ToolConfig.GetUnityPlasticLogConfigFile();
  120. if (!File.Exists(log4netpath))
  121. WriteLogConfiguration.For(log4netpath);
  122. XmlConfigurator.Configure(new FileInfo(log4netpath));
  123. }
  124. catch
  125. {
  126. //it failed configuring the logging info; nothing to do.
  127. }
  128. }
  129. static void RegisterExceptionHandlers()
  130. {
  131. AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException;
  132. Application.logMessageReceivedThreaded += HandleLog;
  133. }
  134. static void RegisterApplicationFocusHandlers()
  135. {
  136. EditorWindowFocus.OnApplicationActivated += EnableFsWatcher;
  137. EditorWindowFocus.OnApplicationDeactivated += DisableFsWatcher;
  138. }
  139. static void UnRegisterExceptionHandlers()
  140. {
  141. AppDomain.CurrentDomain.UnhandledException -= HandleUnhandledException;
  142. Application.logMessageReceivedThreaded -= HandleLog;
  143. }
  144. static void UnRegisterApplicationFocusHandlers()
  145. {
  146. EditorWindowFocus.OnApplicationActivated -= EnableFsWatcher;
  147. EditorWindowFocus.OnApplicationDeactivated -= DisableFsWatcher;
  148. }
  149. static void RegisterAssemblyReloadHandlers()
  150. {
  151. AssemblyReloadEvents.beforeAssemblyReload += DisableFsWatcher;
  152. AssemblyReloadEvents.afterAssemblyReload += EnableFsWatcher;
  153. }
  154. static void UnRegisterAssemblyReloadHandlers()
  155. {
  156. AssemblyReloadEvents.beforeAssemblyReload -= DisableFsWatcher;
  157. AssemblyReloadEvents.afterAssemblyReload -= EnableFsWatcher;
  158. }
  159. static void HandleUnhandledException(object sender, UnhandledExceptionEventArgs args)
  160. {
  161. Exception ex = (Exception)args.ExceptionObject;
  162. if (IsExitGUIException(ex) ||
  163. !IsPlasticStackTrace(ex.StackTrace))
  164. throw ex;
  165. GUIActionRunner.RunGUIAction(delegate {
  166. ExceptionsHandler.HandleException("HandleUnhandledException", ex);
  167. });
  168. }
  169. static void HandleLog(string logString, string stackTrace, LogType type)
  170. {
  171. if (type != LogType.Exception)
  172. return;
  173. if (!IsPlasticStackTrace(stackTrace))
  174. return;
  175. GUIActionRunner.RunGUIAction(delegate {
  176. mLog.ErrorFormat("[HandleLog] Unexpected error: {0}", logString);
  177. mLog.DebugFormat("Stack trace: {0}", stackTrace);
  178. string message = logString;
  179. if (ExceptionsHandler.DumpStackTrace())
  180. message += Environment.NewLine + stackTrace;
  181. GuiMessage.ShowError(message);
  182. });
  183. }
  184. static void EnableFsWatcher()
  185. {
  186. MonoFileSystemWatcher.IsEnabled = true;
  187. }
  188. static void DisableFsWatcher()
  189. {
  190. MonoFileSystemWatcher.IsEnabled = false;
  191. }
  192. static void SetupFsWatcher()
  193. {
  194. if (!PlatformIdentifier.IsMac())
  195. return;
  196. WorkspaceWatcherFsNodeReadersCache.Get().SetMacFsWatcherBuilder(
  197. new MacFsWatcherBuilder());
  198. }
  199. static bool IsPlasticStackTrace(string stackTrace)
  200. {
  201. if (stackTrace == null)
  202. return false;
  203. string[] namespaces = new[] {
  204. "Codice.",
  205. "GluonGui.",
  206. "PlasticGui."
  207. };
  208. return namespaces.Any(stackTrace.Contains);
  209. }
  210. static bool IsExitGUIException(Exception ex)
  211. {
  212. return ex is ExitGUIException;
  213. }
  214. static bool mIsInitialized;
  215. static EventSenderScheduler sEventSenderScheduler;
  216. static PingEventLoop sPingEventLoop;
  217. static readonly ILog mLog = LogManager.GetLogger("PlasticApp");
  218. }
  219. }