Exemplo n.º 1
0
        /// <summary>
        /// Forces the plugin to download the latest version of itself.
        /// (Coroutine)
        /// </summary>
        /// <param name="prog">uiProgressBar instance which this function will use to display the current update progress.</param>
        /// <returns></returns>
        public IEnumerator force_download(uiProgressBar prog, Updater_File_Download_Completed download_complete_cb)
        {
            if (this.data.UPDATE_METHOD == null)
            {
                throw new ArgumentNullException(String.Format("{0} Plugin has no UPDATE_METHOD specified!", this));
            }

            IEnumerator iter = this.Updater.DownloadAsync(this.data.UPDATE_METHOD.URL, this.FilePath, null,
                                                          (float current, float total) =>
            {   //Download progress
                float f = (float)current / (float)total;

                if (prog != null)
                {
                    float p    = (float)current / (float)total;
                    prog.Value = p;
                }
            },
                                                          (string file) =>
            {
                this.is_update_available = false;
                if (download_complete_cb != null)
                {
                    download_complete_cb(file);
                }
            });

            // Run the download coroutine within this coroutine without starting a seperate instance.
            while (iter.MoveNext())
            {
                yield return(null);
            }

            yield break;// Honestly probably not needed but I like to be safe because I still don't fully know the innerworkings of unity's coroutine system.
        }
Exemplo n.º 2
0
        /// <summary>
        /// Starts downloading the latest version of this plugin.
        /// (Coroutine)
        /// </summary>
        /// <param name="prog">uiProgressBar instance which this function will use to display the current update progress.</param>
        /// <returns></returns>
        public IEnumerator download_update(uiProgressBar prog, Updater_File_Download_Completed download_complete_cb)
        {
            if (!this.check_for_updates())
            {
                SLog.Info("{0} Plugin.download_update():  Already up to date!", this);
                yield break;
            }

            yield return(this.force_download(prog, download_complete_cb));
        }
Exemplo n.º 3
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
        }