private void Save_Files()
 {
     if (image.ID >= 0)
     {
         try
         {
             string imageFile = pluginHost.Get_TempFolder() + Path.DirectorySeparatorChar + Path.GetRandomFileName() + image.FileName;
             image.Write(imageFile, palette);
             pluginHost.ChangeFile(image.ID, imageFile);
         }
         catch (Exception e) { MessageBox.Show("Error writing new image:\n" + e.Message); };
     }
     if (palette.ID >= 0 && radioReplacePal.Checked)
     {
         try
         {
             string paletteFile = pluginHost.Get_TempFolder() + Path.DirectorySeparatorChar + Path.GetRandomFileName() + palette.FileName;
             palette.Write(paletteFile);
             pluginHost.ChangeFile(palette.ID, paletteFile);
         }
         catch (Exception e) { MessageBox.Show("Error writing new palette:\n" + e.Message); };
     }
     if (isMap && map.ID >= 0)
     {
         try
         {
             string mapFile = pluginHost.Get_TempFolder() + Path.DirectorySeparatorChar + Path.GetRandomFileName() + map.FileName;
             map.Write(mapFile, image, palette);
             pluginHost.ChangeFile(map.ID, mapFile);
         }
         catch (Exception e) { MessageBox.Show("Error writing new map:\n" + e.Message); };
     }
 }
        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog o = new OpenFileDialog();

            o.CheckFileExists = true;
            o.DefaultExt      = ".wav";
            o.Filter          = "WAV sound file (*.wav)|*.wav";
            o.Multiselect     = false;

            if (o.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            try {
                string imported = pluginHost.Get_TempFolder() + Path.DirectorySeparatorChar + Path.GetRandomFileName();
                soundBase.Import(o.FileName);

                byte[] enc_data = soundBase.Encode();
                soundBase.Write_File(imported, enc_data);

                wav_file = o.FileName;
                pluginHost.ChangeFile(soundBase.ID, imported);
            } catch (NotImplementedException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemplo n.º 3
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            string fileOut = pluginHost.Get_TempFolder() + Path.DirectorySeparatorChar + Path.GetRandomFileName();

            Write(fileOut);
            pluginHost.ChangeFile(id, fileOut);
        }
        private void Write()
        {
            string       fileOut = pluginHost.Get_TempFolder() + Path.DirectorySeparatorChar + Path.GetRandomFileName();
            BinaryWriter bw      = new BinaryWriter(File.OpenWrite(fileOut));

            for (int i = 0; i < us.Length; i++)
            {
                int length = Encoding.GetEncoding("shift_jis").GetByteCount(us[i].text1);
                bw.Write(Encoding.GetEncoding("shift_jis").GetBytes(us[i].text1));
                while (length != 0x10)
                {
                    bw.Write((byte)0x00);
                    length++;
                }

                length = Encoding.GetEncoding("shift_jis").GetByteCount(us[i].text2);
                bw.Write(Encoding.GetEncoding("shift_jis").GetBytes(us[i].text2));
                while (length != 0x18)
                {
                    bw.Write((byte)0x00);
                    length++;
                }

                bw.Write(us[i].unknown);
            }

            bw.Flush();
            bw.Close();
            pluginHost.ChangeFile(id, fileOut);
        }
Exemplo n.º 5
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            String fontFile = System.IO.Path.GetTempFileName();

            NFTR.Write(font, fontFile);
            pluginHost.ChangeFile(font.id, fontFile);
        }
        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog o = new OpenFileDialog();

            o.AddExtension = true;
            o.DefaultExt   = ".xml";
            o.Filter       = "eXtensive Markup Language (*.xml)|*.xml";
            o.FileName     = fileName + ".xml";
            if (o.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            itext.Import(o.FileName);

            string outFile = pluginHost.Get_TempFile();

            itext.Write(outFile);
            pluginHost.ChangeFile(id, outFile);

            o.Dispose();
            o = null;

            text             = itext.Get_Text();
            numBlock.Maximum = text.Length - 1;
            label3.Text      = "of " + numBlock.Maximum.ToString();
            numBlock_ValueChanged(null, null);
        }
