示例#1
0
        public void Update()
        {
            if (isDone)
            {
                return;
            }

            switch (state)
            {
            case State.HeadRequest:
                if (request.error != null)
                {
                    error = request.error;
                }
                if (request.isDone)
                {
                    maxlen = long.Parse(request.GetResponseHeader("Content-Length"));
                    request.Dispose();
                    request = null;
                    var dir = Path.GetDirectoryName(savePath);
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    fs  = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
                    len = fs.Length;
                    var emptyVersion    = string.IsNullOrEmpty(version);
                    var oldVersion      = Versions.Get(savePath);
                    var emptyOldVersion = string.IsNullOrEmpty(oldVersion);
                    if (emptyVersion || emptyOldVersion || !oldVersion.Equals(version))
                    {
                        Versions.Set(savePath, version);
                        len = 0;
                    }
                    if (len < maxlen)
                    {
                        fs.Seek(len, SeekOrigin.Begin);
                        request = UnityWebRequest.Get(url);
                        request.SetRequestHeader("Range", "bytes=" + len + "-");
                        request.SendWebRequest();
                        state = State.ContentRequest;
                        index = 0;
                    }
                    else
                    {
                        state = State.FinishRequest;
                    }
                }

                break;

            case State.ContentRequest:
                if (request.error != null)
                {
                    error = request.error;
                }

                if (!request.isDone)
                {
                    WriteBuffer();
                }
                else
                {
                    WriteBuffer();

                    if (fs != null)
                    {
                        fs.Close();
                        fs.Dispose();
                    }

                    request.Dispose();
                    state = State.FinishRequest;
                }

                break;

            case State.FinishRequest:
                if (completed != null)
                {
                    completed.Invoke();
                }

                isDone = true;
                state  = State.Completed;
                break;
            }
        }