forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildProfileDataSource.cs
More file actions
319 lines (267 loc) · 12.5 KB
/
Copy pathBuildProfileDataSource.cs
File metadata and controls
319 lines (267 loc) · 12.5 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace UnityEditor.Build.Profile.Handlers
{
internal class BuildProfileDataSource : IDisposable
{
internal IList<BuildProfile> classicPlatforms { get; }
internal IList<BuildProfile> customBuildProfiles { get; }
BuildProfileWindow m_Window;
List<BuildProfile> m_DuplicatedProfiles;
const string k_AssetFolderPath = "Assets/Settings/Build Profiles";
static string GetNewProfileName(string displayName) => $"{k_AssetFolderPath}/{displayName}.asset";
internal BuildProfileDataSource(BuildProfileWindow window)
{
this.m_Window = window;
classicPlatforms = BuildProfileContext.instance.classicPlatformProfiles;
customBuildProfiles = FindAllBuildProfiles();
m_DuplicatedProfiles = new List<BuildProfile>();
BuildProfile.AddOnBuildProfileEnable(OnBuildProfileCreated);
BuildProfileModuleUtil.CleanUpPlayerSettingsForDeletedBuildProfiles(currentBuildProfiles: customBuildProfiles);
}
public void Dispose()
{
BuildProfile.RemoveOnBuildProfileEnable(OnBuildProfileCreated);
}
/// <summary>
/// Removes all null unity objects from the custom profiles list.
/// </summary>
internal bool ClearDeletedProfiles()
{
bool changed = false;
for (int i = customBuildProfiles.Count - 1; i >= 0; --i)
{
var obj = customBuildProfiles[i];
if (obj != null)
continue;
if (BuildProfileContext.instance.activeProfile == obj)
BuildProfileContext.instance.activeProfile = null;
customBuildProfiles.RemoveAt(i);
changed = true;
}
if (changed)
BuildProfileModuleUtil.CleanUpPlayerSettingsForDeletedBuildProfiles(currentBuildProfiles: customBuildProfiles);
return changed;
}
/// <summary>
/// Helper function that takes a list of profiles and duplicates them
/// </summary>
internal List<BuildProfile> DuplicateProfiles(List<BuildProfile> profilesToDuplicate, bool isClassic)
{
m_DuplicatedProfiles.Clear();
var profilesCount = profilesToDuplicate.Count;
for (int i = 0; i < profilesCount; ++i)
{
var profile = profilesToDuplicate[i];
var duplicatedProfile = BuildProfileDataSource.DuplicateAsset(profile, isClassic);
if (duplicatedProfile != null)
m_DuplicatedProfiles.Add(duplicatedProfile);
}
// When duplicating an asset it will be created with the BaseName(Clone) in its name.
// At the time the proper name is set, OnEnable will be already called and the build
// profile will not be added in proper order, so we need to sort in here
SortCustomBuildProfiles();
return m_DuplicatedProfiles;
}
/// <summary>
/// Create a custom build profile asset, making sure to create the folders
/// if needed
/// </summary>
internal static void CreateAsset(string platformId, string displayName)
{
CheckCreateCustomBuildProfileFolders();
BuildProfile.CreateInstance(platformId, GetNewProfileName(displayName));
}
/// <summary>
/// Clone build profile and create new build profile asset based on it. The
/// build profile will be added to the custom build profile list on enable
/// </summary>
internal static BuildProfile DuplicateAsset(BuildProfile buildProfile, bool isClassic)
{
if (buildProfile == null)
return null;
string path = isClassic ? GetDuplicatedBuildProfilePathForClassic(buildProfile) : AssetDatabase.GetAssetPath(buildProfile);
if (string.IsNullOrEmpty(path))
return null;
BuildProfile duplicatedProfile = UnityEngine.Object.Instantiate(buildProfile);
// If it's a classic profile we need to copy the scenes from the editor build settings
// since classic profiles share scenes
if (isClassic)
duplicatedProfile.scenes = EditorBuildSettings.GetEditorBuildSettingsSceneIgnoreProfile();
CheckCreateCustomBuildProfileFolders();
string uniqueFilePath = AssetDatabase.GenerateUniqueAssetPath(path);
AssetDatabase.CreateAsset(duplicatedProfile, uniqueFilePath);
EditorAnalytics.SendAnalytic(new BuildProfileCreatedEvent(new BuildProfileCreatedEvent.Payload
{
creationType = (isClassic)
? BuildProfileCreatedEvent.CreationType.DuplicateClassic
: BuildProfileCreatedEvent.CreationType.DuplicateProfile,
platformId = duplicatedProfile.platformId,
platformDisplayName = BuildProfileModuleUtil.GetClassicPlatformDisplayName(duplicatedProfile.platformId),
}));
return duplicatedProfile;
}
/// <summary>
/// Delete build profile asset and remove from the list of
/// custom build profiles
/// </summary>
internal void DeleteAsset(BuildProfile buildProfile)
{
if (buildProfile == null || !customBuildProfiles.Contains(buildProfile))
return;
customBuildProfiles.Remove(buildProfile);
string assetPath = AssetDatabase.GetAssetPath(buildProfile);
if (!string.IsNullOrEmpty(assetPath))
{
BuildProfileModuleUtil.DeleteLastRunnableBuildKeyForProfile(buildProfile);
// We call DestroyImmediate so the build profile's OnDisable gets called
UnityEngine.Object.DestroyImmediate(buildProfile, allowDestroyingAssets: true);
AssetDatabase.DeleteAsset(assetPath);
}
}
internal void DeleteNullProfiles()
{
bool removedProfile = false;
for (int i = customBuildProfiles.Count - 1; i >= 0; i--)
{
if (customBuildProfiles[i] == null)
{
customBuildProfiles.RemoveAt(i);
removedProfile = true;
}
}
if (removedProfile)
{
BuildProfileModuleUtil.CleanUpPlayerSettingsForDeletedBuildProfiles(currentBuildProfiles: customBuildProfiles);
BuildProfileModuleUtil.DeleteLastRunnableBuildKeyForDeletedProfiles();
}
}
/// <summary>
/// Rename build profile asset and remove build profile from custom
/// build profile list. The build profile will be re-added when it
/// gets enabled after renaming.
/// </summary>
internal void RenameAsset(BuildProfile buildProfile, string newName)
{
if (buildProfile?.name == newName || string.IsNullOrEmpty(newName))
return;
var originalPath = AssetDatabase.GetAssetPath(buildProfile);
var newPath = ReplaceFileNameInPath(originalPath, newName);
var uniqueAssetPath = AssetDatabase.GenerateUniqueAssetPath(newPath);
var finalName = Path.GetFileNameWithoutExtension(uniqueAssetPath);
if (!string.IsNullOrEmpty(originalPath))
{
// Remove and rebuild list views before renaming to avoid
// list view 'SerializedObject of SerializedProperty has been
// Disposed' error
customBuildProfiles.Remove(buildProfile);
m_Window.RebuildProfileListViews();
AssetDatabase.RenameAsset(originalPath, finalName);
}
}
/// <summary>
/// Sort custom build profiles by name. Called by the Build Project Window after
/// all selected build profiles gets duplicated
/// </summary>
void SortCustomBuildProfiles()
{
List<BuildProfile> sortedProfiles = new List<BuildProfile>(customBuildProfiles);
sortedProfiles.Sort((lhs, rhs) => EditorUtility.NaturalCompare(lhs.name, rhs.name));
for (int i = 0; i < sortedProfiles.Count; i++)
{
customBuildProfiles[i] = sortedProfiles[i];
}
}
/// <summary>
/// This is called by <see cref="BuildProfile.OnEnable"/>
/// (creation originated by user or by code)
/// </summary>
void OnBuildProfileCreated(BuildProfile profile)
{
// Only track profiles stored in the assets folder.
if (profile.buildTarget == BuildTarget.NoTarget || string.IsNullOrEmpty(profile.name))
return;
if (BuildProfileContext.IsClassicPlatformProfile(profile))
return;
bool wasChanged = AddNewToCustomProfilesInOrder(profile);
if (wasChanged)
{
m_Window.RebuildProfileListViews();
}
}
/// <summary>
/// Adds a new build profile to the tracked sorted list of custom profiles.
/// </summary>
/// <returns>true, if profile was successfully appended. </returns>
bool AddNewToCustomProfilesInOrder(BuildProfile profile)
{
int index = 0;
foreach (var customBuildProfile in customBuildProfiles)
{
if (customBuildProfile == null)
{
// Consider case where a custom profile was deleted outside the editor.
// Cleanup list entry and try again.
customBuildProfiles.RemoveAt(index);
return AddNewToCustomProfilesInOrder(profile);
}
if (customBuildProfile == profile)
return false;
if (EditorUtility.NaturalCompare(customBuildProfile.name, profile.name) > 0)
{
customBuildProfiles.Insert(index, profile);
return true;
}
index++;
}
customBuildProfiles.Add(profile);
return true;
}
static List<BuildProfile> FindAllBuildProfiles()
{
const string buildProfileAssetSearchString = $"t:{nameof(BuildProfile)}";
var assetsGuids = AssetDatabase.FindAssets(buildProfileAssetSearchString);
var result = new List<BuildProfile>(assetsGuids.Length);
// Suppress missing type warning thrown by serialization. This could happen
// when the build profile window is opened, then entering play mode and the
// module for that profile is not installed.
BuildProfileModuleUtil.SuppressMissingTypeWarning();
foreach (var guid in assetsGuids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
BuildProfile profile = AssetDatabase.LoadAssetAtPath<BuildProfile>(path);
if (profile == null)
{
Debug.LogWarning($"[BuildProfile] Failed to load asset at path: {path}");
continue;
}
result.Add(profile);
}
result.Sort((lhs, rhs) => EditorUtility.NaturalCompare(lhs.name, rhs.name));
return result;
}
static string ReplaceFileNameInPath(string originalPath, string newName)
{
string directory = Path.GetDirectoryName(originalPath);
string extension = Path.GetExtension(originalPath);
return Path.Combine(directory, $"{newName}{extension}");
}
static string GetDuplicatedBuildProfilePathForClassic(BuildProfile buildProfile)
{
string name = BuildProfileModuleUtil.GetClassicPlatformDisplayName(buildProfile.platformId);
return Path.Combine(k_AssetFolderPath, $"{name}.asset");
}
static void CheckCreateCustomBuildProfileFolders()
{
if (!AssetDatabase.IsValidFolder("Assets/Settings"))
AssetDatabase.CreateFolder("Assets", "Settings");
if (!AssetDatabase.IsValidFolder(k_AssetFolderPath))
AssetDatabase.CreateFolder("Assets/Settings", "Build Profiles");
}
}
}