private void myButton3_Click(object sender, EventArgs e)
        {
            formProgressReporter = CreateIProgressReporterDialogue("Converting Data");

            formProgressReporter.DoWork += ConvertData;
            formProgressReporter.UpdateProgressAndStatus(-1, "...");

            formProgressReporter.RunBackgroundOperationAsync();

            formProgressReporter.Dispose();
        }
Пример #2
0
        void doUI(string inputfn, string outputfn, bool showui = true)
        {
            this.inputfn  = inputfn;
            this.outputfn = outputfn;

            prd = new ProgressReporterDialogue();

            prd.DoWork += prd_DoWork;

            prd.UpdateProgressAndStatus(-1, Strings.Converting_bin_to_log);

            this.convertstatus += BinaryLog_convertstatus;

            ThemeManager.ApplyThemeTo(prd);

            prd.RunBackgroundOperationAsync();

            prd.Dispose();
        }
Пример #3
0
        public void Open(bool skipconnectedcheck = false)
        {
            if (BaseStream.IsOpen && !skipconnectedcheck)
            {
                return;
            }

            //MAVlist.Clear();

            frmProgressReporter = new ProgressReporterDialogue
            {
                StartPosition = FormStartPosition.CenterScreen,
                Text          = "Connecting..."
            };

            frmProgressReporter.DoWork += FrmProgressReporterDoWorkNOParams;
            frmProgressReporter.UpdateProgressAndStatus(-1, "Connecting...");
            //ThemeManager.ApplyThemeTo(frmProgressReporter);

            frmProgressReporter.RunBackgroundOperationAsync();

            frmProgressReporter.Dispose();
        }
Пример #4
0
 /// <summary>
 ///     for updating fw list
 /// </summary>
 /// <param name="progress"></param>
 /// <param name="status"></param>
 private void fw_ProgressPDR(int progress, string status)
 {
     pdr.UpdateProgressAndStatus(progress, status);
 }
