Beispiel #1
0
        /// <summary>
        /// Performs a coroutine Http GET request and returns a byte array with the result via both a callback and the current value of the IEnumerator object.
        /// </summary>
        public static IEnumerator GetAsync(string url, Updater_File_Type_Confirm confirm = null, Updater_File_Download_Progress prog_callback = null, Updater_Get_Result callback = null)
        {
            if (remote_file_cache.ContainsKey(url))
            {
                yield return(remote_file_cache[url]);

                yield break;
            }

            WebResponse resp   = null;
            Stream      stream = null;

            HttpWebRequest webRequest = CreateRequest(url);

            WebAsync    webAsync = new WebAsync();
            IEnumerator e        = webAsync.GetResponse(webRequest);

            if (e == null)
            {
                SLog.Info("Updater_Base.Get() Enumerator is NULL!");
                yield return(null);

                yield break;
            }

            while (e.MoveNext())
            {
                yield return(null);
            }                                          // wait for response to arrive
            while (!webAsync.isResponseCompleted)
            {
                yield return(null);                                 // double check for clarity & safety
            }
            RequestState result = webAsync.requestState;

            resp = result.webResponse;

            if (confirm != null)
            {
                if (confirm(resp.ContentType) == false)
                {
                    yield break;//exit routine
                }
            }

            stream = resp.GetResponseStream();
            int total = (int)resp.ContentLength;

            byte[] buf = new byte[total];

            int read   = 0;     //how many bytes we have read so far (offset within the stream)
            int remain = total; //how many bytes are left to read
            int r      = 0;

            while (remain > 0)
            {
                r       = stream.Read(buf, read, Math.Min(remain, CHUNK_SIZE));
                read   += r;
                remain -= r;
                if (prog_callback != null)
                {
                    try
                    {
                        prog_callback(read, total);
                    }
                    catch (Exception ex)
                    {
                        SLog.Error(ex);
                    }
                }
                yield return(null);// yield execution until next frame
            }

            if (!remote_file_cache.ContainsKey(url))
            {
                remote_file_cache.Add(url, buf);
            }
            else
            {
                remote_file_cache[url] = buf;
            }

            callback?.Invoke(buf);
            yield return(buf);

            yield break;
        }
Beispiel #2
0
        // Remember: when this function is used by plugins they will pass their given updater_method URL for 'remote_file'
        // In the case of things like the Git updater this is fine as that url will BE a link to the most current version of the plugin DLL
        // However in the case of the JSON updater that url will instead be a link to the JSON file containing the HASH and DOWNLOAD URL for said plugin.
        // So for the JSON updater this method needs to be overriden and made to download that JSON info and treat the DOWNLOAD url contained therein as if IT was passed as the 'remote_file'
        public virtual IEnumerator DownloadAsync(string remote_file, string local_file, Updater_File_Type_Confirm confirm = null, Updater_File_Download_Progress prog_callback = null, Updater_File_Download_Completed download_completed = null)
        {
            SLog.Debug("Downloading: {0}", remote_file);
            if (local_file == null)
            {
                local_file = String.Format("{0}\\{1}", UnityEngine.Application.dataPath, Path.GetFileName(remote_file));
            }

            WebResponse resp   = null;
            Stream      stream = null;

            HttpWebRequest webRequest = CreateRequest(remote_file);

            WebAsync    webAsync = new WebAsync();
            IEnumerator e        = webAsync.GetResponse(webRequest);

            while (e.MoveNext())
            {
                yield return(e.Current);
            }                                               // wait for response to arrive
            while (!webAsync.isResponseCompleted)
            {
                yield return(null);                                 // double check for clarity & safety
            }
            RequestState result = webAsync.requestState;

            resp = result.webResponse;

            if (confirm != null)
            {
                if (confirm(resp.ContentType) == false)
                {
                    yield break;//exit routine
                }
            }

            stream = resp.GetResponseStream();
            int total = (int)resp.ContentLength;

            byte[] buf = new byte[total];

            int read   = 0;     //how many bytes we have read so far (offset within the stream)
            int remain = total; //how many bytes are left to read
            int r      = 0;

            while (remain > 0)
            {
                r       = stream.Read(buf, read, Math.Min(remain, CHUNK_SIZE));
                read   += r;
                remain -= r;
                if (prog_callback != null)
                {
                    try
                    {
                        prog_callback(read, total);
                    }
                    catch (Exception ex)
                    {
                        SLog.Error(ex);
                    }
                }
                yield return(0);// yield execution until next frame
            }

            // It's good practice when overwriting files to write the new version to a temporary location and then copy it overtop of the original.
            string temp_file = String.Format("{0}.temp", local_file);

            File.WriteAllBytes(temp_file, buf);
            File.Copy(temp_file, local_file, true);
            File.Delete(temp_file);// delete the now unneeded .temp file

            download_completed?.Invoke(local_file);
            yield break;//exit routine
        }