-
Notifications
You must be signed in to change notification settings - Fork 561
Expand file tree
/
Copy pathResourceCollection.cs
More file actions
635 lines (528 loc) · 20.7 KB
/
ResourceCollection.cs
File metadata and controls
635 lines (528 loc) · 20.7 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------
using GameFramework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using UnityEditor;
using UnityEngine;
namespace UnityGameFramework.Editor.ResourceTools
{
/// <summary>
/// 资源集合。
/// </summary>
public sealed class ResourceCollection
{
private const string SceneExtension = ".unity";
private static readonly Regex ResourceNameRegex = new Regex(@"^([A-Za-z0-9\._-]+/)*[A-Za-z0-9\._-]+$");
private static readonly Regex ResourceVariantRegex = new Regex(@"^[a-z0-9_-]+$");
private readonly string m_ConfigurationPath;
private readonly SortedDictionary<string, Resource> m_Resources;
private readonly SortedDictionary<string, Asset> m_Assets;
public ResourceCollection()
{
m_ConfigurationPath = Type.GetConfigurationPath<ResourceCollectionConfigPathAttribute>() ?? Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "GameFramework/Configs/ResourceCollection.xml"));
m_Resources = new SortedDictionary<string, Resource>(StringComparer.Ordinal);
m_Assets = new SortedDictionary<string, Asset>(StringComparer.Ordinal);
}
public int ResourceCount
{
get
{
return m_Resources.Count;
}
}
public int AssetCount
{
get
{
return m_Assets.Count;
}
}
public event GameFrameworkAction<int, int> OnLoadingResource = null;
public event GameFrameworkAction<int, int> OnLoadingAsset = null;
public event GameFrameworkAction OnLoadCompleted = null;
public void Clear()
{
m_Resources.Clear();
m_Assets.Clear();
}
public bool Load()
{
Clear();
if (!File.Exists(m_ConfigurationPath))
{
return false;
}
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(m_ConfigurationPath);
XmlNode xmlRoot = xmlDocument.SelectSingleNode("UnityGameFramework");
XmlNode xmlCollection = xmlRoot.SelectSingleNode("ResourceCollection");
XmlNode xmlResources = xmlCollection.SelectSingleNode("Resources");
XmlNode xmlAssets = xmlCollection.SelectSingleNode("Assets");
XmlNodeList xmlNodeList = null;
XmlNode xmlNode = null;
int count = 0;
xmlNodeList = xmlResources.ChildNodes;
count = xmlNodeList.Count;
for (int i = 0; i < count; i++)
{
if (OnLoadingResource != null)
{
OnLoadingResource(i, count);
}
xmlNode = xmlNodeList.Item(i);
if (xmlNode.Name != "Resource")
{
continue;
}
string name = xmlNode.Attributes.GetNamedItem("Name").Value;
string variant = xmlNode.Attributes.GetNamedItem("Variant") != null ? xmlNode.Attributes.GetNamedItem("Variant").Value : null;
string fileSystem = xmlNode.Attributes.GetNamedItem("FileSystem") != null ? xmlNode.Attributes.GetNamedItem("FileSystem").Value : null;
byte loadType = 0;
if (xmlNode.Attributes.GetNamedItem("LoadType") != null)
{
byte.TryParse(xmlNode.Attributes.GetNamedItem("LoadType").Value, out loadType);
}
bool packed = false;
if (xmlNode.Attributes.GetNamedItem("Packed") != null)
{
bool.TryParse(xmlNode.Attributes.GetNamedItem("Packed").Value, out packed);
}
string[] resourceGroups = xmlNode.Attributes.GetNamedItem("ResourceGroups") != null ? xmlNode.Attributes.GetNamedItem("ResourceGroups").Value.Split(',') : null;
if (!AddResource(name, variant, fileSystem, (LoadType)loadType, packed, resourceGroups))
{
Debug.LogWarning(Utility.Text.Format("Can not add resource '{0}'.", GetResourceFullName(name, variant)));
continue;
}
}
xmlNodeList = xmlAssets.ChildNodes;
count = xmlNodeList.Count;
for (int i = 0; i < count; i++)
{
if (OnLoadingAsset != null)
{
OnLoadingAsset(i, count);
}
xmlNode = xmlNodeList.Item(i);
if (xmlNode.Name != "Asset")
{
continue;
}
string guid = xmlNode.Attributes.GetNamedItem("Guid").Value;
string name = xmlNode.Attributes.GetNamedItem("ResourceName").Value;
string variant = xmlNode.Attributes.GetNamedItem("ResourceVariant") != null ? xmlNode.Attributes.GetNamedItem("ResourceVariant").Value : null;
if (!AssignAsset(guid, name, variant))
{
Debug.LogWarning(Utility.Text.Format("Can not assign asset '{0}' to resource '{1}'.", guid, GetResourceFullName(name, variant)));
continue;
}
}
if (OnLoadCompleted != null)
{
OnLoadCompleted();
}
return true;
}
catch
{
File.Delete(m_ConfigurationPath);
if (OnLoadCompleted != null)
{
OnLoadCompleted();
}
return false;
}
}
public bool Save()
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null));
XmlElement xmlRoot = xmlDocument.CreateElement("UnityGameFramework");
xmlDocument.AppendChild(xmlRoot);
XmlElement xmlCollection = xmlDocument.CreateElement("ResourceCollection");
xmlRoot.AppendChild(xmlCollection);
XmlElement xmlResources = xmlDocument.CreateElement("Resources");
xmlCollection.AppendChild(xmlResources);
XmlElement xmlAssets = xmlDocument.CreateElement("Assets");
xmlCollection.AppendChild(xmlAssets);
XmlElement xmlElement = null;
XmlAttribute xmlAttribute = null;
foreach (Resource resource in m_Resources.Values)
{
xmlElement = xmlDocument.CreateElement("Resource");
xmlAttribute = xmlDocument.CreateAttribute("Name");
xmlAttribute.Value = resource.Name;
xmlElement.Attributes.SetNamedItem(xmlAttribute);
if (resource.Variant != null)
{
xmlAttribute = xmlDocument.CreateAttribute("Variant");
xmlAttribute.Value = resource.Variant;
xmlElement.Attributes.SetNamedItem(xmlAttribute);
}
if (resource.FileSystem != null)
{
xmlAttribute = xmlDocument.CreateAttribute("FileSystem");
xmlAttribute.Value = resource.FileSystem;
xmlElement.Attributes.SetNamedItem(xmlAttribute);
}
xmlAttribute = xmlDocument.CreateAttribute("LoadType");
xmlAttribute.Value = ((byte)resource.LoadType).ToString();
xmlElement.Attributes.SetNamedItem(xmlAttribute);
xmlAttribute = xmlDocument.CreateAttribute("Packed");
xmlAttribute.Value = resource.Packed.ToString();
xmlElement.Attributes.SetNamedItem(xmlAttribute);
string[] resourceGroups = resource.GetResourceGroups();
if (resourceGroups.Length > 0)
{
xmlAttribute = xmlDocument.CreateAttribute("ResourceGroups");
xmlAttribute.Value = string.Join(",", resourceGroups);
xmlElement.Attributes.SetNamedItem(xmlAttribute);
}
xmlResources.AppendChild(xmlElement);
}
foreach (Asset asset in m_Assets.Values)
{
xmlElement = xmlDocument.CreateElement("Asset");
xmlAttribute = xmlDocument.CreateAttribute("Guid");
xmlAttribute.Value = asset.Guid;
xmlElement.Attributes.SetNamedItem(xmlAttribute);
xmlAttribute = xmlDocument.CreateAttribute("ResourceName");
xmlAttribute.Value = asset.Resource.Name;
xmlElement.Attributes.SetNamedItem(xmlAttribute);
if (asset.Resource.Variant != null)
{
xmlAttribute = xmlDocument.CreateAttribute("ResourceVariant");
xmlAttribute.Value = asset.Resource.Variant;
xmlElement.Attributes.SetNamedItem(xmlAttribute);
}
xmlAssets.AppendChild(xmlElement);
}
string configurationDirectoryName = Path.GetDirectoryName(m_ConfigurationPath);
if (!Directory.Exists(configurationDirectoryName))
{
Directory.CreateDirectory(configurationDirectoryName);
}
xmlDocument.Save(m_ConfigurationPath);
AssetDatabase.Refresh();
return true;
}
catch
{
if (File.Exists(m_ConfigurationPath))
{
File.Delete(m_ConfigurationPath);
}
return false;
}
}
public Resource[] GetResources()
{
return m_Resources.Values.ToArray();
}
public Resource GetResource(string name, string variant)
{
if (!IsValidResourceName(name, variant))
{
return null;
}
Resource resource = null;
if (m_Resources.TryGetValue(GetResourceFullName(name, variant).ToLowerInvariant(), out resource))
{
return resource;
}
return null;
}
public bool HasResource(string name, string variant)
{
if (!IsValidResourceName(name, variant))
{
return false;
}
return m_Resources.ContainsKey(GetResourceFullName(name, variant).ToLowerInvariant());
}
public bool AddResource(string name, string variant, string fileSystem, LoadType loadType, bool packed)
{
return AddResource(name, variant, fileSystem, loadType, packed, null);
}
public bool AddResource(string name, string variant, string fileSystem, LoadType loadType, bool packed, string[] resourceGroups)
{
if (!IsValidResourceName(name, variant))
{
return false;
}
if (!IsAvailableResourceName(name, variant, null))
{
return false;
}
if (fileSystem != null && !ResourceNameRegex.IsMatch(fileSystem))
{
return false;
}
Resource resource = Resource.Create(name, variant, fileSystem, loadType, packed, resourceGroups);
m_Resources.Add(resource.FullName.ToLowerInvariant(), resource);
return true;
}
public bool RenameResource(string oldName, string oldVariant, string newName, string newVariant)
{
if (!IsValidResourceName(oldName, oldVariant) || !IsValidResourceName(newName, newVariant))
{
return false;
}
Resource resource = GetResource(oldName, oldVariant);
if (resource == null)
{
return false;
}
if (oldName == newName && oldVariant == newVariant)
{
return true;
}
if (!IsAvailableResourceName(newName, newVariant, resource))
{
return false;
}
m_Resources.Remove(resource.FullName.ToLowerInvariant());
resource.Rename(newName, newVariant);
m_Resources.Add(resource.FullName.ToLowerInvariant(), resource);
return true;
}
public bool RemoveResource(string name, string variant)
{
if (!IsValidResourceName(name, variant))
{
return false;
}
Resource resource = GetResource(name, variant);
if (resource == null)
{
return false;
}
Asset[] assets = resource.GetAssets();
resource.Clear();
m_Resources.Remove(resource.FullName.ToLowerInvariant());
foreach (Asset asset in assets)
{
m_Assets.Remove(asset.Guid);
}
return true;
}
public bool SetResourceLoadType(string name, string variant, LoadType loadType)
{
if (!IsValidResourceName(name, variant))
{
return false;
}
Resource resource = GetResource(name, variant);
if (resource == null)
{
return false;
}
if ((loadType == LoadType.LoadFromBinary || loadType == LoadType.LoadFromBinaryAndQuickDecrypt || loadType == LoadType.LoadFromBinaryAndDecrypt) && resource.GetAssets().Length > 1)
{
return false;
}
resource.LoadType = loadType;
return true;
}
public bool SetResourcePacked(string name, string variant, bool packed)
{
if (!IsValidResourceName(name, variant))
{
return false;
}
Resource resource = GetResource(name, variant);
if (resource == null)
{
return false;
}
resource.Packed = packed;
return true;
}
public Asset[] GetAssets()
{
return m_Assets.Values.ToArray();
}
public Asset[] GetAssets(string name, string variant)
{
if (!IsValidResourceName(name, variant))
{
return new Asset[0];
}
Resource resource = GetResource(name, variant);
if (resource == null)
{
return new Asset[0];
}
return resource.GetAssets();
}
public Asset GetAsset(string guid)
{
if (string.IsNullOrEmpty(guid))
{
return null;
}
Asset asset = null;
if (m_Assets.TryGetValue(guid, out asset))
{
return asset;
}
return null;
}
public bool HasAsset(string guid)
{
if (string.IsNullOrEmpty(guid))
{
return false;
}
return m_Assets.ContainsKey(guid);
}
public bool AssignAsset(string guid, string name, string variant)
{
if (string.IsNullOrEmpty(guid))
{
return false;
}
if (!IsValidResourceName(name, variant))
{
return false;
}
Resource resource = GetResource(name, variant);
if (resource == null)
{
return false;
}
string assetName = AssetDatabase.GUIDToAssetPath(guid);
if (string.IsNullOrEmpty(assetName))
{
return false;
}
Asset[] assetsInResource = resource.GetAssets();
foreach (Asset assetInResource in assetsInResource)
{
if (assetInResource.Name == assetName)
{
continue;
}
if (assetInResource.Name.ToLowerInvariant() == assetName.ToLowerInvariant())
{
return false;
}
}
bool isScene = assetName.EndsWith(SceneExtension, StringComparison.Ordinal);
if (isScene && resource.AssetType == AssetType.Asset || !isScene && resource.AssetType == AssetType.Scene)
{
return false;
}
Asset asset = GetAsset(guid);
if (resource.IsLoadFromBinary && assetsInResource.Length > 0 && asset != null && asset != assetsInResource[0])
{
return false;
}
if (asset == null)
{
asset = Asset.Create(guid);
m_Assets.Add(asset.Guid, asset);
}
resource.AssignAsset(asset, isScene);
return true;
}
public bool UnassignAsset(string guid)
{
if (string.IsNullOrEmpty(guid))
{
return false;
}
Asset asset = GetAsset(guid);
if (asset != null)
{
asset.Resource.UnassignAsset(asset);
m_Assets.Remove(asset.Guid);
}
return true;
}
private string GetResourceFullName(string name, string variant)
{
return !string.IsNullOrEmpty(variant) ? Utility.Text.Format("{0}.{1}", name, variant) : name;
}
private bool IsValidResourceName(string name, string variant)
{
if (string.IsNullOrEmpty(name))
{
return false;
}
if (!ResourceNameRegex.IsMatch(name))
{
return false;
}
if (variant != null && !ResourceVariantRegex.IsMatch(variant))
{
return false;
}
return true;
}
private bool IsAvailableResourceName(string name, string variant, Resource current)
{
Resource found = GetResource(name, variant);
if (found != null && found != current)
{
return false;
}
string[] foundPathNames = name.Split('/');
foreach (Resource resource in m_Resources.Values)
{
if (current != null && resource == current)
{
continue;
}
if (resource.Name == name)
{
if (resource.Variant == null && variant != null)
{
return false;
}
if (resource.Variant != null && variant == null)
{
return false;
}
}
if (resource.Name.Length > name.Length
&& resource.Name.IndexOf(name, StringComparison.CurrentCultureIgnoreCase) == 0
&& resource.Name[name.Length] == '/')
{
return false;
}
if (name.Length > resource.Name.Length
&& name.IndexOf(resource.Name, StringComparison.CurrentCultureIgnoreCase) == 0
&& name[resource.Name.Length] == '/')
{
return false;
}
string[] pathNames = resource.Name.Split('/');
for (int i = 0; i < foundPathNames.Length - 1 && i < pathNames.Length - 1; i++)
{
if (foundPathNames[i].ToLowerInvariant() != pathNames[i].ToLowerInvariant())
{
break;
}
if (foundPathNames[i] != pathNames[i])
{
return false;
}
}
}
return true;
}
}
}