Пример #5
0
        static void GetNewFile(IProgressReporterDialogue frmProgressReporter, string baseurl, string subdir, string file)
        {
            // create dest dir
            string dir = Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + subdir;

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            // get dest path
            string path = Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + subdir +
                          file;

            Exception fail    = null;
            int       attempt = 0;

            // attempt to get file
            while (attempt < 2)
            {
                // check if user canceled
                if (frmProgressReporter.doWorkArgs.CancelRequested)
                {
                    frmProgressReporter.doWorkArgs.CancelAcknowledged = true;
                    throw new Exception("Cancel");
                }

                try
                {
                    string url = baseurl + file + "?" + new Random().Next();
                    // Create a request using a URL that can receive a post.
                    WebRequest request = WebRequest.Create(url);
                    if (!String.IsNullOrEmpty(Settings.Instance.UserAgent))
                    {
                        ((HttpWebRequest)request).UserAgent = Settings.Instance.UserAgent;
                    }
                    log.Info("GetNewFile " + url);
                    // Set the Method property of the request to GET.
                    request.Method = "GET";
                    // Allow compressed content
                    ((HttpWebRequest)request).AutomaticDecompression = DecompressionMethods.GZip |
                                                                       DecompressionMethods.Deflate;
                    // tell server we allow compress content
                    request.Headers.Add("Accept-Encoding", "gzip,deflate");
                    // Get the response.
                    using (WebResponse response = request.GetResponse())
                    {
                        // Display the status.
                        log.Info(((HttpWebResponse)response).StatusDescription);
                        // Get the stream containing content returned by the server.
                        Stream dataStream = response.GetResponseStream();

                        // from head
                        long bytes = response.ContentLength;

                        long contlen = bytes;

                        byte[] buf1 = new byte[4096];

                        // if the file doesnt exist. just save it inplace
                        string fn = path + ".new";

                        using (FileStream fs = new FileStream(fn, FileMode.Create))
                        {
                            DateTime dt = DateTime.Now;

                            log.Debug("ContentLength: " + file + " " + bytes);

                            while (dataStream.CanRead)
                            {
                                try
                                {
                                    if (dt.Second != DateTime.Now.Second)
                                    {
                                        if (frmProgressReporter != null)
                                        {
                                            frmProgressReporter.UpdateProgressAndStatus(
                                                (int)(((double)(contlen - bytes) / (double)contlen) * 100),
                                                Strings.Getting + file + ": " +
                                                (((double)(contlen - bytes) / (double)contlen) * 100)
                                                .ToString("0.0") +
                                                "%"); //+ Math.Abs(bytes) + " bytes");
                                        }
                                        dt = DateTime.Now;
                                    }
                                }
                                catch
                                {
                                }

                                int len = dataStream.Read(buf1, 0, buf1.Length);
                                if (len == 0)
                                {
                                    log.Debug("GetNewFile: 0 byte read " + file);
                                    break;
                                }
                                bytes -= len;
                                fs.Write(buf1, 0, len);
                            }

                            log.Info("GetNewFile: " + file + " Done with length: " + fs.Length);
                            fs.Flush(true);
                            fs.Dispose();
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                    fail = ex;
                    attempt++;
                    continue;
                }

                // break if we have no exception
                break;
            }

            if (attempt == 2)
            {
                throw fail;
            }
        }
Пример #6
0
        public static void updateCheckMain(IProgressReporterDialogue frmProgressReporter)
        {
            var t = Type.GetType("Mono.Runtime");

            MONO = (t != null);

            try
            {
                if (domaster)
                {
                    CheckMD5(frmProgressReporter,
                             ConfigurationManager.AppSettings["MasterUpdateLocationMD5"].ToString(),
                             ConfigurationManager.AppSettings["MasterUpdateLocationZip"]);
                }
                else if (dobeta)
                {
                    CheckMD5(frmProgressReporter,
                             ConfigurationManager.AppSettings["BetaUpdateLocationMD5"].ToString(),
                             ConfigurationManager.AppSettings["BetaUpdateLocationZip"]);
                }
                else
                {
                    CheckMD5(frmProgressReporter,
                             ConfigurationManager.AppSettings["UpdateLocationMD5"].ToString(),
                             ConfigurationManager.AppSettings["UpdateLocation"]);
                }

                var    process = new Process();
                string exePath = Path.GetDirectoryName(Application.ExecutablePath);
                if (MONO)
                {
                    process.StartInfo.FileName  = "mono";
                    process.StartInfo.Arguments = " \"" + exePath + Path.DirectorySeparatorChar + "Updater.exe\"" +
                                                  "  \"" + Application.ExecutablePath + "\"";
                }
                else
                {
                    process.StartInfo.FileName  = exePath + Path.DirectorySeparatorChar + "Updater.exe";
                    process.StartInfo.Arguments = Application.ExecutablePath;
                }

                try
                {
                    foreach (string newupdater in Directory.GetFiles(exePath, "Updater.exe*.new"))
                    {
                        File.Copy(newupdater, newupdater.Remove(newupdater.Length - 4), true);
                        File.Delete(newupdater);
                    }
                }
                catch (Exception ex)
                {
                    log.Error("Exception during update", ex);
                }
                if (frmProgressReporter != null)
                {
                    frmProgressReporter.UpdateProgressAndStatus(-1, "Starting Updater");
                }
                log.Info("Starting new process: " + process.StartInfo.FileName + " with " +
                         process.StartInfo.Arguments);
                process.Start();
                log.Info("Quitting existing process");

                if (frmProgressReporter != null)
                {
                    frmProgressReporter.BeginInvoke((Action) delegate { Application.Exit(); });
                }
            }
            catch (AggregateException ex)
            {
                log.Error("Update Failed", ex.InnerException);
                CustomMessageBox.Show("Update Failed " + ex.InnerException?.Message);
            }
            catch (Exception ex)
            {
                log.Error("Update Failed", ex);
                CustomMessageBox.Show("Update Failed " + ex.Message);
            }
        }
Пример #7
0
        static void CheckMD5(IProgressReporterDialogue frmProgressReporter, string md5url, string baseurl)
        {
            log.InfoFormat("get checksums {0} - base {1}", md5url, baseurl);

            string responseFromServer = "";

            responseFromServer = client.GetStringAsync(md5url).GetAwaiter().GetResult();

            File.WriteAllText(Settings.GetRunningDirectory() + "checksums.txt.new", responseFromServer);

            Regex regex = new Regex(@"([^\s]+)\s+[^/]+/(.*)", RegexOptions.IgnoreCase);

            if (regex.IsMatch(responseFromServer))
            {
                if (frmProgressReporter != null)
                {
                    frmProgressReporter.UpdateProgressAndStatus(-1, "Hashing Files");
                }

                // cleanup dll's with the same exe name
                var           dlls  = Directory.GetFiles(Settings.GetRunningDirectory(), "*.dll", SearchOption.AllDirectories);
                var           exes  = Directory.GetFiles(Settings.GetRunningDirectory(), "*.exe", SearchOption.AllDirectories);
                List <string> files = new List <string>();

                // hash everything
                MatchCollection matchs = regex.Matches(responseFromServer);
                for (int i = 0; i < matchs.Count; i++)
                {
                    string hash = matchs[i].Groups[1].Value.ToString();
                    string file = matchs[i].Groups[2].Value.ToString();

                    files.Add(file);
                }

                // cleanup unused dlls and exes
                dlls.ForEach(dll =>
                {
                    try
                    {
                        var result = files.Any(task => Path.Combine(Settings.GetRunningDirectory(), task).ToLower().Equals(dll.ToLower()));

                        if (result == false)
                        {
                            File.Delete(dll);
                        }
                    }
                    catch { }
                });

                exes.ForEach(exe =>
                {
                    try
                    {
                        var result = files.Any(task => Path.Combine(Settings.GetRunningDirectory(), task).ToLower().Equals(exe.ToLower()));

                        if (result == false)
                        {
                            File.Delete(exe);
                        }
                    }
                    catch { }
                });



                // background md5
                List <Tuple <string, string, Task <bool> > > tasklist = new List <Tuple <string, string, Task <bool> > >();

                for (int i = 0; i < matchs.Count; i++)
                {
                    string hash = matchs[i].Groups[1].Value.ToString();
                    string file = matchs[i].Groups[2].Value.ToString();

                    if (file.ToLower().EndsWith("files.html"))
                    {
                        continue;
                    }

                    Task <bool> ismatch = Task <bool> .Factory.StartNew(() => MD5File(file, hash));

                    tasklist.Add(new Tuple <string, string, Task <bool> >(file, hash, ismatch));
                }
                // get count and wait for all hashing to be done
                int count = tasklist.Count(a =>
                {
                    a.Item3.Wait();
                    return(!a.Item3.Result);
                });

                // parallel download
                ParallelOptions opt = new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 3
                };

                tasklist.Sort((a, b) =>
                {
                    if (a == null || b == null)
                    {
                        return(0);
                    }

                    if (a.Item1.ToLower().EndsWith(".exe") && b.Item1.ToLower().EndsWith(".exe"))
                    {
                        return(a.Item1.CompareTo(b.Item1));
                    }
                    if (a.Item1.ToLower().EndsWith(".exe"))
                    {
                        return(-1);
                    }
                    if (b.Item1.ToLower().EndsWith(".exe"))
                    {
                        return(1);
                    }

                    if (a.Item1.ToLower().EndsWith(".dll") && b.Item1.ToLower().EndsWith(".dll"))
                    {
                        return(a.Item1.CompareTo(b.Item1));
                    }
                    if (a.Item1.ToLower().EndsWith(".dll"))
                    {
                        return(-1);
                    }
                    if (b.Item1.ToLower().EndsWith(".dll"))
                    {
                        return(1);
                    }

                    return(a.Item1.CompareTo(b.Item1));
                });

                int done = 0;

                Parallel.ForEach(tasklist, opt, task =>
                                 //foreach (var task in tasklist)
                {
                    string file = task.Item1;
                    string hash = task.Item2;
                    // check if existing matchs hash
                    task.Item3.Wait();
                    bool match = task.Item3.Result;

                    if (!match)
                    {
                        done++;
                        log.Info("Newer File " + file);

                        if (frmProgressReporter != null && frmProgressReporter.doWorkArgs.CancelRequested)
                        {
                            frmProgressReporter.doWorkArgs.CancelAcknowledged = true;
                            throw new Exception("User Request");
                        }

                        // check is we have already downloaded and matchs hash
                        if (!MD5File(file + ".new", hash))
                        {
                            if (frmProgressReporter != null)
                            {
                                frmProgressReporter.UpdateProgressAndStatus((int)((done / (double)count) * 100),
                                                                            Strings.Getting + file + "\n" + done + " of " + count + " of total " +
                                                                            tasklist.Count);
                            }

                            string subdir = Path.GetDirectoryName(file) + Path.DirectorySeparatorChar;

                            subdir = subdir.Replace("" + Path.DirectorySeparatorChar + Path.DirectorySeparatorChar,
                                                    "" + Path.DirectorySeparatorChar);

                            if (baseurl.ToLower().Contains(".zip"))
                            {
                                GetNewFileZip(frmProgressReporter, baseurl, subdir,
                                              Path.GetFileName(file));
                            }
                            else
                            {
                                GetNewFile(frmProgressReporter, baseurl + subdir.Replace('\\', '/'), subdir,
                                           Path.GetFileName(file));
                            }

                            // check the new downloaded file matchs hash
                            if (!MD5File(file + ".new", hash))
                            {
                                throw new Exception("File downloaded does not match hash: " + file);
                            }
                        }
                        else
                        {
                            log.Info("already got new File " + file);
                        }
                    }
                    else
                    {
                        log.Info("Same File " + file);

                        if (frmProgressReporter != null)
                        {
                            frmProgressReporter.UpdateProgressAndStatus(-1, Strings.Checking + file);
                        }
                    }
                });
            }
        }
Пример #8
0
        static void CheckMD5(IProgressReporterDialogue frmProgressReporter, string md5url, string baseurl)
        {
            L10N.ReplaceMirrorUrl(ref baseurl);

            string responseFromServer = "";

            WebRequest request = WebRequest.Create(md5url);

            request.Timeout = 10000;
            // Set the Method property of the request to POST.
            request.Method = "GET";
            // Get the response.
            // Get the stream containing content returned by the server.
            // Open the stream using a StreamReader for easy access.
            using (WebResponse response = request.GetResponse())
                using (Stream dataStream = response.GetResponseStream())
                    using (StreamReader reader = new StreamReader(dataStream))
                    {
                        // Display the status.
                        log.Info(((HttpWebResponse)response).StatusDescription);
                        // Read the content.
                        responseFromServer = reader.ReadToEnd();
                    }

            Regex regex = new Regex(@"([^\s]+)\s+[^/]+/(.*)", RegexOptions.IgnoreCase);

            if (regex.IsMatch(responseFromServer))
            {
                // background md5
                List <Tuple <string, string, Task <bool> > > tasklist = new List <Tuple <string, string, Task <bool> > >();

                if (frmProgressReporter != null)
                {
                    frmProgressReporter.UpdateProgressAndStatus(-1, "Hashing Files");
                }

                // hash everything
                MatchCollection matchs = regex.Matches(responseFromServer);
                for (int i = 0; i < matchs.Count; i++)
                {
                    string hash = matchs[i].Groups[1].Value.ToString();
                    string file = matchs[i].Groups[2].Value.ToString();

                    Task <bool> ismatch = Task <bool> .Factory.StartNew(() => MD5File(file, hash));

                    tasklist.Add(new Tuple <string, string, Task <bool> >(file, hash, ismatch));
                }

                // get count and wait for all hashing to be done
                int count = tasklist.Count(a =>
                {
                    a.Item3.Wait();
                    return(!a.Item3.Result);
                });

                // parallel download
                ParallelOptions opt = new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 3
                };

                int done = 0;

                Parallel.ForEach(tasklist, opt, task =>
                                 //foreach (var task in tasklist)
                {
                    string file = task.Item1;
                    string hash = task.Item2;
                    // check if existing matchs hash
                    task.Item3.Wait();
                    bool match = task.Item3.Result;

                    if (!match)
                    {
                        done++;
                        log.Info("Newer File " + file);

                        // check is we have already downloaded and matchs hash
                        if (!MD5File(file + ".new", hash))
                        {
                            if (frmProgressReporter != null)
                            {
                                frmProgressReporter.UpdateProgressAndStatus((int)((done / (double)count) * 100),
                                                                            Strings.Getting + file + "\n" + done + " of " + count + " of total " +
                                                                            tasklist.Count);
                            }

                            string subdir = Path.GetDirectoryName(file) + Path.DirectorySeparatorChar;

                            subdir = subdir.Replace("" + Path.DirectorySeparatorChar + Path.DirectorySeparatorChar,
                                                    "" + Path.DirectorySeparatorChar);

                            if (baseurl.ToLower().Contains(".zip"))
                            {
                                GetNewFileZip(frmProgressReporter, baseurl, subdir,
                                              Path.GetFileName(file));
                            }
                            else
                            {
                                GetNewFile(frmProgressReporter, baseurl + subdir.Replace('\\', '/'), subdir,
                                           Path.GetFileName(file));
                            }

                            // check the new downloaded file matchs hash
                            if (!MD5File(file + ".new", hash))
                            {
                                throw new Exception("File downloaded does not match hash: " + file);
                            }
                        }
                        else
                        {
                            log.Info("already got new File " + file);
                        }
                    }
                    else
                    {
                        log.Info("Same File " + file);

                        if (frmProgressReporter != null)
                        {
                            frmProgressReporter.UpdateProgressAndStatus(-1, Strings.Checking + file);
                        }
                    }
                });
            }
        }
