private void OnLoadReadWriteVersionListSuccess(string fileUri, byte[] bytes, float duration, object userData)
        {
            if (m_ReadWriteVersionListReady)
            {
                throw new Exception("Read write version list has been parsed.");
            }

            MemoryStream memoryStream = null;

            try
            {
                memoryStream = new MemoryStream(bytes, false);
                LocalVersionList versionList = m_ResourceComponent.ReadWriteVersionListSerializer.Deserialize(memoryStream);
                if (!versionList.IsValid)
                {
                    throw new Exception("Deserialize read write version list failure.");
                }

                LocalVersionList.Resource[]   resources   = versionList.GetResources();
                LocalVersionList.FileSystem[] fileSystems = versionList.GetFileSystems();

                foreach (LocalVersionList.FileSystem fileSystem in fileSystems)
                {
                    int[] resourceIndexes = fileSystem.GetResourceIndexes();
                    foreach (int resourceIndex in resourceIndexes)
                    {
                        LocalVersionList.Resource resource = resources[resourceIndex];
                        SetCachedFileSystemName(new ResourceName(resource.Name, resource.Variant, resource.Extension, resource.AssetCategory), fileSystem.Name);
                    }
                }

                foreach (LocalVersionList.Resource resource in resources)
                {
                    ResourceName resourceName = new ResourceName(resource.Name, resource.Variant, resource.Extension, resource.AssetCategory);
                    SetReadWriteInfo(resourceName, (LoadType)resource.LoadType, resource.AssetCategory, resource.Length, resource.HashCode);
                }

                m_ReadWriteVersionListReady = true;
                RefreshReadWriteCheckInfoStatus();
            }
            catch (Exception exception)
            {
                if (exception is Exception)
                {
                    throw;
                }

                throw new Exception(Utility.Text.Format("Parse read write version list exception '{0}'.", exception.ToString()), exception);
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Dispose();
                    memoryStream = null;
                }
            }
        }
