123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635 |
- //------------------------------------------------------------
- // Game Framework
- // Copyright © 2013-2021 loyalsoft. All rights reserved.
- // Homepage: http://www.game7000.com/
- // Feedback: http://www.game7000.com/
- //------------------------------------------------------------
- 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 != 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;
- }
- }
- }
|