Exemplo n.º 7
0
        private void Write()
        {
            string       fileOut = pluginHost.Get_TempFolder() + Path.DirectorySeparatorChar + Path.GetRandomFileName();
            BinaryWriter bw      = new BinaryWriter(File.OpenWrite(fileOut));

            for (int i = 0; i < bps.Length; i++)
            {
                bw.Write(bps[i].unknown);

                int length = enc.GetByteCount(bps[i].header_text);
                bw.Write(enc.GetBytes(bps[i].header_text));
                while (length != 0x42)
                {
                    bw.Write((byte)0x00);
                    length++;
                }

                length = enc.GetByteCount(bps[i].text);
                bw.Write(enc.GetBytes(bps[i].text));
                while (length != 0x202)
                {
                    bw.Write((byte)0x00);
                    length++;
                }

                bw.Write(bps[i].index);
            }

            bw.Flush();
            bw.Close();
            pluginHost.ChangeFile(id, fileOut);
        }
        private void btnCompress_Click(object sender, EventArgs e)
        {
            sFile  decompressedFile = pluginHost.Get_DecompressedFiles(id).files[0];
            String filein           = decompressedFile.path;
            String fileout          = System.IO.Path.GetTempFileName();

            try
            {
                Main.Compress(filein, fileout, format);
                pluginHost.ChangeFile(id, fileout);
                txtBoxNewCompress.Text = Enum.GetName(typeof(FormatCompress), format);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Console.WriteLine(ex.Message);
                txtBoxNewCompress.Text = nullString;
            }
        }
Exemplo n.º 9
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            string tempFile = Path.GetTempFileName();

            string textSave = txtBox.Text;

            textSave = textSave.Replace("\\0", "\0");
            text     = Encoding.GetEncoding(comboEncod.Text).GetBytes(textSave);
            File.WriteAllBytes(tempFile, text);
            pluginHost.ChangeFile(id, tempFile);
        }
Exemplo n.º 10
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            string text = ""; // Makes the compiler happy (siempre deseé poner eso :) )

            text = Convertir_Especiales(txtBox.Text, false);

            string tempFile = pluginHost.Get_TempFile();

            File.WriteAllText(tempFile, text);
            pluginHost.ChangeFile(id, tempFile);
        }
Exemplo n.º 11
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            string fileout = file + ".bin";

            if (File.Exists(fileout))
            {
                return;
            }

            Write(fileout);
            pluginHost.ChangeFile(id, fileout);
        }
Exemplo n.º 12
0
 private void Write_File()
 {
     if (palette.ID > 0)
     {
         try
         {
             String fileOut = pluginHost.Get_TempFile();
             palette.Write(fileOut);
             pluginHost.ChangeFile(palette.ID, fileOut);
         }
         catch (Exception ex) { MessageBox.Show("Error writing new palette:\n" + ex.Message); };
     }
 }
Exemplo n.º 13
0
        public void ChangeFile(int id, string newFilePath)
        {
            Sound newFile = new Sound();
            Sound oldFile = SearchFile(id, sdat.files.root);

            newFile.name   = oldFile.name;
            newFile.id     = (ushort)id;
            newFile.offset = 0x00;
            newFile.path   = newFilePath;
            newFile.type   = oldFile.type;
            newFile.size   = (uint)new FileInfo(newFilePath).Length;
            newFile.tag    = oldFile.tag;
            if ((String)newFile.tag == "Descomprimido")
            {
                newFile.tag = String.Format("{0:X}", newFile.size).PadLeft(8, '0') + newFile.path;
            }

            ChangeFile(id, newFile, sdat.files.root);

            if (sdat.files.root.name == "SWAV")
            {
                pluginHost.ChangeFile(sdat.id, newFile.path);
            }
        }
