No Description
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.

PlasticProjectSettingsProvider.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. using Codice.Client.Commands;
  6. using Codice.Client.Common.EventTracking;
  7. using Codice.Client.Common.FsNodeReaders;
  8. using Codice.Client.Common.Threading;
  9. using Codice.CM.Common;
  10. using Codice.Utils;
  11. using PlasticGui;
  12. using PlasticGui.WorkspaceWindow.PendingChanges;
  13. using Unity.PlasticSCM.Editor.UI;
  14. namespace Unity.PlasticSCM.Editor
  15. {
  16. class PlasticProjectSettingsProvider : SettingsProvider
  17. {
  18. public PlasticProjectSettingsProvider(
  19. string path, SettingsScope scope = SettingsScope.User)
  20. : base(path, scope)
  21. {
  22. label = UnityConstants.PROJECT_SETTINGS_TAB_TITLE;
  23. }
  24. [SettingsProvider]
  25. public static SettingsProvider CreateSettingsProvider()
  26. {
  27. if (CollabPlugin.IsEnabled())
  28. return null;
  29. if (!FindWorkspace.HasWorkspace(ApplicationDataPath.Get()))
  30. return null;
  31. PlasticApp.InitializeIfNeeded();
  32. return new PlasticProjectSettingsProvider(
  33. UnityConstants.PROJECT_SETTINGS_TAB_PATH, SettingsScope.Project)
  34. {
  35. keywords = GetSearchKeywordsFromGUIContentProperties<Styles>()
  36. };
  37. }
  38. public override void OnActivate(
  39. string searchContext,
  40. VisualElement rootElement)
  41. {
  42. IAutoRefreshView autoRefreshView = GetPendingChangesView();
  43. if (autoRefreshView != null)
  44. autoRefreshView.DisableAutoRefresh();
  45. mIsPluginEnabled = PlasticPluginIsEnabledPreference.IsEnabled();
  46. mWkInfo = FindWorkspace.InfoForApplicationPath(
  47. ApplicationDataPath.Get(), PlasticGui.Plastic.API);
  48. CheckFsWatcher(mWkInfo);
  49. mInitialOptions = new PendingChangesOptions();
  50. mInitialOptions.LoadPendingChangesOptions();
  51. SetOptions(mInitialOptions);
  52. }
  53. public override void OnDeactivate()
  54. {
  55. if (mInitialOptions == null)
  56. return;
  57. bool isDialogueDirty = false;
  58. try
  59. {
  60. PendingChangesOptions currentOptions = GetOptions();
  61. isDialogueDirty = IsDirty(currentOptions);
  62. if (!isDialogueDirty)
  63. return;
  64. currentOptions.SavePreferences();
  65. }
  66. finally
  67. {
  68. IAutoRefreshView autoRefreshView = GetPendingChangesView();
  69. if (autoRefreshView != null)
  70. {
  71. autoRefreshView.EnableAutoRefresh();
  72. if (isDialogueDirty)
  73. autoRefreshView.ForceRefresh();
  74. }
  75. }
  76. }
  77. public override void OnGUI(string searchContext)
  78. {
  79. DrawSettingsSection(
  80. DoIsEnabledSetting);
  81. if (!mIsPluginEnabled)
  82. return;
  83. DrawSplitter.ForWidth(UnityConstants.SETTINGS_GUI_WIDTH);
  84. DrawSettingsSection(
  85. DoPendingChangesSettings);
  86. }
  87. void DoIsEnabledSetting()
  88. {
  89. using (new EditorGUILayout.HorizontalScope())
  90. {
  91. string message = PlasticLocalization.GetString(
  92. mIsPluginEnabled ?
  93. PlasticLocalization.Name.UnityVCSIsEnabled :
  94. PlasticLocalization.Name.UnityVCSIsDisabled);
  95. GUILayout.Label(
  96. message,
  97. EditorStyles.boldLabel,
  98. GUILayout.Height(20));
  99. EditorGUILayout.Space(8);
  100. DoIsEnabledButton();
  101. GUILayout.FlexibleSpace();
  102. }
  103. }
  104. void DoIsEnabledButton()
  105. {
  106. if (!GUILayout.Button(PlasticLocalization.GetString(
  107. mIsPluginEnabled ?
  108. PlasticLocalization.Name.DisableButton :
  109. PlasticLocalization.Name.EnableButton),
  110. UnityStyles.ProjectSettings.ToggleOn))
  111. {
  112. return;
  113. }
  114. if (!mIsPluginEnabled)
  115. {
  116. mIsPluginEnabled = true;
  117. TrackFeatureUseEvent.For(
  118. PlasticGui.Plastic.API.GetRepositorySpec(mWkInfo),
  119. TrackFeatureUseEvent.Features.UnityPackage.EnableManually);
  120. PlasticPlugin.Enable();
  121. PlasticPluginIsEnabledPreference.Enable();
  122. return;
  123. }
  124. if (mIsPluginEnabled)
  125. {
  126. mIsPluginEnabled = false;
  127. TrackFeatureUseEvent.For(
  128. PlasticGui.Plastic.API.GetRepositorySpec(mWkInfo),
  129. TrackFeatureUseEvent.Features.UnityPackage.DisableManually);
  130. PlasticPluginIsEnabledPreference.Disable();
  131. CloseWindowIfOpened.Plastic();
  132. PlasticShutdown.Shutdown();
  133. return;
  134. }
  135. }
  136. void DoPendingChangesSettings()
  137. {
  138. DoGeneralSettings();
  139. DoFileChangeSettings();
  140. DoFileVisibililySettings();
  141. DoFileDetectionSetings();
  142. EditorGUILayout.Space(10);
  143. DoFsWatcherMessage(mFSWatcherEnabled);
  144. }
  145. void DoGeneralSettings()
  146. {
  147. GUILayout.Label(
  148. PlasticLocalization.GetString(
  149. PlasticLocalization.Name.ProjectSettingsGeneral),
  150. EditorStyles.boldLabel);
  151. EditorGUILayout.Space(1);
  152. mShowCheckouts = EditorGUILayout.Toggle(Styles.ShowCheckouts, mShowCheckouts);
  153. mAutoRefresh = EditorGUILayout.Toggle(Styles.AutoRefresh, mAutoRefresh);
  154. }
  155. void DoFileChangeSettings()
  156. {
  157. EditorGUILayout.Space(5);
  158. GUILayout.Label(
  159. PlasticLocalization.GetString(
  160. PlasticLocalization.Name.ProjectSettingsFileChange),
  161. EditorStyles.boldLabel);
  162. EditorGUILayout.Space(1);
  163. mShowChangedFiles = EditorGUILayout.Toggle(Styles.ShowChangedFiles, mShowChangedFiles);
  164. mCheckFileContent = EditorGUILayout.Toggle(Styles.CheckFileContent, mCheckFileContent);
  165. }
  166. void DoFileVisibililySettings()
  167. {
  168. EditorGUILayout.Space(5);
  169. GUILayout.Label(
  170. PlasticLocalization.GetString(
  171. PlasticLocalization.Name.ProjectSettingsFileVisibility),
  172. EditorStyles.boldLabel);
  173. EditorGUILayout.Space(1);
  174. mUseChangeLists = EditorGUILayout.Toggle(Styles.UseChangeLists, mUseChangeLists);
  175. mShowPrivateFields = EditorGUILayout.Toggle(Styles.ShowPrivateFields, mShowPrivateFields);
  176. mShowIgnoredFiles = EditorGUILayout.Toggle(Styles.ShowIgnoredFields, mShowIgnoredFiles);
  177. mShowHiddenFiles = EditorGUILayout.Toggle(Styles.ShowHiddenFields, mShowHiddenFiles);
  178. mShowDeletedFiles = EditorGUILayout.Toggle(Styles.ShowDeletedFilesDirs, mShowDeletedFiles);
  179. }
  180. void DoFileDetectionSetings()
  181. {
  182. EditorGUILayout.Space(5);
  183. GUILayout.Label(
  184. PlasticLocalization.GetString(
  185. PlasticLocalization.Name.ProjectSettingsMoveAndRename),
  186. EditorStyles.boldLabel);
  187. EditorGUILayout.Space(1);
  188. mShowMovedFiles = EditorGUILayout.Toggle(Styles.ShowMovedFiles, mShowMovedFiles);
  189. mMatchBinarySameExtension = EditorGUILayout.Toggle(Styles.MatchBinarySameExtension, mMatchBinarySameExtension);
  190. mMatchTextSameExtension = EditorGUILayout.Toggle(Styles.MatchTextSameExtension, mMatchTextSameExtension);
  191. mSimilarityPercent = EditorGUILayout.IntSlider(Styles.SimilarityPercent, mSimilarityPercent, 0, 100);
  192. }
  193. void DoFsWatcherMessage(bool isEnabled)
  194. {
  195. GUIContent message = new GUIContent(
  196. isEnabled ?
  197. GetFsWatcherEnabledMessage() :
  198. GetFsWatcherDisabledMessage(),
  199. isEnabled ?
  200. Images.GetInfoIcon() :
  201. Images.GetWarnIcon());
  202. GUILayout.Label(message, UnityStyles.Dialog.Toggle, GUILayout.Height(32));
  203. GUILayout.Space(-10);
  204. string formattedExplanation = isEnabled ?
  205. GetFsWatcherEnabledExplanation() :
  206. GetFsWatcherDisabledExplanation();
  207. string helpLink = GetHelpLink();
  208. DrawTextBlockWithEndLink.For(
  209. helpLink, formattedExplanation, UnityStyles.Paragraph);
  210. }
  211. void CheckFsWatcher(WorkspaceInfo wkInfo)
  212. {
  213. bool isFileSystemWatcherEnabled = false;
  214. IThreadWaiter waiter = ThreadWaiter.GetWaiter(10);
  215. waiter.Execute(
  216. /*threadOperationDelegate*/
  217. delegate
  218. {
  219. isFileSystemWatcherEnabled =
  220. IsFileSystemWatcherEnabled(wkInfo);
  221. },
  222. /*afterOperationDelegate*/
  223. delegate
  224. {
  225. if (waiter.Exception != null)
  226. return;
  227. mFSWatcherEnabled = isFileSystemWatcherEnabled;
  228. });
  229. }
  230. void SetOptions(PendingChangesOptions options)
  231. {
  232. mShowCheckouts = IsEnabled(
  233. WorkspaceStatusOptions.FindCheckouts, options.WorkspaceStatusOptions);
  234. mAutoRefresh = options.AutoRefresh;
  235. mShowChangedFiles = IsEnabled(
  236. WorkspaceStatusOptions.FindChanged, options.WorkspaceStatusOptions);
  237. mCheckFileContent = options.CheckFileContentForChanged;
  238. mUseChangeLists = options.UseChangeLists;
  239. mShowPrivateFields = IsEnabled(
  240. WorkspaceStatusOptions.FindPrivates, options.WorkspaceStatusOptions);
  241. mShowIgnoredFiles = IsEnabled(
  242. WorkspaceStatusOptions.ShowIgnored, options.WorkspaceStatusOptions);
  243. mShowHiddenFiles = IsEnabled(
  244. WorkspaceStatusOptions.ShowHiddenChanges, options.WorkspaceStatusOptions);
  245. mShowDeletedFiles = IsEnabled(
  246. WorkspaceStatusOptions.FindLocallyDeleted, options.WorkspaceStatusOptions);
  247. mShowMovedFiles = IsEnabled(
  248. WorkspaceStatusOptions.CalculateLocalMoves, options.WorkspaceStatusOptions);
  249. mMatchBinarySameExtension =
  250. options.MovedMatchingOptions.bBinMatchingOnlySameExtension;
  251. mMatchTextSameExtension =
  252. options.MovedMatchingOptions.bTxtMatchingOnlySameExtension;
  253. mSimilarityPercent = (int)((1 - options.MovedMatchingOptions.AllowedChangesPerUnit) * 100f);
  254. }
  255. PendingChangesOptions GetOptions()
  256. {
  257. WorkspaceStatusOptions resultWkStatusOptions =
  258. WorkspaceStatusOptions.None;
  259. if (mShowCheckouts)
  260. {
  261. resultWkStatusOptions |= WorkspaceStatusOptions.FindCheckouts;
  262. resultWkStatusOptions |= WorkspaceStatusOptions.FindReplaced;
  263. resultWkStatusOptions |= WorkspaceStatusOptions.FindCopied;
  264. }
  265. if (mShowChangedFiles)
  266. resultWkStatusOptions |= WorkspaceStatusOptions.FindChanged;
  267. if (mShowPrivateFields)
  268. resultWkStatusOptions |= WorkspaceStatusOptions.FindPrivates;
  269. if (mShowIgnoredFiles)
  270. resultWkStatusOptions |= WorkspaceStatusOptions.ShowIgnored;
  271. if (mShowHiddenFiles)
  272. resultWkStatusOptions |= WorkspaceStatusOptions.ShowHiddenChanges;
  273. if (mShowDeletedFiles)
  274. resultWkStatusOptions |= WorkspaceStatusOptions.FindLocallyDeleted;
  275. if (mShowMovedFiles)
  276. resultWkStatusOptions |= WorkspaceStatusOptions.CalculateLocalMoves;
  277. MovedMatchingOptions matchingOptions = new MovedMatchingOptions();
  278. matchingOptions.AllowedChangesPerUnit =
  279. (100 - mSimilarityPercent) / 100f;
  280. matchingOptions.bBinMatchingOnlySameExtension =
  281. mMatchBinarySameExtension;
  282. matchingOptions.bTxtMatchingOnlySameExtension =
  283. mMatchTextSameExtension;
  284. return new PendingChangesOptions(
  285. resultWkStatusOptions,
  286. matchingOptions,
  287. mUseChangeLists,
  288. true,
  289. false,
  290. mAutoRefresh,
  291. false,
  292. mCheckFileContent,
  293. false);
  294. }
  295. bool IsDirty(PendingChangesOptions currentOptions)
  296. {
  297. return !mInitialOptions.AreSameOptions(currentOptions);
  298. }
  299. static void DrawSettingsSection(Action drawSettings)
  300. {
  301. float originalLabelWidth = EditorGUIUtility.labelWidth;
  302. try
  303. {
  304. EditorGUIUtility.labelWidth = UnityConstants.SETTINGS_GUI_WIDTH;
  305. using (new EditorGUILayout.HorizontalScope())
  306. {
  307. GUILayout.Space(10);
  308. using (new EditorGUILayout.VerticalScope())
  309. {
  310. GUILayout.Space(10);
  311. drawSettings();
  312. GUILayout.Space(10);
  313. }
  314. GUILayout.Space(10);
  315. }
  316. }
  317. finally
  318. {
  319. EditorGUIUtility.labelWidth = originalLabelWidth;
  320. }
  321. }
  322. static IAutoRefreshView GetPendingChangesView()
  323. {
  324. if (!EditorWindow.HasOpenInstances<PlasticWindow>())
  325. return null;
  326. PlasticWindow window = EditorWindow.
  327. GetWindow<PlasticWindow>(null, false);
  328. return window.GetPendingChangesView();
  329. }
  330. static string GetFsWatcherEnabledMessage()
  331. {
  332. if (PlatformIdentifier.IsWindows() || PlatformIdentifier.IsMac())
  333. return PlasticLocalization.GetString(
  334. PlasticLocalization.Name.PendingChangesFilesystemWatcherEnabled);
  335. return PlasticLocalization.GetString(
  336. PlasticLocalization.Name.PendingChangesINotifyEnabled);
  337. }
  338. static string GetFsWatcherDisabledMessage()
  339. {
  340. if (PlatformIdentifier.IsWindows() || PlatformIdentifier.IsMac())
  341. return PlasticLocalization.GetString(
  342. PlasticLocalization.Name.PendingChangesFilesystemWatcherDisabled);
  343. return PlasticLocalization.GetString(
  344. PlasticLocalization.Name.PendingChangesINotifyDisabled);
  345. }
  346. static string GetFsWatcherEnabledExplanation()
  347. {
  348. if (PlatformIdentifier.IsWindows() || PlatformIdentifier.IsMac())
  349. return PlasticLocalization.GetString(
  350. PlasticLocalization.Name.PendingChangesFilesystemWatcherEnabledExplanationUnityVCS);
  351. return PlasticLocalization.GetString(
  352. PlasticLocalization.Name.PendingChangesINotifyEnabledExplanation);
  353. }
  354. static string GetFsWatcherDisabledExplanation()
  355. {
  356. if (PlatformIdentifier.IsWindows() || PlatformIdentifier.IsMac())
  357. {
  358. return PlasticLocalization.GetString(
  359. PlasticLocalization.Name.PendingChangesFilesystemWatcherDisabledExplanationUnityVCS)
  360. .Replace("[[HELP_URL|{0}]]", "{0}");
  361. }
  362. return PlasticLocalization.GetString(
  363. PlasticLocalization.Name.PendingChangesINotifyDisabledExplanation);
  364. }
  365. static string GetHelpLink()
  366. {
  367. if (PlatformIdentifier.IsWindows() || PlatformIdentifier.IsMac())
  368. return FS_WATCHER_HELP_URL;
  369. return INOTIFY_HELP_URL;
  370. }
  371. static bool IsFileSystemWatcherEnabled(
  372. WorkspaceInfo wkInfo)
  373. {
  374. return WorkspaceWatcherFsNodeReadersCache.Get().
  375. IsWatcherEnabled(wkInfo);
  376. }
  377. static bool IsEnabled(
  378. WorkspaceStatusOptions option,
  379. WorkspaceStatusOptions options)
  380. {
  381. return (options & option) == option;
  382. }
  383. internal interface IAutoRefreshView
  384. {
  385. void DisableAutoRefresh();
  386. void EnableAutoRefresh();
  387. void ForceRefresh();
  388. }
  389. class Styles
  390. {
  391. internal static GUIContent ShowCheckouts =
  392. new GUIContent(PlasticLocalization.GetString(
  393. PlasticLocalization.Name.PendingChangesShowCheckouts),
  394. PlasticLocalization.GetString(
  395. PlasticLocalization.Name.PendingChangesShowCheckoutsExplanation));
  396. internal static GUIContent AutoRefresh =
  397. new GUIContent(PlasticLocalization.GetString(
  398. PlasticLocalization.Name.PendingChangesAutoRefresh),
  399. PlasticLocalization.GetString(
  400. PlasticLocalization.Name.PendingChangesAutoRefreshExplanation));
  401. internal static GUIContent ShowChangedFiles =
  402. new GUIContent(PlasticLocalization.GetString(
  403. PlasticLocalization.Name.PendingChangesFindChanged),
  404. PlasticLocalization.GetString(
  405. PlasticLocalization.Name.PendingChangesFindChangedExplanation));
  406. internal static GUIContent CheckFileContent =
  407. new GUIContent(PlasticLocalization.GetString(
  408. PlasticLocalization.Name.PendingChangesCheckFileContent),
  409. PlasticLocalization.GetString(
  410. PlasticLocalization.Name.PendingChangesCheckFileContentExplanation));
  411. internal static GUIContent UseChangeLists =
  412. new GUIContent(PlasticLocalization.GetString(
  413. PlasticLocalization.Name.PendingChangesGroupInChangeLists),
  414. PlasticLocalization.GetString(
  415. PlasticLocalization.Name.PendingChangesGroupInChangeListsExplanation));
  416. internal static GUIContent ShowPrivateFields =
  417. new GUIContent(PlasticLocalization.GetString(
  418. PlasticLocalization.Name.PendingChangesShowPrivateFiles),
  419. PlasticLocalization.GetString(
  420. PlasticLocalization.Name.PendingChangesShowPrivateFilesExplanation));
  421. internal static GUIContent ShowIgnoredFields =
  422. new GUIContent(PlasticLocalization.GetString(
  423. PlasticLocalization.Name.PendingChangesShowIgnoredFiles),
  424. PlasticLocalization.GetString(
  425. PlasticLocalization.Name.PendingChangesShowIgnoredFilesExplanation));
  426. internal static GUIContent ShowHiddenFields =
  427. new GUIContent(PlasticLocalization.GetString(
  428. PlasticLocalization.Name.PendingChangesShowHiddenFiles),
  429. PlasticLocalization.GetString(
  430. PlasticLocalization.Name.PendingChangesShowHiddenFilesExplanation));
  431. internal static GUIContent ShowDeletedFilesDirs =
  432. new GUIContent(PlasticLocalization.GetString(
  433. PlasticLocalization.Name.PendingChangesShowDeletedFiles),
  434. PlasticLocalization.GetString(
  435. PlasticLocalization.Name.PendingChangesShowDeletedFilesExplanation));
  436. internal static GUIContent ShowMovedFiles =
  437. new GUIContent(PlasticLocalization.GetString(
  438. PlasticLocalization.Name.PendingChangesFindMovedFiles),
  439. PlasticLocalization.GetString(
  440. PlasticLocalization.Name.PendingChangesFindMovedFilesExplanation));
  441. internal static GUIContent MatchBinarySameExtension =
  442. new GUIContent(PlasticLocalization.GetString(
  443. PlasticLocalization.Name.PendingChangesMatchBinarySameExtension),
  444. PlasticLocalization.GetString(
  445. PlasticLocalization.Name.PendingChangesMatchBinarySameExtensionExplanation));
  446. internal static GUIContent MatchTextSameExtension =
  447. new GUIContent(PlasticLocalization.GetString(
  448. PlasticLocalization.Name.PendingChangesMatchTextSameExtension),
  449. PlasticLocalization.GetString(
  450. PlasticLocalization.Name.PendingChangesMatchTextSameExtensionExplanation));
  451. internal static GUIContent SimilarityPercent =
  452. new GUIContent(PlasticLocalization.GetString(
  453. PlasticLocalization.Name.PendingChangesSimilarityPercentage),
  454. PlasticLocalization.GetString(
  455. PlasticLocalization.Name.PendingChangesSimilarityPercentageExplanation));
  456. }
  457. bool mIsPluginEnabled;
  458. WorkspaceInfo mWkInfo;
  459. PendingChangesOptions mInitialOptions;
  460. bool mShowCheckouts;
  461. bool mAutoRefresh;
  462. bool mFSWatcherEnabled;
  463. bool mShowChangedFiles;
  464. bool mCheckFileContent;
  465. bool mUseChangeLists;
  466. bool mShowPrivateFields;
  467. bool mShowIgnoredFiles;
  468. bool mShowHiddenFiles;
  469. bool mShowDeletedFiles;
  470. bool mShowMovedFiles;
  471. bool mMatchBinarySameExtension;
  472. bool mMatchTextSameExtension;
  473. int mSimilarityPercent;
  474. const string FS_WATCHER_HELP_URL = "https://plasticscm.com/download/help/support";
  475. const string INOTIFY_HELP_URL = "https://plasticscm.com/download/help/inotify";
  476. }
  477. }