A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections.Generic;
3
4using UnityEditor;
5
6using Codice.LogWrapper;
7using PlasticGui;
8using Unity.PlasticSCM.Editor.Hub.Operations;
9
10namespace Unity.PlasticSCM.Editor.Hub
11{
12 internal static class ProcessHubCommand
13 {
14 internal const string IS_PROCESS_COMMAND_ALREADY_EXECUTED_KEY =
15 "PlasticSCM.ProcessHubCommand.IsAlreadyExecuted";
16
17 internal static void Initialize()
18 {
19 EditorApplication.update += RunOnceWhenAccessTokenIsInitialized;
20 }
21
22 static void RunOnceWhenAccessTokenIsInitialized()
23 {
24 if (string.IsNullOrEmpty(CloudProjectSettings.accessToken))
25 {
26 return;
27 }
28
29 EditorApplication.update -= RunOnceWhenAccessTokenIsInitialized;
30
31 Execute(CloudProjectSettings.accessToken);
32 }
33
34 static void Execute(string unityAccessToken)
35 {
36 // When the Hub creates a workspace from an Editor with an older Version Control < 2.7.1,
37 // the Editor updates the package to its latest version after the workspace creation.
38 // Because the name of the key was changed in 2.7.1, there is a mismatch between
39 // the first execution with the old package and the second execution with the new one.
40 // This produces a console error that the path is already contained in a workspace.
41 // To prevent that, we have to check with both keys.
42 if (SessionState.GetBool(IS_PROCESS_COMMAND_ALREADY_EXECUTED_KEY, false) ||
43 SessionState.GetBool(IS_PROCESS_COMMAND_ALREADY_EXECUTED_OLD_KEY, false))
44 {
45 return;
46 }
47
48 ProcessCommandFromArgs(Environment.GetCommandLineArgs(), unityAccessToken);
49
50 SessionState.SetBool(IS_PROCESS_COMMAND_ALREADY_EXECUTED_KEY, true);
51 }
52
53 internal static void ProcessCommandFromArgs(
54 string[] commandLineArgs,
55 string unityAccessToken)
56 {
57 Dictionary<string, string> args = CommandLineArguments.Build(commandLineArgs);
58
59 ParseArguments.Command command = ParseArguments.GetCommand(args);
60
61 if (!command.IsValid())
62 {
63 return;
64 }
65
66 PlasticApp.InitializeIfNeeded();
67
68 mLog.DebugFormat("Command line arguments: {0}", string.Join(" ", commandLineArgs));
69 mLog.DebugFormat("Processing command: {0}", command.OperationType);
70
71 OperationParams parameters = OperationParams.
72 BuildFromCommand(command, unityAccessToken);
73
74 string errorMessage;
75 if (InputValidator.CheckWorkspaceExists(
76 null, parameters.WorkspaceFullPath, out errorMessage))
77 {
78 mLog.Error(errorMessage);
79 UnityEngine.Debug.LogError(errorMessage);
80 return;
81 }
82
83 switch (command.OperationType)
84 {
85 case ParseArguments.Command.Operation.CreateWorkspace:
86 CreateWorkspace.LaunchOperation(parameters);
87 return;
88 case ParseArguments.Command.Operation.DownloadRepository:
89 DownloadRepository.LaunchOperation(parameters);
90 return;
91 }
92 }
93
94 const string IS_PROCESS_COMMAND_ALREADY_EXECUTED_OLD_KEY =
95 "PlasticSCM.ProcessCommand.IsAlreadyExecuted";
96
97 static readonly ILog mLog = PlasticApp.GetLogger("ProcessHubCommand");
98 }
99}