Пример #1
0
        private void ConfirmButton_Click(object sender, RoutedEventArgs ev)
        {
            var handler = new ProgressHandler();
            var t       = new Thread(delegate()
            {
                var request  = (HttpWebRequest)WebRequest.Create("http://www.modapi.cc/app/archives/" + NewVersion + ".zip");
                var response = (HttpWebResponse)request.GetResponse();
                var s        = response.GetResponseStream();
                var buffer   = new byte[4096];
                var memory   = new MemoryStream();
                var count    = 0;
                long current = 0;
                var progress = 0f;
                handler.Task = "Download";
                while ((count = s.Read(buffer, 0, buffer.Length)) > 0)
                {
                    memory.Write(buffer, 0, count);
                    current         += count;
                    progress         = (float)(((current / (double)response.ContentLength)) * 70.0);
                    handler.Progress = progress;
                }

                memory.Position = 0;
                var zip         = ZipFile.Read(memory);
                var directory   = "./_update";
                var n           = 0;
                handler.Task    = "Extracting";
                foreach (var e in zip)
                {
                    try
                    {
                        e.Extract(directory, ExtractExistingFileAction.OverwriteSilently);
                    }
                    catch (Exception ex3)
                    {
                    }
                    n += 1;
                    handler.Progress = 70f + (n / (float)zip.Count) * 30f;
                }

                var p = new Process();
                p.StartInfo.FileName = "Updater.exe";
                p.StartInfo.Verb     = "runas";
                p.Start();
                Environment.Exit(0);
            });

            var window = new OperationPending("Lang.Windows.OperationPending", "Update", handler);

            if (!window.Completed)
            {
                window.ShowSubWindow();
                window.Show();
                Close();
            }
            t.Start();
        }
Пример #2
0
        private void Restore_Click(object sender, RoutedEventArgs e)
        {
            var progressHandler = new ProgressHandler();

            progressHandler.OnComplete += (s, ev) => Dispatcher.Invoke(delegate { Task.Complete(); });
            var t = new Thread(delegate()
            {
                try
                {
                    var steamPath               = Configuration.GetPath("Steam");
                    var p                       = new Process();
                    p.StartInfo.FileName        = steamPath + Path.DirectorySeparatorChar + "Steam.exe";
                    p.StartInfo.Arguments       = "steam://validate/" + App.Game.GameConfiguration.SteamAppId;
                    p.StartInfo.UseShellExecute = false;
                    p.Start();
                    progressHandler.Task     = "Restore";
                    progressHandler.Progress = 50f;
                    var state    = 0;
                    var lastLine = "";
                    while (true)
                    {
                        var processes  = Process.GetProcesses();
                        var foundSteam = false;
                        foreach (var pp in processes)
                        {
                            try
                            {
                                if (!pp.HasExited && pp.ProcessName == "Steam")
                                {
                                    foundSteam = true;
                                    break;
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }

                        var logFile = steamPath + Path.DirectorySeparatorChar + "logs" + Path.DirectorySeparatorChar + "content_log.txt";
                        var s       = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                        var f       = new FileInfo(logFile);
                        var data    = new byte[f.Length];
                        s.Read(data, 0, (int)f.Length);
                        var content = Encoding.UTF8.GetString(data);

                        var l = content.Split(new[] { "\r\n" }, StringSplitOptions.None); // System.IO.File.ReadAllLines(SteamPath + "\\logs\\content_log.txt");
                        if (lastLine == "")
                        {
                            lastLine = l[l.Length - 2];
                        }
                        var check = false;
                        foreach (var n in l)
                        {
                            if (n == lastLine)
                            {
                                check = true;
                            }
                            if (check)
                            {
                                if (n.EndsWith("AppID " + App.Game.GameConfiguration.SteamAppId + " state changed : Fully Installed,"))
                                {
                                    state = 3;
                                }
                                else if (n.Contains("AppID " + App.Game.GameConfiguration.SteamAppId) && n.Contains("(Suspended)"))
                                {
                                    state = 1;
                                }
                                else if (n.Contains("Failed to get list of download sources from any known CS!"))
                                {
                                    state = 2;
                                }
                            }
                        }

                        if (state == 0 && !foundSteam)
                        {
                            state = 4;
                        }

                        if (state > 0)
                        {
                            break;
                        }

                        Thread.Sleep(500);
                    }

                    if (state == 3)
                    {
                        progressHandler.Task     = "Finish";
                        progressHandler.Progress = 100f;
                    }
                    else
                    {
                        if (state == 1)
                        {
                            progressHandler.Task = "Error.Cancelled";
                        }
                        else if (state == 2)
                        {
                            progressHandler.Task = "Error.NoConnection";
                        }
                        else if (state == 4)
                        {
                            progressHandler.Task = "Error.SteamClosed";
                        }

                        Dispatcher.Invoke(delegate { Task.Complete(); });
                    }
                }
                catch (Exception exx)
                {
                    Console.WriteLine(exx.ToString());
                }
            });

            Close();
            var win4 = new OperationPending("Lang.Windows.OperationPending", "RestoreGameFiles", progressHandler, null, true);

            if (!win4.Completed)
            {
                win4.ShowSubWindow();
            }
            t.Start();
        }