Exemplo n.º 1
0
        void HeaderReader(int HeaderLenght, ref FARCFile[] Files, ref KKtIO reader)
        {
            if (Files == null)
            {
                int  Count    = 0;
                long Position = reader.BaseStream.Position;
                while (reader.BaseStream.Position < HeaderLenght)
                {
                    reader.NullTerminated(0x00);
                    reader.ReadInt32();
                    if (Signature != Farc.FArc)
                    {
                        reader.ReadInt32();
                    }
                    reader.ReadInt32();
                    if (Signature == Farc.FARC && FT)
                    {
                        reader.ReadInt32();
                    }
                    Count++;
                }
                reader.Seek(Position, 0);
                Files = new FARCFile[Count];
            }

            int LocalMode = 0;

            for (int i = 0; i < Files.Length; i++)
            {
                Files[i].Name   = Text.ToUTF8(reader.NullTerminated(0x00));
                Files[i].Offset = reader.ReadInt32Endian(true);
                if (Signature != Farc.FArc)
                {
                    Files[i].SizeComp = reader.ReadInt32Endian(true);
                }
                Files[i].SizeUnc = reader.ReadInt32Endian(true);
                if (Signature == Farc.FARC && FT)
                {
                    LocalMode     = reader.ReadInt32Endian(true);
                    Files[i].GZip = (LocalMode & 2) == 2;
                    Files[i].ECB  = (LocalMode & 4) == 4;
                }
            }
        }
Exemplo n.º 2
0
        public static void Decrypt(int I, string file)
        {
            KKtIO reader = KKtIO.OpenReader(file);

            if (reader.ReadInt64() != 0x454C494641564944)
            {
                reader.Close();
                Encrypt(I, file);
            }
            else
            {
                Console.Title = "PD_Tool: DIVAFILE Decryptor - File: " + Path.GetFileName(file);
                reader.ReadUInt32();
                int    StreamLenght = (int)reader.Length;
                int    FileLenght   = reader.ReadInt32();
                byte[] decrypted    = new byte[StreamLenght];
                reader.Seek(0, 0);
                using (AesManaged crypto = new AesManaged())
                {
                    crypto.Key     = Key;
                    crypto.IV      = new byte[16];
                    crypto.Mode    = CipherMode.ECB;
                    crypto.Padding = PaddingMode.Zeros;
                    using (CryptoStream cryptoData = new CryptoStream(reader.BaseStream,
                                                                      crypto.CreateDecryptor(crypto.Key, crypto.IV), CryptoStreamMode.Read))
                        cryptoData.Read(decrypted, 0, StreamLenght);
                }
                KKtIO writer = KKtIO.OpenWriter(file, FileLenght);
                for (int i = 0x10; i < StreamLenght && i < FileLenght + 0x10; i++)
                {
                    writer.Write(decrypted[i]);
                }
                writer.Close();
            }
            Console.Title = "PD_Tool";
        }
Exemplo n.º 3
0
        public int DEXReader()
        {
            KKtIO reader = KKtIO.OpenReader(filepath + ext);

            Header = new KKtMain.Header();

            Header.Format    = KKtMain.Format.F;
            Header.Signature = reader.ReadInt32();
            if (Header.Signature == 0x43505845)
            {
                Header = reader.ReadHeader(true);
            }
            if (Header.Signature != 0x64)
            {
                return(0);
            }

            Offset = reader.Position - 0x4;
            DEX    = new EXP[reader.ReadInt32()];
            int DEXOffset = reader.ReadInt32();

            if (reader.ReadInt32() == 0x00)
            {
                Header.Format = KKtMain.Format.X;
            }

            reader.Seek(DEXOffset + Offset, 0);
            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                DEX[i0] = new EXP {
                    Main = new List <EXPElement>(), Eyes = new List <EXPElement>()
                }
            }
            ;

            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                DEX[i0].MainOffset = reader.ReadInt32();
                if (Header.Format == KKtMain.Format.X)
                {
                    reader.ReadInt32();
                }
                DEX[i0].EyesOffset = reader.ReadInt32();
                if (Header.Format == KKtMain.Format.X)
                {
                    reader.ReadInt32();
                }
            }
            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                DEX[i0].NameOffset = reader.ReadInt32();
                if (Header.Format == KKtMain.Format.X)
                {
                    reader.ReadInt32();
                }
            }

            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                EXPElement element = new EXPElement();
                reader.Seek(DEX[i0].MainOffset + Offset, 0);
                while (true)
                {
                    element.Frame = reader.ReadSingle();
                    element.Both  = reader.ReadUInt16();
                    element.ID    = reader.ReadUInt16();
                    element.Value = reader.ReadSingle();
                    element.Trans = reader.ReadSingle();
                    DEX[i0].Main.Add(element);

                    if (element.Frame == 999999 || element.Both == 0xFFFF)
                    {
                        break;
                    }
                }

                reader.Seek(DEX[i0].EyesOffset + Offset, 0);
                while (true)
                {
                    element.Frame = reader.ReadSingle();
                    element.Both  = reader.ReadUInt16();
                    element.ID    = reader.ReadUInt16();
                    element.Value = reader.ReadSingle();
                    element.Trans = reader.ReadSingle();
                    DEX[i0].Eyes.Add(element);

                    if (element.Frame == 999999 || element.Both == 0xFFFF)
                    {
                        break;
                    }
                }

                reader.Seek(DEX[i0].NameOffset + Offset, 0);
                DEX[i0].Name = KKtText.ToUTF8(reader.NullTerminated());
            }

            reader.Close();
            return(1);
        }
