示例#1
0
        private void OnDownloadSuccess(int serialId, string downloadPath, string downloadUri, int currentLength, object userData)
        {
            UpdateInfo updateInfo = userData as UpdateInfo;

            if (updateInfo == null)
            {
                return;
            }

            using (FileStream fileStream = new FileStream(downloadPath, FileMode.Open, FileAccess.ReadWrite))
            {
                bool zip = (updateInfo.Length != updateInfo.ZipLength || updateInfo.HashCode != updateInfo.ZipHashCode);

                int length = (int)fileStream.Length;
                if (length != updateInfo.ZipLength)
                {
                    fileStream.Close();
                    string errorMessage = TextUtil.Format("Zip length error, need '{0}', downloaded '{1}'.", updateInfo.ZipLength.ToString(), length.ToString());
                    OnDownloadFailure(serialId, downloadPath, downloadUri, errorMessage, userData);
                    return;
                }

                if (m_ResourceManager.UpdateFileCacheLength < length)
                {
                    m_ResourceManager.UpdateFileCacheLength = (length / OneMegaBytes + 1) * OneMegaBytes;
                }

                int offset = 0;
                int count  = length;
                while (count > 0)
                {
                    int bytesRead = fileStream.Read(m_ResourceManager.UpdateFileCache, offset, count);
                    if (bytesRead <= 0)
                    {
                        throw new Exception(TextUtil.Format("Unknown error when load file '{0}'.", downloadPath));
                    }

                    offset += bytesRead;
                    count  -= bytesRead;
                }

                int hashCode = ConverterUtil.GetInt32(VerifierUtil.GetCrc32(m_ResourceManager.UpdateFileCache, 0, length));
                if (hashCode != updateInfo.ZipHashCode)
                {
                    fileStream.Close();
                    string errorMessage = TextUtil.Format("Zip hash code error, need '{0}', downloaded '{1}'.", updateInfo.ZipHashCode.ToString("X8"), hashCode.ToString("X8"));
                    OnDownloadFailure(serialId, downloadPath, downloadUri, errorMessage, userData);
                    return;
                }

                if (zip)
                {
                    try
                    {
                        if (m_ResourceManager.DecompressCache == null)
                        {
                            m_ResourceManager.DecompressCache = new MemoryStream();
                        }

                        m_ResourceManager.DecompressCache.Position = 0L;
                        m_ResourceManager.DecompressCache.SetLength(0L);
                        if (!ZipUtil.Decompress(m_ResourceManager.UpdateFileCache, 0, length, m_ResourceManager.DecompressCache))
                        {
                            fileStream.Close();
                            string errorMessage = TextUtil.Format("Unable to decompress from file '{0}'.", downloadPath);
                            OnDownloadFailure(serialId, downloadPath, downloadUri, errorMessage, userData);
                            return;
                        }

                        if (m_ResourceManager.DecompressCache.Length != updateInfo.Length)
                        {
                            fileStream.Close();
                            string errorMessage = TextUtil.Format("Resource length error, need '{0}', downloaded '{1}'.", updateInfo.Length.ToString(), m_ResourceManager.UpdateFileCache.Length.ToString());
                            OnDownloadFailure(serialId, downloadPath, downloadUri, errorMessage, userData);
                            return;
                        }

                        fileStream.Position = 0L;
                        fileStream.SetLength(0L);
                        m_ResourceManager.DecompressCache.Position = 0L;
                        int bytesRead = 0;
                        while ((bytesRead = m_ResourceManager.DecompressCache.Read(m_ResourceManager.UpdateFileCache, 0, m_ResourceManager.UpdateFileCache.Length)) > 0)
                        {
                            fileStream.Write(m_ResourceManager.UpdateFileCache, 0, bytesRead);
                        }
                    }
                    catch (Exception exception)
                    {
                        fileStream.Close();
                        string errorMessage = TextUtil.Format("Unable to decompress from file '{0}' with error message '{1}'.", downloadPath, exception.Message);
                        OnDownloadFailure(serialId, downloadPath, downloadUri, errorMessage, userData);
                        return;
                    }
                }
            }

            m_UpdatingCount--;

            if (m_ResourceManager.ResourceInfos.ContainsKey(updateInfo.ResourceName))
            {
                throw new Exception(TextUtil.Format("Resource info '{0}' is already exist.", updateInfo.ResourceName));
            }

            m_ResourceManager.ResourceInfos.Add(updateInfo.ResourceName, new ResourceInfo(updateInfo.ResourceName, updateInfo.Length, updateInfo.HashCode, false));
            m_ResourceManager.ResourceNames.Add(updateInfo.ResourceName);

            if (m_ResourceManager.ReadWriteResourceInfos.ContainsKey(updateInfo.ResourceName))
            {
                throw new Exception(TextUtil.Format("Read-write resource info '{0}' is already exist.", updateInfo.ResourceName));
            }

            m_ResourceManager.ReadWriteResourceInfos.Add(updateInfo.ResourceName, new ReadWriteResourceInfo(updateInfo.Length, updateInfo.HashCode));

            m_CurrentGenerateReadWriteListLength += updateInfo.ZipLength;
            if (m_UpdatingCount <= 0 || m_CurrentGenerateReadWriteListLength >= m_GenerateReadWriteListLength)
            {
                m_CurrentGenerateReadWriteListLength = 0;
                GenerateReadWriteList();
            }

            if (ResourceUpdateSuccess != null)
            {
                ResourceUpdateSuccess(updateInfo.ResourceName, downloadPath, downloadUri, updateInfo.Length, updateInfo.ZipLength);
            }
        }
