Exemple #1
0
        public Mod HandleModFiles(string modpath)
        {
            comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
            mod                = new Mod(Path.GetFileName(modpath));
            mod.category       = new Category(comboBox1.Items[comboBox1.SelectedIndex].ToString());
            this.textBox1.Text = Path.GetFileName(modpath).Replace(Path.GetExtension(modpath), "");
            this.Text          = Path.GetFileName(modpath) + " (" + Path.GetExtension(modpath) + ")";
            Busy               = true;
            button2.Enabled    = false;

            // string filePath = openFileDialog.FileName;
            string fileName   = Path.GetFileName(modpath);
            string extention  = Path.GetExtension(modpath);
            string folderpath = Path.GetDirectoryName(modpath);

            Form1.data.openDirectory = folderpath;

            if (extention == ".zip" || extention == ".7z")
            {
                progressBar1.Visible = true;
                //read files in zip an process them
                using (ZipArchive archive = ZipFile.Open(modpath, ZipArchiveMode.Read))
                {
                    progressBar1.Maximum = archive.Entries.Count;
                    foreach (ZipArchiveEntry item in archive.Entries)
                    {
                        if (!String.IsNullOrEmpty(item.Name))//check if not folder
                        {
                            string itemName = item.FullName;
                            if (itemName.Contains('/'))
                            {
                                itemName = itemName.Split('/')[1];
                            }

                            string ext = Path.GetExtension(itemName);
                            if (ForbinnenExtentions.Contains(ext))
                            {
                                continue;
                            }

                            item.ExtractToFile(extractPath + "\\" + itemName, true);
                            listBox1.Items.Add(item);
                            mod.files.Add(extractPath + "\\" + itemName);
                            progressBar1.Value = archive.Entries.IndexOf(item);
                        }
                    }
                }

                progressBar1.Visible = false;
            }
            else if (extention == ".rar")
            {
                int fileAmount = 0;
                // SharpCompress.Readers.Rar;
                using (RarReader reader = RarReader.Open(File.OpenRead(modpath)))
                {
                    while (reader.MoveToNextEntry())
                    {
                        if (!reader.Entry.IsDirectory)
                        {
                            fileAmount++;
                        }
                    }
                }
                progressBar1.Maximum = fileAmount;
                using (RarReader reader = RarReader.Open(File.OpenRead(modpath)))
                {
                    while (reader.MoveToNextEntry())
                    {
                        if (!reader.Entry.IsDirectory)
                        {
                            using (EntryStream entryStream = reader.OpenEntryStream())
                            {
                                string file = Path.GetFileName(reader.Entry.Key);
                                if (null != file)
                                {
                                    string destinationFileName = Path.Combine(extractPath + "\\", file);
                                    using (FileStream fs = File.OpenWrite(destinationFileName))
                                    {
                                        string ext = Path.GetExtension(file);
                                        if (ForbinnenExtentions.Contains(ext))
                                        {
                                            continue;
                                        }
                                        func.TransferTo(reader, entryStream, fs);
                                        listBox1.Items.Add(file);
                                        mod.files.Add(extractPath + "\\" + file);
                                        //  progressBar1.Value++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                mod.files.Add(modpath);
                listBox1.Items.Add(Path.GetFileName(modpath));
            }

            button2.Enabled = true;
            Busy            = false;
            return(mod);
        }
Exemple #2
0
        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            if (!editMode)
            {
                using (OpenFileDialog openFileDialog = new OpenFileDialog())
                {
                    openFileDialog.Multiselect      = true;
                    openFileDialog.InitialDirectory = Form1.data.openDirectory;
                    openFileDialog.Filter           = "Mod files (*.zip,*.package;*.t4script;*.rar;)|*.zip;*.package;*.t4script;*.rar|Whatever (testing) (*.*)|*.*";
                    openFileDialog.FilterIndex      = 1;
                    openFileDialog.RestoreDirectory = true;

                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        //Get the path of specified file
                        comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
                        mod                = new Mod(Path.GetFileName(openFileDialog.FileNames[0]));
                        mod.category       = new Category(comboBox1.Items[comboBox1.SelectedIndex].ToString());
                        this.textBox1.Text = Path.GetFileName(openFileDialog.FileNames[0]).Replace(Path.GetExtension(openFileDialog.FileNames[0]), "");
                        this.Text          = Path.GetFileName(openFileDialog.FileNames[0]) + " (" + Path.GetExtension(openFileDialog.FileNames[0]) + ")";
                        Busy               = true;
                        button2.Enabled    = false;
                        foreach (string filePath in openFileDialog.FileNames)
                        {
                            // string filePath = openFileDialog.FileName;
                            string fileName   = Path.GetFileName(filePath);
                            string extention  = Path.GetExtension(filePath);
                            string folderpath = Path.GetDirectoryName(filePath);
                            Form1.data.openDirectory = folderpath;

                            if (extention == ".zip" || extention == ".7z")
                            {
                                progressBar1.Visible = true;
                                //read files in zip an process them
                                using (ZipArchive archive = ZipFile.Open(filePath, ZipArchiveMode.Read))
                                {
                                    progressBar1.Maximum = archive.Entries.Count;
                                    foreach (ZipArchiveEntry item in archive.Entries)
                                    {
                                        if (!String.IsNullOrEmpty(item.Name))//check if not folder
                                        {
                                            string itemName = item.FullName;
                                            if (itemName.Contains('/'))
                                            {
                                                itemName = itemName.Split('/')[1];
                                            }

                                            string ext = Path.GetExtension(itemName);
                                            if (ForbinnenExtentions.Contains(ext))
                                            {
                                                continue;
                                            }

                                            item.ExtractToFile(extractPath + itemName, true);
                                            listBox1.Items.Add(item);
                                            mod.files.Add(extractPath + itemName);
                                            progressBar1.Value = archive.Entries.IndexOf(item);
                                        }
                                    }
                                }

                                progressBar1.Visible = false;
                            }
                            else if (extention == ".rar")
                            {
                                int fileAmount = 0;
                                // SharpCompress.Readers.Rar;
                                using (RarReader reader = RarReader.Open(File.OpenRead(filePath)))
                                {
                                    while (reader.MoveToNextEntry())
                                    {
                                        if (!reader.Entry.IsDirectory)
                                        {
                                            fileAmount++;
                                        }
                                    }
                                }
                                progressBar1.Maximum = fileAmount;
                                using (RarReader reader = RarReader.Open(File.OpenRead(filePath)))
                                {
                                    while (reader.MoveToNextEntry())
                                    {
                                        if (!reader.Entry.IsDirectory)
                                        {
                                            using (EntryStream entryStream = reader.OpenEntryStream())
                                            {
                                                string file = Path.GetFileName(reader.Entry.Key);
                                                if (null != file)
                                                {
                                                    string destinationFileName = extractPath + file;
                                                    using (FileStream fs = File.OpenWrite(destinationFileName))
                                                    {
                                                        string ext = Path.GetExtension(file);
                                                        if (ForbinnenExtentions.Contains(ext))
                                                        {
                                                            continue;
                                                        }
                                                        func.TransferTo(reader, entryStream, fs);
                                                        listBox1.Items.Add(file);
                                                        mod.files.Add(extractPath + file);
                                                        //  progressBar1.Value++;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                mod.files.Add(filePath);
                                listBox1.Items.Add(Path.GetFileName(filePath));
                            }
                        }
                        button2.Enabled = true;
                        Busy            = false;
                    }
                }
            }
        }
Exemple #3
0
        public static async Task <bool> Download(string version, string gameId, string outputFile, CancellationToken cancellation)
        {
            // Check if the version is known
            Update update = Update.Find(version);

            string dir = Path.GetDirectoryName(outputFile);

            if (string.IsNullOrEmpty(dir))
            {
                throw new ArgumentException(@"Outputfile must be a full path", outputFile);
            }
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            Interaction.Instance.SetProgressMaximum(updateSize != 0 ? updateSize : update.Size);
            try
            {
                using (Stream s = await OpenUrl(update, gameId))
                {
                    if (cancellation.IsCancellationRequested)
                    {
                        return(false);
                    }

                    string tmpFile = string.Concat(outputFile, ".tmp");
                    using (FileStream fs = new FileStream(tmpFile, FileMode.Create, FileAccess.Write))
                    {
                        if (isRarStream)
                        {
                            // Wrap into unrar
                            RarReader reader = RarReader.Open(s, RarOptions.None);
                            if (!reader.MoveToNextEntry())
                            {
                                return(false);
                            }
                            reader.WriteEntryTo(new WriteProgressStream(fs), cancellation);
                        }
                        else
                        {
                            int    bytesRead = -1, totalBytesRead = 0;
                            byte[] buffer = new byte[2048];
                            while (bytesRead != 0)
                            {
                                if (cancellation.IsCancellationRequested)
                                {
                                    break;
                                }

                                bytesRead = await s.ReadAsync(buffer, 0, buffer.Length);

                                fs.Write(buffer, 0, bytesRead);
                                totalBytesRead += bytesRead;

                                Interaction.Instance.ReportProgress(totalBytesRead);
                            }
                        }
                    }

                    if (cancellation.IsCancellationRequested)
                    {
                        File.Delete(tmpFile);
                        return(false);
                    }
                    if (File.Exists(outputFile))
                    {
                        File.Delete(outputFile);
                    }
                    File.Move(tmpFile, outputFile);
                    return(true);
                }
            }
            catch (WebException)
            {
                return(false);
            }
        }