Ejemplo n.º 1
0
    void    SaveFile(byte[] data, BundleItem item)
    {
        string     localPath = FixedPath(item.bundlePath);
        FileStream file      = new FileStream(localPath, FileMode.Create);

        //file.Write(data,0,data.Length);
        if (item.isCompress)
        {
            SD.Decompress(new MemoryStream(data), file);
        }
        else
        {
            file.Write(data, 0, data.Length);
        }
        file.Close();
    }
Ejemplo n.º 2
0
    IEnumerator     UpdateAllBundle()
    {
        //if(!updating)
        {
            string xml_version = "";
            if (version.Length == 0)
            {
                string svnVersion = AppVersion()["versionName"].Split('.')[3];

                if (Application.platform == RuntimePlatform.Android)
                {
                    version = "0/";
                }
                else if (Application.platform == RuntimePlatform.IPhonePlayer)
                {
                    version = "10/";
                }
                else if (Application.platform == RuntimePlatform.WP8Player)
                {
                    version = "20/";
                }
                else if (Application.platform == RuntimePlatform.WindowsEditor)
                {
#if UNITY_ANDROID
                    version = "0/";
#endif

#if UNITY_IPHONE
                    version = "10/";
#endif
                }

                version    += svnVersion + "/";
                xml_version = svnVersion;
            }

            cdnIndex = 0;


            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            string url     = GetServer() + "bundleinfo.xml" + xml_version;
            byte[] xmlData = null;
            if (InternalNetDebug)
            {
                xmlData = File.ReadAllBytes(url);
            }
            else
            {
                string bundleInfo = "";
                while (true)
                {
                    long[] velocity = new long[cdn.Length];
                    for (int i = 0; i < cdn.Length; i++)
                    {
                        url = GetServer() + "bundleinfo.xml" + xml_version;
                        Debug.Log(url);
                        System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                        watch.Start();
                        wwwPercent = new WWW(url);
                        yield return(wwwPercent);

                        if (wwwPercent.error != null)
                        {
                            velocity[i] = -1;
                            Debug.Log(wwwPercent.error);
                        }
                        else
                        {
                            bundleInfo = wwwPercent.text;
                            watch.Stop();
                            velocity[i] = watch.ElapsedTicks;
                        }
                        ChangeCDN();
                    }
                    bool bValid = false;
                    long last   = long.MaxValue;
                    for (int i = 0; i < cdn.Length; i++)
                    {
                        if (velocity[i] != -1 && velocity[i] < last)
                        {
                            last     = velocity[i];
                            cdnIndex = i;
                            bValid   = true;
                        }
                    }
                    if (!bValid)
                    {
                        sdUICharacter.Instance.ShowLoginMsg("Can't Get Resource Version Information,Retry in 5 Second Later");
                        yield return(new WaitForSeconds(5.0f));
                    }
                    else
                    {
                        sdUICharacter.Instance.HideLoginMsg();
                        break;
                    }
                }

                Debug.Log("parse server bundleinfo.xml ");

                xmlData    = wwwPercent.bytes;
                wwwPercent = null;
            }

            byte[] buffer = new byte[1024 * 1024];

            MemoryStream dst = new MemoryStream(buffer, true);
            MemoryStream src = new MemoryStream(xmlData);
            SD.Decompress(src, dst);

            string csvContent = Encoding.ASCII.GetString(buffer, 0, (int)dst.Position);
            //Debug.Log(csvContent);

            string[] lines = csvContent.Split('\n');
            ParseBundleCSV(lines, ref bundles, bundleTable);


            Debug.Log("load local bundleinfo.xml");
            //加载本地xml..
            //System.Xml.XmlDocument docLocal =new System.Xml.XmlDocument();
            Hashtable downloadBundle = new Hashtable();
            try
            {
                if (File.Exists(LocalPath + "bundleinfo.txt"))
                {
                    //docLocal.Load(local+"bundleinfo.xml");
                    StreamReader r = File.OpenText(LocalPath + "bundleinfo.txt");
                    while (!r.EndOfStream)
                    {
                        string line = r.ReadLine();
                        if (line.Length > 0)
                        {
                            string[] strElement = line.Split(',');
                            downloadBundle[strElement[0]] = uint.Parse(strElement[1]);
                        }
                        //localContent	 = docLocal.SelectSingleNode("Bundles");
                    }
                    r.Close();
                }
            }catch (System.Exception e)
            {
                Debug.Log(e.Message + "load xml failed!");
                //root1=null;
            }

            BundleGlobalItem[] updateBundles = null;
            strVersionFile = "";
            if (downloadBundle.Count > 0)
            {
                Debug.Log("delete unused bundle");
                //删除上个版本中(不在这个版本中)的文件..
                //BundleItem[] oldBundle	=	null;
                //Hashtable oldTable		=	new Hashtable();
                //ParseXmlBundle(root1,ref oldBundle,oldTable);



                Debug.Log("Last Bundle Count = " + downloadBundle.Count);
                //Debug.Log("Current Bundle Count = " + BundleIndex.Count);
                foreach (DictionaryEntry de in downloadBundle)
                {
                    string name = de.Key  as string;
                    uint   ver  = (uint)de.Value;
                    //Debug.Log("Compare Bundle Index ["+item.bundlePath+"]");
                    if (!bundleTable.ContainsKey(name))
                    {
                        string strLocalPath = FixedPath(name);
                        Debug.Log("delete bundle " + name + ver);
                        File.Delete(strLocalPath);
                        if (File.Exists(strLocalPath))
                        {
                            Debug.Log("delete bundle Failed! " + strLocalPath);
                        }
                    }
                    else
                    {
                        //Debug.Log("bundle is exist! "+item.bundlePath + item.version);
                    }
                }


                Debug.Log("find bundle need for update");
                List <BundleGlobalItem> lstDownload = new List <BundleGlobalItem>();
                //判断是否需要更新文件..
                for (int i = 0; i < bundles.Length; i++)
                {
                    BundleGlobalItem item = bundles[i];
                    if (item == null)
                    {
                        continue;
                    }
                    if (!downloadBundle.ContainsKey(item.itemInfo.bundlePath))
                    {
                        lstDownload.Add(item);
                    }
                    else
                    {
                        uint ver = (uint)downloadBundle[item.itemInfo.bundlePath];
                        if (ver != item.itemInfo.version)
                        {
                            lstDownload.Add(item);
                            continue;
                        }

                        if (!File.Exists(FixedPath(item.itemInfo.bundlePath)))
                        {
                            lstDownload.Add(item);
                            continue;
                        }
                        strVersionFile += item.itemInfo.bundlePath + "," + item.itemInfo.version + ",\r\n";
                    }
                }
                SaveVersion(strVersionFile);

                Debug.Log("update some bundle = " + lstDownload.Count);

                updateBundles = lstDownload.ToArray();
            }
            else
            {
                Debug.Log("update all bundle" + bundles.Length);
                updateBundles = bundles;
            }
            if (updateBundles != null)
            {
                Thread UnCompressThread = new Thread(new ThreadStart(UnCompressMain));
                UnCompressThread.Start();


                for (int i = 0; i < updateBundles.Length;)
                {
                    OnDownload(i, updateBundles.Length);
                    BundleGlobalItem item = updateBundles[i];
                    Debug.Log("download bundle " + item.itemInfo.bundlePath);
                    //guiText.text	=	i+"/"+BundleInfoArray.Length;
                    //Download(BundleInfoArray[i]);

                    string bundleurl  = GetServer() + item.itemInfo.bundlePath.Replace("$", "__") + item.itemInfo.version.ToString();
                    byte[] bundleData = null;
                    if (InternalNetDebug)
                    {
                        yield return(0);

                        if (!File.Exists(bundleurl))
                        {
                            Debug.Log("Bundle Don't Exist! " + bundleurl);
                            i++;
                            continue;
                        }

                        bundleData = File.ReadAllBytes(bundleurl);
                    }
                    else
                    {
                        wwwPercent = new WWW(bundleurl);
                        yield return(wwwPercent);

                        if (wwwPercent.error != null)
                        {
                            Debug.Log(wwwPercent.error);
                            if (wwwPercent.error.Contains("FileNotFoundException") ||
                                wwwPercent.error.Contains("404: not found") ||
                                wwwPercent.error.Contains("404 Not Found"))
                            {
                                sdUICharacter.Instance.HideLoginMsg();
                                i++;
                            }
                            else
                            {
                                sdUICharacter.Instance.ShowLoginMsg(wwwPercent.error);
                                ChangeCDN();
                            }
                            wwwPercent = null;
                            continue;
                        }
                        else
                        {
                            //校验压缩之后的CRC是否匹配 不匹配则更换CDN 重新下载..
                            BundleItem iteminfo = item.itemInfo;
                            byte[]     data     = wwwPercent.bytes;
                            uint       comp_crc = SevenZip.CRC.CalculateDigest(data, 0, (uint)data.Length);
                            if (comp_crc != iteminfo.compress_crc)
                            {
                                Debug.Log(iteminfo.bundlePath + " compress_crc doesn't match " + comp_crc + " " + iteminfo.compress_crc);
                                sdUICharacter.Instance.ShowLoginMsg("crc doesn't match,download again!");
                                ChangeCDN();
                                continue;
                            }
                            else
                            {
                                sdUICharacter.Instance.HideLoginMsg();
                            }
                        }
                        bundleData = wwwPercent.bytes;
                    }

                    SaveFileAsync(bundleData, item);

                    wwwPercent = null;

                    i++;
                }
            }

            ExitUnCompress = true;
            while (true)
            {
                if (ExitUnCompress)
                {
                    yield return(0);
                }
                else
                {
                    break;
                }
            }

            Debug.Log("update finished!");

            OnDownloadFinished();
        }
        StartCoroutine(updateLoad());
    }