Beispiel #2
0
        private void GenerateReadWriteVersionList()
        {
            if (File.Exists(m_ReadWriteVersionListFileName))
            {
                if (File.Exists(m_ReadWriteVersionListBackupFileName))
                {
                    File.Delete(m_ReadWriteVersionListBackupFileName);
                }

                File.Move(m_ReadWriteVersionListFileName, m_ReadWriteVersionListBackupFileName);
            }

            FileStream fileStream = null;

            try
            {
                fileStream = new FileStream(m_ReadWriteVersionListFileName, FileMode.Create, FileAccess.Write);

                int             length          = 0;
                AssetCategory[] assetCategories = m_ResourceComponent.AssetCategories;
                var             item            = assetCategories.GetEnumerator();
                while (item.MoveNext())
                {
                    length += m_ResourceComponent.ReadWriteResourceInfoDic[(AssetCategory)item.Current].Count;
                }

                LocalVersionList.Resource[] resources = length > 0 ? new LocalVersionList.Resource[length] : null;
                if (resources != null)
                {
                    int index = 0;

                    foreach (var ou in m_ResourceComponent.ReadWriteResourceInfoDic)
                    {
                        foreach (KeyValuePair <ResourceName, ReadWriteResourceInfo> i in ou.Value)
                        {
                            resources[index] = new LocalVersionList.Resource(i.Key.Name, i.Key.Variant, i.Key.Extension, (byte)i.Value.LoadType, i.Value.Length, i.Value.HashCode, null, i.Key.AssetCategory);
                            if (i.Value.UseFileSystem)
                            {
                                List <int> resourceIndexes = null;
                                if (!m_CachedFileSystemsForGenerateReadWriteVersionList.TryGetValue(i.Value.FileSystemName, out resourceIndexes))
                                {
                                    resourceIndexes = new List <int>();
                                    m_CachedFileSystemsForGenerateReadWriteVersionList.Add(i.Value.FileSystemName, resourceIndexes);
                                }

                                resourceIndexes.Add(index);
                            }

                            index++;
                        }
                    }
                }

                LocalVersionList.FileSystem[] fileSystems = m_CachedFileSystemsForGenerateReadWriteVersionList.Count > 0 ? new LocalVersionList.FileSystem[m_CachedFileSystemsForGenerateReadWriteVersionList.Count] : null;
                if (fileSystems != null)
                {
                    int index = 0;
                    foreach (KeyValuePair <string, List <int> > i in m_CachedFileSystemsForGenerateReadWriteVersionList)
                    {
                        fileSystems[index++] = new LocalVersionList.FileSystem(i.Key, i.Value.ToArray());
                        i.Value.Clear();
                    }
                }

                LocalVersionList versionList = new LocalVersionList(null, resources, fileSystems);
                if (!m_ResourceComponent.ReadWriteVersionListSerializer.Serialize(fileStream, versionList))
                {
                    throw new Exception("Serialize read write version list failure.");
                }

                if (fileStream != null)
                {
                    fileStream.Dispose();
                    fileStream = null;
                }

                if (File.Exists(m_ReadWriteVersionListBackupFileName))
                {
                    File.Delete(m_ReadWriteVersionListBackupFileName);
                }
            }
            catch (Exception exception)
            {
                if (fileStream != null)
                {
                    fileStream.Dispose();
                    fileStream = null;
                }

                if (File.Exists(m_ReadWriteVersionListFileName))
                {
                    File.Delete(m_ReadWriteVersionListFileName);
                }

                if (File.Exists(m_ReadWriteVersionListBackupFileName))
                {
                    File.Move(m_ReadWriteVersionListBackupFileName, m_ReadWriteVersionListFileName);
                }

                throw new Exception(Utility.Text.Format("Generate read write version list exception '{0}'.", exception.ToString()), exception);
            }
        }
        /// <summary>
        /// 序列化本地版本资源列表(版本 1)回调函数。
        /// </summary>
        /// <param name="binaryWriter">目标流。</param>
        /// <param name="versionList">要序列化的本地版本资源列表(版本 1)。</param>
        /// <returns>是否序列化本地版本资源列表(版本 1)成功。</returns>
        public static bool LocalVersionListSerializeCallback_V1(BinaryWriter binaryWriter, LocalVersionList versionList)
        {
            if (!versionList.IsValid)
            {
                return(false);
            }

            Utility.Random.GetRandomBytes(s_CachedHashBytes);
            binaryWriter.Write(s_CachedHashBytes);

            LocalVersionList.Asset[] assets = versionList.GetAssets();
            binaryWriter.Write7BitEncodedInt32(assets.Length);
            foreach (LocalVersionList.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);
                }
            }

            LocalVersionList.Resource[] resources = versionList.GetResources();
            binaryWriter.Write7BitEncodedInt32(resources.Length);
            foreach (LocalVersionList.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.Write((byte)resource.AssetCategory);
                int[] assetIndexes = resource.GetAssetIndexes();
                binaryWriter.Write7BitEncodedInt32(assetIndexes.Length);
                foreach (int assetIndex in assetIndexes)
                {
                    binaryWriter.Write7BitEncodedInt32(assetIndex);
                }
            }

            Array.Clear(s_CachedHashBytes, 0, CachedHashBytesLength);
            return(true);
        }
        /// <summary>
        /// 序列化本地版本资源列表(版本 0)回调函数。
        /// </summary>
        /// <param name="binaryWriter">目标流。</param>
        /// <param name="versionList">要序列化的本地版本资源列表(版本 0)。</param>
        /// <returns>是否序列化本地版本资源列表(版本 0)成功。</returns>
        public static bool LocalVersionListSerializeCallback_V0(BinaryWriter binaryWriter, LocalVersionList versionList)
        {
            if (!versionList.IsValid)
            {
                return(false);
            }

            Utility.Random.GetRandomBytes(s_CachedHashBytes);
            binaryWriter.Write(s_CachedHashBytes);
            LocalVersionList.Asset[] assets = versionList.GetAssets();
            binaryWriter.Write(assets.Length);
            LocalVersionList.Resource[] resources = versionList.GetResources();
            binaryWriter.Write(resources.Length);
            foreach (LocalVersionList.Resource resource in resources)
            {
                binaryWriter.WriteEncryptedString(resource.Name, s_CachedHashBytes);
                binaryWriter.WriteEncryptedString(resource.Variant, s_CachedHashBytes);
                binaryWriter.Write(resource.LoadType);
                binaryWriter.Write(resource.Length);
                binaryWriter.Write(resource.HashCode);
                binaryWriter.Write((byte)resource.AssetCategory);
                int[] assetIndexes = resource.GetAssetIndexes();
                binaryWriter.Write(assetIndexes.Length);
                byte[] hashBytes = new byte[CachedHashBytesLength];
                foreach (int assetIndex in assetIndexes)
                {
                    Utility.Converter.GetBytes(resource.HashCode, hashBytes);
                    LocalVersionList.Asset asset = assets[assetIndex];
                    binaryWriter.WriteEncryptedString(asset.Name, hashBytes);
                    int[] dependencyAssetIndexes = asset.GetDependencyAssetIndexes();
                    binaryWriter.Write(dependencyAssetIndexes.Length);
                    foreach (int dependencyAssetIndex in dependencyAssetIndexes)
                    {
                        binaryWriter.WriteEncryptedString(assets[dependencyAssetIndex].Name, hashBytes);
                        binaryWriter.Write((byte)assets[dependencyAssetIndex].AssetCategory);
                    }
                }
            }

            Array.Clear(s_CachedHashBytes, 0, CachedHashBytesLength);
            return(true);
        }
        private void OnLoadReadOnlyVersionListSuccess(string fileUri, byte[] bytes, float duration, object userData)
        {
            if (m_ReadOnlyVersionListReady)
            {
                throw new Exception("Read only version list has been parsed.");
            }

            MemoryStream memoryStream = null;

            try
            {
                memoryStream = new MemoryStream(bytes, false);
                LocalVersionList versionList = m_ResourceComponent.ReadOnlyVersionListSerializer.Deserialize(memoryStream);
                if (!versionList.IsValid)
                {
                    throw new Exception("Deserialize read only version list failure.");
                }

                LocalVersionList.Asset[]      assets      = versionList.GetAssets();
                LocalVersionList.Resource[]   resources   = versionList.GetResources();
                LocalVersionList.FileSystem[] fileSystems = versionList.GetFileSystems();

                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>();
                    }
                }

                foreach (LocalVersionList.FileSystem fileSystem in fileSystems)
                {
                    int[] resourceIndexes = fileSystem.GetResourceIndexes();
                    foreach (int resourceIndex in resourceIndexes)
                    {
                        LocalVersionList.Resource resource = resources[resourceIndex];
                        SetCachedFileSystemName(new ResourceName(resource.Name, resource.Variant, resource.Extension, resource.AssetCategory), fileSystem.Name);
                    }
                }

                foreach (LocalVersionList.Resource resource in resources)
                {
                    ResourceName resourceName = new ResourceName(resource.Name, resource.Variant, resource.Extension, resource.AssetCategory);
                    int[]        assetIndexes = resource.GetAssetIndexes();
                    foreach (int assetIndex in assetIndexes)
                    {
                        LocalVersionList.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);
                        }

                        m_ResourceComponent.AssetInfoDic[asset.AssetCategory].Add(asset.Name, new AssetInfo(asset.Name, resourceName, dependencyAssetNames));
                    }


                    SetReadOnlyInfo(new ResourceName(resource.Name, resource.Variant, resource.Extension, resource.AssetCategory), (LoadType)resource.LoadType, resource.AssetCategory, resource.Length, resource.HashCode);
                }

                m_ReadOnlyVersionListReady = true;
                RefreshReadOnlyCheckInfoStatus();
            }
            catch (Exception exception)
            {
                if (exception is Exception)
                {
                    throw;
                }

                throw new Exception(Utility.Text.Format("Parse read only version list exception '{0}'.", exception.ToString()), exception);
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Dispose();
                    memoryStream = null;
                }
            }
        }