Beispiel #1
0
        private void ABProcess()
        {
            LogMgr.UnityLog("ABProcess # " + mIdx + " Started ..");
            while (!isExit)
            {
                int               count     = 0;
                DownloadedItem    item      = null;
                string            mSaveFile = null;
                AssetBundleConfig mConfig   = null;

                lock (HttpDownload.shareObject)
                {
                    count = HttpDownload.downloadedQueue.Count;
                    if (count > 0)
                    {
                        item = HttpDownload.downloadedQueue[0];
                        HttpDownload.downloadedQueue.RemoveAt(0);
                    }
                }
                LogMgr.UnityLog("ABProcessor mid: " + mIdx);
                if (null != item)
                {
                    mSaveFile = item.saveFile;
                    mConfig   = item.config;

                    string outFilePath = mSaveFile.Replace("_Compress", "");
                    if (File.Exists(outFilePath))
                    {
                        File.Delete(outFilePath);
                    }
                    LZMATool.DecompressFileLZMA(mSaveFile, outFilePath);
                    if (File.Exists(mSaveFile))
                    {
                        File.Delete(mSaveFile);
                    }

                    if (null != mConfig)
                    {
                        List <AssetBundleConfig> bundleConfigGroup = new List <AssetBundleConfig>();
                        bundleConfigGroup.Add(mConfig);

                        XMLTool.SaveABConfig(HttpDownloadTask.configPath, bundleConfigGroup);
                        LogMgr.UnityLog("HttpDownLoad SaveABConfig: " + count + "," + mConfig.RelativePath
                                        + "," + mConfig.ABName + "," + mConfig.Build + "," + Thread.CurrentThread.ManagedThreadId + ",@" + mIdx
                                        );
                    }
                }
                else
                {
                    LogMgr.UnityLog("ABProcessor continue " + mIdx);
                    Thread.Sleep(10);
                }
            }
        }
Beispiel #2
0
        private void DownLoadFile()
        {
            bool bExit    = false;
            bool bTask    = false;
            bool bPause   = false;
            bool bProcess = false;

            lock (mLockObject)
            {
                bExit = mIsExit;
            }
            while (!bExit)
            {
                lock (mLockObject)
                {
                    bTask    = mHasTask;
                    bPause   = mIsPause;
                    bExit    = mIsExit;
                    bProcess = mIsProcess;
                }

                if (!bTask || bPause || bProcess)
                {
                    //LogMgr.UnityLog("DownLoadFile continue: ");
                    Thread.Sleep(10);
                    continue;
                }

                try
                {
                    lock (mLockObject)
                    {
                        bProcess = true;
                    }

                    string localFileDir = mSaveFile.Remove(mSaveFile.LastIndexOf("/"));
                    if (!Directory.Exists(localFileDir))
                    {
                        Directory.CreateDirectory(localFileDir);
                    }

                    HttpWebRequest request    = WebRequest.Create(mUrl + BundleCommon.GetRandomUrl()) as HttpWebRequest;
                    WebResponse    response   = request.GetResponse();
                    Stream         stream     = response.GetResponseStream();
                    byte[]         buffer     = new byte[mBufferSize];
                    FileStream     fileStream = new FileStream(mSaveFile, FileMode.OpenOrCreate);

                    int readSize = stream.Read(buffer, 0, mBufferSize);
                    while (readSize > 0 && !bPause && !bExit)
                    {
                        fileStream.Write(buffer, 0, readSize);
                        mDownloadedSize += readSize;

                        readSize = stream.Read(buffer, 0, mBufferSize);

                        lock (mLockObject)
                        {
                            bPause = mIsPause;
                            bExit  = mIsExit;
                        }
                    }

                    stream.Close();
                    fileStream.Close();
                    response.Close();

                    DownloadedItem item = new DownloadedItem();
                    item.saveFile = mSaveFile;
                    item.config   = mConfig;
                    lock (shareObject)
                    {
                        downloadedQueue.Add(item);
                    }

                    if (!bPause && !bExit)
                    {
                        string outFilePath = mSaveFile.Replace("_Compress", "");
                        if (File.Exists(outFilePath))
                        {
                            File.Delete(outFilePath);
                        }
                        LZMATool.DecompressFileLZMA(mSaveFile, outFilePath);
                        if (File.Exists(mSaveFile))
                        {
                            File.Delete(mSaveFile);
                        }

                        if (null != mConfig)
                        {
                            List <AssetBundleConfig> bundleConfigGroup = new List <AssetBundleConfig>();
                            bundleConfigGroup.Add(mConfig);
                            lock (mLockObject)
                            {
                                XMLTool.SaveABConfig(HttpDownloadTask.configPath, bundleConfigGroup);
                                LogMgr.UnityLog("HttpDownLoad SaveABConfig: " + mConfig.RelativePath
                                                + "," + mConfig.ABName + "," + mConfig.Build
                                                );
                            }
                        }

                        if (null != mFinishCallback)
                        {
                            mFinishCallback(true);
                        }
                    }
                }
                catch (Exception e)
                {
                    LogMgr.UnityError("download file error:" + e.ToString());
                    if (null != mFinishCallback && !bPause && !bExit)
                    {
                        mFinishCallback(false);
                    }
                }

                lock (mLockObject)
                {
                    bExit = mIsExit;

                    mIsProcess = false;
                    mHasTask   = false;
                }
            }
        }