private void OnLoadUpdatableVersionListSuccess(string fileUri, byte[] bytes, float duration, object userData) { if (m_UpdatableVersionListReady) { throw new GameFrameworkException("Updatable version list has been parsed."); } MemoryStream memoryStream = null; try { memoryStream = new MemoryStream(bytes, false); UpdatableVersionList versionList = m_ResourceManager.m_UpdatableVersionListSerializer.Deserialize(memoryStream); if (!versionList.IsValid) { throw new GameFrameworkException("Deserialize updatable version list failure."); } UpdatableVersionList.Asset[] assets = versionList.GetAssets(); UpdatableVersionList.Resource[] resources = versionList.GetResources(); UpdatableVersionList.FileSystem[] fileSystems = versionList.GetFileSystems(); UpdatableVersionList.ResourceGroup[] resourceGroups = versionList.GetResourceGroups(); m_ResourceManager.m_ApplicableGameVersion = versionList.ApplicableGameVersion; m_ResourceManager.m_InternalResourceVersion = versionList.InternalResourceVersion; m_ResourceManager.m_AssetInfos = new Dictionary <string, AssetInfo>(assets.Length, StringComparer.Ordinal); m_ResourceManager.m_ResourceInfos = new Dictionary <ResourceName, ResourceInfo>(resources.Length, new ResourceNameComparer()); m_ResourceManager.m_ReadWriteResourceInfos = new SortedDictionary <ResourceName, ReadWriteResourceInfo>(new ResourceNameComparer()); ResourceGroup defaultResourceGroup = m_ResourceManager.GetOrAddResourceGroup(string.Empty); foreach (UpdatableVersionList.FileSystem fileSystem in fileSystems) { int[] resourceIndexes = fileSystem.GetResourceIndexes(); foreach (int resourceIndex in resourceIndexes) { UpdatableVersionList.Resource resource = resources[resourceIndex]; if (resource.Variant != null && resource.Variant != m_CurrentVariant) { continue; } SetCachedFileSystemName(new ResourceName(resource.Name, resource.Variant, resource.Extension), fileSystem.Name); } } foreach (UpdatableVersionList.Resource resource in resources) { if (resource.Variant != null && resource.Variant != m_CurrentVariant) { continue; } ResourceName resourceName = new ResourceName(resource.Name, resource.Variant, resource.Extension); int[] assetIndexes = resource.GetAssetIndexes(); foreach (int assetIndex in assetIndexes) { UpdatableVersionList.Asset asset = assets[assetIndex]; int[] dependencyAssetIndexes = asset.GetDependencyAssetIndexes(); int index = 0; string[] dependencyAssetNames = new string[dependencyAssetIndexes.Length]; foreach (int dependencyAssetIndex in dependencyAssetIndexes) { dependencyAssetNames[index++] = assets[dependencyAssetIndex].Name; } m_ResourceManager.m_AssetInfos.Add(asset.Name, new AssetInfo(asset.Name, resourceName, dependencyAssetNames)); } SetVersionInfo(resourceName, (LoadType)resource.LoadType, resource.Length, resource.HashCode, resource.CompressedLength, resource.CompressedHashCode); defaultResourceGroup.AddResource(resourceName, resource.Length, resource.CompressedLength); } foreach (UpdatableVersionList.ResourceGroup resourceGroup in resourceGroups) { ResourceGroup group = m_ResourceManager.GetOrAddResourceGroup(resourceGroup.Name); int[] resourceIndexes = resourceGroup.GetResourceIndexes(); foreach (int resourceIndex in resourceIndexes) { UpdatableVersionList.Resource resource = resources[resourceIndex]; if (resource.Variant != null && resource.Variant != m_CurrentVariant) { continue; } group.AddResource(new ResourceName(resource.Name, resource.Variant, resource.Extension), resource.Length, resource.CompressedLength); } } m_UpdatableVersionListReady = true; RefreshCheckInfoStatus(); } catch (Exception exception) { if (exception is GameFrameworkException) { throw; } throw new GameFrameworkException(Utility.Text.Format("Parse updatable version list exception '{0}'.", exception), exception); } finally { if (memoryStream != null) { memoryStream.Dispose(); memoryStream = null; } } }
/// <summary> /// 解析版本资源列表。 /// </summary> /// <param name="fileUri">版本资源列表文件路径。</param> /// <param name="bytes">要解析的数据。</param> /// <param name="errorMessage">错误信息。</param> private void ParseVersionList(string fileUri, byte[] bytes, string errorMessage) { if (m_VersionListReady) { throw new GameFrameworkException("Version list has been parsed."); } if (bytes == null || bytes.Length <= 0) { throw new GameFrameworkException(Utility.Text.Format("Version list '{0}' is invalid, error message is '{1}'.", fileUri, string.IsNullOrEmpty(errorMessage) ? "<Empty>" : errorMessage)); } MemoryStream memoryStream = null; try { memoryStream = new MemoryStream(bytes); using (BinaryReader binaryReader = new BinaryReader(memoryStream)) { memoryStream = null; char[] header = binaryReader.ReadChars(3); if (header[0] != VersionListHeader[0] || header[1] != VersionListHeader[1] || header[2] != VersionListHeader[2]) { throw new GameFrameworkException("Version list header is invalid."); } byte listVersion = binaryReader.ReadByte(); if (listVersion == 0) { byte[] encryptBytes = binaryReader.ReadBytes(4); m_ResourceManager.m_ApplicableGameVersion = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), encryptBytes)); m_ResourceManager.m_InternalResourceVersion = binaryReader.ReadInt32(); int resourceCount = binaryReader.ReadInt32(); string[] names = new string[resourceCount]; string[] variants = new string[resourceCount]; int[] lengths = new int[resourceCount]; Dictionary <string, string[]> dependencyAssetNamesCollection = new Dictionary <string, string[]>(); for (int i = 0; i < resourceCount; i++) { names[i] = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), encryptBytes)); variants[i] = null; byte variantLength = binaryReader.ReadByte(); if (variantLength > 0) { variants[i] = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(variantLength), encryptBytes)); } LoadType loadType = (LoadType)binaryReader.ReadByte(); lengths[i] = binaryReader.ReadInt32(); int hashCode = binaryReader.ReadInt32(); int zipLength = binaryReader.ReadInt32(); int zipHashCode = binaryReader.ReadInt32(); int assetNamesCount = binaryReader.ReadInt32(); string[] assetNames = new string[assetNamesCount]; for (int j = 0; j < assetNamesCount; j++) { assetNames[j] = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), Utility.Converter.GetBytes(hashCode))); int dependencyAssetNamesCount = binaryReader.ReadInt32(); string[] dependencyAssetNames = new string[dependencyAssetNamesCount]; for (int k = 0; k < dependencyAssetNamesCount; k++) { dependencyAssetNames[k] = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), Utility.Converter.GetBytes(hashCode))); } if (variants[i] == null || variants[i] == m_CurrentVariant) { dependencyAssetNamesCollection.Add(assetNames[j], dependencyAssetNames); } } ResourceName resourceName = new ResourceName(names[i], variants[i]); SetVersionInfo(resourceName, loadType, lengths[i], hashCode, zipLength, zipHashCode); if (variants[i] == null || variants[i] == m_CurrentVariant) { ProcessAssetInfo(resourceName, assetNames); } } ProcessAssetDependencyInfo(dependencyAssetNamesCollection); ResourceGroup resourceGroupAll = m_ResourceManager.GetResourceGroup(string.Empty); for (int i = 0; i < resourceCount; i++) { resourceGroupAll.AddResource(names[i], variants[i], lengths[i]); } int resourceGroupCount = binaryReader.ReadInt32(); for (int i = 0; i < resourceGroupCount; i++) { string groupName = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), encryptBytes)); ResourceGroup resourceGroup = m_ResourceManager.GetResourceGroup(groupName); int groupResourceCount = binaryReader.ReadInt32(); for (int j = 0; j < groupResourceCount; j++) { ushort versionIndex = binaryReader.ReadUInt16(); if (versionIndex >= resourceCount) { throw new GameFrameworkException(Utility.Text.Format("Version index '{0}' is invalid, resource count is '{1}'.", versionIndex, resourceCount)); } resourceGroup.AddResource(names[versionIndex], variants[versionIndex], lengths[versionIndex]); } } } else { throw new GameFrameworkException("Version list version is invalid."); } } m_VersionListReady = true; RefreshCheckInfoStatus(); } catch (Exception exception) { if (exception is GameFrameworkException) { throw; } throw new GameFrameworkException(Utility.Text.Format("Parse version list exception '{0}'.", exception.Message), exception); } finally { if (memoryStream != null) { memoryStream.Dispose(); memoryStream = null; } } }
/// <summary> /// 解析资源包资源列表。 /// </summary> /// <param name="fileUri">版本资源列表文件路径。</param> /// <param name="bytes">要解析的数据。</param> /// <param name="errorMessage">错误信息。</param> private void ParsePackageList(string fileUri, byte[] bytes, string errorMessage) { if (bytes == null || bytes.Length <= 0) { throw new GameFrameworkException(Utility.Text.Format("Package list '{0}' is invalid, error message is '{1}'.", fileUri, string.IsNullOrEmpty(errorMessage) ? "<Empty>" : errorMessage)); } MemoryStream memoryStream = null; try { memoryStream = new MemoryStream(bytes, false); using (BinaryReader binaryReader = new BinaryReader(memoryStream, Encoding.UTF8)) { memoryStream = null; if (binaryReader.ReadChar() != PackageListHeader[0] || binaryReader.ReadChar() != PackageListHeader[1] || binaryReader.ReadChar() != PackageListHeader[2]) { throw new GameFrameworkException("Package list header is invalid."); } byte listVersion = binaryReader.ReadByte(); if (listVersion == 0) { byte[] encryptBytes = binaryReader.ReadBytes(4); m_ResourceManager.m_ApplicableGameVersion = m_ResourceManager.GetEncryptedString(binaryReader, encryptBytes); m_ResourceManager.m_InternalResourceVersion = binaryReader.ReadInt32(); int assetCount = binaryReader.ReadInt32(); m_ResourceManager.m_AssetInfos = new Dictionary <string, AssetInfo>(assetCount); int resourceCount = binaryReader.ReadInt32(); m_ResourceManager.m_ResourceInfos = new Dictionary <ResourceName, ResourceInfo>(resourceCount, new ResourceNameComparer()); ResourceLength[] resourceLengths = new ResourceLength[resourceCount]; for (int i = 0; i < resourceCount; i++) { string name = m_ResourceManager.GetEncryptedString(binaryReader, encryptBytes); string variant = m_ResourceManager.GetEncryptedString(binaryReader, encryptBytes); ResourceName resourceName = new ResourceName(name, variant); LoadType loadType = (LoadType)binaryReader.ReadByte(); int length = binaryReader.ReadInt32(); int hashCode = binaryReader.ReadInt32(); Utility.Converter.GetBytes(hashCode, m_CachedHashBytes); resourceLengths[i] = new ResourceLength(resourceName, length, length); int assetNamesCount = binaryReader.ReadInt32(); for (int j = 0; j < assetNamesCount; j++) { string assetName = m_ResourceManager.GetEncryptedString(binaryReader, m_CachedHashBytes); int dependencyAssetNamesCount = binaryReader.ReadInt32(); string[] dependencyAssetNames = new string[dependencyAssetNamesCount]; for (int k = 0; k < dependencyAssetNamesCount; k++) { dependencyAssetNames[k] = m_ResourceManager.GetEncryptedString(binaryReader, m_CachedHashBytes); } if (variant == null || variant == m_CurrentVariant) { m_ResourceManager.m_AssetInfos.Add(assetName, new AssetInfo(assetName, resourceName, dependencyAssetNames)); } } if (variant == null || variant == m_CurrentVariant) { ProcessResourceInfo(resourceName, loadType, length, hashCode); } } Array.Clear(m_CachedHashBytes, 0, CachedHashBytesLength); ResourceGroup defaultResourceGroup = m_ResourceManager.GetOrAddResourceGroup(string.Empty); for (int i = 0; i < resourceCount; i++) { if (resourceLengths[i].ResourceName.Variant == null || resourceLengths[i].ResourceName.Variant == m_CurrentVariant) { defaultResourceGroup.AddResource(resourceLengths[i].ResourceName, resourceLengths[i].Length, resourceLengths[i].ZipLength); } } int resourceGroupCount = binaryReader.ReadInt32(); for (int i = 0; i < resourceGroupCount; i++) { string resourceGroupName = m_ResourceManager.GetEncryptedString(binaryReader, encryptBytes); ResourceGroup resourceGroup = m_ResourceManager.GetOrAddResourceGroup(resourceGroupName); int resourceGroupResourceCount = binaryReader.ReadInt32(); for (int j = 0; j < resourceGroupResourceCount; j++) { ushort index = binaryReader.ReadUInt16(); if (index >= resourceCount) { throw new GameFrameworkException(Utility.Text.Format("Package index '{0}' is invalid, resource count is '{1}'.", index.ToString(), resourceCount.ToString())); } if (resourceLengths[index].ResourceName.Variant == null || resourceLengths[index].ResourceName.Variant == m_CurrentVariant) { resourceGroup.AddResource(resourceLengths[index].ResourceName, resourceLengths[index].Length, resourceLengths[index].ZipLength); } } } } else { throw new GameFrameworkException("Package list version is invalid."); } } ResourceInitComplete(); } catch (Exception exception) { if (exception is GameFrameworkException) { throw; } throw new GameFrameworkException(Utility.Text.Format("Parse package list exception '{0}'.", exception.Message), exception); } finally { if (memoryStream != null) { memoryStream.Dispose(); memoryStream = null; } } }
private void OnLoadPackageVersionListSuccess(string fileUri, byte[] bytes, float duration, object userData) { MemoryStream memoryStream = null; try { memoryStream = new MemoryStream(bytes, false); PackageVersionList versionList = m_ResourceManager.m_PackageVersionListSerializer.Deserialize(memoryStream); if (!versionList.IsValid) { throw new GameFrameworkException("Deserialize package version list failure."); } PackageVersionList.Asset[] assets = versionList.GetAssets(); PackageVersionList.Resource[] resources = versionList.GetResources(); PackageVersionList.ResourceGroup[] resourceGroups = versionList.GetResourceGroups(); m_ResourceManager.m_ApplicableGameVersion = versionList.ApplicableGameVersion; m_ResourceManager.m_InternalResourceVersion = versionList.InternalResourceVersion; m_ResourceManager.m_AssetInfos = new Dictionary <string, AssetInfo>(assets.Length); m_ResourceManager.m_ResourceInfos = new Dictionary <ResourceName, ResourceInfo>(resources.Length, new ResourceNameComparer()); ResourceGroup defaultResourceGroup = m_ResourceManager.GetOrAddResourceGroup(string.Empty); foreach (PackageVersionList.Resource resource in resources) { if (resource.Variant != null && resource.Variant != m_CurrentVariant) { continue; } ResourceName resourceName = new ResourceName(resource.Name, resource.Variant, resource.Extension); int[] assetIndexes = resource.GetAssetIndexes(); foreach (int assetIndex in assetIndexes) { PackageVersionList.Asset asset = assets[assetIndex]; int[] dependencyAssetIndexes = asset.GetDependencyAssetIndexes(); int index = 0; string[] dependencyAssetNames = new string[dependencyAssetIndexes.Length]; foreach (int dependencyAssetIndex in dependencyAssetIndexes) { dependencyAssetNames[index++] = assets[dependencyAssetIndex].Name; } m_ResourceManager.m_AssetInfos.Add(asset.Name, new AssetInfo(asset.Name, resourceName, dependencyAssetNames)); } m_ResourceManager.m_ResourceInfos.Add(resourceName, new ResourceInfo(resourceName, (LoadType)resource.LoadType, resource.Length, resource.HashCode, true, true)); defaultResourceGroup.AddResource(resourceName, resource.Length, resource.Length); } foreach (PackageVersionList.ResourceGroup resourceGroup in resourceGroups) { ResourceGroup group = m_ResourceManager.GetOrAddResourceGroup(resourceGroup.Name); int[] resourceIndexes = resourceGroup.GetResourceIndexes(); foreach (int resourceIndex in resourceIndexes) { PackageVersionList.Resource resource = resources[resourceIndex]; if (resource.Variant != null && resource.Variant != m_CurrentVariant) { continue; } group.AddResource(new ResourceName(resource.Name, resource.Variant, resource.Extension), resource.Length, resource.Length); } } ResourceInitComplete(); } catch (Exception exception) { if (exception is GameFrameworkException) { throw; } throw new GameFrameworkException(Utility.Text.Format("Parse package version list exception '{0}'.", exception.ToString()), exception); } finally { if (memoryStream != null) { memoryStream.Dispose(); memoryStream = null; } } }
private void ParsePackageList(string fileUri, byte[] bytes, string errorMessage) { MemoryStream memoryStream = null; try { memoryStream = new MemoryStream(bytes); using (BinaryReader binaryReader = new BinaryReader(memoryStream)) { memoryStream = null; char[] header = binaryReader.ReadChar(3); if (header[0] != PackageListHeader[0]) { throw; } byte listVersion = binaryReader.ReadByte(); if (listVersion == 0) { byte[] encryptBytes = binaryReader.ReadBytes(4); m_ResourceManager.m_ApplicableGameVersion = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), encryptBytes)); m_ResourceManager.m_InternalResourceVersion = binaryReader.ReadInt32(); int resourceCount = binaryReader.ReadInt32(); string[] names = new string[resourceCount]; string[] variants = new string[resourceCount]; int[] lengths = new int[resourceCount]; Dictionary <string, string[]> dependencyAssetNamesCollection = new Dictionary <string, string[]>(); for (int i = 0; i < resouceCount; i++) { names[i] = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), encryptBytes)); variants[i] = null; byte variantLength = binaryReader.ReadByte(); if (varintLegnth > 0) { variants[i] Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(variantLength), encryptBytes)); } LoadType loadType = (LoadType)binaryReader.ReadByte(); lengths[i] = binaryReader.ReadInt32(); int hashCode = BinaryReader.ReadInt32(); int assetNamesCount = binaryReader.ReadInt32(); string[] assetNames = new string[assetNamesCount]; for (int j = 0; j < assetNamesCount; j++) { assetNames[j] = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), Utility.Converter.GetBytes(hashCode))); int dependencyAssetNamesCount = binaryReader.ReadInt32(); string[] dependencyAssetNames = new string[dependencyAssetNamesCount]; for (int k = 0; k < dependencyAssetNames; k++) { dependencyAssetNames[k] = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), Utility.Converter.GetBytes(hashCode))); } if (variants[i] == null) { dependencyAssetNamesCollection.Add(assetNames[j], dependencyAssetNames); } } if (variants[i] == null) { ResourceName resourceName = new ResourceName(names[i], variants[i]); ProcessAssetInfo(resourceName, assetNames); ProcessResourceInfo(resourceName, loadType, lengths[i]); } } ProcessAssetDependencyInfo(dependencyAssetNamesCollection); ResourceGroup resourceGroupAll = m_ResourceManager.GetResourceGroup(string.Empty); for (int i = 0; i < resourceCount; i++) { resourceGroupAll.AddResource(names[i], variants[i], lengths[i]); } int resourceGroupCount = binaryReader.ReadInt32(); for (int i = 0; i < resourceGroupCount; i++) { string groupName = Utiliey.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), encryptBytes)); resourceGroupCount resourceGroup = m_ResourceManager.GetResourceGroup(groupName); int groupResourceCount = binaryReader.ReadInt32(); for (int j = 0; j < groupResourceCount; j++) { ushort versionIndex = binaryReader.ReadUInt16(); if (versionIndex >= resourceCount) { } resourceGroup.AddResource(names[versionIndex], variants[versionIndex], lengths[versionIndex]); } } } } ResourceInitComplete(); } catch (Exception e) { if (e is GameFrameworkExcepton) { throw; } throw; } finally { if (memoryStream != null) { memoryStream.Dispose(); memoryStream = null; } } }
/// <summary> /// 解析版本资源列表。 /// </summary> /// <param name="fileUri">版本资源列表文件路径。</param> /// <param name="bytes">要解析的数据。</param> /// <param name="errorMessage">错误信息。</param> private void ParseUpdatableVersionList(string fileUri, byte[] bytes, string errorMessage) { if (m_UpdatableVersionListReady) { throw new GameFrameworkException("Updatable version list has been parsed."); } if (bytes == null || bytes.Length <= 0) { throw new GameFrameworkException(Utility.Text.Format("Updatable version list '{0}' is invalid, error message is '{1}'.", fileUri, string.IsNullOrEmpty(errorMessage) ? "<Empty>" : errorMessage)); } MemoryStream memoryStream = null; try { memoryStream = new MemoryStream(bytes, false); UpdatableVersionList versionList = m_ResourceManager.m_UpdatableVersionListSerializer.Deserialize(memoryStream); if (!versionList.IsValid) { throw new GameFrameworkException("Deserialize updatable version list failure."); } UpdatableVersionList.Asset[] assets = versionList.GetAssets(); UpdatableVersionList.Resource[] resources = versionList.GetResources(); UpdatableVersionList.ResourceGroup[] resourceGroups = versionList.GetResourceGroups(); m_ResourceManager.m_ApplicableGameVersion = versionList.ApplicableGameVersion; m_ResourceManager.m_InternalResourceVersion = versionList.InternalResourceVersion; m_ResourceManager.m_AssetInfos = new Dictionary <string, AssetInfo>(assets.Length); m_ResourceManager.m_ResourceInfos = new Dictionary <ResourceName, ResourceInfo>(resources.Length, new ResourceNameComparer()); ResourceGroup defaultResourceGroup = m_ResourceManager.GetOrAddResourceGroup(string.Empty); foreach (UpdatableVersionList.Resource resource in resources) { ResourceName resourceName = new ResourceName(resource.Name, resource.Variant); int[] assetIndexes = resource.GetAssetIndexes(); foreach (int assetIndex in assetIndexes) { UpdatableVersionList.Asset asset = assets[assetIndex]; int[] dependencyAssetIndexes = asset.GetDependencyAssetIndexes(); string[] dependencyAssetNames = new string[dependencyAssetIndexes.Length]; int index = 0; foreach (int dependencyAssetIndex in dependencyAssetIndexes) { dependencyAssetNames[index++] = assets[dependencyAssetIndex].Name; } if (resource.Variant == null || resource.Variant == m_CurrentVariant) { m_ResourceManager.m_AssetInfos.Add(asset.Name, new AssetInfo(asset.Name, resourceName, dependencyAssetNames)); } } if (resource.Variant == null || resource.Variant == m_CurrentVariant) { SetVersionInfo(resourceName, (LoadType)resource.LoadType, resource.Length, resource.HashCode, resource.ZipLength, resource.ZipHashCode); defaultResourceGroup.AddResource(resourceName, resource.Length, resource.ZipLength); } } foreach (UpdatableVersionList.ResourceGroup resourceGroup in resourceGroups) { ResourceGroup group = m_ResourceManager.GetOrAddResourceGroup(resourceGroup.Name); int[] resourceIndexes = resourceGroup.GetResourceIndexes(); foreach (int resourceIndex in resourceIndexes) { if (resources[resourceIndex].Variant == null || resources[resourceIndex].Variant == m_CurrentVariant) { group.AddResource(new ResourceName(resources[resourceIndex].Name, resources[resourceIndex].Variant), resources[resourceIndex].Length, resources[resourceIndex].ZipLength); } } } m_UpdatableVersionListReady = true; RefreshCheckInfoStatus(); } catch (Exception exception) { if (exception is GameFrameworkException) { throw; } throw new GameFrameworkException(Utility.Text.Format("Parse updatable version list exception '{0}'.", exception.ToString()), exception); } finally { if (memoryStream != null) { memoryStream.Dispose(); memoryStream = null; } } }
/// <summary> /// 解析资源包资源列表。 /// </summary> /// <param name="fileUri">版本资源列表文件路径。</param> /// <param name="bytes">要解析的数据。</param> /// <param name="errorMessage">错误信息。</param> private void ParsePackageList(string fileUri, byte[] bytes, string errorMessage) { if (bytes == null || bytes.Length <= 0) { throw new AshException(string.Format("Package list '{0}' is invalid, error message is '{1}'.", fileUri, string.IsNullOrEmpty(errorMessage) ? "<Empty>" : errorMessage)); } MemoryStream memoryStream = null; try { memoryStream = new MemoryStream(bytes); using (BinaryReader binaryReader = new BinaryReader(memoryStream)) { memoryStream = null; char[] header = binaryReader.ReadChars(3); if (header[0] != PackageListHeader[0] || header[1] != PackageListHeader[1] || header[2] != PackageListHeader[2]) { throw new AshException("Package list header is invalid."); } byte listVersion = binaryReader.ReadByte(); if (listVersion == 0) { //用于解密的Bytes byte[] encryptBytes = binaryReader.ReadBytes(4); //获取当前资源适用的游戏版本号。 m_ResourceManager.m_ApplicableGameVersion = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), encryptBytes)); //获取当前资源内部版本号。 m_ResourceManager.m_InternalResourceVersion = binaryReader.ReadInt32(); int resourceCount = binaryReader.ReadInt32(); string[] names = new string[resourceCount]; string[] variants = new string[resourceCount]; int[] lengths = new int[resourceCount]; Dictionary <string, string[]> dependencyAssetNamesCollection = new Dictionary <string, string[]>(); for (int i = 0; i < resourceCount; i++) { //名字 names[i] = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), encryptBytes)); //变体 variants[i] = null; byte variantLength = binaryReader.ReadByte(); if (variantLength > 0) { variants[i] = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(variantLength), encryptBytes)); } //资源加载方式类型 LoadType loadType = (LoadType)binaryReader.ReadByte(); //长度?? lengths[i] = binaryReader.ReadInt32(); //hashCode int hashCode = binaryReader.ReadInt32(); //resource(资源)里面的asset(资产)数量 int assetNamesCount = binaryReader.ReadInt32(); string[] assetNames = new string[assetNamesCount]; //遍历asset for (int j = 0; j < assetNamesCount; j++) { //资产名字 assetNames[j] = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), Utility.Converter.GetBytes(hashCode))); //关联资产的数量 int dependencyAssetNamesCount = binaryReader.ReadInt32(); string[] dependencyAssetNames = new string[dependencyAssetNamesCount]; //遍历关联的资产 for (int k = 0; k < dependencyAssetNamesCount; k++) { //关联的资产的名字 dependencyAssetNames[k] = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), Utility.Converter.GetBytes(hashCode))); } //缓存当前变体的关联资产 if (variants[i] == null || variants[i] == m_CurrentVariant) { dependencyAssetNamesCollection.Add(assetNames[j], dependencyAssetNames); } } //缓存当前变体的资产信息和资源信息 if (variants[i] == null || variants[i] == m_CurrentVariant) { ResourceName resourceName = new ResourceName(names[i], variants[i]); ProcessAssetInfo(resourceName, assetNames); ProcessResourceInfo(resourceName, loadType, lengths[i], hashCode); } } //处理完所有资源信息和资产信息后,移除互相引用的关联资产,统计离散的关联资产。 ProcessAssetDependencyInfo(dependencyAssetNamesCollection); //建个ResourceGroup,加入所有资源 ResourceGroup resourceGroupAll = m_ResourceManager.GetResourceGroup(string.Empty); for (int i = 0; i < resourceCount; i++) { resourceGroupAll.AddResource(names[i], variants[i], lengths[i]); } //资源组的数量?? int resourceGroupCount = binaryReader.ReadInt32(); for (int i = 0; i < resourceGroupCount; i++) { //资源组名字 string groupName = Utility.Converter.GetString(Utility.Encryption.GetXorBytes(binaryReader.ReadBytes(binaryReader.ReadByte()), encryptBytes)); //找到资源组,没有就新建 ResourceGroup resourceGroup = m_ResourceManager.GetResourceGroup(groupName); //资源组里面资源的数量 int groupResourceCount = binaryReader.ReadInt32(); for (int j = 0; j < groupResourceCount; j++) { //版本Index? ushort versionIndex = binaryReader.ReadUInt16(); if (versionIndex >= resourceCount) { throw new AshException(string.Format("Package index '{0}' is invalid, resource count is '{1}'.", versionIndex, resourceCount)); } resourceGroup.AddResource(names[versionIndex], variants[versionIndex], lengths[versionIndex]); } } } else { throw new AshException("Package list version is invalid."); } } ResourceInitComplete(); } catch (Exception exception) { if (exception is AshException) { throw; } throw new AshException(string.Format("Parse package list exception '{0}'.", exception.Message), exception); } finally { if (memoryStream != null) { memoryStream.Dispose(); memoryStream = null; } } }