123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679 |
- //------------------------------------------------------------
- // 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.Xml;
- using UnityEditor;
- using UnityEngine;
- namespace UnityGameFramework.Editor.ResourceTools
- {
- public sealed class ResourceEditorController
- {
- private const string DefaultSourceAssetRootPath = "Assets";
- private readonly string m_ConfigurationPath;
- private readonly ResourceCollection m_ResourceCollection;
- private readonly List<string> m_SourceAssetSearchPaths;
- private readonly List<string> m_SourceAssetSearchRelativePaths;
- private readonly Dictionary<string, SourceAsset> m_SourceAssets;
- private SourceFolder m_SourceAssetRoot;
- private string m_SourceAssetRootPath;
- private string m_SourceAssetUnionTypeFilter;
- private string m_SourceAssetUnionLabelFilter;
- private string m_SourceAssetExceptTypeFilter;
- private string m_SourceAssetExceptLabelFilter;
- private AssetSorterType m_AssetSorter;
- public ResourceEditorController()
- {
- m_ConfigurationPath = Type.GetConfigurationPath<ResourceEditorConfigPathAttribute>() ?? Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "GameFramework/Configs/ResourceEditor.xml"));
- m_ResourceCollection = new ResourceCollection();
- m_ResourceCollection.OnLoadingResource += delegate (int index, int count)
- {
- if (OnLoadingResource != null)
- {
- OnLoadingResource(index, count);
- }
- };
- m_ResourceCollection.OnLoadingAsset += delegate (int index, int count)
- {
- if (OnLoadingAsset != null)
- {
- OnLoadingAsset(index, count);
- }
- };
- m_ResourceCollection.OnLoadCompleted += delegate ()
- {
- if (OnLoadCompleted != null)
- {
- OnLoadCompleted();
- }
- };
- m_SourceAssetSearchPaths = new List<string>();
- m_SourceAssetSearchRelativePaths = new List<string>();
- m_SourceAssets = new Dictionary<string, SourceAsset>(StringComparer.Ordinal);
- m_SourceAssetRoot = null;
- m_SourceAssetRootPath = null;
- m_SourceAssetUnionTypeFilter = null;
- m_SourceAssetUnionLabelFilter = null;
- m_SourceAssetExceptTypeFilter = null;
- m_SourceAssetExceptLabelFilter = null;
- m_AssetSorter = AssetSorterType.Path;
- SourceAssetRootPath = DefaultSourceAssetRootPath;
- }
- public int ResourceCount
- {
- get
- {
- return m_ResourceCollection.ResourceCount;
- }
- }
- public int AssetCount
- {
- get
- {
- return m_ResourceCollection.AssetCount;
- }
- }
- public SourceFolder SourceAssetRoot
- {
- get
- {
- return m_SourceAssetRoot;
- }
- }
- public string SourceAssetRootPath
- {
- get
- {
- return m_SourceAssetRootPath;
- }
- set
- {
- if (m_SourceAssetRootPath == value)
- {
- return;
- }
- m_SourceAssetRootPath = value.Replace('\\', '/');
- m_SourceAssetRoot = new SourceFolder(m_SourceAssetRootPath, null);
- RefreshSourceAssetSearchPaths();
- }
- }
- public string SourceAssetUnionTypeFilter
- {
- get
- {
- return m_SourceAssetUnionTypeFilter;
- }
- set
- {
- if (m_SourceAssetUnionTypeFilter == value)
- {
- return;
- }
- m_SourceAssetUnionTypeFilter = value;
- }
- }
- public string SourceAssetUnionLabelFilter
- {
- get
- {
- return m_SourceAssetUnionLabelFilter;
- }
- set
- {
- if (m_SourceAssetUnionLabelFilter == value)
- {
- return;
- }
- m_SourceAssetUnionLabelFilter = value;
- }
- }
- public string SourceAssetExceptTypeFilter
- {
- get
- {
- return m_SourceAssetExceptTypeFilter;
- }
- set
- {
- if (m_SourceAssetExceptTypeFilter == value)
- {
- return;
- }
- m_SourceAssetExceptTypeFilter = value;
- }
- }
- public string SourceAssetExceptLabelFilter
- {
- get
- {
- return m_SourceAssetExceptLabelFilter;
- }
- set
- {
- if (m_SourceAssetExceptLabelFilter == value)
- {
- return;
- }
- m_SourceAssetExceptLabelFilter = value;
- }
- }
- public AssetSorterType AssetSorter
- {
- get
- {
- return m_AssetSorter;
- }
- set
- {
- if (m_AssetSorter == value)
- {
- return;
- }
- m_AssetSorter = value;
- }
- }
- public event GameFrameworkAction<int, int> OnLoadingResource = null;
- public event GameFrameworkAction<int, int> OnLoadingAsset = null;
- public event GameFrameworkAction OnLoadCompleted = null;
- public event GameFrameworkAction<SourceAsset[]> OnAssetAssigned = null;
- public event GameFrameworkAction<SourceAsset[]> OnAssetUnassigned = null;
- public bool Load()
- {
- if (!File.Exists(m_ConfigurationPath))
- {
- return false;
- }
- try
- {
- XmlDocument xmlDocument = new XmlDocument();
- xmlDocument.Load(m_ConfigurationPath);
- XmlNode xmlRoot = xmlDocument.SelectSingleNode("UnityGameFramework");
- XmlNode xmlEditor = xmlRoot.SelectSingleNode("ResourceEditor");
- XmlNode xmlSettings = xmlEditor.SelectSingleNode("Settings");
- XmlNodeList xmlNodeList = null;
- XmlNode xmlNode = null;
- xmlNodeList = xmlSettings.ChildNodes;
- for (int i = 0; i < xmlNodeList.Count; i++)
- {
- xmlNode = xmlNodeList.Item(i);
- switch (xmlNode.Name)
- {
- case "SourceAssetRootPath":
- SourceAssetRootPath = xmlNode.InnerText;
- break;
- case "SourceAssetSearchPaths":
- m_SourceAssetSearchRelativePaths.Clear();
- XmlNodeList xmlNodeListInner = xmlNode.ChildNodes;
- XmlNode xmlNodeInner = null;
- for (int j = 0; j < xmlNodeListInner.Count; j++)
- {
- xmlNodeInner = xmlNodeListInner.Item(j);
- if (xmlNodeInner.Name != "SourceAssetSearchPath")
- {
- continue;
- }
- m_SourceAssetSearchRelativePaths.Add(xmlNodeInner.Attributes.GetNamedItem("RelativePath").Value);
- }
- break;
- case "SourceAssetUnionTypeFilter":
- SourceAssetUnionTypeFilter = xmlNode.InnerText;
- break;
- case "SourceAssetUnionLabelFilter":
- SourceAssetUnionLabelFilter = xmlNode.InnerText;
- break;
- case "SourceAssetExceptTypeFilter":
- SourceAssetExceptTypeFilter = xmlNode.InnerText;
- break;
- case "SourceAssetExceptLabelFilter":
- SourceAssetExceptLabelFilter = xmlNode.InnerText;
- break;
- case "AssetSorter":
- AssetSorter = (AssetSorterType)Enum.Parse(typeof(AssetSorterType), xmlNode.InnerText);
- break;
- }
- }
- RefreshSourceAssetSearchPaths();
- }
- catch
- {
- File.Delete(m_ConfigurationPath);
- return false;
- }
- ScanSourceAssets();
- m_ResourceCollection.Load();
- return true;
- }
- 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 xmlEditor = xmlDocument.CreateElement("ResourceEditor");
- xmlRoot.AppendChild(xmlEditor);
- XmlElement xmlSettings = xmlDocument.CreateElement("Settings");
- xmlEditor.AppendChild(xmlSettings);
- XmlElement xmlElement = null;
- XmlAttribute xmlAttribute = null;
- xmlElement = xmlDocument.CreateElement("SourceAssetRootPath");
- xmlElement.InnerText = SourceAssetRootPath.ToString();
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("SourceAssetSearchPaths");
- xmlSettings.AppendChild(xmlElement);
- foreach (string sourceAssetSearchRelativePath in m_SourceAssetSearchRelativePaths)
- {
- XmlElement xmlElementInner = xmlDocument.CreateElement("SourceAssetSearchPath");
- xmlAttribute = xmlDocument.CreateAttribute("RelativePath");
- xmlAttribute.Value = sourceAssetSearchRelativePath;
- xmlElementInner.Attributes.SetNamedItem(xmlAttribute);
- xmlElement.AppendChild(xmlElementInner);
- }
- xmlElement = xmlDocument.CreateElement("SourceAssetUnionTypeFilter");
- xmlElement.InnerText = SourceAssetUnionTypeFilter ?? string.Empty;
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("SourceAssetUnionLabelFilter");
- xmlElement.InnerText = SourceAssetUnionLabelFilter ?? string.Empty;
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("SourceAssetExceptTypeFilter");
- xmlElement.InnerText = SourceAssetExceptTypeFilter ?? string.Empty;
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("SourceAssetExceptLabelFilter");
- xmlElement.InnerText = SourceAssetExceptLabelFilter ?? string.Empty;
- xmlSettings.AppendChild(xmlElement);
- xmlElement = xmlDocument.CreateElement("AssetSorter");
- xmlElement.InnerText = AssetSorter.ToString();
- xmlSettings.AppendChild(xmlElement);
- string configurationDirectoryName = Path.GetDirectoryName(m_ConfigurationPath);
- if (!Directory.Exists(configurationDirectoryName))
- {
- Directory.CreateDirectory(configurationDirectoryName);
- }
- xmlDocument.Save(m_ConfigurationPath);
- AssetDatabase.Refresh();
- }
- catch
- {
- if (File.Exists(m_ConfigurationPath))
- {
- File.Delete(m_ConfigurationPath);
- }
- return false;
- }
- return m_ResourceCollection.Save();
- }
- public Resource[] GetResources()
- {
- return m_ResourceCollection.GetResources();
- }
- public Resource GetResource(string name, string variant)
- {
- return m_ResourceCollection.GetResource(name, variant);
- }
- public bool HasResource(string name, string variant)
- {
- return m_ResourceCollection.HasResource(name, variant);
- }
- public bool AddResource(string name, string variant, string fileSystem, LoadType loadType, bool packed)
- {
- return m_ResourceCollection.AddResource(name, variant, fileSystem, loadType, packed);
- }
- public bool RenameResource(string oldName, string oldVariant, string newName, string newVariant)
- {
- return m_ResourceCollection.RenameResource(oldName, oldVariant, newName, newVariant);
- }
- public bool RemoveResource(string name, string variant)
- {
- Asset[] assetsToRemove = m_ResourceCollection.GetAssets(name, variant);
- if (m_ResourceCollection.RemoveResource(name, variant))
- {
- List<SourceAsset> unassignedSourceAssets = new List<SourceAsset>();
- foreach (Asset asset in assetsToRemove)
- {
- SourceAsset sourceAsset = GetSourceAsset(asset.Guid);
- if (sourceAsset != null)
- {
- unassignedSourceAssets.Add(sourceAsset);
- }
- }
- if (OnAssetUnassigned != null)
- {
- OnAssetUnassigned(unassignedSourceAssets.ToArray());
- }
- return true;
- }
- return false;
- }
- public bool SetResourceLoadType(string name, string variant, LoadType loadType)
- {
- return m_ResourceCollection.SetResourceLoadType(name, variant, loadType);
- }
- public bool SetResourcePacked(string name, string variant, bool packed)
- {
- return m_ResourceCollection.SetResourcePacked(name, variant, packed);
- }
- public int RemoveUnusedResources()
- {
- List<Resource> resources = new List<Resource>(m_ResourceCollection.GetResources());
- List<Resource> removeResources = resources.FindAll(resource => GetAssets(resource.Name, resource.Variant).Length <= 0);
- foreach (Resource removeResource in removeResources)
- {
- m_ResourceCollection.RemoveResource(removeResource.Name, removeResource.Variant);
- }
- return removeResources.Count;
- }
- public Asset[] GetAssets(string name, string variant)
- {
- List<Asset> assets = new List<Asset>(m_ResourceCollection.GetAssets(name, variant));
- switch (AssetSorter)
- {
- case AssetSorterType.Path:
- assets.Sort(AssetPathComparer);
- break;
- case AssetSorterType.Name:
- assets.Sort(AssetNameComparer);
- break;
- case AssetSorterType.Guid:
- assets.Sort(AssetGuidComparer);
- break;
- }
- return assets.ToArray();
- }
- public Asset GetAsset(string guid)
- {
- return m_ResourceCollection.GetAsset(guid);
- }
- public bool AssignAsset(string guid, string name, string variant)
- {
- if (m_ResourceCollection.AssignAsset(guid, name, variant))
- {
- if (OnAssetAssigned != null)
- {
- OnAssetAssigned(new SourceAsset[] { GetSourceAsset(guid) });
- }
- return true;
- }
- return false;
- }
- public bool UnassignAsset(string guid)
- {
- if (m_ResourceCollection.UnassignAsset(guid))
- {
- SourceAsset sourceAsset = GetSourceAsset(guid);
- if (sourceAsset != null)
- {
- if (OnAssetUnassigned != null)
- {
- OnAssetUnassigned(new SourceAsset[] { sourceAsset });
- }
- }
- return true;
- }
- return false;
- }
- public int RemoveUnknownAssets()
- {
- List<Asset> assets = new List<Asset>(m_ResourceCollection.GetAssets());
- List<Asset> removeAssets = assets.FindAll(asset => GetSourceAsset(asset.Guid) == null);
- foreach (Asset asset in removeAssets)
- {
- m_ResourceCollection.UnassignAsset(asset.Guid);
- }
- return removeAssets.Count;
- }
- public SourceAsset[] GetSourceAssets()
- {
- int count = 0;
- SourceAsset[] sourceAssets = new SourceAsset[m_SourceAssets.Count];
- foreach (KeyValuePair<string, SourceAsset> sourceAsset in m_SourceAssets)
- {
- sourceAssets[count++] = sourceAsset.Value;
- }
- return sourceAssets;
- }
- public SourceAsset GetSourceAsset(string guid)
- {
- if (string.IsNullOrEmpty(guid))
- {
- return null;
- }
- SourceAsset sourceAsset = null;
- if (m_SourceAssets.TryGetValue(guid, out sourceAsset))
- {
- return sourceAsset;
- }
- return null;
- }
- public void ScanSourceAssets()
- {
- m_SourceAssets.Clear();
- m_SourceAssetRoot.Clear();
- string[] sourceAssetSearchPaths = m_SourceAssetSearchPaths.ToArray();
- HashSet<string> tempGuids = new HashSet<string>();
- tempGuids.UnionWith(AssetDatabase.FindAssets(SourceAssetUnionTypeFilter, sourceAssetSearchPaths));
- tempGuids.UnionWith(AssetDatabase.FindAssets(SourceAssetUnionLabelFilter, sourceAssetSearchPaths));
- tempGuids.ExceptWith(AssetDatabase.FindAssets(SourceAssetExceptTypeFilter, sourceAssetSearchPaths));
- tempGuids.ExceptWith(AssetDatabase.FindAssets(SourceAssetExceptLabelFilter, sourceAssetSearchPaths));
- string[] guids = new List<string>(tempGuids).ToArray();
- foreach (string guid in guids)
- {
- string fullPath = AssetDatabase.GUIDToAssetPath(guid);
- if (AssetDatabase.IsValidFolder(fullPath))
- {
- // Skip folder.
- continue;
- }
- string assetPath = fullPath.Substring(SourceAssetRootPath.Length + 1);
- string[] splitedPath = assetPath.Split('/');
- SourceFolder folder = m_SourceAssetRoot;
- for (int i = 0; i < splitedPath.Length - 1; i++)
- {
- SourceFolder subFolder = folder.GetFolder(splitedPath[i]);
- folder = subFolder == null ? folder.AddFolder(splitedPath[i]) : subFolder;
- }
- SourceAsset asset = folder.AddAsset(guid, fullPath, splitedPath[splitedPath.Length - 1]);
- m_SourceAssets.Add(asset.Guid, asset);
- }
- }
- private void RefreshSourceAssetSearchPaths()
- {
- m_SourceAssetSearchPaths.Clear();
- if (string.IsNullOrEmpty(m_SourceAssetRootPath))
- {
- SourceAssetRootPath = DefaultSourceAssetRootPath;
- }
- if (m_SourceAssetSearchRelativePaths.Count > 0)
- {
- foreach (string sourceAssetSearchRelativePath in m_SourceAssetSearchRelativePaths)
- {
- m_SourceAssetSearchPaths.Add(Utility.Path.GetRegularPath(Path.Combine(m_SourceAssetRootPath, sourceAssetSearchRelativePath)));
- }
- }
- else
- {
- m_SourceAssetSearchPaths.Add(m_SourceAssetRootPath);
- }
- }
- private int AssetPathComparer(Asset a, Asset b)
- {
- SourceAsset sourceAssetA = GetSourceAsset(a.Guid);
- SourceAsset sourceAssetB = GetSourceAsset(b.Guid);
- if (sourceAssetA != null && sourceAssetB != null)
- {
- return sourceAssetA.Path.CompareTo(sourceAssetB.Path);
- }
- if (sourceAssetA == null && sourceAssetB == null)
- {
- return a.Guid.CompareTo(b.Guid);
- }
- if (sourceAssetA == null)
- {
- return -1;
- }
- if (sourceAssetB == null)
- {
- return 1;
- }
- return 0;
- }
- private int AssetNameComparer(Asset a, Asset b)
- {
- SourceAsset sourceAssetA = GetSourceAsset(a.Guid);
- SourceAsset sourceAssetB = GetSourceAsset(b.Guid);
- if (sourceAssetA != null && sourceAssetB != null)
- {
- return sourceAssetA.Name.CompareTo(sourceAssetB.Name);
- }
- if (sourceAssetA == null && sourceAssetB == null)
- {
- return a.Guid.CompareTo(b.Guid);
- }
- if (sourceAssetA == null)
- {
- return -1;
- }
- if (sourceAssetB == null)
- {
- return 1;
- }
- return 0;
- }
- private int AssetGuidComparer(Asset a, Asset b)
- {
- SourceAsset sourceAssetA = GetSourceAsset(a.Guid);
- SourceAsset sourceAssetB = GetSourceAsset(b.Guid);
- if (sourceAssetA != null && sourceAssetB != null || sourceAssetA == null && sourceAssetB == null)
- {
- return a.Guid.CompareTo(b.Guid);
- }
- if (sourceAssetA == null)
- {
- return -1;
- }
- if (sourceAssetB == null)
- {
- return 1;
- }
- return 0;
- }
- }
- }
|