Exemplo n.º 14
0
        public string Pack(ref sFolder unpacked, sFile file)
        {
            string fileout = pluginHost.Get_TempFile();

            if (file.name.ToUpper().EndsWith(".PAC_") || file.name.ToUpper().EndsWith(".PAC"))
            {
                Console.WriteLine("Packing to " + fileout);
                PAC.Pack(ref unpacked, fileout);
                return(fileout);
            }
            else if (file.name.ToUpper().EndsWith(".PKB"))
            {
                sFile pkh = pluginHost.Search_File((short)(file.id + 1));

                Console.WriteLine("Packing to " + fileout);
                PKB.Pack(ref unpacked, pkh, fileout, pluginHost);
                return(fileout);
            }
            else if (file.name.ToUpper().EndsWith(".SPF_"))
            {
                SFP.Pack(ref unpacked, fileout, fileout, false);
                return(fileout);
            }
            else if (file.name.ToUpper().EndsWith(".SPD"))
            {
                string spl_out = pluginHost.Get_TempFile();
                SFP.Pack(ref unpacked, spl_out, fileout, true);

                pluginHost.ChangeFile(file.id + 1, spl_out);    // Change SPL file
                return(fileout);                                // Return new SPD file
            }

            if (gameCode == "BOEJ")
            {
                if (file.id == 0x110)
                {
                    Encryption.Encrypt(unpacked.files[0].path, 0x2C, fileout);
                }
                else if (file.id == 0x18A)
                {
                    Encryption.Encrypt(unpacked.files[0].path, 0x48, fileout);
                }
                return(fileout);
            }

            return(null);
        }
        public void Write()
        {
            string       fileOut = pluginHost.Get_TempFolder() + Path.DirectorySeparatorChar + Path.GetRandomFileName();
            BinaryWriter bw      = new BinaryWriter(File.OpenWrite(fileOut));

            for (int i = 0; i < subsNew.Count; i++)
            {
                bw.Write(subsNew[i].time_start);
                bw.Write(subsNew[i].time_end);

                uint length = (uint)Encoding.GetEncoding("shift_jis").GetByteCount(subsNew[i].text);
                length += 4 - (length % 4);
                bw.Write(length);
                bw.Write(Encoding.GetEncoding("shift_jis").GetBytes(subsNew[i].text));
                bw.Write(new byte[4 - (bw.BaseStream.Position % 4)]);
            }
            bw.Write(0xFFFFFFFF);

            bw.Flush();
            bw.Close();

            pluginHost.ChangeFile(id, fileOut);
        }
Exemplo n.º 16
0
        void Compile_Events(string path)
        {
            string fout;
            string fin;

            string mainQuest = path + "MainQuest.xml";

            if (File.Exists(mainQuest))
            {
                fout = pluginHost.Get_TempFile();
                fin  = pluginHost.Search_File(0xF76);
                MQuestText mqc = new MQuestText(pluginHost, fin, 0xF76);
                mqc.Import(mainQuest);
                mqc.Write(fout);
                pluginHost.ChangeFile(0xF76, fout);
            }

            if (Directory.Exists(path + "SubQuest"))
            {
                string[] sub  = Directory.GetFiles(path + "SubQuest", "*.xml", SearchOption.TopDirectoryOnly);
                sFolder  subf = pluginHost.Search_Folder(0xF07E);
                for (int i = 0; i < subf.files.Count; i++)
                {
                    string cfile = Array.Find(sub, a => Path.GetFileNameWithoutExtension(a) == subf.files[i].name);
                    if (cfile == null)
                    {
                        continue;
                    }

                    string tempsub = Save_File(subf.files[i]);
                    fout = pluginHost.Get_TempFile();

                    SQcontrol sqc = new SQcontrol(pluginHost, tempsub, subf.files[i].id);
                    SQ        sq  = sqc.Read(tempsub);
                    sqc.Import_XML(cfile, ref sq);
                    sqc.Write(fout, sq);

                    pluginHost.ChangeFile(subf.files[i].id, fout);
                }
            }

            string scenario = path + "Scenario.xml";

            if (File.Exists(scenario))
            {
                fout = pluginHost.Get_TempFile();
                fin  = pluginHost.Search_File(0xFF2);
                ScenarioText st = new ScenarioText(pluginHost, fin, 0xFF2);
                st.Import(scenario);
                st.Write(fout);
                pluginHost.ChangeFile(0xFF2, fout);
            }

            string system = path + "System.xml";

            if (File.Exists(system))
            {
                fout = pluginHost.Get_TempFile();
                fin  = pluginHost.Search_File(0xFF4);
                SystemText st = new SystemText(fin, 0xFF4, pluginHost);
                st.Import(system);
                st.Write(fout);
                pluginHost.ChangeFile(0xFF4, fout);
            }
        }
