Beispiel #1
0
        void OnHttpRead(HttpClientResponse response, long totalRead)
        {
            AutoUpdateCfgItem item = m_Items[m_Curr];

            long delta = totalRead - response.ReadBytes;

            if (delta > 0)
            {
                double curM = AutoUpdateMgr.Instance.CurDownM;
                curM += ((double)delta) / ((double)1024 * 1024);
                AutoUpdateMgr.Instance.CurDownM = curM;
            }

            item.readBytes = totalRead;
            if (totalRead >= response.MaxReadBytes)
            {
                item.isDone = true;
            }
            AutoUpdateMgr.Instance.DownloadUpdateToUpdateTxt(item);

            float currProcess = 0;

            if (response.MaxReadBytes > 0)
            {
                currProcess = (float)totalRead / (float)response.MaxReadBytes;
            }
            CalcDownProcess(currProcess);

            if (totalRead >= response.MaxReadBytes)
            {
                StartNextDownload();
            }
        }
Beispiel #2
0
        void DebugFileError()
        {
            AutoUpdateCfgItem item = m_Items[m_Curr];

            UnityEngine.Debug.LogErrorFormat("[downloadFileErr]{0} download: {1:D} isOk: {2}", item.fileContentMd5,
                                             item.readBytes, item.isDone.ToString());
        }
 public void Reset()
 {
     if (http != null)
     {
         http.Dispose();
         http = null;
     }
     item = new AutoUpdateCfgItem();
 }
        public static AutoUpdateCfgItem LoadBinary(Stream stream)
        {
            AutoUpdateCfgItem ret = new AutoUpdateCfgItem();

            ret.fileContentMd5 = FilePathMgr.Instance.ReadString(stream);
            ret.readBytes      = FilePathMgr.Instance.ReadLong(stream);
            ret.isDone         = FilePathMgr.Instance.ReadBool(stream);
            return(ret);
        }
        public override void Enter(AutoUpdateMgr target)
        {
            string oldVer = target.LocalResVersion;
            string newVer = target.CurrServeResrVersion;

            var updateFile = target.LocalUpdateFile;

            m_ZipFileName = ZipTools.GetZipFileName(oldVer, newVer);

            long read = 0;
            AutoUpdateCfgItem item;

            m_ZipFileName = string.Format("{0}.zip", m_ZipFileName);
            bool isSaveUpdateFile = false;

            if (updateFile.FindItem(m_ZipFileName, out item))
            {
                if (item.isDone)
                {
                    ToNextState();
                    return;
                }

                read = item.readBytes;
            }
            else
            {
                item = new AutoUpdateCfgItem();
                item.fileContentMd5 = m_ZipFileName;
                item.isDone         = false;
                item.readBytes      = 0;
                updateFile.AddOrSet(item);
                isSaveUpdateFile = true;
            }

            isSaveUpdateFile = isSaveUpdateFile || updateFile.RemoveDowningZipFiles(m_ZipFileName);
            if (isSaveUpdateFile)
            {
                updateFile.SaveToLastFile();
            }

            string resAddr = target.ResServerAddr;
            bool   isHttps = resAddr.StartsWith("https://", StringComparison.CurrentCultureIgnoreCase);
            string url;

            if (isHttps)
            {
                url = string.Format("{0}/{1}", resAddr, m_ZipFileName);
            }
            else
            {
                long tt = DateTime.UtcNow.Ticks;
                url = string.Format("{0}/{1}?time={2}", resAddr, m_ZipFileName, tt.ToString());
            }
            target.CreateHttpFile(url, read, OnHttpRead, OnHttpError);
        }
