//------------------------------------------------------------ // Game Framework // Copyright © 2013-2021 loyalsoft. All rights reserved. // Homepage: http://www.game7000.com/ // Feedback: http://www.game7000.com/ //------------------------------------------------------------ using System.Collections.Generic; namespace GameFramework.Resource { internal sealed partial class ResourceManager : GameFrameworkModule, IResourceManager { /// /// 资源组。 /// private sealed class ResourceGroup : IResourceGroup { private readonly string m_Name; private readonly Dictionary m_ResourceInfos; private readonly HashSet m_ResourceNames; private long m_TotalLength; private long m_TotalCompressedLength; /// /// 初始化资源组的新实例。 /// /// 资源组名称。 /// 资源信息引用。 public ResourceGroup(string name, Dictionary resourceInfos) { if (name == null) { throw new GameFrameworkException("Name is invalid."); } if (resourceInfos == null) { throw new GameFrameworkException("Resource infos is invalid."); } m_Name = name; m_ResourceInfos = resourceInfos; m_ResourceNames = new HashSet(); } /// /// 获取资源组名称。 /// public string Name { get { return m_Name; } } /// /// 获取资源组是否准备完毕。 /// public bool Ready { get { return ReadyCount >= TotalCount; } } /// /// 获取资源组包含资源数量。 /// public int TotalCount { get { return m_ResourceNames.Count; } } /// /// 获取资源组中已准备完成资源数量。 /// public int ReadyCount { get { int readyCount = 0; foreach (ResourceName resourceName in m_ResourceNames) { ResourceInfo resourceInfo = null; if (m_ResourceInfos.TryGetValue(resourceName, out resourceInfo) && resourceInfo.Ready) { readyCount++; } } return readyCount; } } /// /// 获取资源组包含资源的总大小。 /// public long TotalLength { get { return m_TotalLength; } } /// /// 获取资源组包含资源压缩后的总大小。 /// public long TotalCompressedLength { get { return m_TotalCompressedLength; } } /// /// 获取资源组中已准备完成资源的总大小。 /// public long ReadyLength { get { long readyLength = 0L; foreach (ResourceName resourceName in m_ResourceNames) { ResourceInfo resourceInfo = null; if (m_ResourceInfos.TryGetValue(resourceName, out resourceInfo) && resourceInfo.Ready) { readyLength += resourceInfo.Length; } } return readyLength; } } /// /// 获取资源组中已准备完成资源压缩后的总大小。 /// public long ReadyCompressedLength { get { long readyCompressedLength = 0L; foreach (ResourceName resourceName in m_ResourceNames) { ResourceInfo resourceInfo = null; if (m_ResourceInfos.TryGetValue(resourceName, out resourceInfo) && resourceInfo.Ready) { readyCompressedLength += resourceInfo.CompressedLength; } } return readyCompressedLength; } } /// /// 获取资源组的完成进度。 /// public float Progress { get { return m_TotalLength > 0L ? (float)ReadyLength / m_TotalLength : 1f; } } /// /// 获取资源组包含的资源名称列表。 /// /// 资源组包含的资源名称列表。 public string[] GetResourceNames() { int index = 0; string[] resourceNames = new string[m_ResourceNames.Count]; foreach (ResourceName resourceName in m_ResourceNames) { resourceNames[index++] = resourceName.FullName; } return resourceNames; } /// /// 获取资源组包含的资源名称列表。 /// /// 资源组包含的资源名称列表。 public void GetResourceNames(List results) { if (results == null) { throw new GameFrameworkException("Results is invalid."); } results.Clear(); foreach (ResourceName resourceName in m_ResourceNames) { results.Add(resourceName.FullName); } } /// /// 获取资源组包含的资源名称列表。 /// /// 资源组包含的资源名称列表。 public ResourceName[] InternalGetResourceNames() { int index = 0; ResourceName[] resourceNames = new ResourceName[m_ResourceNames.Count]; foreach (ResourceName resourceName in m_ResourceNames) { resourceNames[index++] = resourceName; } return resourceNames; } /// /// 获取资源组包含的资源名称列表。 /// /// 资源组包含的资源名称列表。 public void InternalGetResourceNames(List results) { if (results == null) { throw new GameFrameworkException("Results is invalid."); } results.Clear(); foreach (ResourceName resourceName in m_ResourceNames) { results.Add(resourceName); } } /// /// 检查指定资源是否属于资源组。 /// /// 要检查的资源的名称。 /// 指定资源是否属于资源组。 public bool HasResource(ResourceName resourceName) { return m_ResourceNames.Contains(resourceName); } /// /// 向资源组中增加资源。 /// /// 资源名称。 /// 资源大小。 /// 资源压缩后的大小。 public void AddResource(ResourceName resourceName, int length, int compressedLength) { m_ResourceNames.Add(resourceName); m_TotalLength += length; m_TotalCompressedLength += compressedLength; } } } }