Exemplo n.º 17
0
        public static void Pack_PKH2(ref sFolder unpacked, sFile pkh, string fileOut, IPluginHost pluginHost)
        {
            string fileOut2 = pluginHost.Get_TempFile();

            BinaryReader br = new BinaryReader(File.OpenRead(pkh.path));
            BinaryWriter bwPkb = new BinaryWriter(File.OpenWrite(fileOut));
            BinaryWriter bwPkh = new BinaryWriter(File.OpenWrite(fileOut2));

            int num_files = (int)br.BaseStream.Length / 0x10;

            // Get the PKH info
            uint[,] info = new uint[num_files, 5];
            for (int i = 0; i < num_files; i++)
            {
                info[i, 2] = br.ReadUInt32();   // ID
                info[i, 0] = br.ReadUInt32();   // old offset, used to get the file order
                info[i, 1] = br.ReadUInt32();   // old size
                br.BaseStream.Position += 4;    // Skip LZSS header
            }
            br.Close();
            br = null;

            File_Order2(ref info);  // Get the file order

            // Create PKB file, we write it in order
            uint offset = 0;
            for (int i = 0; i < num_files; i++)
            {
                // Get the index of the next file to write
                int index = 0;
                for (; index < num_files; index++)
                    if (info[index, 4] == i)
                        break;
                if (info[index, 4] != i)
                    Console.WriteLine("Error searching index file!");

                sFile cfile = unpacked.files[index];

                // Write PKB file
                BinaryReader br_file = new BinaryReader(File.OpenRead(cfile.path));
                br_file.BaseStream.Position = cfile.offset;

                bwPkb.Write(br_file.ReadBytes((int)cfile.size));        //Write File in new PKB
                while (bwPkb.BaseStream.Position % 0x10 != 0)           // Padding
                    bwPkb.Write((byte)0xFF);
                bwPkb.Flush();

                // Get LZSS header
                br_file.BaseStream.Position = cfile.offset;
                info[index, 3] = br_file.ReadUInt32();
                if ((info[index, 3] & 0xFF) != 0x10 || ((info[index, 3] >> 8) == 0))         // File no compressed, it can be a PAC file
                    info[index, 3] = 0;

                br_file.Close();
                br_file = null;

                // Set new values
                cfile.offset = offset;
                cfile.path = fileOut;
                offset += cfile.size;
                if (offset % 0x10 != 0)                 // Padding offset
                    offset += 0x10 - (offset % 0x10);

                unpacked.files[index] = cfile;
            }

            for (int i = 0; i < num_files; i++)
            {
                // Write PKH file
                bwPkh.Write(info[i, 2]);           // Unknown ID
                bwPkh.Write(unpacked.files[i].offset);
                bwPkh.Write(unpacked.files[i].size);
                bwPkh.Write(info[i, 3]);
            }

            // The file is padded
            while (bwPkh.BaseStream.Position % 0x10 != 0)
                bwPkh.Write((byte)0xFF);

            // Flush and close files
            bwPkh.Flush();

            bwPkh.Close();
            bwPkb.Close();

            pluginHost.ChangeFile(pkh.id, fileOut2);
        }