Beispiel #6
0
        public bool DownloadUpdate(AutoUpdateCfgItem item)
        {
            if (m_Dict.ContainsKey(item.fileContentMd5))
            {
                m_Dict[item.fileContentMd5] = item;
                return(true);
            }

            return(false);
        }
        void OnHttpRead(HttpClientResponse response, long totalRead)
        {
            FileThreadInfo info = response.UserData as FileThreadInfo;

            if (info != null && m_Data != null && info.fileIdx >= 0)
            {
                AutoUpdateCfgItem item = m_Data[info.fileIdx];
                long delta             = totalRead - response.ReadBytes;
                if (delta > 0)
                {
                    double curM = AutoUpdateMgr.Instance.CurDownM;
                    curM += ((double)delta) / ((double)1024 * 1024);
                    AutoUpdateMgr.Instance.CurDownM = curM;
                    item.readBytes += delta;

                    float  process;
                    double maxM = AutoUpdateMgr.Instance.TotalDownM;
                    if (maxM > float.Epsilon)
                    {
                        process = (float)(curM / maxM);
                        if (process > 1f)
                        {
                            process = 1f;
                        }
                    }
                    else
                    {
                        process = 0f;
                    }
                    AutoUpdateMgr.Instance.DownProcess = process;
                }

                if (totalRead >= response.MaxReadBytes)
                {
                    item.isDone = true;
                }

                m_Data[info.fileIdx] = item;

                lock (m_Lock)
                {
                    AutoUpdateMgr.Instance.DownloadUpdateToUpdateTxt(item);
                }

                if (totalRead >= response.MaxReadBytes)
                {
                    lock (m_Lock)
                    {
                        info.Reset();
                    }

                    StartNextDown();
                }
            }
        }
Beispiel #8
0
 internal void DownloadUpdateToUpdateTxt(AutoUpdateCfgItem item)
 {
     if (m_UpdateFile == null)
     {
         return;
     }
     if (m_UpdateFile.DownloadUpdate(item))
     {
         m_UpdateFile.SaveToLastFile();
     }
 }
Beispiel #9
0
 public bool FindItem(string fileContentMd5, out AutoUpdateCfgItem item)
 {
     item = new AutoUpdateCfgItem();
     if (string.IsNullOrEmpty(fileContentMd5))
     {
         return(false);
     }
     if (!m_Dict.TryGetValue(fileContentMd5, out item))
     {
         return(false);
     }
     return(true);
 }
        public void LoadBinary(Stream stream)
        {
            if (stream == null)
            {
                return;
            }
            int cnt = FilePathMgr.Instance.ReadInt(stream);

            for (int i = 0; i < cnt; ++i)
            {
                AutoUpdateCfgItem item = AutoUpdateCfgItem.LoadBinary(stream);
                AddOrSet(item);
            }
        }
Beispiel #11
0
 public void AddOrSet(AutoUpdateCfgItem item)
 {
     if (string.IsNullOrEmpty(item.fileContentMd5))
     {
         return;
     }
     if (m_Dict.ContainsKey(item.fileContentMd5))
     {
         m_Dict[item.fileContentMd5] = item;
     }
     else
     {
         m_Dict.Add(item.fileContentMd5, item);
     }
 }
Beispiel #12
0
        void OnHttpRead(HttpClientResponse response, long totalRead)
        {
            AutoUpdateCfgItem item = m_Items[m_Curr];

            long delta = totalRead - response.ReadBytes;

            if (delta > 0)
            {
                double curM = AutoUpdateMgr.Instance.CurDownM;
                curM += ((double)delta) / ((double)1024 * 1024);
                AutoUpdateMgr.Instance.CurDownM = curM;
                item.readBytes += delta;
            }

            //item.readBytes = totalRead;
            if (totalRead >= response.MaxReadBytes)
            {
                item.isDone = true;
            }

            m_Items[m_Curr] = item;
            AutoUpdateMgr.Instance.DownloadUpdateToUpdateTxt(item);

            /*
             *          float currProcess = 0;
             *          if (response.MaxReadBytes > 0)
             *                  currProcess = (float)totalRead/(float)response.MaxReadBytes;
             *          CalcDownProcess(currProcess);
             *          AutoUpdateMgr.Instance.DownProcess = currProcess;*/
            double cur = AutoUpdateMgr.Instance.CurDownM;
            double max = AutoUpdateMgr.Instance.TotalDownM;

            if (max <= float.Epsilon)
            {
                AutoUpdateMgr.Instance.DownProcess = 0;
            }
            else
            {
                AutoUpdateMgr.Instance.DownProcess = (float)(cur / max);
            }

            if (totalRead >= response.MaxReadBytes)
            {
                StartNextDownload();
            }
        }
        bool GetNextDownload(out AutoUpdateCfgItem item)
        {
            if (m_WaitList == null || m_WaitList.Count <= 0)
            {
                item = new AutoUpdateCfgItem();
                return(false);
            }
            var first = m_WaitList.First;

            if (first == null)
            {
                item = new AutoUpdateCfgItem();
                return(false);
            }
            item = first.Value;
            m_WaitList.RemoveFirst();
            return(true);
        }
