Beispiel #1
0
        public static BnSDatWriter Modify(string filename, bool is64)
        {
            FileStream   fs     = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
            BnSDatWriter result = BnSDatWriter.Create(fs, false);

            using (BnSDatArchive archive = BnSDatArchive.Read(fs, true))
                using (IReader reader = archive.ExtractAllEntries())
                    while (reader.MoveToNextEntry())
                    {
                        reader.CopyEntryTo(result);
                    }
            return(result);
        }
Beispiel #2
0
        private void Bworker_DoWork(object sender, DoWorkEventArgs e)
        {
            this.sync.Post(new SendOrPostCallback(delegate
            {
                this.label1.Text = "Preparing";
            }), null);
            Operation operation = this.Operation;
            string    tmpFolder, path_original, path_output, path_info;
            int       counting = 0;

            if (operation == Operation.Patch)
            {
                tmpFolder     = this.TemporaryFolder;
                path_original = Path.GetFullPath(this.OriginalXML);
                path_output   = Path.GetFullPath(this.OutputXML);
                path_info     = Path.GetFullPath(this.DirectoryOfPatchcingInfo);

                if (!Directory.Exists(path_info))
                {
                    throw new DirectoryNotFoundException();
                }

                if (!File.Exists(path_original))
                {
                    throw new FileNotFoundException("The original xml file is not existed.");
                }

                if (path_original.IsEqual(path_output, true))
                {
                    throw new InvalidDataException("You can't specify output as same as original xml file.");
                }

                string  tmpString;
                IniFile currentIni;
                Dictionary <string, string> nodes;

                Dictionary <string, Dictionary <string, string> > patchingList = new Dictionary <string, Dictionary <string, string> >(StringComparer.OrdinalIgnoreCase);
                foreach (string iniPath in Directory.EnumerateFiles(path_info, "*.xmlpatch", SearchOption.AllDirectories))
                {
                    tmpString  = PathHelper.PathTrim(iniPath.Substring(path_info.Length + 1));
                    currentIni = new IniFile(iniPath);
                    nodes      = new Dictionary <string, string>();
                    foreach (string section in currentIni.Sections)
                    {
                        nodes.Add(section, currentIni.GetValue(section, "Replace", string.Empty));
                    }
                    if (nodes.Count > 0)
                    {
                        patchingList.Add(Path.ChangeExtension(tmpString, ".xml"), nodes);
                    }
                    currentIni.Close();
                }

                if (patchingList.Count > 0)
                {
                    using (BnSDatArchive archive = BnSDatArchive.Read(path_original))
                        using (BnSDatWriter outputArchive = string.IsNullOrWhiteSpace(tmpFolder) ? BnSDatWriter.Create(path_output) : BnSDatWriter.Create(path_output, tmpFolder))
                            using (IReader reader = archive.ExtractAllEntries())
                            {
                                this.sync.Post(new SendOrPostCallback(delegate
                                {
                                    progressBar1.Maximum = archive.EntryCount;
                                }), null);
                                while (reader.MoveToNextEntry())
                                {
                                    if (this.bworker.CancellationPending)
                                    {
                                        this.sync.Post(new SendOrPostCallback(delegate
                                        {
                                            this.label1.Text = "Cancelling";
                                        }), null);
                                        e.Cancel = true;
                                        break;
                                    }
                                    this.sync.Post(new SendOrPostCallback(delegate
                                    {
                                        this.label1.Text = $"Progressing: {reader.Entry.FilePath}";
                                    }), null);
                                    if (patchingList.ContainsKey(reader.Entry.FilePath))
                                    {
                                        using (EntryStream entryStream = reader.GetEntryStream())
                                            using (StreamReader sr = new StreamReader(entryStream))
                                            {
                                                tmpString = sr.ReadToEnd();
                                                foreach (var keypair in patchingList[reader.Entry.FilePath])
                                                {
                                                    tmpString = tmpString.Replace(keypair.Key, keypair.Value);
                                                }
                                                outputArchive.CompressString(reader.Entry.FilePath, tmpString, sr.CurrentEncoding);
                                            }
                                        tmpString = null;
                                    }
                                    else
                                    {
                                        // Just copy the file
                                        reader.CopyEntryTo(outputArchive);
                                    }
                                    counting++;
                                    this.sync.Post(new SendOrPostCallback(delegate
                                    {
                                        progressBar1.Value = counting;
                                    }), null);
                                }
                                if (!this.bworker.CancellationPending)
                                {
                                    outputArchive.WriteArchive();
                                }
                            }
                }
                else
                {
                    throw new Exception("There is nothing to patch.");
                }
            }
            else if (operation == Operation.Extract)
            {
                path_original = Path.GetFullPath(this.OriginalXML);
                path_output   = Path.GetFullPath(this.OutputXML);
                using (BnSDatArchive archive = BnSDatArchive.Read(path_original))
                    using (IReader reader = archive.ExtractAllEntries())
                    {
                        string filepath;
                        this.sync.Post(new SendOrPostCallback(delegate
                        {
                            progressBar1.Maximum = archive.EntryCount;
                        }), null);

                        while (reader.MoveToNextEntry())
                        {
                            this.sync.Post(new SendOrPostCallback(delegate
                            {
                                this.label1.Text = $"Extracting: {reader.Entry.FilePath}";
                            }), null);
                            if (this.bworker.CancellationPending)
                            {
                                this.sync.Post(new SendOrPostCallback(delegate
                                {
                                    this.label1.Text = "Cancelling";
                                }), null);
                                e.Cancel = true;
                                break;
                            }

                            filepath = Path.Combine(path_output, reader.Entry.FilePath);
                            Microsoft.VisualBasic.FileIO.FileSystem.CreateDirectory(Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(filepath));
                            using (FileStream fs = File.Create(filepath))
                                reader.ExtractTo(fs);

                            counting++;
                            this.sync.Post(new SendOrPostCallback(delegate
                            {
                                progressBar1.Value = counting;
                            }), null);
                        }
                    }
            }
            else if (operation == Operation.Compress)
            {
                tmpFolder     = this.TemporaryFolder;
                path_original = Path.GetFullPath(this.OriginalXML);
                path_info     = Path.GetFullPath(this.DirectoryOfPatchcingInfo);
                if (!Directory.Exists(path_info))
                {
                    throw new DirectoryNotFoundException();
                }

                string[] filelist = Directory.GetFiles(path_info, "*", SearchOption.AllDirectories);
                if (filelist.Length == 0)
                {
                    throw new Exception("There is nothing to compress.");
                }

                using (BnSDatWriter writer = string.IsNullOrWhiteSpace(tmpFolder) ? BnSDatWriter.Create(path_original) : BnSDatWriter.Create(path_original, tmpFolder))
                {
                    string filepath;
                    this.sync.Post(new SendOrPostCallback(delegate
                    {
                        progressBar1.Maximum = filelist.Length;
                    }), null);
                    for (int i = 0; i < filelist.Length; i++)
                    {
                        filepath = filelist[i].Remove(0, path_info.Length + 1);
                        this.sync.Post(new SendOrPostCallback(delegate
                        {
                            this.label1.Text   = $"Compressing: {filepath}";
                            progressBar1.Value = i + 1;
                        }), null);

                        if (this.bworker.CancellationPending)
                        {
                            e.Cancel = true;
                            break;
                        }
                        writer.CompressFile(filepath, filelist[i]);
                    }
                    this.sync.Post(new SendOrPostCallback(delegate
                    {
                        this.label1.Text = "Finalizing archive...";
                    }), null);
                    writer.WriteArchive();
                }
            }
            else
            {
                throw new InvalidEnumArgumentException();
            }
        }