示例#1
0
        private void B_Patch_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            //Setup Paths
            UIUsage(false);
            string ModPath   = TB_InPath.Text;
            string PatchPath = TB_OriPath.Text;

            if (PatchPath.Length == 0 || PatchPath == null || ModPath.Length == 0 || ModPath == null)
            {
                AddLine(richTextBox1, "Invaid Folders");
                UIUsage(true);
                return;
            }
            string PatchNum = FindPatch(PatchPath);

            if (PatchNum == null)
            {
                AddLine(richTextBox1, "Invaid Original Patch Folder");
                UIUsage(true);
                return;
            }
            string info0file = ModPath + Path.DirectorySeparatorChar + PatchNum + Path.DirectorySeparatorChar + "INFO0.bin";

            NewEntryList = new List <INFO0.INFO0Entry>();

            //detele existing files
            if (File.Exists(ModPath + Path.DirectorySeparatorChar + PatchNum + Path.DirectorySeparatorChar + "INFO0.bin"))
            {
                File.Delete(ModPath + Path.DirectorySeparatorChar + PatchNum + Path.DirectorySeparatorChar + "INFO0.bin");
            }
            if (File.Exists(ModPath + Path.DirectorySeparatorChar + PatchNum + Path.DirectorySeparatorChar + "INFO2.bin"))
            {
                File.Delete(ModPath + Path.DirectorySeparatorChar + PatchNum + Path.DirectorySeparatorChar + "INFO2.bin");
            }

            //Load Info
            infofile = new INFO0.Info0();
            infofile.ReadData(PatchPath + Path.DirectorySeparatorChar + PatchNum + Path.DirectorySeparatorChar + "INFO0.bin");
            if (CB_Log.Checked)
            {
                AddLine(richTextBox1, $"Original Patch Entry Count: {infofile.numOfEntries}");
            }

            //Load modded files
            string[] files = Directory.GetFiles(ModPath, "*", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                long   entryid;
                string newfile = file.Replace(ModPath, "rom:").Replace("\\", "/");
                try
                {
                    if (Path.GetFileName(newfile).Contains('-'))
                    {
                        entryid = Convert.ToInt64(Path.GetFileName(newfile).Split('-')[0].Replace(" ", ""));
                    }
                    else
                    {
                        entryid = Convert.ToInt64(Path.GetFileName(newfile).Split('.')[0].Replace(" ", ""));
                    }
                    Console.WriteLine();
                }
                catch (FormatException)
                {
                    if (CB_Log.Checked)
                    {
                        AddLine(richTextBox1, $"File Found with no ID: {newfile}");
                    }
                    continue;
                }
                if (CB_Log.Checked)
                {
                    AddLine(richTextBox1, $"File Found: {newfile}");
                }

                //Write new Entry
                byte[] bfile = File.ReadAllBytes(file);
                if (Bin.GetMagic(bfile) == ".gz")
                {
                    byte[] dec = GZip.Decompress(File.ReadAllBytes(file));
                    NewEntry = new INFO0.INFO0Entry()
                    {
                        EntryID = entryid, CompressedSize = bfile.Length, DecompressedSize = dec.Length, IsCompressed = 1, FileName = newfile
                    };
                    if (CB_Log.Checked)
                    {
                        AddLine(richTextBox1, $"Created Compressed Entry: {entryid}");
                    }
                }
                else
                {
                    NewEntry = new INFO0.INFO0Entry()
                    {
                        EntryID = entryid, CompressedSize = bfile.Length, DecompressedSize = bfile.Length, IsCompressed = 0, FileName = newfile
                    };
                    if (CB_Log.Checked)
                    {
                        AddLine(richTextBox1, $"Created Decompressed Entry: {entryid}");
                    }
                }
                if (!CB_Log.Checked)
                {
                    AddLine(richTextBox1, $"Added {entryid}");
                }
                NewEntryList.Add(NewEntry);
            }

            //Check for existing entries and remove is needed
            if (CB_Log.Checked)
            {
                AddLine(richTextBox1, $"Removing dupicates and sorting list");
            }
            foreach (var entry in NewEntryList)
            {
                long id = entry.EntryID;
                if (infofile.EntryIDs.Contains(id))
                {
                    int index = infofile.EntryIDs.FindIndex(x => x == id);
                    infofile.INFO0Entries.RemoveAt(index);
                    infofile.EntryIDs.RemoveAt(index);
                }
                infofile.INFO0Entries.Add(entry);
            }
            //Sort entires based on IDs
            List <INFO0.INFO0Entry> finallist = infofile.INFO0Entries.OrderBy(o => o.EntryID).ToList();

            //Write Each entry to the info0
            if (!Directory.Exists($"{ModPath}\\{PatchNum}"))
            {
                Directory.CreateDirectory($"{ModPath}\\{PatchNum}");
            }
            using (EndianBinaryWriter bw = new EndianBinaryWriter(File.Create($"{ModPath}\\{PatchNum}\\INFO0.bin"), Endianness.Little))
            {
                if (CB_Log.Checked)
                {
                    AddLine(richTextBox1, $"Writing INFO0.bin...");
                }
                foreach (var item in finallist)
                {
                    item.Write(bw);
                }
            }

            //Write Info2 file
            if (!Directory.Exists($"{ModPath}\\{PatchNum}"))
            {
                Directory.CreateDirectory($"{ModPath}\\{PatchNum}");
            }
            using (EndianBinaryWriter bw = new EndianBinaryWriter(File.Create($"{ModPath}\\{PatchNum}\\INFO2.bin"), Endianness.Little))
            {
                if (CB_Log.Checked)
                {
                    AddLine(richTextBox1, $"Writing INFO2.bin...");
                }
                bw.WriteInt64(infofile.INFO0Entries.Count);
                bw.WriteInt64(4062);
            }

            AddLine(richTextBox1, $"Done");
            UIUsage(true);
        }