Пример #9
0
 void BinaryLog_convertstatus(IProgressReporterDialogue prd, float progress)
 {
     prd.UpdateProgressAndStatus((int)progress, Strings.Converting_bin_to_log);
 }
Пример #10
0
        private void OpenBg(object PRsender, bool getparams, ProgressWorkerEventArgs progressWorkerEventArgs)
        {
            frmProgressReporter.UpdateProgressAndStatus(-1, "正在连接.....");

            giveComport = true;

            byte[] buffer = new byte[1024];

            if (BaseStream is SerialPort)
            {
                // allow settings to settle - previous dtr
                Thread.Sleep(1000);
            }

            //Terrain = new TerrainFollow(this);

            try
            {
                BaseStream.ReadBufferSize = 1024;

                lock (objlock) // so we dont have random traffic
                {
                    log.Info("Open port with " + BaseStream.PortName + " " + BaseStream.BaudRate);

                    if (BaseStream is UdpSerial)
                    {
                        progressWorkerEventArgs.CancelRequestChanged += (o, e) =>
                        {
                            ((UdpSerial)BaseStream).CancelConnect = true;
                            ((ProgressWorkerEventArgs)o)
                            .CancelAcknowledged = true;
                        };
                    }

                    BaseStream.Open();
                    // 读取数据 待修改
                    //BaseStream.DiscardInBuffer();

                    // other boards seem to have issues if there is no delay? posible bootloader timeout issue
                    if (BaseStream is SerialPort)
                    {
                        Thread.Sleep(1000);
                    }
                }

                //List<MAVLinkMessage> hbhistory = new List<MAVLinkMessage>();

                DateTime start    = DateTime.Now;
                DateTime deadline = start.AddSeconds(CONNECT_TIMEOUT_SECONDS);

                var countDown = new Timer {
                    Interval = 1000, AutoReset = false
                };
                countDown.Elapsed += (sender, e) =>
                {
                    int secondsRemaining = (deadline - e.SignalTime).Seconds;
                    frmProgressReporter.UpdateProgressAndStatus(-1, string.Format(Strings.Trying, secondsRemaining));
                    if (secondsRemaining > 0)
                    {
                        countDown.Start();
                    }
                };
                countDown.Start();

                int count = 0;

                while (true)
                {
                    if (progressWorkerEventArgs.CancelRequested)
                    {
                        progressWorkerEventArgs.CancelAcknowledged = true;
                        countDown.Stop();
                        if (BaseStream.IsOpen)
                        {
                            BaseStream.Close();
                        }
                        giveComport = false;
                        return;
                    }

                    log.Info(DateTime.Now.Millisecond + " Start connect loop ");

                    if (DateTime.Now > deadline)
                    {
                        //if (Progress != null)
                        //    Progress(-1, "No Heartbeat Packets");
                        countDown.Stop();
                        this.Close();
                    }

                    Thread.Sleep(1);

                    //BaseStream.Read(buffer, count, 1);
                    count++;
                    // 解析数据,获取 sysid ,comid

                    sysidcurrent  = 1;
                    compidcurrent = 50;

                    // if we get no data, try enableing rts/cts
                    //if (buffer.Length == 0 && BaseStream is SerialPort)
                    //{
                    //    BaseStream.RtsEnable = !BaseStream.RtsEnable;
                    //}

                    if (count > 2)
                    {
                        break;
                    }

                    SetupMavConnect(1, 50, "AIR");
                }

                countDown.Stop();

                if (frmProgressReporter.doWorkArgs.CancelAcknowledged == true)
                {
                    giveComport = false;
                    if (BaseStream.IsOpen)
                    {
                        BaseStream.Close();
                    }
                    return;
                }
            }
            catch (Exception e)
            {
                try
                {
                    BaseStream.Close();
                }
                catch
                {
                }
                giveComport = false;
                if (string.IsNullOrEmpty(progressWorkerEventArgs.ErrorMessage))
                {
                    progressWorkerEventArgs.ErrorMessage = Strings.ConnectFailed;
                }
                log.Error(e);
                Console.WriteLine(e.ToString());
                throw;
            }
            //frmProgressReporter.Close();
            giveComport = false;
            frmProgressReporter.UpdateProgressAndStatus(100, Strings.Done);
            //log.Info("Done open " + MAV.sysid + " " + MAV.compid);
            //MAV.packetslost = 0;
            //MAV.synclost = 0;
        }
