Exemplo n.º 1
0
    IEnumerator RestoreUpdate()
    {
        if (Application.internetReachability == NetworkReachability.NotReachable)
        {
            Log.LogInfo("connect time out");
            MainLoader.ChangeStep(LoadStep.NotNeedUpdate);
            yield break;
        }
        else if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
        {
            //3G Tip
        }
        //Restore DownLoad From Progress.xml
        System.Net.Sockets.Socket         sClient     = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.IP);
        System.Net.EndPoint               server      = new System.Net.IPEndPoint(Dns.GetHostAddresses(GameData.Domain)[0], System.Convert.ToInt32(strPort));
        System.Threading.ManualResetEvent connectDone = new System.Threading.ManualResetEvent(false);
        try
        {
            connectDone.Reset();
            sClient.BeginConnect(server, delegate(IAsyncResult ar) {
                try
                {
                    System.Net.Sockets.Socket client = (System.Net.Sockets.Socket)ar.AsyncState;
                    client.EndConnect(ar);
                }
                catch (System.Exception e)
                {
                    Log.LogInfo(e.Message + "|" + e.StackTrace);
                }
                finally
                {
                    connectDone.Set();
                }
            }
                                 , sClient);
            //timeout
            if (!connectDone.WaitOne(2000))
            {
                Log.LogInfo("connect time out");
                MainLoader.ChangeStep(LoadStep.CannotConnect);
                yield break;
            }
            else
            {
                if (!sClient.Connected)
                {
                    Log.LogInfo("connect disabled by server");
                    MainLoader.ChangeStep(LoadStep.CannotConnect);
                    yield break;
                }
            }
        }
        catch
        {
            sClient.Close();
            MainLoader.ChangeStep(LoadStep.CannotConnect);
            yield break;
        }
        finally
        {
            connectDone.Close();
        }
        //Log.LogInfo("download:" + string.Format(strVFile, strHost, strPort, strProjectUrl, strPlatform, strVFileName));
        using (WWW vFile = new WWW(string.Format(strVFile, strHost, strPort, strProjectUrl, strPlatform, strVFileName)))
        {
            //Log.LogInfo("error:" + vFile.error);
            yield return(vFile);

            if (vFile.error != null && vFile.error.Length != 0)
            {
                //Log.LogInfo("error " + vFile.error);
                vFile.Dispose();
                //not have new version file
                //can continue game
                MainLoader.ChangeStep(LoadStep.NotNeedUpdate);
                yield break;
            }
            if (vFile.bytes != null && vFile.bytes.Length != 0)
            {
                File.WriteAllBytes(strUpdatePath + "/" + "v.zip", vFile.bytes);
                DeCompressFile(strUpdatePath + "/" + "v.zip", strUpdatePath + "/" + "v.xml");
                vFile.Dispose();
            }
            else
            {
                MainLoader.ChangeStep(LoadStep.NotNeedUpdate);
                yield break;
            }
        }

        XmlDocument xmlVer = new XmlDocument();

        xmlVer.Load(strUpdatePath + "/" + "v.xml");
        XmlElement ServerVer = xmlVer.DocumentElement;

        if (ServerVer != null)
        {
            string strServer = ServerVer.GetAttribute("ServerV");
            UpdateClient = HttpManager.AllocClient(string.Format(strDirectoryBase, strHost, strPort, strProjectUrl, strPlatform, strServer, ""));
            //if (strServer != null && GameData.Version().CompareTo(strServer) == -1)
            //{
            //	strServerVer = strServer;
            //	foreach (XmlElement item in ServerVer)
            //	{
            //		string strClientV = item.GetAttribute("ClientV");
            //		if (strClientV == GameData.Version())
            //		{
            //			strUpdateFile = item.GetAttribute("File");
            //			break;
            //		}
            //	}

            //	if (strUpdateFile != null && strUpdateFile.Length != 0)
            //		StartCoroutine("DownloadNecessaryData");
            //}
            //else
            //{
            //	Log.LogInfo("not need update");
            //             MainLoader.ChangeStep(LoadStep.NotNeedUpdate);
            //}
        }
    }