示例#2
0
        private void Open(string path)
        {
            bool usegust = false;

            if (Settings.settingsInfo.GustPath != "")
            {
                usegust = true;
            }
            if (Path.GetFileName(path) == "INFO0.bin")
            {
                infofile = new INFO0.Info0();
                infofile.ReadData(path);
                AddLine(RTB_Output, Convert.ToString(infofile.INFO0Entries[1].FileName));
                return;
            }
            if (Directory.Exists(path))
            {
                if (ModifierKeys == Keys.Control || ModifierKeys == Keys.Shift)
                {
                    string outfile = "";
                    if (ModifierKeys == Keys.Shift)
                    {
                        outfile = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + Path.GetFileName(path) + ".bin.gz";
                    }
                    else
                    {
                        outfile = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + Path.GetFileName(path) + ".bin";
                    }
                    Bin.FileList = new List <byte[]>();
                    List <string> list  = new List <string>();
                    string[]      files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
                    foreach (string file in files)
                    {
                        string filename = Path.GetFileName(file);
                        list.Add(filename);
                    }
                    try
                    {
                        list = list.OrderBy(x => int.Parse(x.Split('.')[0])).ToList();
                    }
                    catch (FormatException ex)
                    {
                        // File names big borked
                        AddLine(RTB_Output, string.Format("Unable to pack {0}. Filename Error", Path.GetFileName(path)));
                        Console.WriteLine(ex.Message);
                        return;
                    }
                    foreach (string file in list)
                    {
                        byte[] filetoadd = File.ReadAllBytes(path + Path.DirectorySeparatorChar + file);
                        Bin.FileList.Add(filetoadd);
                    }
                    Bin.Build(Bin.FileList, outfile);
                    if (ModifierKeys == Keys.Shift)
                    {
                        AddLine(RTB_Output, string.Format("Packed {0} to {1}", Path.GetFileName(path), Path.GetFileName(path) + ".bin.gz"));
                    }
                    else
                    {
                        AddLine(RTB_Output, string.Format("Packed {0} to {1}", Path.GetFileName(path), Path.GetFileName(path) + ".bin"));
                    }
                }
                else if (ModifierKeys == Keys.Alt && usegust == true)
                {
                    GustG1t(path);
                    AddLine(RTB_Output, string.Format("Successfully built g1t from {0}.", Path.GetFileName(path)));
                }
            }
            if (File.Exists(path))
            {
                byte[] infile   = File.ReadAllBytes(path);
                string magic    = Bin.GetMagic(infile);
                string ext      = Path.GetExtension(path).ToLower();
                string filename = Path.GetFileNameWithoutExtension(path).ToLower();

                if (ModifierKeys == Keys.Control || B_BatchCompress.Checked)
                {
                    File.WriteAllBytes(path + ".gz", GZip.Compress(File.ReadAllBytes(path)));
                    AddLine(RTB_Output, string.Format("KTGZ compressed {0} to {1}", Path.GetFileName(path), Path.GetFileName(path) + ".gz"));
                }
                else if (magic == ".gz")
                {
                    try
                    {
                        byte[] dec    = GZip.Decompress(File.ReadAllBytes(path));
                        string decext = Bin.GetMagic(dec);
                        string decpath;
                        if (decext == ".bin")
                        {
                            decpath = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(path);
                        }
                        else
                        {
                            decpath = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(path) + decext;
                        }
                        File.WriteAllBytes(decpath, dec);
                        AddLine(RTB_Output, string.Format("Successfully decompressed {0}.", Path.GetFileName(decpath)));
                    }
                    catch (Exception ex)
                    {
                        AddLine(RTB_Output, string.Format("Unable to automatically decompress {0}.", Path.GetFileName(path)));
                        Console.WriteLine(ex.Message);
                    }
                }
                else if (magic == ".bin" || ext == ".datatable")
                {
                    string decpath = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(path);
                    Bin.Extract(path, decpath);
                    AddLine(RTB_Output, string.Format("Successfully extracted {0}.", Path.GetFileName(path)));
                }

                /* On hold until I fix the arugment issue
                 * else if (magic == ".g1t" && usegust == true)
                 * {
                 *  AddLine(RTB_Output, string.Format($"gust path: {Settings.settingsInfo.GustPath + Path.DirectorySeparatorChar + "gust_g1t.exe"}"));
                 *  GustG1t(path);
                 *  AddLine(RTB_Output, string.Format("Successfully extracted textures from {0}.", Path.GetFileName(path)));
                 * }
                 */
                if (B_DeleteAfterProcessing.Checked)
                {
                    File.Delete(path);
                }
            }
        }