A game about forced loneliness, made by TACStudios
1using System.IO;
2
3using UnityEditor;
4
5using Codice.Client.Common;
6using Codice.Utils;
7using Unity.PlasticSCM.Editor.Tool;
8using Unity.PlasticSCM.Editor.Views;
9
10namespace Unity.PlasticSCM.Editor
11{
12 internal static class UnityConfigurationChecker
13 {
14 internal static void SynchronizeUnityEditionToken()
15 {
16 string plasticClientBinDir = PlasticInstallPath.GetClientBinDir();
17
18 if (!string.IsNullOrEmpty(plasticClientBinDir) && !IsPlasticInstalling())
19 SetupUnityEditionToken.FromPlasticInstallation(plasticClientBinDir);
20 }
21
22 internal static bool NeedsConfiguration()
23 {
24 SynchronizeUnityEditionToken();
25
26 if (ConfigurationChecker.NeedConfiguration())
27 return true;
28
29 if (ClientConfig.Get().GetClientConfigData().WorkingMode == "SSOWorkingMode" &&
30 !CmConnection.Get().IsAnyTokenConfigured())
31 return true;
32
33 return false;
34 }
35
36 static bool IsPlasticInstalling()
37 {
38 if (!EditorWindow.HasOpenInstances<DownloadPlasticExeDialog>())
39 return false;
40
41 DownloadPlasticExeDialog window = EditorWindow.
42 GetWindow<DownloadPlasticExeDialog>(null,false);
43 if (window == null)
44 return false;
45
46 return window.IsPlasticInstalling;
47 }
48 }
49
50 // The plugin rely on the "cloudedition.token" to be created in the "plastic4e config folder, instead of checking
51 // the one in the UVCS installation directory, since it can run without an existing installation.
52 internal static class SetupUnityEditionToken
53 {
54 /// <summary>
55 /// If the UVCS installation directory is found, synchronize the token file from it.
56 /// Else, create the "cloudedition.token" file unconditionally so it's treated as a Cloud Edition going forward.
57 /// </summary>
58 internal static void CreateCloudEditionTokenIfNeeded()
59 {
60 string plasticClientBinDir = PlasticInstallPath.GetClientBinDir();
61
62 if (!string.IsNullOrEmpty(plasticClientBinDir))
63 {
64 FromPlasticInstallation(plasticClientBinDir);
65 return;
66 }
67
68 string tokenFilePath = UserConfigFolder.GetConfigFile(EditionToken.CLOUD_EDITION_FILE_NAME);
69
70 if (!File.Exists(tokenFilePath))
71 File.Create(tokenFilePath).Dispose();
72 }
73
74 // Synchronize the "cloudedition.token" file between the installation directory and the "plastic4" config folder
75 internal static void FromPlasticInstallation(string plasticClientBinDir)
76 {
77 bool isCloudPlasticInstall = IsPlasticInstallOfEdition(
78 plasticClientBinDir,
79 EditionToken.CLOUD_EDITION_FILE_NAME);
80
81 bool isDvcsPlasticInstall = IsPlasticInstallOfEdition(
82 plasticClientBinDir,
83 EditionToken.DVCS_EDITION_FILE_NAME);
84
85 SetupTokenFiles(
86 isCloudPlasticInstall,
87 isDvcsPlasticInstall);
88 }
89
90 static void SetupTokenFiles(
91 bool isCloudPlasticInstall,
92 bool isDvcsPlasticInstall)
93 {
94 string unityCloudEditionTokenFile = UserConfigFolder.GetConfigFile(
95 EditionToken.CLOUD_EDITION_FILE_NAME);
96
97 string unityDvcsEditionTokenFile = UserConfigFolder.GetConfigFile(
98 EditionToken.DVCS_EDITION_FILE_NAME);
99
100 CreateOrDeleteTokenFile(isCloudPlasticInstall, unityCloudEditionTokenFile);
101 CreateOrDeleteTokenFile(isDvcsPlasticInstall, unityDvcsEditionTokenFile);
102 }
103
104 static void CreateOrDeleteTokenFile(bool isEdition, string editionTokenFile)
105 {
106 if (isEdition && !File.Exists(editionTokenFile))
107 {
108 File.Create(editionTokenFile).Dispose();
109
110 return;
111 }
112
113 if (!isEdition && File.Exists(editionTokenFile))
114 {
115 File.Delete(editionTokenFile);
116
117 return;
118 }
119 }
120
121 static bool IsPlasticInstallOfEdition(
122 string plasticClientBinDir,
123 string editionFileName)
124 {
125 return File.Exists(Path.Combine(
126 plasticClientBinDir,
127 editionFileName));
128 }
129 }
130}