Exemplo n.º 18
0
        public static void Pack_PKH1(ref sFolder unpacked, sFile pkh, string fileOut, IPluginHost pluginHost)
        {
            string pkh_out = pluginHost.Get_TempFile();

            BinaryReader br = new BinaryReader(File.OpenRead(pkh.path));
            BinaryWriter bwPkb = new BinaryWriter(File.OpenWrite(fileOut));
            BinaryWriter bwPkh = new BinaryWriter(File.OpenWrite(pkh_out));

            // Write header
            bwPkh.Write(Encoding.ASCII.GetChars(br.ReadBytes(16))); // Header
            bwPkh.Write(br.ReadUInt16());                           //File_Size
            ushort type = br.ReadUInt16();
            bwPkh.Write(type);                                      //Type
            bwPkh.Write(br.ReadUInt16());                           //Unknown
            ushort num_files = br.ReadUInt16();
            bwPkh.Write(num_files);                                 //Num_files
            bwPkh.Write(br.ReadUInt32());                           //Unknown

            if (type == 0)
                bwPkh.Write(br.ReadUInt32());                       //Block_Length
            else if (type == 2 || type == 3)
            {
                bwPkh.Write(unpacked.files[0].size);
                br.BaseStream.Position += 4;
            }

            bwPkh.Write(br.ReadBytes(0x10));                        // Unknown, usually 0x00 but not always

            uint offset = 0;
            for (int i = 0; i < num_files; i++)
            {
                BinaryReader br_file = new BinaryReader(File.OpenRead(unpacked.files[i].path));
                br_file.BaseStream.Position = unpacked.files[i].offset;
                bwPkb.Write(br_file.ReadBytes((int)unpacked.files[i].size));  //Write File in new PKB
                while (bwPkb.BaseStream.Position % 0x10 != 0)       // Padding
                    bwPkb.Write((byte)0xFF);
                br_file.Close();
                bwPkb.Flush();

                if (type == 0)
                {
                    bwPkh.Write(br.ReadUInt32());           // Unknown, ID¿?
                    bwPkh.Write(offset);                    // Offset
                    bwPkh.Write(unpacked.files[i].size);    //Size File

                    sFile cfile = unpacked.files[i];
                    cfile.offset = offset;
                    cfile.path = fileOut;
                    unpacked.files[i] = cfile;

                    br.BaseStream.Position += 8;
                    offset += unpacked.files[i].size;
                    if (offset % 0x10 != 0)
                        offset += 0x10 - (offset % 0x10);
                }
                else if (type == 2)
                    bwPkh.Write(br.ReadUInt64());
                else if (type == 3)
                    bwPkh.Write(br.ReadUInt32());
            }

            while (bwPkh.BaseStream.Position % 0x10 != 0)
                bwPkh.Write((byte)0xFF);

            bwPkh.Flush();

            bwPkb.Close();
            bwPkh.Close();

            br.Close();

            pluginHost.ChangeFile(pkh.id, pkh_out);
        }