示例#2
0
        private void OnDownloadSuccess(int serialId, string downloadPath, string downloadUri, int currentLength, object userData)
        {
            VersionListProcessor versionListProcessor = userData as VersionListProcessor;

            if (versionListProcessor == null || versionListProcessor != this)
            {
                return;
            }

            using (FileStream fileStream = new FileStream(downloadPath, FileMode.Open, FileAccess.ReadWrite))
            {
                int length = (int)fileStream.Length;
                if (length != m_VersionListZipLength)
                {
                    fileStream.Close();
                    string errorMessage = TextUtil.Format("Latest version list zip length error, need '{0}', downloaded '{1}'.", m_VersionListZipLength.ToString(), length.ToString());
                    OnDownloadFailure(serialId, downloadPath, downloadUri, errorMessage, userData);
                    return;
                }

                if (m_UpdateFileCache == null || m_UpdateFileCache.Length < length)
                {
                    m_UpdateFileCache = new byte[(length / OneMegaBytes + 1) * OneMegaBytes];
                }

                int offset = 0;
                int count  = length;
                while (count > 0)
                {
                    int bytesRead = fileStream.Read(m_UpdateFileCache, offset, count);
                    if (bytesRead <= 0)
                    {
                        throw new Exception(TextUtil.Format("Unknown error when load file '{0}'.", downloadPath));
                    }

                    offset += bytesRead;
                    count  -= bytesRead;
                }

                int hashCode = ConverterUtil.GetInt32(VerifierUtil.GetCrc32(m_UpdateFileCache, 0, length));
                if (hashCode != m_VersionListZipHashCode)
                {
                    fileStream.Close();
                    string errorMessage = TextUtil.Format("Latest version list zip hash code error, need '{0}', downloaded '{1}'.", m_VersionListZipHashCode.ToString("X8"), hashCode.ToString("X8"));
                    OnDownloadFailure(serialId, downloadPath, downloadUri, errorMessage, userData);
                    return;
                }

                try
                {
                    if (m_DecompressCache == null)
                    {
                        m_DecompressCache = new MemoryStream();
                    }

                    m_DecompressCache.Position = 0L;
                    m_DecompressCache.SetLength(0L);
                    if (!ZipUtil.Decompress(m_UpdateFileCache, 0, length, m_DecompressCache))
                    {
                        fileStream.Close();
                        string errorMessage = TextUtil.Format("Unable to decompress latest version list '{0}'.", downloadPath);
                        OnDownloadFailure(serialId, downloadPath, downloadUri, errorMessage, userData);
                        return;
                    }


                    if (m_DecompressCache.Length != m_VersionListLength)
                    {
                        fileStream.Close();
                        string errorMessage = TextUtil.Format("Latest version list length error, need '{0}', downloaded '{1}'.", m_VersionListLength.ToString(), m_DecompressCache.Length.ToString());
                        OnDownloadFailure(serialId, downloadPath, downloadUri, errorMessage, userData);
                        return;
                    }

                    fileStream.Position = 0L;
                    fileStream.SetLength(0L);
                    m_DecompressCache.Position = 0L;
                    int bytesRead = 0;
                    while ((bytesRead = m_DecompressCache.Read(m_UpdateFileCache, 0, m_UpdateFileCache.Length)) > 0)
                    {
                        fileStream.Write(m_UpdateFileCache, 0, bytesRead);
                    }
                }
                catch (Exception exception)
                {
                    fileStream.Close();
                    string errorMessage = TextUtil.Format("Unable to decompress latest version list '{0}' with error message '{1}'.", downloadPath, exception.Message);
                    OnDownloadFailure(serialId, downloadPath, downloadUri, errorMessage, userData);
                    return;
                }
            }

            if (VersionListUpdateSuccess != null)
            {
                VersionListUpdateSuccess(downloadPath, downloadUri);
            }
        }