コード例 #1
0
        /// <summary>
        /// 应用指定资源包的资源。
        /// </summary>
        /// <param name="resourcePackPath">要应用的资源包路径。</param>
        public void ApplyResources(string resourcePackPath)
        {
            if (!m_CheckResourcesComplete)
            {
                throw new Exception("You must check resources complete first.");
            }

            if (m_ApplyingResourcePackStream != null)
            {
                throw new Exception(Utility.Text.Format("There is already a resource pack '{0}' being applied.", m_ApplyingResourcePackPath));
            }

            if (m_UpdatingResourceGroup != null)
            {
                throw new Exception(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_ResourceComponent.ResourcePackVersionListSerializer.Deserialize(fileStream);
                }

                if (!versionList.IsValid)
                {
                    throw new Exception("Deserialize resource pack version list failure.");
                }

                if (versionList.Offset + versionList.Length != length)
                {
                    throw new Exception("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)
                {
                    ResourceName resourceName = new ResourceName(resource.Name, resource.Variant, resource.Extension, resource.AssetCategory);
                    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)
                    {
                        m_ApplyWaitingInfo.Add(new ApplyInfo(resourceName, updateInfo.FileSystemName, (LoadType)resource.LoadType, resource.Offset, resource.Length, resource.HashCode, resource.ZipLength, resource.ZipHashCode, updateInfo.ResourcePath));
                    }
                }
            }
            catch (Exception exception)
            {
                if (m_ApplyingResourcePackStream != null)
                {
                    m_ApplyingResourcePackStream.Dispose();
                    m_ApplyingResourcePackStream = null;
                }

                throw new Exception(Utility.Text.Format("Apply resources '{0}' with exception '{1}'.", resourcePackPath, exception.ToString()), exception);
            }
        }
コード例 #2
0
        /// <summary>
        /// 序列化资源包版本资源列表(版本 0)回调函数。
        /// </summary>
        /// <param name="binaryWriter">目标流。</param>
        /// <param name="versionList">要序列化的资源包版本资源列表(版本 0)。</param>
        /// <returns>是否序列化资源包版本资源列表(版本 0)成功。</returns>
        public static bool ResourcePackVersionListSerializeCallback_V0(BinaryWriter binaryWriter, ResourcePackVersionList versionList)
        {
            if (!versionList.IsValid)
            {
                return(false);
            }

            Utility.Random.GetRandomBytes(s_CachedHashBytes);
            binaryWriter.Write(s_CachedHashBytes);
            binaryWriter.Write(versionList.Offset);
            binaryWriter.Write(versionList.Length);
            binaryWriter.Write(versionList.HashCode);
            ResourcePackVersionList.Resource[] resources = versionList.GetResources();
            binaryWriter.Write7BitEncodedInt32(resources.Length);
            foreach (ResourcePackVersionList.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.Write7BitEncodedInt64(resource.Offset);
                binaryWriter.Write7BitEncodedInt32(resource.Length);
                binaryWriter.Write(resource.HashCode);
                binaryWriter.Write7BitEncodedInt32(resource.ZipLength);
                binaryWriter.Write(resource.ZipHashCode);
            }

            Array.Clear(s_CachedHashBytes, 0, CachedHashBytesLength);
            return(true);
        }