A game about forced loneliness, made by TACStudios
1using System;
2using System.Linq;
3
4using UnityEditor;
5using UnityEngine;
6
7using Codice.Client.Common;
8using Codice.Client.Common.Threading;
9using Codice.CM.Common;
10using Codice.LogWrapper;
11using PlasticGui.WorkspaceWindow.Home;
12using PlasticGui;
13
14namespace Unity.PlasticSCM.Editor.AssetUtils.Processor
15{
16 internal class UnityCloudProjectLinkMonitor : AssetModificationProcessor
17 {
18 internal class CloudSettings
19 {
20 internal string OrganizationId;
21 internal string OrganizationName;
22 internal string ProjectId;
23 internal string ProjectName;
24 }
25
26 /// <summary>
27 /// Factory class to retrieve the cloud settings for the current project.
28 /// </summary>
29 internal static Func<CloudSettings> CloudSettingsFactory = () => new CloudSettings()
30 {
31 OrganizationId = CloudProjectSettings.organizationId,
32 OrganizationName = CloudProjectSettings.organizationName,
33 ProjectId = CloudProjectSettings.projectId,
34 ProjectName = CloudProjectSettings.projectName
35 };
36
37 /// <summary>
38 /// Checks if the workspace project is aligned with the linked project in the cloud settings (if any).
39 /// If not, logs a warning message for users to inform about the issue and give indications on how to re-link.
40 /// </summary>
41 internal static void CheckCloudProjectAlignmentAsync(WorkspaceInfo wkInfo)
42 {
43 mLog.Debug("Checking for cloud project alignment...");
44
45 mCachedWkInfo = wkInfo;
46
47 CloudSettings cloudSettings = CloudSettingsFactory();
48
49 if (string.IsNullOrEmpty(cloudSettings.OrganizationId) || string.IsNullOrEmpty(cloudSettings.ProjectId))
50 {
51 mCachedCloudSettings = cloudSettings;
52
53 mLog.Debug("The project is not connected to Unity Cloud");
54 return;
55 }
56
57 if (cloudSettings.OrganizationId.Equals(mCachedCloudSettings.OrganizationId) &&
58 cloudSettings.ProjectId.Equals(mCachedCloudSettings.ProjectId))
59 {
60 mLog.Debug("The linked project didn't change");
61 return;
62 }
63
64 mCachedCloudSettings = cloudSettings;
65
66 CheckCloudProjectAlignmentAsync();
67 }
68
69 static void CheckCloudProjectAlignmentAsync()
70 {
71 PlasticThreadPool.Run(delegate
72 {
73 try
74 {
75 RepositorySpec repSpec = PlasticGui.Plastic.API.GetRepositorySpec(mCachedWkInfo);
76 if (repSpec == null || !OrganizationsInformation.IsUnityOrganization(repSpec.Server))
77 {
78 mLog.Debug("Skipping check for not covered organization");
79 return;
80 }
81
82 RepositoryInfo repositoryProject = ProjectInfo.ForRepSpec(repSpec);
83
84 if (repositoryProject == null || !repositoryProject.GUID.ToString().Equals(mCachedCloudSettings.ProjectId))
85 {
86 string repositoryOrganizationName = CloudServer.GetOrganizationName(ResolveServer.ToDisplayString(repSpec.Server));
87 string repositoryProjectName = CloudProject.GetProjectName(repSpec.Name);
88
89 LogMismatchingProject(
90 mCachedCloudSettings.OrganizationName, mCachedCloudSettings.ProjectName,
91 repositoryOrganizationName, repositoryProjectName);
92 }
93 else
94 {
95 mLog.Debug("The linked cloud project is properly aligned");
96 }
97 }
98 catch (Exception e)
99 {
100 ExceptionsHandler.LogException(typeof(UnityCloudProjectLinkMonitor).Name, e);
101 }
102 });
103 }
104
105 static void LogMismatchingProject(string cloudOrganization, string cloudProject, string repoOrganization, string repoProject)
106 {
107 string localOrganizationAndProject = string.Format("{0}/{1}", repoOrganization, repoProject);
108 string cloudOrganizationAndProject = string.Format("{0}/{1}", cloudOrganization, cloudProject);
109
110 string mismatchingProjectMessage = string.Format(
111 PlasticLocalization.Name.MismatchingRepositoryProjectMessage.GetString(),
112 cloudOrganizationAndProject,
113 localOrganizationAndProject,
114 localOrganizationAndProject,
115 repoOrganization,
116 localOrganizationAndProject,
117 cloudOrganizationAndProject
118 );
119
120 Debug.LogWarning(mismatchingProjectMessage);
121 mLog.Warn(mismatchingProjectMessage);
122 }
123
124 static string[] OnWillSaveAssets(string[] paths)
125 {
126 if (mCachedWkInfo != null && paths.Any(path => path.Equals(ProjectSettingsAsset)))
127 {
128 CheckCloudProjectAlignmentAsync(mCachedWkInfo);
129 }
130
131 return paths;
132 }
133
134 static WorkspaceInfo mCachedWkInfo;
135 static CloudSettings mCachedCloudSettings = new CloudSettings();
136
137 static readonly string ProjectSettingsAsset = "ProjectSettings/ProjectSettings.asset";
138
139 static readonly ILog mLog = PlasticApp.GetLogger("UnityCloudProjectLinkMonitor");
140 }
141}