Exemplo n.º 4
0
        public void DEXWriter()
        {
            KKtIO writer = KKtIO.OpenWriter(filepath + (Header.Format > KKtMain.Format.F ? ".dex" : ".bin"), true);

            writer.Format = Header.Format;

            if (writer.Format > KKtMain.Format.F)
            {
                Header.Lenght      = 0x20;
                Header.DataSize    = 0x00;
                Header.Signature   = 0x43505845;
                Header.SectionSize = 0x00;
                writer.Write(Header);
            }

            writer.Write(0x64);
            writer.Write(DEX.Length);
            if (Header.Format == KKtMain.Format.X)
            {
                writer.Write(0x28);
                writer.Write(0x00);
                writer.Write(0x18 + (int)KKtMain.Align(DEX.Length * 3 * 8, 0x10));
                writer.Write(0x00);
                writer.Seek(KKtMain.Align(DEX.Length * 3 * 8, 0x10) + 0x20 + Header.Lenght, 0);
            }
            else
            {
                writer.Write(0x20);
                writer.Write(0x10 + (int)KKtMain.Align(DEX.Length * 3 * 4, 0x10));
                writer.Seek(KKtMain.Align(DEX.Length * 3 * 4, 0x10) + 0x30 + Header.Lenght, 0);
            }
            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                DEX[i0].MainOffset = writer.Position - Header.Lenght;
                for (int i1 = 0; i1 < DEX[i0].Main.Count; i1++)
                {
                    writer.Write((float)DEX[i0].Main[i1].Frame);
                    writer.Write(DEX[i0].Main[i1].Both);
                    writer.Write(DEX[i0].Main[i1].ID);
                    writer.Write((float)DEX[i0].Main[i1].Value);
                    writer.Write((float)DEX[i0].Main[i1].Trans);
                }
                writer.Align(0x20, true);

                DEX[i0].EyesOffset = writer.Position - Header.Lenght;
                for (int i1 = 0; i1 < DEX[i0].Eyes.Count; i1++)
                {
                    writer.Write((float)DEX[i0].Eyes[i1].Frame);
                    writer.Write(DEX[i0].Eyes[i1].Both);
                    writer.Write(DEX[i0].Eyes[i1].ID);
                    writer.Write((float)DEX[i0].Eyes[i1].Value);
                    writer.Write((float)DEX[i0].Eyes[i1].Trans);
                }
                writer.Align(0x20, true);
            }
            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                DEX[i0].NameOffset = writer.Position - Header.Lenght;
                writer.Write(DEX[i0].Name + "\0");
            }
            writer.Align(0x10, true);

            if (Header.Format == KKtMain.Format.X)
            {
                writer.Seek(Header.Lenght + 0x28, 0);
            }
            else
            {
                writer.Seek(Header.Lenght + 0x20, 0);
            }
            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                writer.Write(DEX[i0].MainOffset);
                if (Header.Format == KKtMain.Format.X)
                {
                    writer.Write(0x00);
                }
                writer.Write(DEX[i0].EyesOffset);
                if (Header.Format == KKtMain.Format.X)
                {
                    writer.Write(0x00);
                }
            }
            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                writer.Write(DEX[i0].NameOffset);
                if (Header.Format == KKtMain.Format.X)
                {
                    writer.Write(0x00);
                }
            }

            if (writer.Format > KKtMain.Format.F)
            {
                Offset = writer.Length - Header.Lenght;
                writer.Seek(writer.Length, 0);
                writer.WriteEOFC(0);
                writer.Seek(0, 0);
                Header.DataSize    = Offset;
                Header.SectionSize = Offset;
                writer.Write(Header);
            }
            writer.Close();
        }
