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;
                }
            }
        }
        /// <summary>
        /// 反序列化本地版本资源列表(版本 1)回调函数。
        /// </summary>
        /// <param name="binaryReader">指定流。</param>
        /// <returns>反序列化的本地版本资源列表(版本 1)。</returns>
        public static LocalVersionList LocalVersionListDeserializeCallback_V1(BinaryReader binaryReader)
        {
            byte[] encryptBytes = binaryReader.ReadBytes(CachedHashBytesLength);

            int assetCount = binaryReader.Read7BitEncodedInt32();

            LocalVersionList.Asset[] assets = assetCount > 0 ? new LocalVersionList.Asset[assetCount] : null;
            for (int i = 0; i < assetCount; i++)
            {
                string        name                   = binaryReader.ReadEncryptedString(encryptBytes);
                AssetCategory assetCategory          = (AssetCategory)binaryReader.ReadByte();
                int           dependencyAssetCount   = binaryReader.Read7BitEncodedInt32();
                int[]         dependencyAssetIndexes = dependencyAssetCount > 0 ? new int[dependencyAssetCount] : null;
                for (int j = 0; j < dependencyAssetCount; j++)
                {
                    dependencyAssetIndexes[j] = binaryReader.Read7BitEncodedInt32();
                }

                assets[i] = new LocalVersionList.Asset(name, dependencyAssetIndexes, assetCategory);
            }

            int resourceCount = binaryReader.Read7BitEncodedInt32();

            LocalVersionList.Resource[] resources = resourceCount > 0 ? new LocalVersionList.Resource[resourceCount] : null;
            for (int i = 0; i < resourceCount; i++)
            {
                string        name            = binaryReader.ReadEncryptedString(encryptBytes);
                string        variant         = binaryReader.ReadEncryptedString(encryptBytes);
                string        extension       = binaryReader.ReadEncryptedString(encryptBytes) ?? DefaultExtension;
                byte          loadType        = binaryReader.ReadByte();
                int           length          = binaryReader.Read7BitEncodedInt32();
                int           hashCode        = binaryReader.ReadInt32();
                AssetCategory assetCategory   = (AssetCategory)binaryReader.ReadByte();
                int           assetIndexCount = binaryReader.Read7BitEncodedInt32();
                int[]         assetIndexes    = assetIndexCount > 0 ? new int[assetIndexCount] : null;
                for (int j = 0; j < assetIndexCount; j++)
                {
                    assetIndexes[j] = binaryReader.Read7BitEncodedInt32();
                }
                resources[i] = new LocalVersionList.Resource(name, variant, extension, loadType, length, hashCode, assetIndexes, assetCategory);
            }

            return(new LocalVersionList(assets, resources, null));
        }
        /// <summary>
        /// 反序列化本地版本资源列表(版本 0)回调函数。
        /// </summary>
        /// <param name="binaryReader">指定流。</param>
        /// <returns>反序列化的本地版本资源列表(版本 0)。</returns>
        public static LocalVersionList LocalVersionListDeserializeCallback_V0(BinaryReader binaryReader)
        {
            byte[] encryptBytes = binaryReader.ReadBytes(CachedHashBytesLength);

            int assetCount = binaryReader.ReadInt32();

            LocalVersionList.Asset[] assets = assetCount > 0 ? new LocalVersionList.Asset[assetCount] : null;

            int resourceCount = binaryReader.ReadInt32();

            LocalVersionList.Resource[] resources = resourceCount > 0 ? new LocalVersionList.Resource[resourceCount] : null;

            string[][] resourceToAssetNames = new string[resourceCount][];
            List <KeyValuePair <string, Dictionary <string, AssetCategory> > > assetNameToDependencyAssetNames = new List <KeyValuePair <string, Dictionary <string, AssetCategory> > >(assetCount);
            List <KeyValuePair <string, AssetCategory> > assetNameToDependencyAssetCategory = new List <KeyValuePair <string, AssetCategory> >(assetCount);

            for (int i = 0; i < resourceCount; i++)
            {
                string name     = binaryReader.ReadEncryptedString(encryptBytes);
                string variant  = binaryReader.ReadEncryptedString(encryptBytes);
                byte   loadType = binaryReader.ReadByte();
                int    length   = binaryReader.ReadInt32();
                int    hashCode = binaryReader.ReadInt32();

                AssetCategory assetCategory = (AssetCategory)binaryReader.ReadByte();
                Utility.Converter.GetBytes(hashCode, s_CachedHashBytes);

                int      assetNameCount = binaryReader.ReadInt32();
                string[] assetNames     = assetNameCount > 0 ? new string[assetNameCount] : null;

                for (int j = 0; j < assetNameCount; j++)
                {
                    assetNames[j] = binaryReader.ReadEncryptedString(s_CachedHashBytes);
                    AssetCategory dependencyAssetCategory  = (AssetCategory)binaryReader.ReadByte();
                    int           dependencyAssetNameCount = binaryReader.ReadInt32();
                    Dictionary <string, AssetCategory> dependencyAssetNames = dependencyAssetNameCount > 0 ? new Dictionary <string, AssetCategory>(dependencyAssetNameCount) : null;
                    for (int k = 0; k < dependencyAssetNameCount; k++)
                    {
                        string        dependencyAssetName      = binaryReader.ReadEncryptedString(s_CachedHashBytes);
                        AssetCategory dependencyAssetCategory2 = (AssetCategory)binaryReader.ReadByte();
                        dependencyAssetNames[dependencyAssetName] = dependencyAssetCategory2;
                    }

                    assetNameToDependencyAssetNames.Add(new KeyValuePair <string, Dictionary <string, AssetCategory> >(assetNames[j], dependencyAssetNames));
                    assetNameToDependencyAssetCategory.Add(new KeyValuePair <string, AssetCategory>(assetNames[j], dependencyAssetCategory));
                }

                resourceToAssetNames[i] = assetNames;
                resources[i]            = new LocalVersionList.Resource(name, variant, null, loadType, length, hashCode, assetNameCount > 0 ? new int[assetNameCount] : null, assetCategory);
            }

            assetNameToDependencyAssetNames.Sort(AssetNameToDependencyAssetNamesComparer);
            assetNameToDependencyAssetCategory.Sort(AssetNameToDependencyAssetNamesComparer);

            Array.Clear(s_CachedHashBytes, 0, CachedHashBytesLength);
            int index  = 0;
            int index2 = 0;

            foreach (KeyValuePair <string, Dictionary <string, AssetCategory> > i in assetNameToDependencyAssetNames)
            {
                if (i.Value != null)
                {
                    int[] dependencyAssetIndexes = new int[i.Value.Count];
                    int   dependencyAssetIndexe  = 0;
                    foreach (var item in i.Value)
                    {
                        dependencyAssetIndexes[dependencyAssetIndexe] = GetAssetNameIndex(assetNameToDependencyAssetNames, item.Key);
                        dependencyAssetIndexe += 1;
                    }

                    assets[index++] = new LocalVersionList.Asset(i.Key, dependencyAssetIndexes, assetNameToDependencyAssetCategory[index2].Value);
                }
                else
                {
                    assets[index++] = new LocalVersionList.Asset(i.Key, null, assetNameToDependencyAssetCategory[index2].Value);
                }

                index2 += 1;
            }

            for (int i = 0; i < resources.Length; i++)
            {
                int[] assetIndexes = resources[i].GetAssetIndexes();
                for (int j = 0; j < assetIndexes.Length; j++)
                {
                    assetIndexes[j] = GetAssetNameIndex(assetNameToDependencyAssetNames, resourceToAssetNames[i][j]);
                }
            }

            return(new LocalVersionList(assets, resources, null));
        }
Exemplo n.º 4
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);
            }
        }
        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;
                }
            }
        }