public static void ReplaceKnownEntry(TreeView tree, ArcEntryWrapper node, string filename, DefaultWrapper entrytobuild, DefaultWrapper entrytoreplace, Type filetype = null)
        {
            tree.BeginUpdate();


            //We build the entrytobuild starting from the uncompressed data.
            entrytobuild.UncompressedData = System.IO.File.ReadAllBytes(filename);

            //Then Compress.
            entrytobuild.CompressedData = Zlibber.Compressor(entrytobuild.UncompressedData);

            entrytobuild.DSize = entrytobuild.UncompressedData.Length;
            entrytobuild.CSize = entrytobuild.CompressedData.Length;

            //Gets the filename of the file to inject without the directory.
            string trname = filename;

            while (trname.Contains("\\"))
            {
                trname = trname.Substring(trname.IndexOf("\\") + 1);
            }



            //Code that replaces the name with the new node's name.

            //Enters name related parameters of the entrytobuild.
            entrytobuild.TrueName = trname;
            entrytobuild.TrueName = Path.GetFileNameWithoutExtension(trname);
            entrytobuild.FileExt  = trname.Substring(trname.LastIndexOf("."));

            string TypeHash = "";

            //Looks through the archive_filetypes.cfg file to find the typehash associated with the extension.
            try
            {
                using (var sr2 = new StreamReader("archive_filetypes.cfg"))
                {
                    while (!sr2.EndOfStream)
                    {
                        var keyword = Console.ReadLine() ?? entrytobuild.FileExt;
                        var line    = sr2.ReadLine();
                        if (String.IsNullOrEmpty(line))
                        {
                            continue;
                        }
                        if (line.IndexOf(keyword, StringComparison.CurrentCultureIgnoreCase) >= 0)
                        {
                            TypeHash = line;
                            TypeHash = TypeHash.Split(' ')[0];
                            entrytobuild.TypeHash = TypeHash;
                            break;
                        }
                    }
                }
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("Cannot find archive_filetypes.cfg so I cannot continue parsing this file.\n Find archive_filetypes.cfg and then restart this program.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                using (StreamWriter sw = File.AppendText("Log.txt"))
                {
                    sw.WriteLine("Cannot find archive_filetypes.cfg so I cannot continue parsing the file.");
                }
                Process.GetCurrentProcess().Kill();
                return;
            }

            var tag = node.Tag;

            if (tag is DefaultWrapper)
            {
                entrytoreplace = tag as DefaultWrapper;
            }

            string path  = "";
            int    index = entrytoreplace.EntryName.LastIndexOf("\\");

            if (index > 0)
            {
                path = entrytoreplace.EntryName.Substring(0, index);
            }

            entrytobuild.EntryName = path + "\\" + entrytobuild.TrueName;

            //Enters name related parameters of the entrytobuild. Meant to preserve the orignal filename.
            entrytobuild.TrueName  = entrytoreplace.TrueName;
            entrytobuild.FileExt   = entrytoreplace.FileExt;
            entrytobuild.EntryName = entrytoreplace.EntryName;

            tag = entrytobuild;

            if (node.Tag is DefaultWrapper)
            {
                node.Tag  = entrytobuild;
                node.Name = Path.GetFileNameWithoutExtension(entrytobuild.EntryName);
                node.Text = Path.GetFileNameWithoutExtension(entrytobuild.EntryName);
            }

            var aew = node as ArcEntryWrapper;

            string type = node.GetType().ToString();

            if (type == "ThreeWorkTool.Resources.Wrappers.ArcEntryWrapper")
            {
                aew.entryfile = entrytobuild;
            }

            node           = aew;
            node.entryfile = entrytobuild;
        }
示例#2
0
        public static void ReplaceEntry(TreeView tree, ArcEntryWrapper node, string filename, DefaultWrapper entrytobuild, DefaultWrapper entrytoreplace, Type filetype = null)
        {
            tree.BeginUpdate();

            try
            {
                using (BinaryReader bnr = new BinaryReader(File.OpenRead(filename)))
                {
                    //We build the entrytobuild starting from the uncompressed data.
                    entrytobuild.UncompressedData = System.IO.File.ReadAllBytes(filename);

                    //Then Compress.
                    entrytobuild.CompressedData = Zlibber.Compressor(entrytobuild.UncompressedData);


                    //Gets the filename of the file to inject without the directory.
                    string trname = filename;
                    while (trname.Contains("\\"))
                    {
                        trname = trname.Substring(trname.IndexOf("\\") + 1);
                    }

                    //Enters name related parameters of the entrytobuild.
                    entrytobuild.TrueName = trname;
                    entrytobuild.TrueName = Path.GetFileNameWithoutExtension(trname);
                    entrytobuild.FileExt  = trname.Substring(trname.LastIndexOf("."));

                    string TypeHash = "";

                    //Looks through the archive_filetypes.cfg file to find the typehash associated with the extension.
                    try
                    {
                        using (var sr2 = new StreamReader("archive_filetypes.cfg"))
                        {
                            while (!sr2.EndOfStream)
                            {
                                var keyword = Console.ReadLine() ?? entrytobuild.FileExt;
                                var line    = sr2.ReadLine();
                                if (String.IsNullOrEmpty(line))
                                {
                                    continue;
                                }
                                if (line.IndexOf(keyword, StringComparison.CurrentCultureIgnoreCase) >= 0)
                                {
                                    TypeHash = line;
                                    TypeHash = TypeHash.Split(' ')[0];
                                    entrytobuild.TypeHash = TypeHash;
                                    break;
                                }
                            }
                        }
                    }
                    catch (FileNotFoundException)
                    {
                        MessageBox.Show("I cannot find and/or access archive_filetypes.cfg so I cannot finish parsing the arc.", "Oh Boy");
                        using (StreamWriter sw = File.AppendText("Log.txt"))
                        {
                            sw.WriteLine("Cannot find archive_filetypes.cfg so I cannot continue parsing the file.");
                        }
                        return;
                    }



                    var tag = node.Tag;
                    if (tag is DefaultWrapper)
                    {
                        entrytoreplace = tag as DefaultWrapper;
                    }

                    string path  = "";
                    int    index = entrytoreplace.EntryName.LastIndexOf("\\");
                    if (index > 0)
                    {
                        path = entrytoreplace.EntryName.Substring(0, index);
                    }

                    entrytobuild.EntryName = path + "\\" + entrytobuild.TrueName;

                    tag = entrytobuild;

                    if (node.Tag is DefaultWrapper)
                    {
                        node.Tag  = entrytobuild;
                        node.Name = Path.GetFileNameWithoutExtension(entrytobuild.EntryName);
                        node.Text = Path.GetFileNameWithoutExtension(entrytobuild.EntryName);
                    }

                    var aew = node as ArcEntryWrapper;

                    string type = node.GetType().ToString();
                    if (type == "ThreeWorkTool.Resources.Wrappers.ArcEntryWrapper")
                    {
                        aew.entryfile = entrytobuild;
                    }

                    node           = aew;
                    node.entryfile = entrytobuild;

                    /*
                     * //ArcEntryWrapper aew = new ArcEntryWrapper();
                     * if (node is ArcEntryWrapper)
                     * {
                     *  node.entryfile as ArcEntryWrapper = node.Tag;
                     * }
                     */
                    tree.EndUpdate();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Read error. Is the file readable?");
                using (StreamWriter sw = File.AppendText("Log.txt"))
                {
                    sw.WriteLine("Read Error! Here's the exception info:\n" + ex);
                }
            }



            //return node.entryfile as ArcEntry;
        }