Exemplo n.º 19
0
        public static void Pack_PKH1(ref sFolder unpacked, sFile pkh, string fileOut, IPluginHost pluginHost)
        {
            string pkh_out = pluginHost.Get_TempFile();

            BinaryReader br    = new BinaryReader(File.OpenRead(pkh.path));
            BinaryWriter bwPkb = new BinaryWriter(File.OpenWrite(fileOut));
            BinaryWriter bwPkh = new BinaryWriter(File.OpenWrite(pkh_out));

            // Write header
            bwPkh.Write(Encoding.ASCII.GetChars(br.ReadBytes(16))); // Header
            bwPkh.Write(br.ReadUInt16());                           //File_Size
            ushort type = br.ReadUInt16();

            bwPkh.Write(type);                                      //Type
            bwPkh.Write(br.ReadUInt16());                           //Unknown
            ushort num_files = br.ReadUInt16();

            bwPkh.Write(num_files);                                 //Num_files
            bwPkh.Write(br.ReadUInt32());                           //Unknown

            if (type == 0)
            {
                bwPkh.Write(br.ReadUInt32());                       //Block_Length
            }
            else if (type == 2 || type == 3)
            {
                bwPkh.Write(unpacked.files[0].size);
                br.BaseStream.Position += 4;
            }

            bwPkh.Write(br.ReadBytes(0x10));                        // Unknown, usually 0x00 but not always

            uint offset = 0;

            for (int i = 0; i < num_files; i++)
            {
                BinaryReader br_file = new BinaryReader(File.OpenRead(unpacked.files[i].path));
                br_file.BaseStream.Position = unpacked.files[i].offset;
                bwPkb.Write(br_file.ReadBytes((int)unpacked.files[i].size)); //Write File in new PKB
                while (bwPkb.BaseStream.Position % 0x10 != 0)                // Padding
                {
                    bwPkb.Write((byte)0xFF);
                }
                br_file.Close();
                bwPkb.Flush();

                if (type == 0)
                {
                    bwPkh.Write(br.ReadUInt32());           // Unknown, ID¿?
                    bwPkh.Write(offset);                    // Offset
                    bwPkh.Write(unpacked.files[i].size);    //Size File

                    sFile cfile = unpacked.files[i];
                    cfile.offset      = offset;
                    cfile.path        = fileOut;
                    unpacked.files[i] = cfile;

                    br.BaseStream.Position += 8;
                    offset += unpacked.files[i].size;
                    if (offset % 0x10 != 0)
                    {
                        offset += 0x10 - (offset % 0x10);
                    }
                }
                else if (type == 2)
                {
                    bwPkh.Write(br.ReadUInt64());
                }
                else if (type == 3)
                {
                    bwPkh.Write(br.ReadUInt32());
                }
            }

            while (bwPkh.BaseStream.Position % 0x10 != 0)
            {
                bwPkh.Write((byte)0xFF);
            }

            bwPkh.Flush();

            bwPkb.Close();
            bwPkh.Close();

            br.Close();

            pluginHost.ChangeFile(pkh.id, pkh_out);
        }
Exemplo n.º 20
0
        void Save_Files()
        {
            if (sprite.ID >= 0)
            {
                try
                {
                    string spriteFile = "";
                    if (pluginHost is IPluginHost)
                    {
                        spriteFile = pluginHost.Get_TempFolder() + Path.DirectorySeparatorChar + Path.GetRandomFileName() + sprite.FileName;
                    }
                    else
                    {
                        SaveFileDialog o = new SaveFileDialog();
                        o.Title    = "Save OAMs";
                        o.FileName = sprite.FileName;
                        if (o.ShowDialog() != DialogResult.OK)
                        {
                            return;
                        }
                        spriteFile = o.FileName;
                    }

                    sprite.Write(spriteFile, image, palette);

                    if (pluginHost is IPluginHost)
                    {
                        pluginHost.ChangeFile(sprite.ID, spriteFile);
                    }
                }
                catch (Exception e) { MessageBox.Show("Error writing new sprite:\n" + e.Message); };
            }
            if (image.ID >= 0)
            {
                try
                {
                    string imageFile = "";
                    if (pluginHost is IPluginHost)
                    {
                        imageFile = pluginHost.Get_TempFolder() + Path.DirectorySeparatorChar + Path.GetRandomFileName() + image.FileName;
                    }
                    else
                    {
                        SaveFileDialog o = new SaveFileDialog();
                        o.Title    = "Save image";
                        o.FileName = image.FileName;
                        if (o.ShowDialog() != DialogResult.OK)
                        {
                            return;
                        }
                        imageFile = o.FileName;
                    }

                    image.Write(imageFile, palette);

                    if (pluginHost is IPluginHost)
                    {
                        pluginHost.ChangeFile(image.ID, imageFile);
                    }
                }
                catch (Exception e) { MessageBox.Show("Error writing new image:\n" + e.Message); };
            }
            if (radioReplacePal.Checked && palette.ID >= 0)
            {
                try
                {
                    string paletteFile = "";
                    if (pluginHost is IPluginHost)
                    {
                        paletteFile = pluginHost.Get_TempFolder() + Path.DirectorySeparatorChar + Path.GetRandomFileName() + palette.FileName;
                    }
                    else
                    {
                        SaveFileDialog o = new SaveFileDialog();
                        o.Title    = "Save palette";
                        o.FileName = palette.FileName;
                        if (o.ShowDialog() != DialogResult.OK)
                        {
                            return;
                        }
                        paletteFile = o.FileName;
                    }

                    palette.Write(paletteFile);

                    if (pluginHost is IPluginHost)
                    {
                        pluginHost.ChangeFile(palette.ID, paletteFile);
                    }
                }
                catch (Exception e) { MessageBox.Show("Error writing new palette:\n" + e.Message); };
            }
        }
