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

PlasticApp.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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.CM.WorkspaceServer;
  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.Configuration;
  26. using Unity.PlasticSCM.Editor.Tool;
  27. using Unity.PlasticSCM.Editor.UI;
  28. namespace Unity.PlasticSCM.Editor
  29. {
  30. internal static class PlasticApp
  31. {
  32. static PlasticApp()
  33. {
  34. ConfigureLogging();
  35. RegisterDomainUnloadHandler();
  36. mLog = GetLogger("PlasticApp");
  37. }
  38. internal static ILog GetLogger(string name)
  39. {
  40. return LogManager.GetLogger(name);
  41. }
  42. internal static bool HasRunningOperation()
  43. {
  44. if (mWorkspaceWindow != null &&
  45. mWorkspaceWindow.IsOperationInProgress())
  46. return true;
  47. if (mWkInfo == null)
  48. return false;
  49. return TransactionManager.Get().ExistsAnyWorkspaceTransaction(mWkInfo);
  50. }
  51. internal static void InitializeIfNeeded()
  52. {
  53. if (mIsInitialized)
  54. return;
  55. mIsInitialized = true;
  56. mLog.Debug("InitializeIfNeeded");
  57. // Ensures that the Edition Token is initialized from the UVCS installation regardless of if the PlasticWindow is opened
  58. UnityConfigurationChecker.SynchronizeUnityEditionToken();
  59. PlasticInstallPath.LogInstallationInfo();
  60. if (!PlasticPlugin.IsUnitTesting)
  61. GuiMessage.Initialize(new UnityPlasticGuiMessage());
  62. RegisterExceptionHandlers();
  63. RegisterBeforeAssemblyReloadHandler();
  64. RegisterEditorWantsToQuit();
  65. RegisterEditorQuitting();
  66. InitLocalization();
  67. if (!PlasticPlugin.IsUnitTesting)
  68. ThreadWaiter.Initialize(new UnityThreadWaiterBuilder());
  69. ServicePointConfigurator.ConfigureServicePoint();
  70. CertificateUi.RegisterHandler(new ChannelCertificateUiImpl());
  71. SetupFsWatcher();
  72. // The plastic library holds an internal cache of slugs that relies on the file unityorgs.conf.
  73. // This file might contain outdated information or not exist at all, so we need to ensure
  74. // the cloud organizations are loaded and populated to the internal cache during the initialization.
  75. if (!PlasticPlugin.IsUnitTesting)
  76. SetupCloudOrganizations();
  77. EditionManager.Get().DisableCapability(EnumEditionCapabilities.Extensions);
  78. ClientHandlers.Register();
  79. PlasticGuiConfig.SetConfigFile(PlasticGuiConfig.UNITY_GUI_CONFIG_FILE);
  80. if (!PlasticPlugin.IsUnitTesting)
  81. {
  82. mEventSenderScheduler = EventTracking.Configure(
  83. (PlasticWebRestApi)PlasticGui.Plastic.WebRestAPI,
  84. ApplicationIdentifier.UnityPackage,
  85. IdentifyEventPlatform.Get());
  86. }
  87. if (mEventSenderScheduler != null)
  88. {
  89. UVCPackageVersion.AsyncGetVersion();
  90. mPingEventLoop = new PingEventLoop(
  91. BuildGetEventExtraInfoFunction.ForPingEvent());
  92. mPingEventLoop.Start();
  93. }
  94. PlasticMethodExceptionHandling.InitializeAskCredentialsUi(
  95. new CredentialsUiImpl());
  96. ClientEncryptionServiceProvider.SetEncryptionPasswordProvider(
  97. new MissingEncryptionPasswordPromptHandler());
  98. }
  99. internal static void SetWorkspace(WorkspaceInfo wkInfo)
  100. {
  101. mWkInfo = wkInfo;
  102. RegisterApplicationFocusHandlers();
  103. if (mEventSenderScheduler == null)
  104. return;
  105. mPingEventLoop.SetWorkspace(mWkInfo);
  106. PlasticGui.Plastic.WebRestAPI.SetToken(
  107. CmConnection.Get().BuildWebApiTokenForCloudEditionDefaultUser());
  108. }
  109. internal static void RegisterWorkspaceWindow(IWorkspaceWindow workspaceWindow)
  110. {
  111. mWorkspaceWindow = workspaceWindow;
  112. }
  113. internal static void UnRegisterWorkspaceWindow()
  114. {
  115. mWorkspaceWindow = null;
  116. }
  117. internal static void EnableMonoFsWatcherIfNeeded()
  118. {
  119. if (PlatformIdentifier.IsMac())
  120. return;
  121. MonoFileSystemWatcher.IsEnabled = true;
  122. }
  123. internal static void DisableMonoFsWatcherIfNeeded()
  124. {
  125. if (PlatformIdentifier.IsMac())
  126. return;
  127. MonoFileSystemWatcher.IsEnabled = false;
  128. }
  129. internal static void Dispose()
  130. {
  131. if (!mIsInitialized)
  132. return;
  133. try
  134. {
  135. mLog.Debug("Dispose");
  136. UnRegisterExceptionHandlers();
  137. UnRegisterApplicationFocusHandlers();
  138. UnRegisterEditorWantsToQuit();
  139. UnRegisterEditorQuitting();
  140. if (mEventSenderScheduler != null)
  141. {
  142. mPingEventLoop.Stop();
  143. // Launching and forgetting to avoid a timeout when sending events files and no
  144. // network connection is available.
  145. // This will be refactored once a better mechanism to send event is in place
  146. mEventSenderScheduler.EndAndSendEventsAsync();
  147. }
  148. if (mWkInfo == null)
  149. return;
  150. WorkspaceFsNodeReaderCachesCleaner.CleanWorkspaceFsNodeReader(mWkInfo);
  151. }
  152. finally
  153. {
  154. mIsInitialized = false;
  155. }
  156. }
  157. static void RegisterDomainUnloadHandler()
  158. {
  159. AppDomain.CurrentDomain.DomainUnload += AppDomainUnload;
  160. }
  161. static void RegisterEditorWantsToQuit()
  162. {
  163. EditorApplication.wantsToQuit += OnEditorWantsToQuit;
  164. }
  165. static void RegisterEditorQuitting()
  166. {
  167. EditorApplication.quitting += OnEditorQuitting;
  168. }
  169. static void RegisterBeforeAssemblyReloadHandler()
  170. {
  171. AssemblyReloadEvents.beforeAssemblyReload += BeforeAssemblyReload;
  172. }
  173. static void RegisterApplicationFocusHandlers()
  174. {
  175. EditorWindowFocus.OnApplicationActivated += OnApplicationActivated;
  176. EditorWindowFocus.OnApplicationDeactivated += OnApplicationDeactivated;
  177. }
  178. static void RegisterExceptionHandlers()
  179. {
  180. AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException;
  181. Application.logMessageReceivedThreaded += HandleLog;
  182. }
  183. static void UnRegisterDomainUnloadHandler()
  184. {
  185. AppDomain.CurrentDomain.DomainUnload -= AppDomainUnload;
  186. }
  187. static void UnRegisterEditorWantsToQuit()
  188. {
  189. EditorApplication.wantsToQuit -= OnEditorWantsToQuit;
  190. }
  191. static void UnRegisterEditorQuitting()
  192. {
  193. EditorApplication.quitting -= OnEditorQuitting;
  194. }
  195. static void UnRegisterBeforeAssemblyReloadHandler()
  196. {
  197. AssemblyReloadEvents.beforeAssemblyReload -= BeforeAssemblyReload;
  198. }
  199. static void UnRegisterApplicationFocusHandlers()
  200. {
  201. EditorWindowFocus.OnApplicationActivated -= OnApplicationActivated;
  202. EditorWindowFocus.OnApplicationDeactivated -= OnApplicationDeactivated;
  203. }
  204. static void UnRegisterExceptionHandlers()
  205. {
  206. AppDomain.CurrentDomain.UnhandledException -= HandleUnhandledException;
  207. Application.logMessageReceivedThreaded -= HandleLog;
  208. }
  209. static void AppDomainUnload(object sender, EventArgs e)
  210. {
  211. mLog.Debug("AppDomainUnload");
  212. UnRegisterDomainUnloadHandler();
  213. }
  214. static void HandleUnhandledException(object sender, UnhandledExceptionEventArgs args)
  215. {
  216. Exception ex = (Exception)args.ExceptionObject;
  217. if (IsExitGUIException(ex) ||
  218. !IsPlasticStackTrace(ex.StackTrace))
  219. throw ex;
  220. GUIActionRunner.RunGUIAction(delegate {
  221. ExceptionsHandler.HandleException("HandleUnhandledException", ex);
  222. });
  223. }
  224. static void HandleLog(string logString, string stackTrace, LogType type)
  225. {
  226. if (type != LogType.Exception)
  227. return;
  228. if (!IsPlasticStackTrace(stackTrace))
  229. return;
  230. GUIActionRunner.RunGUIAction(delegate {
  231. mLog.ErrorFormat("[HandleLog] Unexpected error: {0}", logString);
  232. mLog.DebugFormat("Stack trace: {0}", stackTrace);
  233. string message = logString;
  234. if (ExceptionsHandler.DumpStackTrace())
  235. message += Environment.NewLine + stackTrace;
  236. GuiMessage.ShowError(message);
  237. });
  238. }
  239. static void OnApplicationActivated()
  240. {
  241. mLog.Debug("OnApplicationActivated");
  242. EnableMonoFsWatcherIfNeeded();
  243. }
  244. static void OnApplicationDeactivated()
  245. {
  246. mLog.Debug("OnApplicationDeactivated");
  247. DisableMonoFsWatcherIfNeeded();
  248. }
  249. static void OnEditorQuitting()
  250. {
  251. mLog.Debug("OnEditorQuitting");
  252. PlasticPlugin.Shutdown();
  253. }
  254. static bool OnEditorWantsToQuit()
  255. {
  256. mLog.Debug("OnEditorWantsToQuit");
  257. if (!HasRunningOperation())
  258. return true;
  259. return GuiMessage.ShowQuestion(
  260. PlasticLocalization.GetString(PlasticLocalization.Name.OperationRunning),
  261. PlasticLocalization.GetString(PlasticLocalization.Name.ConfirmClosingRunningOperation),
  262. PlasticLocalization.GetString(PlasticLocalization.Name.YesButton));
  263. }
  264. static void BeforeAssemblyReload()
  265. {
  266. mLog.Debug("BeforeAssemblyReload");
  267. UnRegisterBeforeAssemblyReloadHandler();
  268. PlasticShutdown.Shutdown();
  269. }
  270. static void InitLocalization()
  271. {
  272. string language = null;
  273. try
  274. {
  275. language = ClientConfig.Get().GetLanguage();
  276. }
  277. catch
  278. {
  279. language = string.Empty;
  280. }
  281. Localization.Init(language);
  282. PlasticLocalization.SetLanguage(language);
  283. }
  284. static void SetupFsWatcher()
  285. {
  286. if (!PlatformIdentifier.IsMac())
  287. return;
  288. WorkspaceWatcherFsNodeReadersCache.Get().SetMacFsWatcherBuilder(
  289. new MacFsWatcherBuilder());
  290. }
  291. static void SetupCloudOrganizations()
  292. {
  293. if (!EditionToken.IsCloudEdition())
  294. {
  295. return;
  296. }
  297. OrganizationsInformation.LoadCloudOrganizationsAsync();
  298. }
  299. static bool IsPlasticStackTrace(string stackTrace)
  300. {
  301. if (stackTrace == null)
  302. return false;
  303. string[] namespaces = new[] {
  304. "Codice.",
  305. "GluonGui.",
  306. "PlasticGui."
  307. };
  308. return namespaces.Any(stackTrace.Contains);
  309. }
  310. static bool IsExitGUIException(Exception ex)
  311. {
  312. return ex is ExitGUIException;
  313. }
  314. static void ConfigureLogging()
  315. {
  316. try
  317. {
  318. string log4netpath = ToolConfig.GetUnityPlasticLogConfigFile();
  319. if (!File.Exists(log4netpath))
  320. WriteLogConfiguration.For(log4netpath);
  321. XmlConfigurator.Configure(new FileInfo(log4netpath));
  322. }
  323. catch
  324. {
  325. //it failed configuring the logging info; nothing to do.
  326. }
  327. }
  328. static bool mIsInitialized;
  329. static IWorkspaceWindow mWorkspaceWindow;
  330. static WorkspaceInfo mWkInfo;
  331. static EventSenderScheduler mEventSenderScheduler;
  332. static PingEventLoop mPingEventLoop;
  333. static ILog mLog;
  334. }
  335. }