File size: 7,397 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | using System;
using System.Threading;
using UnityEditor;
using Codice.Client.BaseCommands;
using Codice.Client.Commands;
using Codice.CM.Common;
using Codice.LogWrapper;
using PlasticGui;
using PlasticGui.WebApi;
using PlasticGui.WorkspaceWindow;
using PlasticGui.WorkspaceWindow.Update;
using Unity.PlasticSCM.Editor.AssetUtils;
using Unity.PlasticSCM.Editor.UI;
using Unity.PlasticSCM.Editor.WebApi;
using Unity.PlasticSCM.Editor.Configuration;
namespace Unity.PlasticSCM.Editor.ProjectDownloader
{
internal class DownloadRepositoryOperation
{
internal void DownloadRepositoryToPathIfNeeded(
string cloudRepository,
string cloudOrganization,
string projectPath,
string unityAccessToken)
{
RefreshAsset.BeforeLongAssetOperation();
try
{
BuildProgressSpeedAndRemainingTime.ProgressData progressData =
new BuildProgressSpeedAndRemainingTime.ProgressData(DateTime.Now);
ThreadPool.QueueUserWorkItem(
DownloadRepository,
new DownloadRepositoryParameters()
{
CloudOrganization = cloudOrganization,
CloudRepository = cloudRepository,
ProjectPath = projectPath,
AccessToken = unityAccessToken
});
while (!mOperationFinished)
{
if (mDisplayProgress)
{
DisplayProgress(
mUpdateNotifier.GetUpdateStatus(),
progressData,
cloudRepository);
}
Thread.Sleep(150);
}
}
finally
{
EditorUtility.ClearProgressBar();
RefreshAsset.AfterLongAssetOperation();
if (!mOperationFailed)
{
PlasticPlugin.Enable();
ShowWindow.PlasticAfterDownloadingProject();
}
}
}
void DownloadRepository(object state)
{
DownloadRepositoryParameters parameters = (DownloadRepositoryParameters)state;
try
{
if (FindWorkspace.HasWorkspace(parameters.ProjectPath))
{
// each domain reload, the package is reloaded.
// way need to check if we already downloaded it
return;
}
mDisplayProgress = true;
IPlasticWebRestApi restApi = new PlasticWebRestApi();
string defaultCloudAlias = restApi.GetDefaultCloudAlias();
RepositorySpec repSpec = BuildRepSpec(
parameters.CloudRepository,
parameters.CloudOrganization,
defaultCloudAlias);
TokenExchangeResponse tokenExchangeResponse =
AutoConfig.PlasticCredentials(
parameters.AccessToken,
repSpec.Server,
parameters.ProjectPath);
if (tokenExchangeResponse.Error != null)
{
mOperationFailed = true;
UnityEngine.Debug.LogErrorFormat(
PlasticLocalization.GetString(PlasticLocalization.Name.ErrorDownloadingCloudProject),
string.Format("Unable to get TokenExchangeResponse: {0} [code {1}]",
tokenExchangeResponse.Error.Message,
tokenExchangeResponse.Error.ErrorCode));
return;
}
WorkspaceInfo wkInfo = CreateWorkspace(
repSpec, parameters.ProjectPath);
mLog.DebugFormat("Created workspace {0} on {1}",
wkInfo.Name,
wkInfo.ClientPath);
PlasticGui.Plastic.API.Update(
wkInfo.ClientPath,
UpdateFlags.None,
null,
mUpdateNotifier);
}
catch (Exception ex)
{
LogException(ex);
UnityEngine.Debug.LogErrorFormat(
PlasticLocalization.GetString(PlasticLocalization.Name.ErrorDownloadingCloudProject),
ex.Message);
mOperationFailed = true;
}
finally
{
mOperationFinished = true;
}
}
static void DisplayProgress(
UpdateOperationStatus status,
BuildProgressSpeedAndRemainingTime.ProgressData progressData,
string cloudRepository)
{
string totalProgressMessage = UpdateProgressRender.
GetProgressString(status, progressData);
float totalProgressPercent = GetProgressBarPercent.
ForTransfer(status.UpdatedSize, status.TotalSize) / 100f;
EditorUtility.DisplayProgressBar(
string.Format("{0} {1}",
PlasticLocalization.GetString(PlasticLocalization.Name.DownloadingProgress),
cloudRepository),
totalProgressMessage, totalProgressPercent);
}
static WorkspaceInfo CreateWorkspace(
RepositorySpec repositorySpec,
string projectPath)
{
CreateWorkspaceDialogUserAssistant assistant = new CreateWorkspaceDialogUserAssistant(
PlasticGuiConfig.Get().Configuration.DefaultWorkspaceRoot,
PlasticGui.Plastic.API.GetAllWorkspacesArray());
assistant.RepositoryChanged(
repositorySpec.ToString(),
string.Empty,
string.Empty);
return PlasticGui.Plastic.API.CreateWorkspace(
projectPath,
assistant.GetProposedWorkspaceName(),
repositorySpec.ToString());
}
static RepositorySpec BuildRepSpec(
string cloudRepository,
string cloudOrganization,
string defaultCloudAlias)
{
return new RepositorySpec()
{
Name = cloudRepository,
Server = CloudServer.BuildFullyQualifiedName(
cloudOrganization, defaultCloudAlias)
};
}
static void LogException(Exception ex)
{
mLog.WarnFormat("Message: {0}", ex.Message);
mLog.DebugFormat(
"StackTrace:{0}{1}",
Environment.NewLine, ex.StackTrace);
}
class DownloadRepositoryParameters
{
internal string CloudRepository;
internal string CloudOrganization;
internal string ProjectPath;
internal string AccessToken;
}
volatile bool mOperationFinished = false;
volatile bool mOperationFailed = false;
volatile bool mDisplayProgress;
UpdateNotifier mUpdateNotifier = new UpdateNotifier();
static readonly ILog mLog = LogManager.GetLogger("DownloadRepositoryOperation");
}
}
|