PackageCollection.cs
13.8 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UnityEditor.PackageManager.UI
{
[Serializable]
internal class PackageCollection
{
private const string k_UnityPackage = "Unity";
private static PackageCollection instance = new PackageCollection();
public static PackageCollection Instance { get { return instance; } }
public event Action<IEnumerable<Package>> OnPackagesChanged = delegate {};
public event Action<PackageFilter> OnFilterChanged = delegate {};
private readonly Dictionary<string, Package> packages;
private PackageFilter filter;
private string selectedListPackage;
private string selectedSearchUnityPackage;
private string selectedSearchOtherPackage;
private List<string> collapsedListGroups;
private List<string> collapsedSearchUnityGroups;
private List<string> collapsedSearchOtherGroups;
internal string lastUpdateTime;
private List<PackageInfo> listPackagesOffline;
private List<PackageInfo> listPackages;
private List<PackageInfo> searchUnityPackages;
private List<PackageInfo> searchOtherPackages;
private List<PackageError> packageErrors;
private int listPackagesVersion;
private int listPackagesOfflineVersion;
private bool searchOperationOngoing;
private bool listOperationOngoing;
private bool listOperationOfflineOngoing;
private IListOperation listOperationOffline;
private IListOperation listOperation;
private ISearchOperation searchOperation;
public readonly OperationSignal<ISearchOperation> SearchSignal = new OperationSignal<ISearchOperation>();
public readonly OperationSignal<IListOperation> ListSignal = new OperationSignal<IListOperation>();
public static void InitInstance(ref PackageCollection value)
{
if (value == null) // UI window opened
{
value = instance;
Instance.OnPackagesChanged = delegate {};
Instance.OnFilterChanged = delegate {};
Instance.SearchSignal.ResetEvents();
Instance.ListSignal.ResetEvents();
Instance.FetchListOfflineCache(true);
Instance.FetchListCache(true);
Instance.FetchSearchCache(true);
}
else // Domain reload
{
instance = value;
Instance.RebuildPackageDictionary();
// Resume operations interrupted by domain reload
Instance.FetchListOfflineCache(Instance.listOperationOfflineOngoing);
Instance.FetchListCache(Instance.listOperationOngoing);
Instance.FetchSearchCache(Instance.searchOperationOngoing);
}
}
public PackageFilter Filter
{
get { return filter; }
// For public usage, use SetFilter() instead
private set
{
var changed = value != filter;
if (changed)
{
var selectedPackageName = SelectedPackage;
Package package;
if (!string.IsNullOrEmpty(selectedPackageName) && packages.TryGetValue(selectedPackageName, out package))
{
var groupName = GetGroupName(package);
if (CollapsedGroups.Contains(groupName))
CollapsedGroups.Remove(groupName);
}
}
filter = value;
if (changed)
OnFilterChanged(filter);
}
}
public List<PackageInfo> LatestListPackages
{
get { return listPackagesVersion > listPackagesOfflineVersion ? listPackages : listPackagesOffline; }
}
public List<PackageInfo> LatestSearchUnityPackages { get { return searchUnityPackages; } }
public List<PackageInfo> LatestSearchOtherPackages { get { return searchOtherPackages; } }
public string SelectedPackage
{
get
{
switch (Filter)
{
case PackageFilter.Unity:
return selectedSearchUnityPackage;
case PackageFilter.Other:
return selectedSearchOtherPackage;
default:
return selectedListPackage;
}
}
set
{
switch (Filter)
{
case PackageFilter.Unity:
selectedSearchUnityPackage = value;
break;
case PackageFilter.Other:
selectedSearchOtherPackage = value;
break;
default:
selectedListPackage = value;
break;
}
}
}
public List<string> CollapsedGroups
{
get
{
switch (Filter)
{
case PackageFilter.Unity:
return collapsedSearchUnityGroups;
case PackageFilter.Other:
return collapsedSearchOtherGroups;
case PackageFilter.Local:
return collapsedListGroups;
default:
return new List<string>();
}
}
}
public static string GetGroupName(Package package)
{
if (package.IsBuiltIn)
return PackageGroupOrigins.BuiltInPackages.ToString();
else if (package.IsUnityPackage)
return k_UnityPackage;
else
return package.Latest != null ? package.Latest.Author : package.Current.Author;
}
private PackageCollection()
{
packages = new Dictionary<string, Package>();
listPackagesOffline = new List<PackageInfo>();
listPackages = new List<PackageInfo>();
searchUnityPackages = new List<PackageInfo>();
searchOtherPackages = new List<PackageInfo>();
collapsedListGroups = new List<string>();
collapsedSearchUnityGroups = new List<string>();
collapsedSearchOtherGroups = new List<string>();
packageErrors = new List<PackageError>();
listPackagesVersion = 0;
listPackagesOfflineVersion = 0;
searchOperationOngoing = false;
listOperationOngoing = false;
listOperationOfflineOngoing = false;
Filter = PackageFilter.Unity;
}
public bool SetFilter(PackageFilter value, bool refresh = true)
{
if (value == Filter)
return false;
Filter = value;
if (refresh)
{
UpdatePackageCollection();
}
return true;
}
public void UpdatePackageCollection(bool rebuildDictionary = false)
{
if (rebuildDictionary)
{
lastUpdateTime = DateTime.Now.ToString("HH:mm");
RebuildPackageDictionary();
}
if (packages.Any())
OnPackagesChanged(OrderedPackages());
}
internal void FetchListOfflineCache(bool forceRefetch = false)
{
if (!forceRefetch && (listOperationOfflineOngoing || listPackagesOffline.Any())) return;
if (listOperationOffline != null)
listOperationOffline.Cancel();
listOperationOfflineOngoing = true;
listOperationOffline = OperationFactory.Instance.CreateListOperation(true);
listOperationOffline.OnOperationFinalized += () =>
{
listOperationOfflineOngoing = false;
UpdatePackageCollection(true);
};
listOperationOffline.GetPackageListAsync(
infos =>
{
var version = listPackagesVersion;
UpdateListPackageInfosOffline(infos, version);
},
error => { Debug.LogError("Error fetching package list (offline mode)."); });
}
internal void FetchListCache(bool forceRefetch = false)
{
if (!forceRefetch && (listOperationOngoing || listPackages.Any())) return;
if (listOperation != null)
listOperation.Cancel();
listOperationOngoing = true;
listOperation = OperationFactory.Instance.CreateListOperation();
listOperation.OnOperationFinalized += () =>
{
listOperationOngoing = false;
UpdatePackageCollection(true);
};
listOperation.GetPackageListAsync(UpdateListPackageInfos,
error => { Debug.LogError("Error fetching package list."); });
ListSignal.SetOperation(listOperation);
}
internal void FetchSearchCache(bool forceRefetch = false)
{
if (!forceRefetch && (searchOperationOngoing || searchUnityPackages.Any() || searchOtherPackages.Any())) return;
if (searchOperation != null)
searchOperation.Cancel();
searchOperationOngoing = true;
searchOperation = OperationFactory.Instance.CreateSearchOperation();
searchOperation.OnOperationFinalized += () =>
{
searchOperationOngoing = false;
UpdatePackageCollection(true);
};
searchOperation.GetAllPackageAsync(UpdateSearchPackageInfos,
error => { Debug.LogError("Error searching packages online."); });
SearchSignal.SetOperation(searchOperation);
}
private void UpdateListPackageInfosOffline(IEnumerable<PackageInfo> newInfos, int version)
{
listPackagesOfflineVersion = version;
listPackagesOffline = newInfos.Where(p => p.IsUserVisible).ToList();
}
private void UpdateListPackageInfos(IEnumerable<PackageInfo> newInfos)
{
// Each time we fetch list packages, the cache for offline mode will be updated
// We keep track of the list packages version so that we know which version of cache
// we are getting with the offline fetch operation.
listPackagesVersion++;
listPackages = newInfos.Where(p => p.IsUserVisible).ToList();
listPackagesOffline = listPackages;
}
private void UpdateSearchPackageInfos(IEnumerable<PackageInfo> newInfos)
{
searchUnityPackages = newInfos.Where(p => p.IsUserVisible && p.IsUnityPackage).ToList();
searchOtherPackages = newInfos.Where(p => p.IsUserVisible && !p.IsUnityPackage).ToList();
if (!searchOtherPackages.Any() && Filter == PackageFilter.Other)
{
SetFilter(PackageFilter.Unity);
}
}
private IEnumerable<Package> OrderedPackages()
{
return packages.Values.OrderByDescending(pkg => pkg.IsUnityPackage).
ThenBy(pkg => pkg.VersionToDisplay.Author == "Other").
ThenBy(pkg => pkg.VersionToDisplay.Author).
ThenBy(pkg => pkg.Versions.LastOrDefault() == null ? pkg.Name : pkg.Versions.Last().DisplayName);
}
public Package GetPackageByName(string name)
{
Package package;
packages.TryGetValue(name, out package);
return package;
}
public Error GetPackageError(Package package)
{
if (null == package) return null;
var firstMatchingError = packageErrors.FirstOrDefault(p => p.PackageName == package.Name);
return firstMatchingError != null ? firstMatchingError.Error : null;
}
public void AddPackageError(Package package, Error error)
{
if (null == package || null == error) return;
packageErrors.Add(new PackageError(package.Name, error));
}
public void RemovePackageErrors(Package package)
{
if (null == package) return;
packageErrors.RemoveAll(p => p.PackageName == package.Name);
}
public void ResetExpandedGroups()
{
collapsedListGroups = new List<string>();
collapsedSearchUnityGroups = new List<string>();
collapsedSearchOtherGroups = new List<string>();
}
private void RebuildPackageDictionary()
{
// Merge list & search packages
var allPackageInfos = new List<PackageInfo>(LatestListPackages);
var installedPackageIds = new HashSet<string>(allPackageInfos.Select(p => p.PackageId));
allPackageInfos.AddRange(searchUnityPackages.Where(p => !installedPackageIds.Contains(p.PackageId)));
allPackageInfos.AddRange(searchOtherPackages.Where(p => !installedPackageIds.Contains(p.PackageId)));
if (!PackageManagerPrefs.ShowPreviewPackages)
{
allPackageInfos = allPackageInfos.Where(p => !p.IsPreRelease || installedPackageIds.Contains(p.PackageId)).ToList();
}
// Rebuild packages dictionary
packages.Clear();
foreach (var p in allPackageInfos)
{
var packageName = p.Name;
if (packages.ContainsKey(packageName))
continue;
var packageQuery = from pkg in allPackageInfos where pkg.Name == packageName select pkg;
var package = new Package(packageName, packageQuery);
packages[packageName] = package;
}
}
}
}