Пример #1
0
        private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            try {
                string hexString = BitConverter.ToString(File.ReadAllBytes(archive).Take(2).ToArray()).Replace("-", " ");

                if (hexString == "50 4B")
                {
                    ZIP.InstallFromZip(archive, Properties.Settings.Default.Path_ModsDirectory);
                }
                else
                {
                    ZIP.InstallFromCustomArchive(archive, Properties.Settings.Default.Path_ModsDirectory);
                }

                UnifyMessenger.UnifyMessage.ShowDialog($"{item.ModName} has been installed in your mods directory.",
                                                       "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                File.Delete(archive);
                Close();
            } catch {
                UnifyMessenger.UnifyMessage.ShowDialog($"Failed to extract {item.ModName}...",
                                                       "Extract failed...", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
        }
Пример #2
0
        private async void Btn_Update_Click(object sender, EventArgs e)
        {
            string archive = Path.GetTempFileName();
            string metadata, version = string.Empty;

            try { metadata = await Client.RequestString(this.metadata); }
            catch { return; }

            if (metadata.Length != 0)
            {
                string[] splitMetadata = metadata.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                foreach (string line in splitMetadata)
                {
                    string entryValue = string.Empty;
                    if (line.StartsWith("Version"))
                    {
                        entryValue = line.Substring(line.IndexOf("=") + 2);
                        entryValue = entryValue.Remove(entryValue.Length - 1);
                        version    = entryValue;
                    }
                    if (line.StartsWith("Data"))
                    {
                        entryValue = line.Substring(line.IndexOf("=") + 2);
                        entryValue = entryValue.Remove(entryValue.Length - 1);
                        if (data != entryValue)
                        {
                            data = entryValue;
                        }
                    }
                }
            }

            if (version != this.version)
            {
                using (WebClient client = new WebClient()) {
                    client.DownloadProgressChanged += (s, dlevent) => { pgb_Progress.Value = dlevent.ProgressPercentage; };
                    await client.DownloadFileTaskAsync(new Uri(data), archive);

                    client.DownloadFileCompleted += (s, dlevent) => {
                        byte[]        bytes       = File.ReadAllBytes(archive).Take(2).ToArray();
                        string        hexString   = BitConverter.ToString(bytes); hexString = hexString.Replace("-", " ");
                        DirectoryInfo extractData = new DirectoryInfo(modDirectory);
                        pic_Thumbnail.BackgroundImage.Dispose();
                        pic_Thumbnail.BackgroundImage = Properties.Resources.Exception_Logo_Full_Colour;

                        try {
                            if (Directory.Exists(modDirectory))
                            {
                                foreach (FileInfo file in extractData.GetFiles())
                                {
                                    file.Delete();
                                }
                                foreach (DirectoryInfo directory in extractData.GetDirectories())
                                {
                                    directory.Delete(true);
                                }
                            }
                            Directory.Delete(modDirectory);
                        } catch { }

                        if (hexString == "50 4B")
                        {
                            using (ZipArchive zip = new ZipArchive(new MemoryStream(File.ReadAllBytes(archive))))
                                ZIP.ExtractToDirectory(zip, Properties.Settings.Default.Path_ModsDirectory, true);
                        }
                        else
                        {
                            ZIP.InstallFromCustomArchive(archive, Properties.Settings.Default.Path_ModsDirectory);
                        }

                        UnifyMessenger.UnifyMessage.ShowDialog($"{lbl_Title.Text} has been updated successfully...",
                                                               lbl_Title.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

                        File.Delete(archive);
                        Close();
                    };
                }
            }
            else
            {
                UnifyMessenger.UnifyMessage.ShowDialog("There are currently no updates available.",
                                                       lbl_Title.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }