/// <summary> /// 应用指定资源包的资源。 /// </summary> /// <param name="resourcePackPath">要应用的资源包路径。</param> public void ApplyResources(string resourcePackPath) { if (!m_CheckResourcesComplete) { throw new GameFrameworkException("You must check resources complete first."); } if (m_ApplyingResourcePackStream != null) { throw new GameFrameworkException(Utility.Text.Format("There is already a resource pack '{0}' being applied.", m_ApplyingResourcePackPath)); } if (m_UpdatingResourceGroup != null) { throw new GameFrameworkException(Utility.Text.Format("There is already a resource group '{0}' being updated.", m_UpdatingResourceGroup.Name)); } try { long length = 0L; ResourcePackVersionList versionList = default(ResourcePackVersionList); using (FileStream fileStream = new FileStream(resourcePackPath, FileMode.Open, FileAccess.Read)) { length = fileStream.Length; versionList = m_ResourceManager.m_ResourcePackVersionListSerializer.Deserialize(fileStream); } if (!versionList.IsValid) { throw new GameFrameworkException("Deserialize resource pack version list failure."); } if (versionList.Offset + versionList.Length != length) { throw new GameFrameworkException("Resource pack length is invalid."); } m_ApplyingResourcePackPath = resourcePackPath; m_ApplyingResourcePackStream = new FileStream(resourcePackPath, FileMode.Open, FileAccess.Read); m_ApplyingResourcePackStream.Position = versionList.Offset; m_FailureFlag = false; ResourcePackVersionList.Resource[] resources = versionList.GetResources(); foreach (ResourcePackVersionList.Resource resource in resources) { foreach (UpdateInfo updateInfo in m_UpdateCandidateInfo) { if (updateInfo.ResourceName.Name == resource.Name && updateInfo.ResourceName.Variant == resource.Variant && updateInfo.ResourceName.Extension == resource.Extension) { if (updateInfo.LoadType == (LoadType)resource.LoadType && updateInfo.Length == resource.Length && updateInfo.HashCode == resource.HashCode) { m_ApplyWaitingInfo.Add(new ApplyInfo(updateInfo.ResourceName, updateInfo.LoadType, resource.Offset, resource.Length, resource.HashCode, resource.ZipLength, resource.ZipHashCode, updateInfo.ResourcePath)); } break; } } } } catch (Exception exception) { if (m_ApplyingResourcePackStream != null) { m_ApplyingResourcePackStream.Dispose(); m_ApplyingResourcePackStream = null; } throw new GameFrameworkException(Utility.Text.Format("Apply resources '{0}' with exception '{1}'.", resourcePackPath, exception.ToString()), exception); } }
/// <summary> /// 应用指定资源包的资源。 /// </summary> /// <param name="resourcePackPath">要应用的资源包路径。</param> public void ApplyResources(string resourcePackPath) { if (!m_CheckResourcesComplete) { throw new GameFrameworkException("You must check resources complete first."); } if (m_ApplyingResourcePackStream != null) { throw new GameFrameworkException(Utility.Text.Format("There is already a resource pack '{0}' being applied.", m_ApplyingResourcePackPath)); } if (m_UpdatingResourceGroup != null) { throw new GameFrameworkException(Utility.Text.Format("There is already a resource group '{0}' being updated.", m_UpdatingResourceGroup.Name)); } if (m_UpdateWaitingInfoWhilePlaying.Count > 0) { throw new GameFrameworkException("There are already some resources being updated while playing."); } try { long length = 0L; ResourcePackVersionList versionList = default(ResourcePackVersionList); using (FileStream fileStream = new FileStream(resourcePackPath, FileMode.Open, FileAccess.Read)) { length = fileStream.Length; versionList = m_ResourceManager.m_ResourcePackVersionListSerializer.Deserialize(fileStream); } if (!versionList.IsValid) { throw new GameFrameworkException("Deserialize resource pack version list failure."); } if (versionList.Offset + versionList.Length != length) { throw new GameFrameworkException("Resource pack length is invalid."); } m_ApplyingResourcePackPath = resourcePackPath; m_ApplyingResourcePackStream = new FileStream(resourcePackPath, FileMode.Open, FileAccess.Read); m_ApplyingResourcePackStream.Position = versionList.Offset; m_FailureFlag = false; long totalLength = 0L; ResourcePackVersionList.Resource[] resources = versionList.GetResources(); foreach (ResourcePackVersionList.Resource resource in resources) { ResourceName resourceName = new ResourceName(resource.Name, resource.Variant, resource.Extension); UpdateInfo updateInfo = null; if (!m_UpdateCandidateInfo.TryGetValue(resourceName, out updateInfo)) { continue; } if (updateInfo.LoadType == (LoadType)resource.LoadType && updateInfo.Length == resource.Length && updateInfo.HashCode == resource.HashCode) { totalLength += resource.Length; m_ApplyWaitingInfo.Enqueue(new ApplyInfo(resourceName, updateInfo.FileSystemName, (LoadType)resource.LoadType, resource.Offset, resource.Length, resource.HashCode, resource.CompressedLength, resource.CompressedHashCode, updateInfo.ResourcePath)); } } if (ResourceApplyStart != null) { ResourceApplyStart(m_ApplyingResourcePackPath, m_ApplyWaitingInfo.Count, totalLength); } } catch (Exception exception) { if (m_ApplyingResourcePackStream != null) { m_ApplyingResourcePackStream.Dispose(); m_ApplyingResourcePackStream = null; } throw new GameFrameworkException(Utility.Text.Format("Apply resources '{0}' with exception '{1}'.", resourcePackPath, exception), exception); } }