Exemplo n.º 5
0
        public void Pack(string file)
        {
            Files = null;
            FT    = false;
            string[] files = Directory.GetFiles(file);
            Files = new FARCFile[files.Length];

            for (int i = 0; i < files.Length; i++)
            {
                Files[i] = new FARCFile {
                    Name = files[i]
                };
                string ext = Path.GetExtension(files[i]).ToLower();
                if (ext == ".a3da" || ext == ".mot" || ext == ".vag")
                {
                    Signature = Farc.FArc;
                }
            }
            files = null;

            KKtIO writer = KKtIO.OpenWriter(file + ".farc", true);

            if (Signature == Farc.FArc)
            {
                writer.Write(Text.ToASCII("FArc"));
            }
            else if (Signature == Farc.FArC)
            {
                writer.Write(Text.ToASCII("FArC"));
            }
            else
            {
                writer.Write(Text.ToASCII("FARC"));
            }

            KKtIO HeaderWriter = new KKtIO(new MemoryStream());

            for (int i = 0; i < 3; i++)
            {
                HeaderWriter.Write((byte)0x00);
            }
            if (Signature == Farc.FArc)
            {
                HeaderWriter.Write((byte)0x20);
            }
            else if (Signature == Farc.FArC)
            {
                HeaderWriter.Write((byte)0x10);
            }
            else if (Signature == Farc.FARC)
            {
                HeaderWriter.Write((byte)0x06);
                for (int i = 0; i < 7; i++)
                {
                    HeaderWriter.Write((byte)0x00);
                }
                HeaderWriter.Write((byte)0x40);
                for (int i = 0; i < 8; i++)
                {
                    HeaderWriter.Write((byte)0x00);
                }
            }
            for (int i = 0; i < Files.Length; i++)
            {
                for (int i1 = 0; i1 < Path.GetFileName(Files[i].Name).Length +
                     (Signature == Farc.FArc ? 0x09 : 0x0D); i1++)
                {
                    HeaderWriter.Write((byte)0x00);
                }
            }
            writer.WriteEndian((uint)HeaderWriter.Length, true);
            writer.Write(HeaderWriter.ToArray());
            HeaderWriter = null;

            int Align = (int)Main.Align(writer.Position, 0x10) - (int)writer.Position;

            for (int i1 = 0; i1 < Align; i1++)
            {
                if (Signature == Farc.FArc)
                {
                    writer.Write((byte)0x00);
                }
                else
                {
                    writer.Write((byte)0x78);
                }
            }

            for (int i = 0; i < Files.Length; i++)
            {
                CompressStuff(i, ref Files, ref writer);
            }

            if (Signature == Farc.FARC)
            {
                writer.Seek(0x1C, 0);
            }
            else
            {
                writer.Seek(0x0C, 0);
            }
            for (int i = 0; i < Files.Length; i++)
            {
                writer.Write(Text.ToUTF8(Path.GetFileName(Files[i].Name) + "\0"));
                writer.WriteEndian(Files[i].Offset, true);
                if (Signature != Farc.FArc)
                {
                    writer.WriteEndian(Files[i].SizeComp, true);
                }
                writer.WriteEndian(Files[i].SizeUnc, true);
            }
            writer.Close();
        }
