PlasticApp.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEngine;
  5. using Codice.Client.BaseCommands;
  6. using Codice.Client.Common;
  7. using Codice.Client.Common.FsNodeReaders;
  8. using Codice.Client.Common.Threading;
  9. using Codice.CM.Common;
  10. using Codice.CM.ConfigureHelper;
  11. using Codice.LogWrapper;
  12. using Codice.Utils;
  13. using PlasticGui;
  14. using PlasticPipe.Certificates;
  15. using Unity.PlasticSCM.Editor.Configuration;
  16. using Unity.PlasticSCM.Editor.UI;
  17. using MacUI;
  18. namespace Unity.PlasticSCM.Editor
  19. {
  20. internal static class PlasticApp
  21. {
  22. internal static void InitializeIfNeeded()
  23. {
  24. if (mIsInitialized)
  25. return;
  26. ConfigureLogging();
  27. RegisterExceptionHandlers();
  28. InitLocalization();
  29. ThreadWaiter.Initialize(new UnityThreadWaiterBuilder());
  30. ServicePointConfigurator.ConfigureServicePoint();
  31. CertificateUi.RegisterHandler(new ChannelCertificateUiImpl());
  32. SetupFsWatcher();
  33. EditionManager.Get().DisableCapability(
  34. EnumEditionCapabilities.Extensions);
  35. ClientHandlers.Register();
  36. PlasticGuiConfig.SetConfigFile(
  37. PlasticGuiConfig.UNITY_GUI_CONFIG_FILE);
  38. mIsInitialized = true;
  39. }
  40. internal static void Dispose()
  41. {
  42. UnRegisterExceptionHandlers();
  43. }
  44. static void InitLocalization()
  45. {
  46. string language = null;
  47. try
  48. {
  49. language = ClientConfig.Get().GetLanguage();
  50. }
  51. catch
  52. {
  53. language = string.Empty;
  54. }
  55. Localization.Init(language);
  56. PlasticLocalization.SetLanguage(language);
  57. }
  58. static void ConfigureLogging()
  59. {
  60. try
  61. {
  62. string log4netpath = ToolConfig.GetUnityPlasticLogConfigFile();
  63. if (!File.Exists(log4netpath))
  64. WriteLogConfiguration.For(log4netpath);
  65. XmlConfigurator.Configure(new FileInfo(log4netpath));
  66. }
  67. catch
  68. {
  69. //it failed configuring the logging info; nothing to do.
  70. }
  71. }
  72. static void RegisterExceptionHandlers()
  73. {
  74. AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException;
  75. Application.logMessageReceivedThreaded += HandleLog;
  76. }
  77. static void UnRegisterExceptionHandlers()
  78. {
  79. AppDomain.CurrentDomain.UnhandledException -= HandleUnhandledException;
  80. Application.logMessageReceivedThreaded -= HandleLog;
  81. }
  82. static void HandleUnhandledException(object sender, UnhandledExceptionEventArgs args)
  83. {
  84. Exception ex = (Exception)args.ExceptionObject;
  85. if (IsExitGUIException(ex) ||
  86. !IsPlasticStackTrace(ex.StackTrace))
  87. return;
  88. GUIActionRunner.RunGUIAction(delegate {
  89. ExceptionsHandler.HandleException("HandleUnhandledException", ex);
  90. });
  91. }
  92. static void HandleLog(string logString, string stackTrace, LogType type)
  93. {
  94. if (type != LogType.Exception)
  95. return;
  96. if (!IsPlasticStackTrace(stackTrace))
  97. return;
  98. GUIActionRunner.RunGUIAction(delegate {
  99. mLog.ErrorFormat("[HandleLog] Unexpected error: {0}", logString);
  100. mLog.DebugFormat("Stack trace: {0}", stackTrace);
  101. string message = logString;
  102. if (ExceptionsHandler.DumpStackTrace())
  103. message += Environment.NewLine + stackTrace;
  104. GuiMessage.ShowError(message);
  105. });
  106. }
  107. static void SetupFsWatcher()
  108. {
  109. if (!PlatformIdentifier.IsMac())
  110. return;
  111. WorkspaceWatcherFsNodeReadersCache.Get().SetMacFsWatcherBuilder(
  112. new MacFsWatcherBuilder());
  113. }
  114. static bool IsPlasticStackTrace(string stackTrace)
  115. {
  116. if (stackTrace == null)
  117. return false;
  118. string[] namespaces = new[] {
  119. "Codice.",
  120. "GluonGui.",
  121. "PlasticGui."
  122. };
  123. return namespaces.Any(stackTrace.Contains);
  124. }
  125. static bool IsExitGUIException(Exception ex)
  126. {
  127. return ex is ExitGUIException;
  128. }
  129. static bool mIsInitialized;
  130. static readonly ILog mLog = LogManager.GetLogger("PlasticApp");
  131. }
  132. }