Beispiel #14
0
        public void Load(ResListFile listFile)
        {
            Clear();
            if (listFile == null)
            {
                return;
            }
            var iter = listFile.GetIter();

            while (iter.MoveNext())
            {
                AutoUpdateCfgItem item = new AutoUpdateCfgItem();
                item.fileContentMd5 = iter.Current.Value.fileContentMd5;
                item.isDone         = false;
                item.readBytes      = 0;
                AddOrSet(item);
            }
            iter.Dispose();
        }
Beispiel #15
0
        public override void Enter(AutoUpdateMgr target)
        {
            var updateFile = target.LocalUpdateFile;

            m_ZipFileName = target.CurrUpdateZipFileMd5;

            long read = 0;
            AutoUpdateCfgItem item;
            bool isSaveUpdateFile = false;

            if (updateFile.FindItem(m_ZipFileName, out item))
            {
                if (item.isDone)
                {
                    ToUnZipRes();
                    return;
                }

                read = item.readBytes;
            }
            else
            {
                item = new AutoUpdateCfgItem();
                item.fileContentMd5 = m_ZipFileName;
                item.isDone         = false;
                item.readBytes      = 0;
                updateFile.AddOrSet(item);
                isSaveUpdateFile = true;
            }

            isSaveUpdateFile = isSaveUpdateFile || updateFile.RemoveDowningZipFiles(m_ZipFileName);
            if (isSaveUpdateFile)
            {
                updateFile.SaveToLastFile();
            }

            string resAddr = target.ResServerAddr;
            // m_ZipFileName是内容MD5所以不用加时间戳
            string url = string.Format("{0}/{1}", resAddr, m_ZipFileName);

            target.CreateHttpFile(url, read, OnHttpRead, OnHttpError);
        }
        private void RefreshDownBytes()
        {
            if (m_Data == null || m_Data.Length <= 0)
            {
                AutoUpdateMgr.Instance.CurDownM = 0;
                return;
            }

            double downed = 0f;

            for (int i = 0; i < m_Data.Length; ++i)
            {
                AutoUpdateCfgItem info = m_Data[i];
                if (info.readBytes > 0)
                {
                    downed += ((double)info.readBytes) / ((double)(1024 * 1024));
                }
            }

            AutoUpdateMgr.Instance.CurDownM = downed;
        }
Beispiel #17
0
        void OnHttpRead(HttpClientResponse response, long totalRead)
        {
            AutoUpdateCfgItem item = m_Items[m_Curr];

            item.readBytes = totalRead;
            if (totalRead >= response.MaxReadBytes)
            {
                item.isDone = true;
            }
            AutoUpdateMgr.Instance.DownloadUpdateToUpdateTxt(item);

            float currProcess = 0;

            if (response.MaxReadBytes > 0)
            {
                currProcess = (float)totalRead / (float)response.MaxReadBytes;
            }
            CalcDownProcess(currProcess);

            if (totalRead >= response.MaxReadBytes)
            {
                StartNextDownload();
            }
        }
        // 新的下载接口(都是主线程)
        void OnHttpProcess(HttpClient client)
        {
            HttpClientFileStream fileStream = client.Listener as HttpClientFileStream;
            AutoUpdateCfgItem    item       = m_Items[m_Curr];
            long oldReadBytes = item.readBytes;

            item.readBytes = fileStream.StartPos + fileStream.ReadBytes;
            long delta = item.readBytes - oldReadBytes;

            if (delta > 0)
            {
                double curM = AutoUpdateMgr.Instance.CurDownM;
                curM += ((double)delta) / ((double)1024 * 1024);
                AutoUpdateMgr.Instance.CurDownM = curM;
            }
            if (fileStream.ReadBytes >= fileStream.MaxReadBytes)
            {
                item.isDone = true;
            }
            m_Items[m_Curr] = item;
            AutoUpdateMgr.Instance.DownloadUpdateToUpdateTxt(item);

            DoUpdateDownProcess();
        }
 public void SetNull()
 {
     http = null;
     item = new AutoUpdateCfgItem();
 }