Exemplo n.º 6
0
        public int STRReader()
        {
            KKtIO reader = KKtIO.OpenReader(filepath + ext);

            reader.Format    = KKtMain.Format.F;
            Header.Signature = reader.ReadInt32();
            if (Header.Signature == 0x41525453)
            {
                Header          = reader.ReadHeader(true);
                STR             = new List <String>();
                POF             = KKtMain.AddPOF(Header);
                reader.Position = Header.Lenght;

                Count  = reader.ReadInt32Endian();
                Offset = reader.ReadInt32Endian();
                if (Offset == 0)
                {
                    Offset         = Count;
                    OffsetX        = reader.ReadInt64();
                    Count          = reader.ReadInt64();
                    reader.XOffset = Header.Lenght;
                    reader.Format  = KKtMain.Format.X;
                }
                reader.LongPosition = reader.IsX ? Offset + reader.XOffset : Offset;

                for (int i = 0; i < Count; i++)
                {
                    String Str = new String
                    {
                        StrOffset = reader.GetOffset(ref POF).ReadInt32Endian(),
                        ID        = reader.ReadInt32Endian()
                    };
                    if (reader.IsX)
                    {
                        Str.StrOffset += (int)OffsetX;
                    }
                    STR.Add(Str);
                }
                for (int i = 0; i < Count; i++)
                {
                    reader.LongPosition = STR[i].StrOffset + (reader.IsX ? reader.XOffset : 0);
                    STR[i] = new String {
                        ID = STR[i].ID, Str = KKtText.
                                              ToUTF8(reader.NullTerminated()), StrOffset = STR[i].StrOffset
                    };
                }
                reader.Seek(POF.Offset, 0);
                reader.ReadPOF(ref POF);
            }
            else
            {
                reader.Seek(-4, (SeekOrigin)1);
                int i = 0;
                STR = new List <String>();
                while (reader.LongPosition > 0 && reader.LongPosition < reader.LongLength)
                {
                    int a = reader.ReadInt32();
                    if (a != 0)
                    {
                        reader.Seek(-4, (SeekOrigin)1);
                        STR.Add(new String {
                            Str = KKtText.ToUTF8(reader.NullTerminated()), ID = i
                        });
                        i++;
                    }
                    else
                    {
                        break;
                    }
                }
                Count = STR.Count;
            }

            reader.Close();
            return(1);
        }
Exemplo n.º 7
0
        public void STRWriter()
        {
            uint  Offset        = 0;
            uint  CurrentOffset = 0;
            KKtIO writer        = KKtIO.OpenWriter(filepath + ((int)Header.Format > 5 ? ".str" : ".bin"), true);

            writer.Format = Header.Format;
            POF           = new KKtMain.POF {
                Offsets = new List <int>(), POFOffsets = new List <long>()
            };
            writer.IsBE = writer.Format == KKtMain.Format.F2BE;

            if ((int)writer.Format > 1)
            {
                writer.Seek(0x40, 0);
                writer.WriteEndian(Count);
                writer.GetOffset(ref POF).WriteEndian(0x80);
                writer.Seek(0x80, 0);
                for (int i = 0; i < STR.Count; i++)
                {
                    writer.GetOffset(ref POF).Write(0x00);
                    writer.WriteEndian(STR[i].ID);
                }
                writer.Align(16);
            }
            else
            {
                for (int i = 0; i < Count; i++)
                {
                    writer.Write(0x00);
                }
                writer.Align(32);
            }
            List <string> UsedSTR    = new List <string>();
            List <int>    UsedSTRPos = new List <int>();

            int[] STRPos = new int[Count];
            for (int i1 = 0; i1 < Count; i1++)
            {
                if (UsedSTR.Contains(STR[i1].Str))
                {
                    for (int i2 = 0; i2 < Count; i2++)
                    {
                        if (UsedSTR[i2] == STR[i1].Str)
                        {
                            STRPos[i1] = UsedSTRPos[i2];
                            break;
                        }
                    }
                }
                else
                {
                    STRPos[i1] = (int)writer.Position;
                    UsedSTRPos.Add(STRPos[i1]);
                    UsedSTR.Add(STR[i1].Str);
                    writer.Write(STR[i1].Str + "\0");
                }
            }
            if (writer.Format > KKtMain.Format.F)
            {
                writer.Align(16);
                Offset = (uint)writer.Position;
                writer.Seek(0x80, 0);
            }
            else
            {
                writer.Seek(0, 0);
            }
            for (int i1 = 0; i1 < Count; i1++)
            {
                writer.WriteEndian(STRPos[i1]);
                if (writer.Format > KKtMain.Format.F)
                {
                    writer.Seek(4, (SeekOrigin)1);
                }
            }

            if (writer.Format > KKtMain.Format.F)
            {
                writer.Seek(Offset, 0);
                writer.Write(ref POF, 1);
                CurrentOffset = (uint)writer.Length;
                writer.WriteEOFC(0);
                Header.IsBE        = writer.IsBE;
                Header.Lenght      = 0x40;
                Header.DataSize    = (int)(CurrentOffset - Header.Lenght);
                Header.Signature   = 0x41525453;
                Header.SectionSize = (int)(Offset - Header.Lenght);
                writer.Seek(0, 0);
                writer.Write(Header);
            }
            writer.Close();
        }