123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Threading;
- using Codice.Client.BaseCommands;
- using Codice.Client.BaseCommands.Config;
- using Codice.Client.Commands;
- using Codice.Client.Commands.WkTree;
- using Codice.LogWrapper;
- using GluonGui;
- using PlasticGui;
- using PlasticGui.WorkspaceWindow;
- using Unity.PlasticSCM.Editor.AssetUtils;
- using Unity.PlasticSCM.Editor.UI;
- using Unity.PlasticSCM.Editor.Views.IncomingChanges;
- using Unity.PlasticSCM.Editor.Views.PendingChanges;
- namespace Unity.PlasticSCM.Editor
- {
- internal class WorkspaceOperationsMonitor
- {
- public interface IDisableAssetsProcessor
- {
- void Disable();
- }
- internal WorkspaceOperationsMonitor(
- IPlasticAPI plasticApi,
- IDisableAssetsProcessor disableAssetsProcessor,
- bool isGluonMode)
- {
- mPlasticAPI = plasticApi;
- mDisableAssetsProcessor = disableAssetsProcessor;
- mIsGluonMode = isGluonMode;
- }
- internal void RegisterWindow(
- WorkspaceWindow workspaceWindow,
- ViewHost viewHost,
- NewIncomingChangesUpdater incomingChangesUpdater)
- {
- mWorkspaceWindow = workspaceWindow;
- mViewHost = viewHost;
- mNewIncomingChangesUpdater = incomingChangesUpdater;
- }
- internal void UnRegisterWindow()
- {
- mWorkspaceWindow = null;
- mViewHost = null;
- mNewIncomingChangesUpdater = null;
- }
- internal void RegisterPendingChangesView(
- PendingChangesTab pendingChangesTab)
- {
- mPendingChangesTab = pendingChangesTab;
- }
- internal void RegisterIncomingChangesView(
- IIncomingChangesTab incomingChangesTab)
- {
- mIncomingChangesTab = incomingChangesTab;
- }
- internal void UnRegisterViews()
- {
- mPendingChangesTab = null;
- mIncomingChangesTab = null;
- }
- internal void Start()
- {
- mIsRunning = true;
- Thread thread = new Thread(TaskLoopThread);
- thread.IsBackground = true;
- thread.Start();
- }
- internal void Stop()
- {
- SetAsFinished();
- }
- internal void AddAssetsProcessorPathToAdd(
- List<string> paths)
- {
- AddPathsToProcess(
- mAssetsProcessorPathsToAdd, paths,
- mLock, mResetEvent);
- }
- internal void AddAssetsProcessorPathToDelete(
- string path)
- {
- AddPathsToProcess(
- mAssetsProcessorPathsToDelete,
- new List<string> { path },
- mLock, mResetEvent);
- }
- internal void AddAssetsProcessorPathToCheckout(
- List<string> paths)
- {
- AddPathsToProcess(
- mAssetsProcessorPathsToCheckout, paths,
- mLock, mResetEvent);
- }
- internal void AddAssetsProcessorPathToMove(
- string srcPath,
- string dstPath)
- {
- AddPathToMoveToProcess(
- mAssetsProcessorPathsToMove,
- new PathToMove(srcPath, dstPath),
- mLock, mResetEvent);
- }
- internal void AddPathsToCheckout(
- List<string> paths)
- {
- AddPathsToProcess(
- mPathsToCheckout, paths,
- mLock, mResetEvent);
- }
- void TaskLoopThread()
- {
- while (true)
- {
- try
- {
- if (!mIsRunning)
- break;
- ProcessAssetProcessorOperations(
- mPlasticAPI,
- mAssetsProcessorPathsToAdd,
- mAssetsProcessorPathsToDelete,
- mAssetsProcessorPathsToCheckout,
- mAssetsProcessorPathsToMove,
- mLock,
- mDisableAssetsProcessor);
- ProcessCheckoutOperation(
- mPlasticAPI,
- mPathsToCheckout,
- mLock);
- bool hasAssetProcessorOperations = false;
- bool hasCheckoutOperations = false;
- HasPendingOperationsToProcess(
- mAssetsProcessorPathsToAdd,
- mAssetsProcessorPathsToDelete,
- mAssetsProcessorPathsToCheckout,
- mAssetsProcessorPathsToMove,
- mPathsToCheckout,
- mLock,
- out hasAssetProcessorOperations,
- out hasCheckoutOperations);
- if (hasAssetProcessorOperations ||
- hasCheckoutOperations)
- continue;
- if (!hasAssetProcessorOperations)
- EditorDispatcher.Dispatch(AfterAssetProcessorOperation);
- if (!hasCheckoutOperations)
- EditorDispatcher.Dispatch(AfterCheckoutOperation);
- SleepUntilNextWorkload();
- }
- catch (Exception e)
- {
- mLog.ErrorFormat(
- "Error running the tasks loop : {0}", e.Message);
- mLog.DebugFormat(
- "Stacktrace: {0}", e.StackTrace);
- }
- }
- }
- void AfterAssetProcessorOperation()
- {
- AutoRefresh.PendingChangesView(
- mPendingChangesTab);
- AutoRefresh.IncomingChangesView(
- mIncomingChangesTab);
- }
- void AfterCheckoutOperation()
- {
- RefreshAsset.VersionControlCache();
- if (mIsGluonMode)
- {
- RefreshViewsAfterCheckoutForGluon(mViewHost);
- return;
- }
- if (mNewIncomingChangesUpdater != null)
- mNewIncomingChangesUpdater.Update();
- RefreshViewsAfterCheckoutForDeveloper(mWorkspaceWindow);
- }
- void SetAsFinished()
- {
- if (!mIsRunning)
- return;
- mIsRunning = false;
- mResetEvent.Set();
- }
- void SleepUntilNextWorkload()
- {
- mResetEvent.Reset();
- mResetEvent.WaitOne();
- }
- static void ProcessAssetProcessorOperations(
- IPlasticAPI plasticApi,
- List<string> assetsProcessorPathsToAdd,
- List<string> assetsProcessorPathsToDelete,
- List<string> assetsProcessorPathsToCheckout,
- List<PathToMove> assetsProcessorPathsToMove,
- object lockObj,
- IDisableAssetsProcessor disableAssetsProcessor)
- {
- try
- {
- AssetsProcessorOperations.AddIfNotControlled(
- plasticApi, ExtractPathsToProcess(
- assetsProcessorPathsToAdd, lockObj));
- AssetsProcessorOperations.DeleteIfControlled(
- plasticApi, ExtractPathsToProcess(
- assetsProcessorPathsToDelete, lockObj));
- AssetsProcessorOperations.CheckoutIfControlledAndChanged(
- plasticApi, ExtractPathsToProcess(
- assetsProcessorPathsToCheckout, lockObj));
- AssetsProcessorOperations.MoveIfControlled(
- plasticApi, ExtractPathsToMoveToProcess(
- assetsProcessorPathsToMove, lockObj));
- }
- catch (Exception ex)
- {
- LogException(ex);
- disableAssetsProcessor.Disable();
- }
- }
- static void ProcessCheckoutOperation(
- IPlasticAPI plasticApi,
- List<string> pathsToProcess,
- object lockObj)
- {
- List<string> paths = ExtractPathsToProcess(
- pathsToProcess, lockObj);
- if (paths.Count == 0)
- return;
- plasticApi.Checkout(
- paths.ToArray(),
- CheckoutModifiers.ProcessSymlinks);
- }
- static void AddPathsToProcess(
- List<string> pathsToProcess,
- List<string> paths,
- object lockObj,
- ManualResetEvent resetEvent)
- {
- lock (lockObj)
- {
- pathsToProcess.AddRange(paths);
- }
- resetEvent.Set();
- }
- static void AddPathToMoveToProcess(
- List<PathToMove> pathsToProcess,
- PathToMove path,
- object lockObj,
- ManualResetEvent resetEvent)
- {
- lock (lockObj)
- {
- pathsToProcess.Add(path);
- }
- resetEvent.Set();
- }
- static List<string> ExtractPathsToProcess(
- List<string> pathsToProcess,
- object lockObj)
- {
- List<string> result;
- lock (lockObj)
- {
- result = new List<string>(pathsToProcess);
- pathsToProcess.Clear();
- }
- return result;
- }
- static List<PathToMove> ExtractPathsToMoveToProcess(
- List<PathToMove> pathsToProcess,
- object lockObj)
- {
- List<PathToMove> result;
- lock (lockObj)
- {
- result = new List<PathToMove>(pathsToProcess);
- pathsToProcess.Clear();
- }
- return result;
- }
- static void HasPendingOperationsToProcess(
- List<string> assetsProcessorPathsToAdd,
- List<string> assetsProcessorPathsToDelete,
- List<string> assetsProcessorPathsToCheckout,
- List<PathToMove> assetsProcessorPathsToMove,
- List<string> pathsToCheckout,
- object lockObj,
- out bool hasAssetProcessorOperations,
- out bool hasCheckoutOperations)
- {
- lock (lockObj)
- {
- hasAssetProcessorOperations =
- assetsProcessorPathsToAdd.Count > 0 ||
- assetsProcessorPathsToDelete.Count > 0 ||
- assetsProcessorPathsToCheckout.Count > 0 ||
- assetsProcessorPathsToMove.Count > 0;
- hasCheckoutOperations =
- pathsToCheckout.Count > 0;
- }
- }
- static void RefreshViewsAfterCheckoutForDeveloper(
- IWorkspaceWindow workspaceWindow)
- {
- if (workspaceWindow == null)
- return;
- workspaceWindow.RefreshView(ViewType.BranchExplorerView);
- workspaceWindow.RefreshView(ViewType.PendingChangesView);
- workspaceWindow.RefreshView(ViewType.HistoryView);
- }
- static void RefreshViewsAfterCheckoutForGluon(
- ViewHost viewHost)
- {
- if (viewHost == null)
- return;
- viewHost.RefreshView(ViewType.WorkspaceExplorerView);
- viewHost.RefreshView(ViewType.CheckinView);
- viewHost.RefreshView(ViewType.IncomingChangesView);
- viewHost.RefreshView(ViewType.SearchView);
- }
- static void LogException(Exception ex)
- {
- mLog.WarnFormat("Message: {0}", ex.Message);
- mLog.DebugFormat(
- "StackTrace:{0}{1}",
- Environment.NewLine, ex.StackTrace);
- }
- static class AssetsProcessorOperations
- {
- internal static void AddIfNotControlled(
- IPlasticAPI plasticApi,
- List<string> paths)
- {
- List<string> fullPaths = new List<string>();
- IgnoredFilesFilter ignoredFilter = new IgnoredFilesFilter(
- GlobalConfig.Instance);
- foreach (string path in paths)
- {
- string fullPath = Path.GetFullPath(path);
- string fullPathMeta = MetaPath.GetMetaPath(fullPath);
- if (plasticApi.GetWorkspaceFromPath(fullPath) == null)
- return;
- if (plasticApi.GetWorkspaceTreeNode(fullPath) == null &&
- !ignoredFilter.IsIgnored(fullPath))
- fullPaths.Add(fullPath);
- if (File.Exists(fullPathMeta) &&
- plasticApi.GetWorkspaceTreeNode(fullPathMeta) == null &&
- !ignoredFilter.IsIgnored(fullPath))
- fullPaths.Add(fullPathMeta);
- }
- if (fullPaths.Count == 0)
- return;
- IList checkouts;
- plasticApi.Add(
- fullPaths.ToArray(),
- GetDefaultAddOptions(),
- out checkouts);
- }
- internal static void DeleteIfControlled(
- IPlasticAPI plasticApi,
- List<string> paths)
- {
- foreach (string path in paths)
- {
- string fullPath = Path.GetFullPath(path);
- string fullPathMeta = MetaPath.GetMetaPath(fullPath);
- if (plasticApi.GetWorkspaceTreeNode(fullPath) != null)
- {
- plasticApi.DeleteControlled(
- fullPath, DeleteModifiers.None);
- }
- if (plasticApi.GetWorkspaceTreeNode(fullPathMeta) != null)
- {
- plasticApi.DeleteControlled(
- fullPathMeta, DeleteModifiers.None);
- }
- }
- }
- internal static void MoveIfControlled(
- IPlasticAPI plasticApi,
- List<PathToMove> paths)
- {
- foreach (PathToMove pathToMove in paths)
- {
- string fullSrcPath = Path.GetFullPath(pathToMove.SrcPath);
- string fullSrcPathMeta = MetaPath.GetMetaPath(fullSrcPath);
- string fullDstPath = Path.GetFullPath(pathToMove.DstPath);
- string fullDstPathMeta = MetaPath.GetMetaPath(fullDstPath);
- if (plasticApi.GetWorkspaceTreeNode(fullSrcPath) != null)
- {
- plasticApi.Move(
- fullSrcPath, fullDstPath,
- MoveModifiers.None);
- }
- if (plasticApi.GetWorkspaceTreeNode(fullSrcPathMeta) != null)
- {
- plasticApi.Move(
- fullSrcPathMeta, fullDstPathMeta,
- MoveModifiers.None);
- }
- }
- }
- internal static void CheckoutIfControlledAndChanged(
- IPlasticAPI plasticApi,
- List<string> paths)
- {
- List<string> fullPaths = new List<string>();
- foreach (string path in paths)
- {
- string fullPath = Path.GetFullPath(path);
- string fullPathMeta = MetaPath.GetMetaPath(fullPath);
- WorkspaceTreeNode node =
- plasticApi.GetWorkspaceTreeNode(fullPath);
- WorkspaceTreeNode nodeMeta =
- plasticApi.GetWorkspaceTreeNode(fullPathMeta);
- if (node != null && ChangedFileChecker.IsChanged(
- node.LocalInfo, fullPath, false))
- fullPaths.Add(fullPath);
- if (nodeMeta != null && ChangedFileChecker.IsChanged(
- nodeMeta.LocalInfo, fullPathMeta, false))
- fullPaths.Add(fullPathMeta);
- }
- if (fullPaths.Count == 0)
- return;
- plasticApi.Checkout(
- fullPaths.ToArray(),
- CheckoutModifiers.None);
- }
- static AddOptions GetDefaultAddOptions()
- {
- AddOptions options = new AddOptions();
- options.AddPrivateParents = true;
- options.NeedCheckPlatformPath = true;
- return options;
- }
- }
- struct PathToMove
- {
- internal readonly string SrcPath;
- internal readonly string DstPath;
- internal PathToMove(string srcPath, string dstPath)
- {
- SrcPath = srcPath;
- DstPath = dstPath;
- }
- }
- object mLock = new object();
- volatile bool mIsRunning;
- volatile ManualResetEvent mResetEvent = new ManualResetEvent(false);
- List<string> mAssetsProcessorPathsToAdd = new List<string>();
- List<string> mAssetsProcessorPathsToDelete = new List<string>();
- List<string> mAssetsProcessorPathsToCheckout = new List<string>();
- List<PathToMove> mAssetsProcessorPathsToMove = new List<PathToMove>();
- List<string> mPathsToCheckout = new List<string>();
- PendingChangesTab mPendingChangesTab;
- IIncomingChangesTab mIncomingChangesTab;
- NewIncomingChangesUpdater mNewIncomingChangesUpdater;
- ViewHost mViewHost;
- IWorkspaceWindow mWorkspaceWindow;
- readonly bool mIsGluonMode = false;
- readonly IDisableAssetsProcessor mDisableAssetsProcessor;
- readonly IPlasticAPI mPlasticAPI;
- static readonly ILog mLog = LogManager.GetLogger("WorkspaceOperationsMonitor");
- }
- }
|