Beispiel #20
0
        // 有改变返回true
        public bool UpdateToRemoveFiles(ResListFile.ResDiffInfo[] newDiffInfos)
        {
            if (newDiffInfos == null)
            {
                return(false);
            }

            if (newDiffInfos.Length <= 0)
            {
                RemoveAllDowningFiles();
                Clear();
                return(true);
            }

            bool             ret     = false;
            HashSet <string> hashSet = new HashSet <string>();

            for (int i = 0; i < newDiffInfos.Length; ++i)
            {
                string contentMd5 = newDiffInfos[i].fileContentMd5;
                if (string.IsNullOrEmpty(contentMd5))
                {
                    continue;
                }

                hashSet.Add(contentMd5);
                if (!m_Dict.ContainsKey(contentMd5))
                {
                    AutoUpdateCfgItem item = new AutoUpdateCfgItem();
                    item.fileContentMd5 = contentMd5;
                    item.isDone         = false;
                    item.readBytes      = 0;
                    m_Dict.Add(contentMd5, item);
                    ret = true;
                }
            }

            List <string> delMd5List = new List <string>();
            string        writePath  = AutoUpdateMgr.Instance.WritePath;

            if (!string.IsNullOrEmpty(writePath))
            {
                Dictionary <string, AutoUpdateCfgItem> .Enumerator iter = m_Dict.GetEnumerator();
                while (iter.MoveNext())
                {
                    string contentMd5 = iter.Current.Value.fileContentMd5;
                    if (hashSet.Contains(contentMd5))
                    {
                        continue;
                    }

                    delMd5List.Add(contentMd5);
                }
                iter.Dispose();
            }

            if (delMd5List.Count > 0)
            {
                ret = true;
                for (int i = 0; i < delMd5List.Count; ++i)
                {
                    string contentMd5 = delMd5List[i];
                    m_Dict.Remove(contentMd5);
                    string fileName = string.Format("{0}/{1}", writePath, contentMd5);
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }
                }
            }

            return(ret);
        }
Beispiel #21
0
        public void Load(string str)
        {
            Clear();
            if (string.IsNullOrEmpty(str))
            {
                return;
            }
            char[] splits = new char[1];
            splits[0] = '\n';
            string[] lines = str.Split(splits, StringSplitOptions.RemoveEmptyEntries);
            if (lines == null || lines.Length <= 0)
            {
                return;
            }
            for (int i = 0; i < lines.Length; ++i)
            {
                string line = lines[i].Trim();
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }
                int idx = line.IndexOf('=');
                if (idx < 0)
                {
                    continue;
                }
                string name = line.Substring(0, idx).Trim();
                if (string.IsNullOrEmpty(name))
                {
                    continue;
                }
                string value  = line.Substring(idx + 1).Trim();
                int    subIdx = value.IndexOf(';');
                long   readBytes;
                bool   isDone;
                if (subIdx < 0)
                {
                    if (!long.TryParse(value, out readBytes))
                    {
                        readBytes = 0;
                    }
                    isDone = false;
                }
                else
                {
                    string rv = value.Substring(0, subIdx).Trim();
                    if (!long.TryParse(rv, out readBytes))
                    {
                        readBytes = 0;
                    }
                    string bv = value.Substring(subIdx + 1).Trim();
                    if (!bool.TryParse(bv, out isDone))
                    {
                        isDone = false;
                    }
                }

                AutoUpdateCfgItem item = new AutoUpdateCfgItem();
                item.fileContentMd5 = name;
                item.readBytes      = readBytes;
                item.isDone         = isDone;

                AddOrSet(item);
            }
        }