Пример #11
0
        static void CheckMD5(IProgressReporterDialogue frmProgressReporter, string md5url, string baseurl)
        {
            log.InfoFormat("get checksums {0} - base {1}", md5url, baseurl);

            string responseFromServer = "";

            WebRequest request = WebRequest.Create(md5url);

            if (!String.IsNullOrEmpty(Settings.Instance.UserAgent))
            {
                ((HttpWebRequest)request).UserAgent = Settings.Instance.UserAgent;
            }
            request.Timeout = 10000;
            // Set the Method property of the request to POST.
            request.Method = "GET";
            // Get the response.
            // Get the stream containing content returned by the server.
            // Open the stream using a StreamReader for easy access.
            using (WebResponse response = request.GetResponse())
                using (Stream dataStream = response.GetResponseStream())
                    using (StreamReader reader = new StreamReader(dataStream))
                    {
                        // Display the status.
                        log.Info(((HttpWebResponse)response).StatusDescription);
                        // Read the content.
                        responseFromServer = reader.ReadToEnd();
                    }

            Regex regex = new Regex(@"([^\s]+)\s+[^/]+/(.*)", RegexOptions.IgnoreCase);

            if (regex.IsMatch(responseFromServer))
            {
                if (frmProgressReporter != null)
                {
                    frmProgressReporter.UpdateProgressAndStatus(-1, "Hashing Files");
                }

                // cleanup dll's with the same exe name
                var           dlls  = Directory.GetFiles(Settings.GetRunningDirectory(), "*.dll", SearchOption.TopDirectoryOnly);
                var           exes  = Directory.GetFiles(Settings.GetRunningDirectory(), "*.exe", SearchOption.TopDirectoryOnly);
                List <string> files = new List <string>();

                // hash everything
                MatchCollection matchs = regex.Matches(responseFromServer);
                for (int i = 0; i < matchs.Count; i++)
                {
                    string hash = matchs[i].Groups[1].Value.ToString();
                    string file = matchs[i].Groups[2].Value.ToString();

                    files.Add(file);
                }

                // cleanup unused dlls and exes
                dlls.ForEach(dll =>
                {
                    try
                    {
                        var result = files.Any(task => Path.Combine(Settings.GetRunningDirectory(), task).ToLower().Equals(dll.ToLower()));

                        if (result == false)
                        {
                            File.Delete(dll);
                        }
                    }
                    catch { }
                });

                exes.ForEach(exe =>
                {
                    try
                    {
                        var result = files.Any(task => Path.Combine(Settings.GetRunningDirectory(), task).ToLower().Equals(exe.ToLower()));

                        if (result == false)
                        {
                            File.Delete(exe);
                        }
                    }
                    catch { }
                });

                // background md5
                List <Tuple <string, string, Task <bool> > > tasklist = new List <Tuple <string, string, Task <bool> > >();

                for (int i = 0; i < matchs.Count; i++)
                {
                    string hash = matchs[i].Groups[1].Value.ToString();
                    string file = matchs[i].Groups[2].Value.ToString();

                    Task <bool> ismatch = Task <bool> .Factory.StartNew(() => MD5File(file, hash));

                    tasklist.Add(new Tuple <string, string, Task <bool> >(file, hash, ismatch));
                }
                // get count and wait for all hashing to be done
                int count = tasklist.Count(a =>
                {
                    a.Item3.Wait();
                    return(!a.Item3.Result);
                });

                // parallel download
                ParallelOptions opt = new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 3
                };

                int done = 0;

                Parallel.ForEach(tasklist, opt, task =>
                                 //foreach (var task in tasklist)
                {
                    string file = task.Item1;
                    string hash = task.Item2;
                    // check if existing matchs hash
                    task.Item3.Wait();
                    bool match = task.Item3.Result;

                    if (!match)
                    {
                        done++;
                        log.Info("Newer File " + file);

                        if (frmProgressReporter != null && frmProgressReporter.doWorkArgs.CancelRequested)
                        {
                            frmProgressReporter.doWorkArgs.CancelAcknowledged = true;
                            throw new Exception("User Request");
                        }

                        // check is we have already downloaded and matchs hash
                        if (!MD5File(file + ".new", hash))
                        {
                            if (frmProgressReporter != null)
                            {
                                frmProgressReporter.UpdateProgressAndStatus((int)((done / (double)count) * 100),
                                                                            Strings.Getting + file + "\n" + done + " of " + count + " of total " +
                                                                            tasklist.Count);
                            }

                            string subdir = Path.GetDirectoryName(file) + Path.DirectorySeparatorChar;

                            subdir = subdir.Replace("" + Path.DirectorySeparatorChar + Path.DirectorySeparatorChar,
                                                    "" + Path.DirectorySeparatorChar);

                            if (baseurl.ToLower().Contains(".zip"))
                            {
                                GetNewFileZip(frmProgressReporter, baseurl, subdir,
                                              Path.GetFileName(file));
                            }
                            else
                            {
                                GetNewFile(frmProgressReporter, baseurl + subdir.Replace('\\', '/'), subdir,
                                           Path.GetFileName(file));
                            }

                            // check the new downloaded file matchs hash
                            if (!MD5File(file + ".new", hash))
                            {
                                throw new Exception("File downloaded does not match hash: " + file);
                            }
                        }
                        else
                        {
                            log.Info("already got new File " + file);
                        }
                    }
                    else
                    {
                        log.Info("Same File " + file);

                        if (frmProgressReporter != null)
                        {
                            frmProgressReporter.UpdateProgressAndStatus(-1, Strings.Checking + file);
                        }
                    }
                });
            }
        }
