暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RemoteStatusCache.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using Codice.Client.BaseCommands;
  4. using Codice.Client.Commands;
  5. using Codice.Client.Common;
  6. using Codice.Client.Common.Threading;
  7. using Codice.Client.GameUI;
  8. using Codice.Client.GameUI.Update;
  9. using Codice.CM.Common;
  10. using Codice.CM.Common.Merge;
  11. using Codice.Utils;
  12. using GluonGui.WorkspaceWindow.Views;
  13. namespace Unity.PlasticSCM.Editor.AssetsOverlays.Cache
  14. {
  15. internal class RemoteStatusCache
  16. {
  17. internal RemoteStatusCache(
  18. WorkspaceInfo wkInfo,
  19. bool isGluonMode,
  20. Action repaintProjectWindow,
  21. Action repaintInspector)
  22. {
  23. mWkInfo = wkInfo;
  24. mIsGluonMode = isGluonMode;
  25. mRepaintProjectWindow = repaintProjectWindow;
  26. mRepaintInspector = repaintInspector;
  27. }
  28. internal AssetStatus GetStatus(string fullPath)
  29. {
  30. if (!mIsGluonMode)
  31. return AssetStatus.UpToDate;
  32. lock(mLock)
  33. {
  34. if (mStatusByPathCache == null)
  35. {
  36. mStatusByPathCache = BuildPathDictionary.ForPlatform<AssetStatus>();
  37. mCurrentCancelToken.Cancel();
  38. mCurrentCancelToken = new CancelToken();
  39. AsyncCalculateStatus(mCurrentCancelToken);
  40. return AssetStatus.UpToDate;
  41. }
  42. AssetStatus result;
  43. if (mStatusByPathCache.TryGetValue(fullPath, out result))
  44. return result;
  45. return AssetStatus.UpToDate;
  46. }
  47. }
  48. internal void Clear()
  49. {
  50. lock (mLock)
  51. {
  52. mCurrentCancelToken.Cancel();
  53. mStatusByPathCache = null;
  54. }
  55. }
  56. void AsyncCalculateStatus(CancelToken cancelToken)
  57. {
  58. Dictionary<string, AssetStatus> statusByPathCache = null;
  59. IThreadWaiter waiter = ThreadWaiter.GetWaiter(50);
  60. waiter.Execute(
  61. /*threadOperationDelegate*/ delegate
  62. {
  63. OutOfDateItems outOfDateItems =
  64. OutOfDateUpdater.CalculateOutOfDateItems(
  65. mWkInfo, new List<ErrorMessage>(),
  66. OutOfDateCalculator.Options.IsIncomingChanges);
  67. if (cancelToken.IsCancelled())
  68. return;
  69. statusByPathCache = BuildStatusByPathCache.
  70. ForOutOfDateItems(outOfDateItems, mWkInfo.ClientPath);
  71. },
  72. /*afterOperationDelegate*/ delegate
  73. {
  74. if (waiter.Exception != null)
  75. {
  76. ExceptionsHandler.LogException(
  77. "RemoteStatusCache",
  78. waiter.Exception);
  79. return;
  80. }
  81. if (cancelToken.IsCancelled())
  82. return;
  83. lock (mLock)
  84. {
  85. mStatusByPathCache = statusByPathCache;
  86. }
  87. mRepaintProjectWindow();
  88. mRepaintInspector();
  89. });
  90. }
  91. static class BuildStatusByPathCache
  92. {
  93. internal static Dictionary<string, AssetStatus> ForOutOfDateItems(
  94. OutOfDateItems outOfDateItems,
  95. string wkPath)
  96. {
  97. Dictionary<string, AssetStatus> result =
  98. BuildPathDictionary.ForPlatform<AssetStatus>();
  99. if (outOfDateItems == null)
  100. return result;
  101. foreach (OutOfDateItemsByMount diffs in
  102. outOfDateItems.GetOutOfDateItemsByMountList())
  103. {
  104. foreach (Difference diff in diffs.Changed)
  105. {
  106. if (diff is DiffXlinkChanged)
  107. continue;
  108. string path = GetPathForDiff(wkPath, diffs.Mount, diff.Path);
  109. result.Add(path, AssetStatus.OutOfDate);
  110. }
  111. foreach (Difference diff in diffs.Deleted)
  112. {
  113. string path = GetPathForDiff(wkPath, diffs.Mount, diff.Path);
  114. result.Add(path, AssetStatus.DeletedOnServer);
  115. }
  116. }
  117. foreach (GluonFileConflict fileConflict in
  118. outOfDateItems.GetFileConflicts())
  119. {
  120. string path = GetPathForConflict(wkPath, fileConflict.CmPath);
  121. result.Add(path, AssetStatus.Conflicted);
  122. }
  123. return result;
  124. }
  125. static string GetPathForDiff(
  126. string wkPath,
  127. MountPointWithPath mountPoint,
  128. string cmSubPath)
  129. {
  130. return WorkspacePath.GetWorkspacePathFromCmPath(
  131. wkPath,
  132. WorkspacePath.ComposeMountPath(mountPoint.MountPath, cmSubPath),
  133. PathHelper.GetDirectorySeparatorChar(wkPath));
  134. }
  135. static string GetPathForConflict(
  136. string wkPath,
  137. string cmPath)
  138. {
  139. return WorkspacePath.GetWorkspacePathFromCmPath(
  140. wkPath, cmPath,
  141. PathHelper.GetDirectorySeparatorChar(wkPath));
  142. }
  143. }
  144. CancelToken mCurrentCancelToken = new CancelToken();
  145. Dictionary<string, AssetStatus> mStatusByPathCache;
  146. readonly Action mRepaintInspector;
  147. readonly Action mRepaintProjectWindow;
  148. readonly bool mIsGluonMode;
  149. readonly WorkspaceInfo mWkInfo;
  150. static object mLock = new object();
  151. }
  152. }