Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PlasticApp.cs 8.3KB

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