Пример #12
0
        static void CheckMD5(IProgressReporterDialogue frmProgressReporter, string md5url, string baseurl)
        {
            log.InfoFormat("get checksums {0} - base {1}", md5url, baseurl);

            string responseFromServer = "";

            responseFromServer = client.GetStringAsync(md5url).GetAwaiter().GetResult();

            File.WriteAllText(Settings.GetRunningDirectory() + "checksums.txt.new", responseFromServer);

            Regex regex = new Regex(@"([^\s]+)\s+[^/]+/(.*)", RegexOptions.IgnoreCase);

            if (regex.IsMatch(responseFromServer))
            {
                if (frmProgressReporter != null)
                {
                    frmProgressReporter.UpdateProgressAndStatus(-1, "Hashing Files");
                }

                // cleanup dll's with the same exe name
                var           dlls  = Directory.GetFiles(Settings.GetRunningDirectory(), "*.dll", SearchOption.AllDirectories);
                var           exes  = Directory.GetFiles(Settings.GetRunningDirectory(), "*.exe", SearchOption.AllDirectories);
                List <string> files = new List <string>();

                // hash everything
                MatchCollection matchs = regex.Matches(responseFromServer);
                for (int i = 0; i < matchs.Count; i++)
                {
                    string hash = matchs[i].Groups[1].Value.ToString();
                    string file = matchs[i].Groups[2].Value.ToString();

                    files.Add(file);
                }

                // cleanup unused dlls and exes
                dlls.ForEach(dll =>
                {
                    try
                    {
                        var result = files.Any(task => Path.Combine(Settings.GetRunningDirectory(), task).ToLower().Equals(dll.ToLower()));

                        if (result == false)
                        {
                            File.Delete(dll);
                        }
                    }
                    catch { }
                });

                exes.ForEach(exe =>
                {
                    try
                    {
                        var result = files.Any(task => Path.Combine(Settings.GetRunningDirectory(), task).ToLower().Equals(exe.ToLower()));

                        if (result == false)
                        {
                            File.Delete(exe);
                        }
                    }
                    catch { }
                });



                // background md5
                List <Tuple <string, string, Task <bool> > > tasklist = new List <Tuple <string, string, Task <bool> > >();

                for (int i = 0; i < matchs.Count; i++)
                {
                    string hash = matchs[i].Groups[1].Value.ToString().Trim();
                    string file = matchs[i].Groups[2].Value.ToString().Trim();

                    if (file.ToLower().EndsWith("files.html"))
                    {
                        continue;
                    }

                    Task <bool> ismatch = Task <bool> .Factory.StartNew(() => MD5File(file, hash));

                    tasklist.Add(new Tuple <string, string, Task <bool> >(file, hash, ismatch));
                }
                // get count and wait for all hashing to be done
                int count = tasklist.Count(a =>
                {
                    a.Item3.Wait();
                    return(!a.Item3.GetAwaiter().GetResult());
                });

                // parallel download
                ParallelOptions opt = new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 3
                };

                tasklist.Sort((a, b) =>
                {
                    if (a == null || b == null)
                    {
                        return(0);
                    }

                    if (a.Item1.ToLower().EndsWith(".exe") && b.Item1.ToLower().EndsWith(".exe"))
                    {
                        return(a.Item1.CompareTo(b.Item1));
                    }
                    if (a.Item1.ToLower().EndsWith(".exe"))
                    {
                        return(-1);
                    }
                    if (b.Item1.ToLower().EndsWith(".exe"))
                    {
                        return(1);
                    }

                    if (a.Item1.ToLower().EndsWith(".dll") && b.Item1.ToLower().EndsWith(".dll"))
                    {
                        return(a.Item1.CompareTo(b.Item1));
                    }
                    if (a.Item1.ToLower().EndsWith(".dll"))
                    {
                        return(-1);
                    }
                    if (b.Item1.ToLower().EndsWith(".dll"))
                    {
                        return(1);
                    }

                    return(a.Item1.CompareTo(b.Item1));
                });

                /*
                 * if (frmProgressReporter != null)
                 *  frmProgressReporter.UpdateProgressAndStatus(-1, "Downloading parts");
                 *
                 * // start download
                 * if (baseurl.ToLower().Contains(".zip"))
                 * {
                 *  List<(int, int)> ranges = new List<(int, int)>();
                 *
                 *  using (DownloadStream ds = new DownloadStream(baseurl))
                 *  using (ZipArchive zip = new ZipArchive(ds))
                 *  {
                 *      FieldInfo fieldInfo = typeof(ZipArchiveEntry).GetField("_offsetOfLocalHeader", BindingFlags.NonPublic | BindingFlags.Instance);
                 *      var extents = zip.Entries.Select(e =>
                 *      {
                 *          var _offsetOfLocalHeader = (long)fieldInfo.GetValue(e);
                 *          return (e.FullName, _offsetOfLocalHeader);
                 *      }).OrderBy(a => a._offsetOfLocalHeader);
                 *
                 *      tasklist.ForEach(task => {
                 *
                 *          task.Item3.Wait();
                 *          bool match = task.Item3.GetAwaiter().GetResult();
                 *
                 *          if (!match)
                 *          {
                 *              extents.ForEach(entry1 =>
                 *              {
                 *                  var fn = entry1.FullName;
                 *
                 *                  var diskfn = task.Item1;
                 *
                 *                  if (diskfn.EndsWith(fn))
                 *                  {
                 *                      var next = ds.Length;
                 *                      zip.Entries.ForEach(entry2 => {
                 *                          var _offsetOfLocalHeader2 = (long)fieldInfo.GetValue(entry2);
                 *                          if (_offsetOfLocalHeader2 > entry1._offsetOfLocalHeader)
                 *                              next = Math.Min(_offsetOfLocalHeader2, next);
                 *                      });
                 *
                 *                      ranges.Add(((int)entry1._offsetOfLocalHeader, (int)(next)));
                 *                  }
                 *              });
                 *          }
                 *      });
                 *
                 *      ranges = ranges.SimplifyIntervals().ToList();
                 *      ranges.ForEach(range => {
                 *          ds.chunksize = range.Item2 - range.Item1;
                 *          ds.getAllData(range.Item1, range.Item2);
                 *      });
                 *
                 *  }
                 * }
                 */
                int done = 0;

                Parallel.ForEach(tasklist, opt, task =>
                                 //foreach (var task in tasklist)
                {
                    string file = task.Item1;
                    string hash = task.Item2;
                    // check if existing matchs hash
                    task.Item3.Wait();
                    bool match = task.Item3.GetAwaiter().GetResult();

                    if (!match)
                    {
                        done++;
                        log.Info("Newer File " + file);

                        if (frmProgressReporter != null && frmProgressReporter.doWorkArgs.CancelRequested)
                        {
                            frmProgressReporter.doWorkArgs.CancelAcknowledged = true;
                            throw new Exception("User Request");
                        }

                        // check is we have already downloaded and matchs hash
                        if (!MD5File(file + ".new", hash))
                        {
                            if (frmProgressReporter != null)
                            {
                                frmProgressReporter.UpdateProgressAndStatus((int)((done / (double)count) * 100),
                                                                            Strings.Getting + file + "\n" + done + " of " + count + " of total " +
                                                                            tasklist.Count);
                            }

                            string subdir = Path.GetDirectoryName(file) + Path.DirectorySeparatorChar;

                            subdir = subdir.Replace("" + Path.DirectorySeparatorChar + Path.DirectorySeparatorChar,
                                                    "" + Path.DirectorySeparatorChar);

                            if (baseurl.ToLower().Contains(".zip"))
                            {
                                GetNewFileZip(frmProgressReporter, baseurl, subdir,
                                              Path.GetFileName(file));
                            }
                            else
                            {
                                GetNewFile(frmProgressReporter, baseurl + subdir.Replace('\\', '/'), subdir,
                                           Path.GetFileName(file));
                            }

                            // check the new downloaded file matchs hash
                            if (!MD5File(file + ".new", hash))
                            {
                                throw new Exception("File downloaded does not match hash: " + file);
                            }
                        }
                        else
                        {
                            log.Info("already got new File " + file);
                        }
                    }
                    else
                    {
                        log.Info("Same File " + file);

                        if (frmProgressReporter != null)
                        {
                            frmProgressReporter.UpdateProgressAndStatus(-1, Strings.Checking + file);
                        }
                    }
                });
            }
        }