private void OnLoadUpdatableVersionListSuccess(string fileUri, byte[] bytes, float duration, object userData) { if (m_UpdatableVersionListReady) { throw new Exception("Updatable version list has been parsed."); } MemoryStream memoryStream = null; try { memoryStream = new MemoryStream(bytes, false); UpdatableVersionList versionList = m_ResourceComponent.UpdatableVersionListSerializer.Deserialize(memoryStream); if (!versionList.IsValid) { throw new Exception("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_ResourceComponent.ApplicableGameVersion = versionList.ApplicableGameVersion; m_ResourceComponent.InternalResourceVersion = versionList.InternalResourceVersion; if (m_ResourceComponent.AssetInfoDic == null) { m_ResourceComponent.AssetInfoDic = new Dictionary <AssetCategory, Dictionary <string, AssetInfo> >(); } if (m_ResourceComponent.ResourceInfoDic == null) { m_ResourceComponent.ResourceInfoDic = new Dictionary <AssetCategory, Dictionary <ResourceName, ResourceInfo> >(); } if (m_ResourceComponent.ReadWriteResourceInfoDic == null) { m_ResourceComponent.ReadWriteResourceInfoDic = new Dictionary <AssetCategory, SortedDictionary <ResourceName, ReadWriteResourceInfo> >(); } foreach (AssetCategory item in m_ResourceComponent.AssetCategories) { if (!m_ResourceComponent.AssetInfoDic.ContainsKey(item)) { m_ResourceComponent.AssetInfoDic[item] = new Dictionary <string, AssetInfo>(); } if (!m_ResourceComponent.ResourceInfoDic.ContainsKey(item)) { m_ResourceComponent.ResourceInfoDic[item] = new Dictionary <ResourceName, ResourceInfo>(); } if (!m_ResourceComponent.ReadWriteResourceInfoDic.ContainsKey(item)) { m_ResourceComponent.ReadWriteResourceInfoDic[item] = new SortedDictionary <ResourceName, ReadWriteResourceInfo>(); } } ResourceGroup defaultResourceGroup = m_ResourceComponent.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, resource.AssetCategory), 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, resource.AssetCategory); int[] assetIndexes = resource.GetAssetIndexes(); foreach (int assetIndex in assetIndexes) { UpdatableVersionList.Asset asset = assets[assetIndex]; int[] dependencyAssetIndexes = asset.GetDependencyAssetIndexes(); Dictionary <string, AssetCategory> dependencyAssetNames = new Dictionary <string, AssetCategory>(dependencyAssetIndexes.Length); foreach (int dependencyAssetIndex in dependencyAssetIndexes) { dependencyAssetNames.Add(assets[dependencyAssetIndex].Name, assets[dependencyAssetIndex].AssetCategory); } if (m_ResourceComponent.AssetInfoDic[asset.AssetCategory].ContainsKey(asset.Name)) { m_ResourceComponent.AssetInfoDic[asset.AssetCategory].Remove(asset.Name); } m_ResourceComponent.AssetInfoDic[asset.AssetCategory].Add(asset.Name, new AssetInfo(asset.Name, resourceName, dependencyAssetNames)); } SetVersionInfo(resourceName, (LoadType)resource.LoadType, resource.AssetCategory, resource.Length, resource.HashCode, resource.ZipLength, resource.ZipHashCode); defaultResourceGroup.AddResource(resourceName, resource.Length, resource.ZipLength); } foreach (UpdatableVersionList.ResourceGroup resourceGroup in resourceGroups) { ResourceGroup group = m_ResourceComponent.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.AssetCategory), resource.Length, resource.ZipLength); } } m_UpdatableVersionListReady = true; RefreshReadWriteCheckInfoStatus(); } catch (Exception exception) { if (exception is Exception) { throw; } throw new Exception(Utility.Text.Format("Parse updatable version list exception '{0}'.", exception.ToString()), exception); } finally { if (memoryStream != null) { memoryStream.Dispose(); memoryStream = null; } } }
/// <summary> /// 序列化可更新模式版本资源列表(版本 2)回调函数。 /// </summary> /// <param name="binaryWriter">目标流。</param> /// <param name="versionList">要序列化的可更新模式版本资源列表(版本 2)。</param> /// <returns>是否序列化可更新模式版本资源列表(版本 2)成功。</returns> public static bool UpdatableVersionListSerializeCallback_V2(BinaryWriter binaryWriter, UpdatableVersionList versionList) { if (!versionList.IsValid) { return(false); } Utility.Random.GetRandomBytes(s_CachedHashBytes); binaryWriter.Write(s_CachedHashBytes); binaryWriter.WriteEncryptedString(versionList.ApplicableGameVersion, s_CachedHashBytes); binaryWriter.WriteEncryptedString(versionList.InternalResourceVersion, s_CachedHashBytes); UpdatableVersionList.Asset[] assets = versionList.GetAssets(); binaryWriter.Write7BitEncodedInt32(assets.Length); foreach (UpdatableVersionList.Asset asset in assets) { binaryWriter.WriteEncryptedString(asset.Name, s_CachedHashBytes); binaryWriter.Write((byte)asset.AssetCategory); int[] dependencyAssetIndexes = asset.GetDependencyAssetIndexes(); binaryWriter.Write7BitEncodedInt32(dependencyAssetIndexes.Length); foreach (int dependencyAssetIndex in dependencyAssetIndexes) { binaryWriter.Write7BitEncodedInt32(dependencyAssetIndex); } } UpdatableVersionList.Resource[] resources = versionList.GetResources(); binaryWriter.Write7BitEncodedInt32(resources.Length); foreach (UpdatableVersionList.Resource resource in resources) { binaryWriter.WriteEncryptedString(resource.Name, s_CachedHashBytes); binaryWriter.WriteEncryptedString(resource.Variant, s_CachedHashBytes); binaryWriter.WriteEncryptedString(resource.Extension != DefaultExtension ? resource.Extension : null, s_CachedHashBytes); binaryWriter.Write(resource.LoadType); binaryWriter.Write7BitEncodedInt32(resource.Length); binaryWriter.Write(resource.HashCode); binaryWriter.Write7BitEncodedInt32(resource.ZipLength); binaryWriter.Write(resource.ZipHashCode); binaryWriter.Write((byte)resource.AssetCategory); int[] assetIndexes = resource.GetAssetIndexes(); binaryWriter.Write7BitEncodedInt32(assetIndexes.Length); foreach (int assetIndex in assetIndexes) { binaryWriter.Write7BitEncodedInt32(assetIndex); } } UpdatableVersionList.FileSystem[] fileSystems = versionList.GetFileSystems(); binaryWriter.Write7BitEncodedInt32(fileSystems.Length); foreach (UpdatableVersionList.FileSystem fileSystem in fileSystems) { binaryWriter.WriteEncryptedString(fileSystem.Name, s_CachedHashBytes); int[] resourceIndexes = fileSystem.GetResourceIndexes(); binaryWriter.Write7BitEncodedInt32(resourceIndexes.Length); foreach (int resourceIndex in resourceIndexes) { binaryWriter.Write7BitEncodedInt32(resourceIndex); } } UpdatableVersionList.ResourceGroup[] resourceGroups = versionList.GetResourceGroups(); binaryWriter.Write7BitEncodedInt32(resourceGroups.Length); foreach (UpdatableVersionList.ResourceGroup resourceGroup in resourceGroups) { binaryWriter.WriteEncryptedString(resourceGroup.Name, s_CachedHashBytes); int[] resourceIndexes = resourceGroup.GetResourceIndexes(); binaryWriter.Write7BitEncodedInt32(resourceIndexes.Length); foreach (int resourceIndex in resourceIndexes) { binaryWriter.Write7BitEncodedInt32(resourceIndex); } } Array.Clear(s_CachedHashBytes, 0, CachedHashBytesLength); return(true); }