Exemplo n.º 21
0
        public static void Pack_PKH2(ref sFolder unpacked, sFile pkh, string fileOut, IPluginHost pluginHost)
        {
            string fileOut2 = pluginHost.Get_TempFile();

            BinaryReader br    = new BinaryReader(File.OpenRead(pkh.path));
            BinaryWriter bwPkb = new BinaryWriter(File.OpenWrite(fileOut));
            BinaryWriter bwPkh = new BinaryWriter(File.OpenWrite(fileOut2));

            int num_files = (int)br.BaseStream.Length / 0x10;

            // Get the PKH info
            uint[,] info = new uint[num_files, 5];
            for (int i = 0; i < num_files; i++)
            {
                info[i, 2]              = br.ReadUInt32(); // ID
                info[i, 0]              = br.ReadUInt32(); // old offset, used to get the file order
                info[i, 1]              = br.ReadUInt32(); // old size
                br.BaseStream.Position += 4;               // Skip LZSS header
            }
            br.Close();
            br = null;

            File_Order2(ref info);  // Get the file order

            // Create PKB file, we write it in order
            uint offset = 0;

            for (int i = 0; i < num_files; i++)
            {
                // Get the index of the next file to write
                int index = 0;
                for (; index < num_files; index++)
                {
                    if (info[index, 4] == i)
                    {
                        break;
                    }
                }
                if (info[index, 4] != i)
                {
                    Console.WriteLine("Error searching index file!");
                }

                sFile cfile = unpacked.files[index];

                // Write PKB file
                BinaryReader br_file = new BinaryReader(File.OpenRead(cfile.path));
                br_file.BaseStream.Position = cfile.offset;

                bwPkb.Write(br_file.ReadBytes((int)cfile.size));        //Write File in new PKB
                while (bwPkb.BaseStream.Position % 0x10 != 0)           // Padding
                {
                    bwPkb.Write((byte)0xFF);
                }
                bwPkb.Flush();

                // Get LZSS header
                br_file.BaseStream.Position = cfile.offset;
                info[index, 3] = br_file.ReadUInt32();
                if ((info[index, 3] & 0xFF) != 0x10 || ((info[index, 3] >> 8) == 0))         // File no compressed, it can be a PAC file
                {
                    info[index, 3] = 0;
                }

                br_file.Close();
                br_file = null;

                // Set new values
                cfile.offset = offset;
                cfile.path   = fileOut;
                offset      += cfile.size;
                if (offset % 0x10 != 0)                 // Padding offset
                {
                    offset += 0x10 - (offset % 0x10);
                }

                unpacked.files[index] = cfile;
            }

            for (int i = 0; i < num_files; i++)
            {
                // Write PKH file
                bwPkh.Write(info[i, 2]);           // Unknown ID
                bwPkh.Write(unpacked.files[i].offset);
                bwPkh.Write(unpacked.files[i].size);
                bwPkh.Write(info[i, 3]);
            }

            // The file is padded
            while (bwPkh.BaseStream.Position % 0x10 != 0)
            {
                bwPkh.Write((byte)0xFF);
            }

            // Flush and close files
            bwPkh.Flush();

            bwPkh.Close();
            bwPkb.Close();

            pluginHost.ChangeFile(pkh.id, fileOut2);
        }