Exemple #1
0
        public override byte[] Rebuild()
        {
            FileOutput f = new FileOutput();

            f.endian = Endian;
            f.WriteUInt(0x0000FEFF);
            f.endian = Endianness.Big;
            f.WriteUInt(Magic);

            f.endian = Endian;
            f.WriteInt(0); // Always 0
            f.WriteInt(Frames.Count);
            for (int i = 0; i < Frames.Count; i++)
            {
                f.WriteFloat(Frames[i].qx);
                f.WriteFloat(Frames[i].qy);
                f.WriteFloat(Frames[i].qz);
                f.WriteFloat(Frames[i].qw);
                f.WriteFloat(Frames[i].x);
                f.WriteFloat(Frames[i].y);
                f.WriteFloat(Frames[i].z);
            }

            return(f.GetBytes());
        }
Exemple #2
0
        public override byte[] Rebuild()
        {
            FileOutput buf = new FileOutput();

            buf.endian = Endianness.Little;

            var flagsOffset   = 0x10;
            var entriesOffset = flagsOffset + (atlases.Count * 4);
            var stringsOffset = entriesOffset + (textures.Count * 0x20);

            buf.WriteInt(0x544C5354); // TLST
            buf.WriteShort(0);        // idk
            buf.WriteShort((short)atlases.Count);
            buf.WriteShort((short)textures.Count);
            buf.WriteShort((short)flagsOffset);
            buf.WriteShort((short)entriesOffset);
            buf.WriteShort((short)stringsOffset);

            // flags
            foreach (var flag in atlases)
            {
                buf.WriteInt((int)flag);
            }

            // entries
            int namePtr = 0;

            foreach (var texture in textures)
            {
                buf.WriteInt(namePtr);
                buf.WriteInt(namePtr);
                namePtr += texture.name.Length + 1;

                buf.WriteFloat(texture.topLeft.X);
                buf.WriteFloat(texture.topLeft.Y);
                buf.WriteFloat(texture.botRight.X);
                buf.WriteFloat(texture.botRight.Y);

                buf.WriteShort(texture.width);
                buf.WriteShort(texture.height);
                buf.WriteShort(texture.atlasId);
                buf.WriteShort(0); // pad
            }

            //strings
            foreach (var texture in textures)
            {
                buf.WriteString(texture.name);
                buf.WriteByte(0);
            }

            buf.WriteByte(0);

            return(buf.GetBytes());
        }
Exemple #3
0
        public override byte[] Rebuild()
        {
            FileOutput o = new FileOutput();

            o.endian = Endianness.Little;

            o.WriteString(" BWS");
            o.WriteShort(0x05);
            o.WriteShort(0x01);
            o.WriteInt(bones.Count);

            foreach (SBEntry s in bones)
            {
                o.WriteInt((int)s.hash);
                o.WriteFloat(s.param1_1);
                o.WriteInt(s.param1_2);
                o.WriteInt(s.param1_3);
                o.WriteFloat(s.param2_1);
                o.WriteFloat(s.param2_2);
                o.WriteInt(s.param2_3);
                o.WriteFloat(s.rx1);
                o.WriteFloat(s.rx2);
                o.WriteFloat(s.ry1);
                o.WriteFloat(s.ry2);
                o.WriteFloat(s.rz1);
                o.WriteFloat(s.rz2);

                for (int j = 0; j < 8; j++)
                {
                    o.WriteInt((int)s.boneHashes[j]);
                }

                for (int j = 0; j < 4; j++)
                {
                    o.WriteFloat(s.unks1[j]);
                }

                for (int j = 0; j < 6; j++)
                {
                    o.WriteFloat(s.unks2[j]);
                }

                o.WriteFloat(s.factor);

                for (int j = 0; j < 3; j++)
                {
                    o.WriteInt(s.ints[j]);
                }
            }

            return(o.GetBytes());
        }
Exemple #4
0
        public static byte[] FromIDSP(byte[] idsp)
        {
            File.WriteAllBytes("temp.idsp", idsp);
            IntPtr vgm = VGMStreamNative.InitVGMStream("temp.idsp");

            if (vgm == IntPtr.Zero)
            {
                throw new Exception("Error loading idsp");
            }

            int channelCount = VGMStreamNative.GetVGMStreamChannelCount(vgm);
            int bitsPerFrame = VGMStreamNative.GetVGMStreamFrameSize(vgm);
            int size         = VGMStreamNative.GetVGMStreamTotalSamples(vgm);
            int samplerate   = VGMStreamNative.GetVGMStreamSampleRate(vgm);

            int total = (int)((samplerate * bitsPerFrame * channelCount * (size / 24576000f)) / 8 * 1024);

            short[] buffer = new short[total];
            VGMStreamNative.RenderVGMStream(buffer, buffer.Length / 2, vgm);

            FileOutput o = new FileOutput();

            o.endian = Endianness.Little;

            o.WriteString("RIFF");
            o.WriteInt(0);
            o.WriteString("WAVEfmt ");

            o.WriteInt(0x10);
            o.WriteShort(1);
            o.WriteShort(channelCount);
            o.WriteInt(samplerate);
            o.WriteInt(size);
            o.WriteShort(2);
            o.WriteShort(0x10);

            o.WriteString("data");
            o.WriteInt(buffer.Length);

            for (int i = 0; i < buffer.Length / 2; i++)
            {
                o.WriteShort(buffer[i]);
            }

            o.WriteIntAt(o.Size() - 8, 4);

            VGMStreamNative.CloseVGMStream(vgm);
            File.Delete("temp.idsp");
            return(o.GetBytes());
        }
Exemple #5
0
        public override byte[] Rebuild()
        {
            FileOutput o = new FileOutput();

            o.endian = Endianness.Little;

            FileOutput d = new FileOutput();

            d.endian = Endianness.Little;

            o.WriteString("NUS3");
            o.WriteInt(0);

            o.WriteString("BANKTOC ");
            o.WriteInt(0x3C);
            o.WriteInt(0x07);

            // write each section
            o.WriteString("PROP");
            o.WriteInt(prop.Rebuild(d));

            o.WriteString("BINF");
            o.WriteInt(binf.Rebuild(d));

            o.WriteString("GRP ");
            o.WriteInt(grp.Rebuild(d));

            o.WriteString("DTON");
            o.WriteInt(dton.Rebuild(d));

            o.WriteString("TONE");
            o.WriteInt(tone.Rebuild(d));

            o.WriteString("JUNK");
            o.WriteInt(4);
            //d.writeString("JUNK");
            d.WriteInt(4);
            d.WriteInt(0);

            o.WriteString("PACK");
            o.WriteInt(0);

            o.WriteOutput(d);

            o.WriteIntAt(o.Size(), 4);

            // something extra with bgm??

            return(o.GetBytes());
        }
Exemple #6
0
                public void Play()
                {
                    // this cannot be very fast .-.
                    // if anyone know how to pass the file via a byte array then that would be great...
                    File.WriteAllBytes("temp.idsp", idsp);
                    Console.WriteLine("here");
                    IntPtr vgm = VGMStreamNative.InitVGMStream("temp.idsp");

                    if (vgm == IntPtr.Zero)
                    {
                        throw new Exception("Error loading idsp");
                    }

                    Console.WriteLine("here");
                    int channelCount = VGMStreamNative.GetVGMStreamChannelCount(vgm);
                    int bitsPerFrame = VGMStreamNative.GetVGMStreamFrameSize(vgm);
                    int size         = VGMStreamNative.GetVGMStreamTotalSamples(vgm);
                    int samplerate   = VGMStreamNative.GetVGMStreamSampleRate(vgm);

                    Console.WriteLine("here");

                    int total = (int)((samplerate * bitsPerFrame * channelCount * (size / 24576000f)) / 8 * 1024);

                    //Console.WriteLine(channelCount + " " + bitsPerFrame + " " + size + " " + samplerate + " " + total.ToString("x"));

                    short[] buffer = new short[total];

                    VGMStreamNative.RenderVGMStream(buffer, buffer.Length / 2, vgm);
                    Console.WriteLine("here");

                    FileOutput o = new FileOutput();

                    o.endian = Endianness.Little;
                    for (int i = 0; i < buffer.Length / 2; i++)
                    {
                        o.WriteShort(buffer[i]);
                    }
                    o.Save("test.wav");
                    Console.WriteLine("here");
                    WAVE.Play(o.GetBytes(), VGMStreamNative.GetVGMStreamChannelCount(vgm), VGMStreamNative.GetVGMStreamSamplesPerFrame(vgm), VGMStreamNative.GetVGMStreamSampleRate(vgm));

                    VGMStreamNative.CloseVGMStream(vgm);
                    File.Delete("temp.idsp");
                }
Exemple #7
0
        public override byte[] Rebuild()
        {
            FileOutput f = new FileOutput();

            f.endian = endianess;

            f.WriteInt(entries.Count);
            f.WriteInt(otherEntries.Count);
            f.WriteInt(0x30);
            f.WriteInt(0x20);
            f.WriteInt(0x8);
            f.WriteInt(0x30);
            byte[] entryData = RebuildEntries(new FileOutput());
            f.WriteInt(entryData.Length + 0x30);
            f.WriteBytes(new byte[0x14]);
            f.WriteBytes(entryData);
            f.WriteBytes(RebuildOtherEntries(new FileOutput(), f.Pos()));

            return(f.GetBytes());
        }
Exemple #8
0
        public byte[] Rebuild(int pos)
        {
            FileOutput f = new FileOutput();

            f.endian = Endianness.Big;

            f.WriteInt(pos + f.Pos() + 0x20);
            f.WriteInt(unknown);
            f.WriteInt(valueCount);
            f.WriteInt(frames.Count);
            f.WriteShort(unknown2);
            f.WriteShort(animType);
            int position = pos + f.Pos() + 0xC + name.Length + 1;

            while (position % 0x10 != 0)
            {
                position++;
            }

            f.WriteInt(position);
            f.WriteBytes(new byte[8]);
            f.WriteString(name);
            f.WriteByte(0);
            while ((pos + f.Pos()) % 0x10 != 0)
            {
                f.WriteByte(0);
            }

            foreach (frame fr in frames)
            {
                for (int i = 0; i < valueCount; i++)
                {
                    f.WriteFloat(fr.values[i]);
                }
            }
            f.WriteBytes(new byte[0x10]);

            return(f.GetBytes());
        }
Exemple #9
0
        public byte[] Rebuild(int pos)
        {
            FileOutput f = new FileOutput();

            f.endian = Endianness.Big;

            f.WriteInt(pos + f.Pos() + 0x8);
            f.WriteInt(0);
            f.WriteInt(defaultTexId);
            f.WriteInt(keyframes.Count);
            f.WriteInt(pos + f.Pos() + 0x1C);
            f.WriteInt(frameCount - 1);
            f.WriteInt(unknown);
            f.WriteBytes(new byte[0x10]);
            foreach (keyframe k in keyframes)
            {
                f.WriteInt(k.texId);
                f.WriteInt(k.frameNum);
            }

            return(f.GetBytes());
        }
Exemple #10
0
        public byte[] Rebuild(int pos)
        {
            FileOutput f = new FileOutput();

            f.endian = Endianness.Big;

            f.WriteInt(pos + f.Pos() + 0x20);
            f.WriteInt(Convert.ToInt32(unk1));
            int offset = pos + f.Pos() + 0x18;

            offset += name.Length + 1;
            while (offset % 16 != 0)
            {
                offset++;
            }
            offset += 0x10;
            f.WriteInt(offset);
            f.WriteBytes(new byte[0x14]);
            f.WriteString(name);
            f.WriteByte(0);
            while ((pos + f.Pos()) % 16 != 0)
            {
                f.WriteByte(0);
            }
            f.WriteBytes(new byte[0x10]);
            f.WriteInt(frameCount);
            f.WriteShort(Convert.ToInt32(unk2));
            f.WriteShort(frames.Count);
            f.WriteInt(pos + f.Pos() + 0x18);
            f.WriteBytes(new byte[0x14]);
            foreach (frame keyframe in frames)
            {
                f.WriteShort(keyframe.frameNum);
                f.WriteByte(keyframe.state);
                f.WriteByte(keyframe.unknown);
            }

            return(f.GetBytes());
        }
Exemple #11
0
        public byte[] RebuildEntries(FileOutput f)
        {
            List <int> nameOffsets     = new List <int>();
            int        nameTableOffset = 0x30 + (0x20 * entries.Count);
            FileOutput nameTable       = new FileOutput()
            {
                endian = Endianness.Big
            };

            foreach (Entry entry in entries)
            {
                nameOffsets.Add(nameTableOffset + nameTable.Pos());
                nameTable.WriteString(entry.name);
                nameTable.WriteBytes(new byte[4 - (entry.name.Length % 4)]); //Pad to next word
            }
            while (nameTable.Pos() % 0x10 != 0)
            {
                nameTable.WriteByte(0);
            }

            for (int i = 0; i < entries.Count; i++)
            {
                entries[i].values[0] = nameOffsets[i];
            }

            foreach (Entry entry in entries)
            {
                foreach (int value in entry.values)
                {
                    f.WriteInt(value);
                }
            }

            f.WriteBytes(nameTable.GetBytes());

            return(f.GetBytes());
        }
Exemple #12
0
        public static byte[] EncodeImage(Bitmap img, Tex_Formats type)
        {
            if (type == Tex_Formats.ETC1)
            {
                return(RG_ETC1.encodeETC(img));
            }
            if (type == Tex_Formats.ETC1a4)
            {
                return(RG_ETC1.encodeETCa4(img));
            }

            FileOutput o = new FileOutput();

            BitmapData bmpData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.WriteOnly, img.PixelFormat);

            int[] pixels = new int[img.Width * img.Height];
            Marshal.Copy(bmpData.Scan0, pixels, 0, pixels.Length);
            img.UnlockBits(bmpData);

            for (int h = 0; h < img.Height; h += 8)
            {
                for (int w = 0; w < img.Width; w += 8)
                {
                    // 8x8 block
                    List <byte[]> colors = new List <byte[]>();
                    for (int bh = 0; bh < 8; bh++)
                    {
                        for (int bw = 0; bw < 8; bw++)
                        {
                            switch (type)
                            {
                            case Tex_Formats.RGBA8: colors.Add(encode8888(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.RGB8: colors.Add(encode8(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.RGBA4444: colors.Add(encode4444(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.RGBA5551: colors.Add(encode5551(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.RGB565: colors.Add(encode565(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.LA8: colors.Add(encodeLA8(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.HILO8: colors.Add(encodeHILO8(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.L8: colors.Add(encodeL8(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.A8: colors.Add(encodeA8(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.LA4: colors.Add(encodeLA4(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.L4:
                            {
                                colors.Add(new byte[] { (byte)((encodeL8(pixels[(w + bw) + (h + bh) * img.Width])[0] / 0x11) & 0xF | ((encodeL8(pixels[(w + bw) + (h + bh) * img.Width + 1])[0] / 0x11) << 4)) });
                                bw++;
                                break;
                            }

                            case Tex_Formats.A4:
                            {
                                colors.Add(new byte[] { (byte)((encodeA8(pixels[(w + bw) + (h + bh) * img.Width])[0] / 0x11) & 0xF | ((encodeA8(pixels[(w + bw) + (h + bh) * img.Width + 1])[0] / 0x11) << 4)) });
                                bw++;
                                break;
                            }
                            }
                        }
                    }

                    for (int bh = 0; bh < 8; bh++)
                    {
                        for (int bw = 0; bw < 8; bw++)
                        {
                            int pos = bw + bh * 8;
                            for (int i = 0; i < zorder.Length; i++)
                            {
                                if (zorder[i] == pos)
                                {
                                    if (type == Tex_Formats.L4 || type == Tex_Formats.A4)
                                    {
                                        i /= 2; bw++;
                                    }
                                    o.WriteBytes(colors[i]);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return(o.GetBytes());
        }
Exemple #13
0
        public byte[] Rebuild(bool dump = false)
        {
            FileOutput o = new FileOutput();

            TextWriter   oldOut = Console.Out;
            MemoryStream ostrm  = new MemoryStream(0xA00000);
            StreamWriter writer = new StreamWriter(ostrm);

            Console.SetOut(writer);

            // TODO: write correct filesize in header.
            // It isn't checked by the game, but what the hell, right?
            header.Write(o);

            writeSymbols(o);
            writeColors(o);
            writeTransforms(o);
            writePositions(o);
            writeBounds(o);

            Actionscript.Write(o);
            if (Actionscript2 != null)
            {
                Actionscript2.Write(o);
            }

            writeAtlases(o);

            unkF008.Write(o);
            unkF009.Write(o);
            unkF00A.Write(o);
            unk000A.Write(o);
            unkF00B.Write(o);
            properties.Write(o);

            Defines.numShapes  = (uint)Shapes.Count;
            Defines.numSprites = (uint)Sprites.Count;
            Defines.numTexts   = (uint)Texts.Count;
            Defines.Write(o);

            writeShapes(o);
            writeSprites(o);
            writeTexts(o);

            o.WriteInt((int)TagType.End);
            o.WriteInt(0);

            int padSize = (4 - (o.Size() % 4)) % 4;

            for (int i = 0; i < padSize; i++)
            {
                o.WriteByte(0);
            }

            if (dump)
            {
                writer.Flush();
                using (var filestream = new FileStream("dump.txt", FileMode.Create))
                    ostrm.WriteTo(filestream);
            }

            Console.SetOut(oldOut);
            o.Save(Filename);
            return(o.GetBytes());
        }
Exemple #14
0
        public static byte[] encodeETCa4(Bitmap b)
        {
            int width  = b.Width;
            int height = b.Height;

            int[] pixels = new int[width * height];

            int i, j;

            FileOutput o = new FileOutput();

            o.endian = System.IO.Endianness.Little;

            for (i = 0; i < height; i += 8)
            {
                for (j = 0; j < width; j += 8)
                {
                    int x, y;

                    Color[] temp = new Color[16];
                    int     pi   = 0;
                    for (x = i; x < i + 4; x++)
                    {
                        for (y = j; y < j + 4; y++)
                        {
                            temp[pi++] = b.GetPixel(y, x);
                        }
                    }

                    for (int ax = 0; ax < 4; ax++)
                    {
                        for (int ay = 0; ay < 4; ay++)
                        {
                            int a = (temp[ax + ay * 4].A >> 4);
                            ay++;
                            a |= (temp[ax + ay * 4].A >> 4) << 4;
                            o.WriteByte(a);
                        }
                    }

                    ulong g = GenETC1(temp);
                    o.WriteInt((int)(g & 0xFFFFFFFF));
                    o.WriteInt((int)((g >> 32) & 0xFFFFFFFF));


                    temp = new Color[16];
                    pi   = 0;
                    for (x = i; x < i + 4; x++)
                    {
                        for (y = j + 4; y < j + 8; y++)
                        {
                            temp[pi++] = b.GetPixel(y, x);
                        }
                    }

                    for (int ax = 0; ax < 4; ax++)
                    {
                        for (int ay = 0; ay < 4; ay++)
                        {
                            int a = (temp[ax + ay * 4].A >> 4);
                            ay++;
                            a |= (temp[ax + ay * 4].A >> 4) << 4;
                            o.WriteByte(a);
                        }
                    }

                    g = GenETC1(temp);
                    o.WriteInt((int)(g & 0xFFFFFFFF));
                    o.WriteInt((int)((g >> 32) & 0xFFFFFFFF));


                    temp = new Color[16];
                    pi   = 0;
                    for (x = i + 4; x < i + 8; x++)
                    {
                        for (y = j; y < j + 4; y++)
                        {
                            temp[pi++] = b.GetPixel(y, x);
                        }
                    }

                    for (int ax = 0; ax < 4; ax++)
                    {
                        for (int ay = 0; ay < 4; ay++)
                        {
                            int a = (temp[ax + ay * 4].A >> 4);
                            ay++;
                            a |= (temp[ax + ay * 4].A >> 4) << 4;
                            o.WriteByte(a);
                        }
                    }

                    g = GenETC1(temp);
                    o.WriteInt((int)(g & 0xFFFFFFFF));
                    o.WriteInt((int)((g >> 32) & 0xFFFFFFFF));


                    temp = new Color[16];
                    pi   = 0;
                    for (x = i + 4; x < i + 8; x++)
                    {
                        for (y = j + 4; y < j + 8; y++)
                        {
                            temp[pi++] = b.GetPixel(y, x);
                        }
                    }

                    for (int ax = 0; ax < 4; ax++)
                    {
                        for (int ay = 0; ay < 4; ay++)
                        {
                            int a = (temp[ax + ay * 4].A >> 4);
                            ay++;
                            a |= (temp[ax + ay * 4].A >> 4) << 4;
                            o.WriteByte(a);
                        }
                    }

                    g = GenETC1(temp);
                    o.WriteInt((int)(g & 0xFFFFFFFF));
                    o.WriteInt((int)((g >> 32) & 0xFFFFFFFF));
                }
            }

            return(o.GetBytes());
        }
Exemple #15
0
        public override byte[] Rebuild()
        {
            FileOutput file = new FileOutput();

            if (file != null)
            {
                if (Endian == Endianness.Little)
                {
                    file.endian = Endianness.Little;
                    file.WriteString(" NBV");
                    file.WriteShort(0x02);
                    file.WriteShort(0x01);
                }
                else if (Endian == Endianness.Big)
                {
                    file.endian = Endianness.Big;
                    file.WriteString("VBN ");
                    file.WriteShort(0x01);
                    file.WriteShort(0x02);
                }


                file.WriteInt(bones.Count);
                if (boneCountPerType[0] == 0)
                {
                    boneCountPerType[0] = (uint)bones.Count;
                }

                List <Bone> Normal = new List <Bone>();
                List <Bone> Unk    = new List <Bone>();
                List <Bone> Helper = new List <Bone>();
                List <Bone> Swing  = new List <Bone>();

                string[] SpecialBones = new string[] { "TransN",
                                                       "RotN",
                                                       "HipN",
                                                       "LLegJ",
                                                       "LKneeJ",
                                                       "LFootJ",
                                                       "LToeN",
                                                       "RLegJ",
                                                       "RKneeJ",
                                                       "RFootJ",
                                                       "RToeN",
                                                       "WaistN",
                                                       "BustN",
                                                       "LShoulderN",
                                                       "LShoulderJ",
                                                       "LArmJ",
                                                       "LHandN",
                                                       "RShoulderN",
                                                       "RShoulderJ",
                                                       "RArmJ",
                                                       "RHandN",
                                                       "NeckN",
                                                       "HeadN",
                                                       "RHaveN",
                                                       "LHaveN",
                                                       "ThrowN" };
                Bone[] Special      = new Bone[SpecialBones.Length];
                int    specialCount = 0;
                // OrderPass
                foreach (Bone b in bones)
                {
                    for (int i = 0; i < SpecialBones.Length; i++)
                    {
                        if (b.Text.Equals(SpecialBones[i]) || (SpecialBones[i].Equals("RotN") && b.Text.Equals("XRotN")))
                        {
                            specialCount++;
                            Special[i] = b;
                            break;
                        }
                    }
                }
                Console.WriteLine(SpecialBones.Length + " " + specialCount);
                if (specialCount == SpecialBones.Length)
                {
                    Normal.AddRange(Special);
                }

                //Gather Each Bone Type
                foreach (Bone b in bones)
                {
                    switch (b.boneType)
                    {
                    case 0: if (!Normal.Contains(b))
                        {
                            Normal.Add(b);
                        }
                        break;

                    case 2: Helper.Add(b); break;

                    case 3: Swing.Add(b); break;

                    default: Unk.Add(b); break;
                    }
                }

                file.WriteInt(Normal.Count);
                file.WriteInt(Unk.Count);
                file.WriteInt(Helper.Count);
                file.WriteInt(Swing.Count);

                List <Bone> NewBoneOrder = new List <Bone>();
                NewBoneOrder.AddRange(Normal);
                NewBoneOrder.AddRange(Unk);
                NewBoneOrder.AddRange(Helper);
                NewBoneOrder.AddRange(Swing);
                bones.Clear();
                bones = NewBoneOrder;

                for (int i = 0; i < bones.Count; i++)
                {
                    file.WriteString(bones[i].Text);
                    for (int j = 0; j < 64 - bones[i].Text.Length; j++)
                    {
                        file.WriteByte(0);
                    }
                    file.WriteInt((int)bones[i].boneType);
                    if (bones[i].parentIndex == -1)
                    {
                        file.WriteInt(0x0FFFFFFF);
                    }
                    else
                    {
                        file.WriteInt(bones[i].parentIndex);
                    }
                    file.WriteInt((int)bones[i].boneId);
                }

                for (int i = 0; i < bones.Count; i++)
                {
                    file.WriteFloat(bones[i].position[0]);
                    file.WriteFloat(bones[i].position[1]);
                    file.WriteFloat(bones[i].position[2]);
                    file.WriteFloat(bones[i].rotation[0]);
                    file.WriteFloat(bones[i].rotation[1]);
                    file.WriteFloat(bones[i].rotation[2]);
                    file.WriteFloat(bones[i].scale[0]);
                    file.WriteFloat(bones[i].scale[1]);
                    file.WriteFloat(bones[i].scale[2]);
                }
            }
            return(file.GetBytes());
        }
Exemple #16
0
        public override byte[] Rebuild()
        {
            FileOutput f = new FileOutput();

            f.endian = Endianness.Little;
            FileOutput fv = new FileOutput();

            fv.endian = Endianness.Little;

            f.WriteShort(format);
            f.WriteShort(unknown);
            f.WriteInt(flags);
            f.WriteInt(mode);
            bool hasNameTable = (flags & 2) > 0;

            f.WriteInt(mesh.Count);

            int vertSize = 0;

            // Vertex Bank
            for (int i = 0; i < 1; i++)
            {
                if (mode == 0 || i == 0)
                {
                    Descriptor des = descript[i];

                    if (format != 4)
                    {
                        foreach (Vertex v in vertices)
                        {
                            for (int k = 0; k < des.type.Length; k++)
                            {
                                fv.Align(2, 0x00);
                                switch (des.type[k])
                                {
                                case 0:     //Position
                                    writeType(fv, v.pos.X, des.format[k], des.scale[k]);
                                    writeType(fv, v.pos.Y, des.format[k], des.scale[k]);
                                    writeType(fv, v.pos.Z, des.format[k], des.scale[k]);
                                    break;

                                case 1:     //Normal
                                    writeType(fv, v.nrm.X, des.format[k], des.scale[k]);
                                    writeType(fv, v.nrm.Y, des.format[k], des.scale[k]);
                                    writeType(fv, v.nrm.Z, des.format[k], des.scale[k]);
                                    break;

                                case 2:     //Color
                                    writeType(fv, v.col.X, des.format[k], des.scale[k]);
                                    writeType(fv, v.col.Y, des.format[k], des.scale[k]);
                                    writeType(fv, v.col.Z, des.format[k], des.scale[k]);
                                    writeType(fv, v.col.W, des.format[k], des.scale[k]);
                                    break;

                                case 3:     //Tex0
                                    writeType(fv, v.tx[0].X, des.format[k], des.scale[k]);
                                    writeType(fv, v.tx[0].Y, des.format[k], des.scale[k]);
                                    break;

                                case 4:     //Tex1
                                    writeType(fv, v.tx[1].X, des.format[k], des.scale[k]);
                                    writeType(fv, v.tx[1].Y, des.format[k], des.scale[k]);
                                    break;

                                case 5:     //Bone Index
                                    fv.WriteByte(v.node[0]);
                                    fv.WriteByte(v.node[1]);
                                    break;

                                case 6:     //Bone Weight
                                    writeType(fv, v.weight[0], des.format[k], des.scale[k]);
                                    writeType(fv, v.weight[1], des.format[k], des.scale[k]);
                                    break;
                                    //default:
                                    //    Console.WriteLine("WTF is this");
                                }
                            }
                        }
                        vertSize = fv.Size();
                        fv.Align(32, 0xFF);
                    }
                }


                for (int j = 0; j < mesh.Count; j++)
                {
                    foreach (List <int> l in mesh[j].faces)
                    {
                        foreach (int index in l)
                        {
                            fv.WriteShort(index);
                        }
                        fv.Align(32, 0xFF);
                    }
                }
            }


            for (int i = 0; i < mesh.Count; i++)
            {
                if (i == 0 && mode == 1)
                {
                    descript[0].WriteDescription(f);
                    f.WriteInt(vertSize);
                }

                f.WriteInt(mesh[i].nodeList.Count);
                //Console.WriteLine(mesh[i].faces.Count + " " + mesh[i].nodeList.Count);
                for (int j = 0; j < mesh[i].nodeList.Count; j++)
                {
                    f.WriteInt(mesh[i].nodeList[j].Count);

                    for (int k = 0; k < mesh[i].nodeList[j].Count; k++)
                    {
                        f.WriteInt(mesh[i].nodeList[j][k]);
                    }

                    f.WriteInt(mesh[i].faces[j].Count);

                    // TODO: This stuff
                    if (hasNameTable)
                    {
                        //int nameId = d.readInt();
                    }

                    /*if (mode == 0)
                     * {
                     *  if (format == 4)
                     *  {
                     *      int[] buffer = new int[primitiveCount];
                     *      for (int k = 0; k < primitiveCount; k++)
                     *      {
                     *          buffer[k] = d.readShort();
                     *      }
                     *      d.align(4);
                     *      List<int> buf = new List<int>();
                     *      buf.AddRange(buffer);
                     *      m.faces.Add(buf);
                     *  }
                     *  else
                     *  {
                     *      Descriptor des = new Descriptor();
                     *      des.ReadDescription(d);
                     *      descript.Add(des);
                     *  }
                     *
                     * }*/
                }
            }

            // TODO: STRING TABLE

            /*if (hasNameTable)
             * {
             *  for (int i = 0; i < mesh.Count; i++)
             *  {
             *      int index = d.readByte();
             *      nameTable.Add(d.readString());
             *  }
             * }*/


            if (format != 4)
            {
                f.Align(32, 0xFF);
            }

            f.WriteOutput(fv);

            return(f.GetBytes());
        }
Exemple #17
0
        public override byte[] Rebuild()
        {
            FileOutput o    = new FileOutput();
            FileOutput data = new FileOutput();

            //We always want BE for the first six bytes
            o.endian    = Endianness.Big;
            data.endian = Endianness.Big;

            if (Endian == Endianness.Big)
            {
                o.WriteUInt(0x4E545033); //NTP3
            }
            else if (Endian == Endianness.Little)
            {
                o.WriteUInt(0x4E545744); //NTWD
            }

            //Most NTWU NUTs are 0x020E, which isn't valid for NTP3/NTWD
            if (Version > 0x0200)
            {
                Version = 0x0200;
            }
            o.WriteUShort(Version);

            //After that, endian is used appropriately
            o.endian    = Endian;
            data.endian = Endian;

            o.WriteUShort((ushort)Nodes.Count);
            o.WriteInt(0);
            o.WriteInt(0);

            //calculate total header size
            uint headerLength = 0;

            foreach (NutTexture texture in Nodes)
            {
                byte surfaceCount = (byte)texture.surfaces.Count;
                bool isCubemap    = surfaceCount == 6;
                if (surfaceCount < 1 || surfaceCount > 6)
                {
                    throw new NotImplementedException($"Unsupported surface amount {surfaceCount} for texture with hash 0x{texture.HashId:X}. 1 to 6 faces are required.");
                }
                else if (surfaceCount > 1 && surfaceCount < 6)
                {
                    throw new NotImplementedException($"Unsupported cubemap face amount for texture with hash 0x{texture.HashId:X}. Six faces are required.");
                }
                byte mipmapCount = (byte)texture.surfaces[0].mipmaps.Count;

                ushort headerSize = 0x50;
                if (isCubemap)
                {
                    headerSize += 0x10;
                }
                if (mipmapCount > 1)
                {
                    headerSize += (ushort)(mipmapCount * 4);
                    while (headerSize % 0x10 != 0)
                    {
                        headerSize += 1;
                    }
                }

                headerLength += headerSize;
            }

            // write headers+data
            foreach (NutTexture texture in Nodes)
            {
                byte surfaceCount = (byte)texture.surfaces.Count;
                bool isCubemap    = surfaceCount == 6;
                byte mipmapCount  = (byte)texture.surfaces[0].mipmaps.Count;

                uint dataSize = 0;

                foreach (var mip in texture.GetAllMipmaps())
                {
                    dataSize += (uint)mip.Length;
                    while (dataSize % 0x10 != 0)
                    {
                        dataSize += 1;
                    }
                }

                ushort headerSize = 0x50;
                if (isCubemap)
                {
                    headerSize += 0x10;
                }
                if (mipmapCount > 1)
                {
                    headerSize += (ushort)(mipmapCount * 4);
                    while (headerSize % 0x10 != 0)
                    {
                        headerSize += 1;
                    }
                }

                o.WriteUInt(dataSize + headerSize);
                o.WriteUInt(0);
                o.WriteUInt(dataSize);
                o.WriteUShort(headerSize);
                o.WriteUShort(0);

                o.WriteByte(0);
                o.WriteByte(mipmapCount);
                o.WriteByte(0);
                o.WriteByte(texture.getNutFormat());
                o.WriteShort(texture.Width);
                o.WriteShort(texture.Height);
                o.WriteInt(0);
                o.WriteUInt(texture.DdsCaps2);

                if (Version < 0x0200)
                {
                    o.WriteUInt(0);
                }
                else if (Version >= 0x0200)
                {
                    o.WriteUInt((uint)(headerLength + data.Size()));
                }
                headerLength -= headerSize;
                o.WriteInt(0);
                o.WriteInt(0);
                o.WriteInt(0);

                if (isCubemap)
                {
                    o.WriteInt(texture.surfaces[0].mipmaps[0].Length);
                    o.WriteInt(texture.surfaces[0].mipmaps[0].Length);
                    o.WriteInt(0);
                    o.WriteInt(0);
                }

                if (texture.getNutFormat() == 14 || texture.getNutFormat() == 17)
                {
                    texture.SwapChannelOrderDown();
                }

                for (byte surfaceLevel = 0; surfaceLevel < surfaceCount; ++surfaceLevel)
                {
                    for (byte mipLevel = 0; mipLevel < mipmapCount; ++mipLevel)
                    {
                        int ds = data.Size();
                        data.WriteBytes(texture.surfaces[surfaceLevel].mipmaps[mipLevel]);
                        data.Align(0x10);
                        if (mipmapCount > 1 && surfaceLevel == 0)
                        {
                            o.WriteInt(data.Size() - ds);
                        }
                    }
                }
                o.Align(0x10);

                if (texture.getNutFormat() == 14 || texture.getNutFormat() == 17)
                {
                    texture.SwapChannelOrderUp();
                }

                o.WriteBytes(new byte[] { 0x65, 0x58, 0x74, 0x00 }); // "eXt\0"
                o.WriteInt(0x20);
                o.WriteInt(0x10);
                o.WriteInt(0x00);

                o.WriteBytes(new byte[] { 0x47, 0x49, 0x44, 0x58 }); // "GIDX"
                o.WriteInt(0x10);
                o.WriteInt(texture.HashId);
                o.WriteInt(0);

                if (Version < 0x0200)
                {
                    o.WriteOutput(data);
                    data = new FileOutput();
                }
            }

            if (Version >= 0x0200)
            {
                o.WriteOutput(data);
            }

            return(o.GetBytes());
        }
Exemple #18
0
        public override byte[] Rebuild()
        {
            FileOutput f = new FileOutput();

            f.endian = Endianness.Big;

            f.WriteString("MTA4");
            f.WriteUInt(unknown);
            f.WriteUInt(frameCount);
            f.WriteUInt(startFrame);
            f.WriteUInt(endFrame);
            f.WriteUInt(frameRate);

            f.WriteInt(matEntries.Count);
            if (matEntries.Count > 0)
            {
                f.WriteInt(0x38);
            }
            else
            {
                f.WriteInt(0);
            }
            f.WriteInt(visEntries.Count);
            if (visEntries.Count > 0)
            {
                f.WriteInt(0x38 + 4 * matEntries.Count);
            }
            else
            {
                f.WriteInt(0);
            }
            for (int i = 0; i < 0x10; i++)
            {
                f.WriteByte(0);
            }

            List <byte[]> matEntriesBuilt = new List <byte[]>();
            List <byte[]> visEntriesBuilt = new List <byte[]>();

            int position = 0x38 + matEntries.Count + visEntries.Count;

            while (position % 0x10 != 0)
            {
                position++;
            }

            foreach (MatEntry m in matEntries)
            {
                byte[] b = m.Rebuild(position);
                matEntriesBuilt.Add(b);
                f.WriteInt(position);
                position += b.Length;
                while (position % 0x10 != 0)
                {
                    position++;
                }
            }

            foreach (VisEntry v in visEntries)
            {
                byte[] b = v.Rebuild(position);
                matEntriesBuilt.Add(b);
                f.WriteInt(position);
                position += b.Length;
                while (position % 0x10 != 0)
                {
                    position++;
                }
            }

            while (f.Pos() % 0x10 != 0)
            {
                f.WriteByte(0);
            }

            foreach (byte[] b in matEntriesBuilt)
            {
                f.WriteBytes(b);
                while (f.Pos() % 0x10 != 0)
                {
                    f.WriteByte(0);
                }
            }

            foreach (byte[] b in visEntriesBuilt)
            {
                f.WriteBytes(b);
                while (f.Pos() % 0x10 != 0)
                {
                    f.WriteByte(0);
                }
            }

            return(f.GetBytes());
        }
Exemple #19
0
        public byte[] Rebuild(int pos)
        {
            FileOutput f = new FileOutput();

            f.endian = Endianness.Big;

            f.WriteInt(pos + f.Pos() + 0x20);
            f.WriteInt(matHash);
            f.WriteInt(properties.Count);
            int nameOffset = pos + f.Pos() + 0x15 + name.Length;

            while (nameOffset % 4 != 0)
            {
                nameOffset++;
            }
            f.WriteInt(nameOffset);
            f.WriteFlag(hasPat);
            f.WriteBytes(new byte[3]);
            //Write all the mat data into a buffer (g) then write pat offset
            int        pos2 = pos + f.Pos() + 4;
            FileOutput g    = new FileOutput();

            g.endian = Endianness.Big;

            if (matHash2 != 0)
            {
                g.WriteInt(pos2 + g.Pos() + 0x8);
                g.WriteInt(matHash);
            }
            else
            {
                g.WriteBytes(new byte[8]);
            }

            g.WriteString(name);
            g.WriteByte(0);
            while ((pos2 + g.Pos()) % 0x10 != 0)
            {
                g.WriteByte(0);
            }

            int position = pos2 + g.Pos() + properties.Count * 4;

            while (position % 16 != 0)
            {
                position++;
            }

            List <byte[]> builtProperties = new List <byte[]>();

            foreach (MatData prop in properties)
            {
                g.WriteInt(position);
                byte[] b = prop.Rebuild(position);
                builtProperties.Add(b);
                position += b.Length;
                while (position % 16 != 0)
                {
                    position++;
                }
            }

            while ((pos2 + g.Pos()) % 16 != 0)
            {
                g.WriteByte(0);
            }

            foreach (byte[] b in builtProperties)
            {
                g.WriteBytes(b);
                while ((pos2 + g.Pos()) % 16 != 0)
                {
                    g.WriteByte(0);
                }
            }

            f.WriteInt(pos2 + g.Pos());
            f.WriteBytes(g.GetBytes());
            if (hasPat)
            {
                f.WriteBytes(pat0.Rebuild(f.Pos()));
            }

            return(f.GetBytes());
        }
Exemple #20
0
        public static byte[] encodeETC(Bitmap b)
        {
            int width  = b.Width;
            int height = b.Height;

            int[] pixels = new int[width * height];
            init();

            int i, j;

            FileOutput o = new FileOutput();

            o.endian = System.IO.Endianness.Little;

            for (i = 0; i < height; i += 8)
            {
                for (j = 0; j < width; j += 8)
                {
                    int x, y;

                    Color[] temp = new Color[16];
                    int     pi   = 0;
                    for (x = i; x < i + 4; x++)
                    {
                        for (y = j; y < j + 4; y++)
                        {
                            temp[pi++] = b.GetPixel(y, x);
                        }
                    }

                    o.WriteBytes(GenETC1(temp));


                    temp = new Color[16];
                    pi   = 0;
                    for (x = i; x < i + 4; x++)
                    {
                        for (y = j + 4; y < j + 8; y++)
                        {
                            temp[pi++] = b.GetPixel(y, x);
                        }
                    }

                    o.WriteBytes(GenETC1(temp));


                    temp = new Color[16];
                    pi   = 0;
                    for (x = i + 4; x < i + 8; x++)
                    {
                        for (y = j; y < j + 4; y++)
                        {
                            temp[pi++] = b.GetPixel(y, x);
                        }
                    }

                    o.WriteBytes(GenETC1(temp));


                    temp = new Color[16];
                    pi   = 0;
                    for (x = i + 4; x < i + 8; x++)
                    {
                        for (y = j + 4; y < j + 8; y++)
                        {
                            temp[pi++] = b.GetPixel(y, x);
                        }
                    }

                    o.WriteBytes(GenETC1(temp));
                }
            }

            return(o.GetBytes());
        }
Exemple #21
0
        public static byte[] CreateOMOFromAnimation(Animation a, VBN vbn)
        {
            if (vbn == null || a == null)
            {
                return new byte[] { }
            }
            ;

            // Test Actual Bones
            //-------------------------

            List <Animation.KeyNode> toRem = new List <Animation.KeyNode>();

            for (int j = 0; j < a.bones.Count; j++)
            {
                Animation.KeyNode keynode = ((Animation.KeyNode)a.bones[j]);
                Bone b = vbn.getBone(keynode.Text);

                if (b == null)
                {
                    toRem.Add(keynode);
                }
            }
            foreach (Animation.KeyNode r in toRem)
            {
                Console.WriteLine("Removing " + r.Text);
                a.bones.Remove(r);
            }

            //-------------------------

            FileOutput o = new FileOutput();

            o.endian = Endianness.Big;

            FileOutput t1 = new FileOutput();

            t1.endian = Endianness.Big;

            FileOutput t2 = new FileOutput();

            t2.endian = Endianness.Big;

            o.WriteString("OMO ");
            o.WriteShort(1);        //idk
            o.WriteShort(3);        //idk

            o.WriteInt(0x091E100C); //flags??


            o.WriteShort(0);             //padding
            o.WriteShort(a.bones.Count); // numOfNodes

            o.WriteShort(a.frameCount);  // frame size
            o.WriteShort(0);             // frame start ??

            o.WriteInt(0);
            o.WriteInt(0);
            o.WriteInt(0);

            o.WriteIntAt(o.Size(), 0x14);

            // ASSESSMENT
            Vector3[] maxT     = new Vector3[a.bones.Count], minT = new Vector3[a.bones.Count];
            Vector4[] maxR     = new Vector4[a.bones.Count], minR = new Vector4[a.bones.Count];
            Vector3[] maxS     = new Vector3[a.bones.Count], minS = new Vector3[a.bones.Count];
            bool[]    hasScale = new bool[a.bones.Count];
            bool[]    hasTrans = new bool[a.bones.Count];
            bool[]    hasRot   = new bool[a.bones.Count];

            bool[] conScale = new bool[a.bones.Count];
            bool[] conTrans = new bool[a.bones.Count];
            bool[] conRot   = new bool[a.bones.Count];

            a.SetFrame(0);

            List <List <Bone> > Frames = new List <List <Bone> >();

            {
                for (int j = 0; j < a.bones.Count; j++)
                {
                    Animation.KeyNode keynode = ((Animation.KeyNode)a.bones[j]);
                    if (keynode.xpos.HasAnimation() || keynode.ypos.HasAnimation() || keynode.zpos.HasAnimation())
                    {
                        hasTrans[j] = true;
                    }
                    if (keynode.xrot.HasAnimation())
                    {
                        hasRot[j] = true;
                    }
                    if (keynode.xsca.HasAnimation() || keynode.ysca.HasAnimation() || keynode.zsca.HasAnimation())
                    {
                        hasScale[j] = true;
                    }

                    maxT[j] = new Vector3(-999f, -999f, -999f);
                    minT[j] = new Vector3(999f, 999f, 999f);
                    maxS[j] = new Vector3(-999f, -999f, -999f);
                    minS[j] = new Vector3(999f, 999f, 999f);
                    maxR[j] = new Vector4(-999f, -999f, -999f, -999f);
                    minR[j] = new Vector4(999f, 999f, 999f, 999f);

                    foreach (Animation.KeyFrame key in keynode.xpos.keys)
                    {
                        maxT[j].X = Math.Max(maxT[j].X, key.Value);
                        minT[j].X = Math.Min(minT[j].X, key.Value);
                    }
                    foreach (Animation.KeyFrame key in keynode.ypos.keys)
                    {
                        maxT[j].Y = Math.Max(maxT[j].Y, key.Value);
                        minT[j].Y = Math.Min(minT[j].Y, key.Value);
                    }
                    foreach (Animation.KeyFrame key in keynode.zpos.keys)
                    {
                        maxT[j].Z = Math.Max(maxT[j].Z, key.Value);
                        minT[j].Z = Math.Min(minT[j].Z, key.Value);
                    }
                    foreach (Animation.KeyFrame key in keynode.xsca.keys)
                    {
                        maxS[j].X = Math.Max(maxS[j].X, key.Value);
                        minS[j].X = Math.Min(minS[j].X, key.Value);
                    }
                    foreach (Animation.KeyFrame key in keynode.ysca.keys)
                    {
                        maxS[j].Y = Math.Max(maxS[j].Y, key.Value);
                        minS[j].Y = Math.Min(minS[j].Y, key.Value);
                    }
                    foreach (Animation.KeyFrame key in keynode.zsca.keys)
                    {
                        maxS[j].Z = Math.Max(maxS[j].Z, key.Value);
                        minS[j].Z = Math.Min(minS[j].Z, key.Value);
                    }

                    Bone b = vbn.getBone(keynode.Text);
                    for (int i = 0; i < a.frameCount; i++)
                    {
                        Quaternion r = new Quaternion();
                        if (keynode.rotType == Animation.RotationType.Quaternion)
                        {
                            Animation.KeyFrame[] x  = keynode.xrot.GetFrame(i);
                            Animation.KeyFrame[] y  = keynode.yrot.GetFrame(i);
                            Animation.KeyFrame[] z  = keynode.zrot.GetFrame(i);
                            Animation.KeyFrame[] w  = keynode.wrot.GetFrame(i);
                            Quaternion           q1 = new Quaternion(x[0].Value, y[0].Value, z[0].Value, w[0].Value);
                            Quaternion           q2 = new Quaternion(x[1].Value, y[1].Value, z[1].Value, w[1].Value);
                            if (x[0].Frame == i)
                            {
                                r = q1;
                            }
                            else
                            if (x[1].Frame == i)
                            {
                                r = q2;
                            }
                            else
                            {
                                r = Quaternion.Slerp(q1, q2, (i - x[0].Frame) / (x[1].Frame - x[0].Frame));
                            }
                        }
                        else
                        if (keynode.rotType == Animation.RotationType.Euler)
                        {
                            float x = keynode.xrot.HasAnimation() ? keynode.xrot.GetValue(i) : b.rotation[0];
                            float y = keynode.yrot.HasAnimation() ? keynode.yrot.GetValue(i) : b.rotation[1];
                            float z = keynode.zrot.HasAnimation() ? keynode.zrot.GetValue(i) : b.rotation[2];
                            r = Animation.EulerToQuat(z, y, x);
                        }
                        r.Normalize();

                        maxR[j].X = Math.Max(maxR[j].X, r.X);
                        minR[j].X = Math.Min(minR[j].X, r.X);
                        maxR[j].Y = Math.Max(maxR[j].Y, r.Y);
                        minR[j].Y = Math.Min(minR[j].Y, r.Y);
                        maxR[j].Z = Math.Max(maxR[j].Z, r.Z);
                        minR[j].Z = Math.Min(minR[j].Z, r.Z);
                    }

                    //if (b == null)continue;
                    if (b != null)
                    {
                        if (maxT[j].X == -999)
                        {
                            maxT[j].X = b.position[0];
                        }
                        if (maxT[j].Y == -999)
                        {
                            maxT[j].Y = b.position[1];
                        }
                        if (maxT[j].Z == -999)
                        {
                            maxT[j].Z = b.position[2];
                        }
                        if (minT[j].X == -999)
                        {
                            minT[j].X = b.position[0];
                        }
                        if (minT[j].Y == -999)
                        {
                            minT[j].Y = b.position[1];
                        }
                        if (minT[j].Z == -999)
                        {
                            minT[j].Z = b.position[2];
                        }

                        if (maxS[j].X == -999)
                        {
                            maxS[j].X = b.scale[0];
                        }
                        if (maxS[j].Y == -999)
                        {
                            maxS[j].Y = b.scale[1];
                        }
                        if (maxS[j].Z == -999)
                        {
                            maxS[j].Z = b.scale[2];
                        }
                        if (minS[j].X == -999)
                        {
                            minS[j].X = b.scale[0];
                        }
                        if (minS[j].Y == -999)
                        {
                            minS[j].Y = b.scale[1];
                        }
                        if (minS[j].Z == -999)
                        {
                            minS[j].Z = b.scale[2];
                        }
                    }
                }
            }

            //TODO: Euler Rotation Values

            /*VBN tempvbn = new VBN();
             * a.SetFrame(0);
             * for (int i = 0; i < a.FrameCount; i++)
             * {
             *  //Frames.Add(new List<Bone>());
             *  for (int j = 0; j < a.Bones.Count; j++)
             *  {
             *      Animation.KeyNode keynode = a.Bones[j];
             *      Bone b = vbn.getBone(keynode.Text);
             *      //if(b == null) continue;
             *      maxR[j].X = Math.Max(maxR[j].X, b.rot.X);
             *      minR[j].X = Math.Min(minR[j].X, b.rot.X);
             *      maxR[j].Y = Math.Max(maxR[j].Y, b.rot.Y);
             *      minR[j].Y = Math.Min(minR[j].Y, b.rot.Y);
             *      maxR[j].Z = Math.Max(maxR[j].Z, b.rot.Z);
             *      minR[j].Z = Math.Min(minR[j].Z, b.rot.Z);
             *
             *      Bone f1 = new Bone(tempvbn);
             *      f1.pos = b.pos;
             *      f1.rot = b.rot;
             *      f1.sca = b.sca;
             *      //Frames[i].Add(f1);
             *  }
             *  a.NextFrame(vbn);
             * }*/

            // NODE INFO

            int t2Size = 0;

            for (int i = 0; i < a.bones.Count; i++)
            {
                int flag = 0;

                conRot[i]   = false;
                conScale[i] = false;
                conTrans[i] = false;

                // check for constant
                if (maxT[i].Equals(minT[i]))
                {
                    conTrans[i] = true;
                }
                if (maxR[i].Equals(minR[i]))
                {
                    conRot[i] = true;
                }
                if (maxS[i].Equals(minS[i]))
                {
                    conScale[i] = true;
                }

                if (hasTrans[i])
                {
                    flag |= 0x01000000;
                }
                if (hasRot[i])
                {
                    flag |= 0x02000000;
                }
                if (hasScale[i])
                {
                    flag |= 0x04000000;
                }

                if (conTrans[i] && hasTrans[i])
                {
                    flag |= 0x00200000;
                }
                else
                {
                    flag |= 0x00080000;
                }

                if (conRot[i] && hasRot[i])
                {
                    flag |= 0x00007000;
                }
                else
                {
                    flag |= 0x00005000;
                }

                if (conScale[i] && hasScale[i])
                {
                    flag |= 0x00000200;
                }
                else
                {
                    flag |= 0x00000080;
                }

                flag |= 0x00000001;

                //uint id = 999;

                Bone b    = vbn.getBone(a.bones[i].Text);
                int  hash = -1;
                if (MainForm.hashes.names.ContainsKey(a.bones[i].Text))
                {
                    hash = (int)MainForm.hashes.names[a.bones[i].Text];
                }
                else
                {
                    if (b != null)
                    {
                        hash = (int)b.boneId;
                    }
                    else
                    {
                        continue;
                    }
                }
                //if(hash == -1)
                //hash = (int)FileData.crc32(getNodeId(nodeid.get(i)).name);
                o.WriteInt(flag);      // flags...
                o.WriteInt(hash);      //hash
                o.WriteInt(t1.Size()); // Offset in 1 table
                o.WriteInt(t2Size);    // Offset in 2 table

                // calculate size needed
                if (hasTrans[i])
                {
                    t1.WriteFloat(minT[i].X);
                    t1.WriteFloat(minT[i].Y);
                    t1.WriteFloat(minT[i].Z);

                    if (!conTrans[i])
                    {
                        maxT[i].X -= minT[i].X;
                        maxT[i].Y -= minT[i].Y;
                        maxT[i].Z -= minT[i].Z;

                        t1.WriteFloat(maxT[i].X);
                        t1.WriteFloat(maxT[i].Y);
                        t1.WriteFloat(maxT[i].Z);

                        t2Size += 6;
                    }
                }

                if (hasRot[i])
                {
                    t1.WriteFloat(minR[i].X);
                    t1.WriteFloat(minR[i].Y);
                    t1.WriteFloat(minR[i].Z);

                    if (!conRot[i])
                    {
                        maxR[i].X -= minR[i].X;
                        maxR[i].Y -= minR[i].Y;
                        maxR[i].Z -= minR[i].Z;

                        t1.WriteFloat(maxR[i].X);
                        t1.WriteFloat(maxR[i].Y);
                        t1.WriteFloat(maxR[i].Z);

                        t2Size += 6;
                    }
                }

                if (hasScale[i])
                {
                    t1.WriteFloat(minS[i].X);
                    t1.WriteFloat(minS[i].Y);
                    t1.WriteFloat(minS[i].Z);

                    if (!conScale[i])
                    {
                        maxS[i].X -= minS[i].X;
                        maxS[i].Y -= minS[i].Y;
                        maxS[i].Z -= minS[i].Z;

                        t1.WriteFloat(maxS[i].X);
                        t1.WriteFloat(maxS[i].Y);
                        t1.WriteFloat(maxS[i].Z);

                        t2Size += 6;
                    }
                }
            }

            o.WriteIntAt(o.Size(), 0x18);

            o.WriteOutput(t1);

            o.WriteIntAt(o.Size(), 0x1C);

            // INTERPOLATION

            a.SetFrame(0);

            bool go = true;

            for (int i = 0; i < a.frameCount; i++)
            {
                //a.NextFrame(vbn);
                for (int j = 0; j < a.bones.Count; j++)
                {
                    Bone node = vbn.getBone(a.bones[j].Text);

                    Animation.KeyNode anode = a.bones[j];
                    //if (node == null) continue;

                    if (hasTrans[j] && !conTrans[j])
                    {
                        t2.WriteShort((int)(((anode.xpos.GetValue(i) - minT[j].X) / maxT[j].X) * 0xFFFF));
                        t2.WriteShort((int)(((anode.ypos.GetValue(i) - minT[j].Y) / maxT[j].Y) * 0xFFFF));
                        t2.WriteShort((int)(((anode.zpos.GetValue(i) - minT[j].Z) / maxT[j].Z) * 0xFFFF));
                    }

                    if (hasRot[j] && !conRot[j])
                    {
                        Quaternion r = new Quaternion();
                        if (anode.rotType == Animation.RotationType.Quaternion)
                        {
                            Animation.KeyFrame[] x  = anode.xrot.GetFrame(i);
                            Animation.KeyFrame[] y  = anode.yrot.GetFrame(i);
                            Animation.KeyFrame[] z  = anode.zrot.GetFrame(i);
                            Animation.KeyFrame[] w  = anode.wrot.GetFrame(i);
                            Quaternion           q1 = new Quaternion(x[0].Value, y[0].Value, z[0].Value, w[0].Value);
                            Quaternion           q2 = new Quaternion(x[1].Value, y[1].Value, z[1].Value, w[1].Value);
                            if (x[0].Frame == i)
                            {
                                r = q1;
                            }
                            else
                            if (x[1].Frame == i)
                            {
                                r = q2;
                            }
                            else
                            {
                                r = Quaternion.Slerp(q1, q2, (i - x[0].Frame) / (x[1].Frame - x[0].Frame));
                            }
                        }
                        else
                        if (anode.rotType == Animation.RotationType.Euler)
                        {
                            float x = anode.xrot.HasAnimation() ? anode.xrot.GetValue(i) : node.rotation[0];
                            float y = anode.yrot.HasAnimation() ? anode.yrot.GetValue(i) : node.rotation[1];
                            float z = anode.zrot.HasAnimation() ? anode.zrot.GetValue(i) : node.rotation[2];
                            r = Animation.EulerToQuat(z, y, x);
                        }
                        r.Normalize();
                        t2.WriteShort((int)(((r.X - minR[j].X) / maxR[j].X) * 0xFFFF));
                        t2.WriteShort((int)(((r.Y - minR[j].Y) / maxR[j].Y) * 0xFFFF));
                        t2.WriteShort((int)(((r.Z - minR[j].Z) / maxR[j].Z) * 0xFFFF));
                    }

                    if (hasScale[j] && !conScale[j])
                    {
                        t2.WriteShort((int)(((anode.xsca.GetValue(i) - minS[j].X) / maxS[j].X) * 0xFFFF));
                        t2.WriteShort((int)(((anode.ysca.GetValue(i) - minS[j].Y) / maxS[j].Y) * 0xFFFF));
                        t2.WriteShort((int)(((anode.zsca.GetValue(i) - minS[j].Z) / maxS[j].Z) * 0xFFFF));
                    }
                }

                if (go)
                {
                    o.WriteShortAt(t2.Size(), 0x12);
                    go = false;
                }
            }

            o.WriteOutput(t2);
            return(o.GetBytes());
        }
    }
Exemple #22
0
        public static byte[] createOMO(SkelAnimation a, VBN vbn)
        {
            List <int> nodeid = a.GetNodes(true, vbn);

            int startNode = 0;
            int sizeNode  = nodeid.Count;

            FileOutput o = new FileOutput();

            o.endian = Endianness.Big;

            FileOutput t1 = new FileOutput();

            t1.endian = Endianness.Big;

            FileOutput t2 = new FileOutput();

            t2.endian = Endianness.Big;

            o.WriteString("OMO ");
            o.WriteShort(1);        //idk
            o.WriteShort(3);        //idk

            o.WriteInt(0x091E100C); //flags??


            o.WriteShort(0);              //padding
            o.WriteShort(sizeNode);       // numOfNodes

            o.WriteShort(a.frames.Count); // frame size
            o.WriteShort(0);              // frame start ??

            o.WriteInt(0);
            o.WriteInt(0);
            o.WriteInt(0);

            o.WriteIntAt(o.Size(), 0x14);


            // ASSESSMENT
            KeyNode[] minmax   = new KeyNode[sizeNode];
            bool[]    hasScale = new bool[sizeNode];
            bool[]    hasTrans = new bool[sizeNode];
            bool[]    hasRot   = new bool[sizeNode];

            bool[] conScale = new bool[sizeNode];
            bool[] conTrans = new bool[sizeNode];
            bool[] conRot   = new bool[sizeNode];

            a.SetFrame(0);

            List <List <Bone> > Frames = new List <List <Bone> >();
            VBN tempvbn = new VBN();

            for (int i = 0; i < a.Size(); i++)
            {
                a.NextFrame(vbn, true);
                List <Bone> bonelist = new List <Bone>();
                for (int j = 0; j < nodeid.Count; j++)
                {
                    Bone node = getNodeId(vbn, nodeid[j]);

                    Bone f1 = new Bone(tempvbn);
                    f1.pos = node.pos;
                    f1.rot = node.rot;
                    f1.sca = node.sca;
                    bonelist.Add(f1);

                    if (minmax[j] == null)
                    {
                        hasRot[j]   = false;
                        hasScale[j] = false;
                        hasTrans[j] = false;

                        KeyNode n = a.GetFirstNode(nodeid[j]);
                        if (n != null)
                        {
                            if (n.rType != -1)
                            {
                                hasRot[j] = true;
                            }
                            if (n.tType != -1)
                            {
                                hasTrans[j] = true;
                            }
                            if (n.sType != -1)
                            {
                                hasScale[j] = true;
                            }
                        }

                        minmax[j]    = new KeyNode();
                        minmax[j].t  = new Vector3(999f, 999f, 999f);
                        minmax[j].r  = new Quaternion(999f, 999f, 999f, 999f);
                        minmax[j].s  = new Vector3(999f, 999f, 999f);
                        minmax[j].t2 = new Vector3(-999f, -999f, -999f);
                        minmax[j].r2 = new Quaternion(-999f, -999f, -999f, -999f);
                        minmax[j].s2 = new Vector3(-999f, -999f, -999f);
                    }

                    if (node.pos.X < minmax[j].t.X)
                    {
                        minmax[j].t.X = node.pos.X;
                    }
                    if (node.pos.X > minmax[j].t2.X)
                    {
                        minmax[j].t2.X = node.pos.X;
                    }

                    if (node.pos.Y < minmax[j].t.Y)
                    {
                        minmax[j].t.Y = node.pos.Y;
                    }
                    if (node.pos.Y > minmax[j].t2.Y)
                    {
                        minmax[j].t2.Y = node.pos.Y;
                    }

                    if (node.pos.Z < minmax[j].t.Z)
                    {
                        minmax[j].t.Z = node.pos.Z;
                    }
                    if (node.pos.Z > minmax[j].t2.Z)
                    {
                        minmax[j].t2.Z = node.pos.Z;
                    }

                    //				float[] fix = Node.fix360(node.nrx, node.nry, node.nrz);
                    //float[] f = Bone.CalculateRotation(node.nrx, node.nry, node.nrz);
                    Quaternion r = node.rot;

                    if (r.X < minmax[j].r.X)
                    {
                        minmax[j].r.X = r.X;
                    }
                    if (r.X > minmax[j].r2.X)
                    {
                        minmax[j].r2.X = r.X;
                    }

                    if (r.Y < minmax[j].r.Y)
                    {
                        minmax[j].r.Y = r.Y;
                    }
                    if (r.Y > minmax[j].r2.Y)
                    {
                        minmax[j].r2.Y = r.Y;
                    }

                    if (r.Z < minmax[j].r.Z)
                    {
                        minmax[j].r.Z = r.Z;
                    }
                    if (r.Z > minmax[j].r2.Z)
                    {
                        minmax[j].r2.Z = r.Z;
                    }


                    if (node.sca.X < minmax[j].s.X)
                    {
                        minmax[j].s.X = node.sca.X;
                    }
                    if (node.sca.X > minmax[j].s2.X)
                    {
                        minmax[j].s2.X = node.sca.X;
                    }

                    if (node.sca.Y < minmax[j].s.Y)
                    {
                        minmax[j].s.Y = node.sca.Y;
                    }
                    if (node.sca.Y > minmax[j].s2.Y)
                    {
                        minmax[j].s2.Y = node.sca.Y;
                    }

                    if (node.sca.Z < minmax[j].s.Z)
                    {
                        minmax[j].s.Z = node.sca.Z;
                    }
                    if (node.sca.Z > minmax[j].s2.Z)
                    {
                        minmax[j].s2.Z = node.sca.Z;
                    }
                }
            }

            // NODE INFO

            int t2Size = 0;

            for (int i = 0; i < sizeNode; i++)
            {
                int flag = 0;

                conRot[i]   = false;
                conScale[i] = false;
                conTrans[i] = false;

                // check for constant
                if (minmax[i].t.Equals(minmax[i].t2))
                {
                    conTrans[i] = true;
                }
                if (minmax[i].r.Equals(minmax[i].r2))
                {
                    conRot[i] = true;
                }
                if (minmax[i].s.Equals(minmax[i].s2))
                {
                    conScale[i] = true;
                }

                if (hasTrans[i])
                {
                    flag |= 0x01000000;
                }
                if (hasRot[i])
                {
                    flag |= 0x02000000;
                }
                if (hasScale[i])
                {
                    flag |= 0x04000000;
                }

                if (conTrans[i] && hasTrans[i])
                {
                    flag |= 0x00200000;
                }
                else
                {
                    flag |= 0x00080000;
                }

                if (conRot[i] && hasRot[i])
                {
                    flag |= 0x00007000;
                }
                else
                {
                    flag |= 0x00005000;
                }

                if (conScale[i] && hasScale[i])
                {
                    flag |= 0x00000200;
                }
                else
                {
                    flag |= 0x00000080;
                }

                flag |= 0x00000001;

                int hash = -1;
                if (MainForm.hashes.names.ContainsKey(getNodeId(vbn, nodeid[i]).Text))
                {
                    hash = (int)MainForm.hashes.names[getNodeId(vbn, nodeid[i]).Text];
                }
                //else hash = (int)FileData.crc12(getNodeId(vbn, nodeid[i]).Text);
                o.WriteInt(flag);      // flags...
                o.WriteInt(hash);      //hash
                o.WriteInt(t1.Size()); // Offset in 1 table
                o.WriteInt(t2Size);    // Offset in 2 table

                // calculate size needed
                if (hasTrans[i])
                {
                    t1.WriteFloat(minmax[i].t.X);
                    t1.WriteFloat(minmax[i].t.Y);
                    t1.WriteFloat(minmax[i].t.Z);

                    if (!conTrans[i])
                    {
                        minmax[i].t2.X -= minmax[i].t.X;
                        minmax[i].t2.Y -= minmax[i].t.Y;
                        minmax[i].t2.Z -= minmax[i].t.Z;

                        t1.WriteFloat(minmax[i].t2.X);
                        t1.WriteFloat(minmax[i].t2.Y);
                        t1.WriteFloat(minmax[i].t2.Z);

                        t2Size += 6;
                    }
                }

                if (hasRot[i])
                {
                    t1.WriteFloat(minmax[i].r.X);
                    t1.WriteFloat(minmax[i].r.Y);
                    t1.WriteFloat(minmax[i].r.Z);

                    if (!conRot[i])
                    {
                        minmax[i].r2.X -= minmax[i].r.X;
                        minmax[i].r2.Y -= minmax[i].r.Y;
                        minmax[i].r2.Z -= minmax[i].r.Z;

                        t1.WriteFloat(minmax[i].r2.X);
                        t1.WriteFloat(minmax[i].r2.Y);
                        t1.WriteFloat(minmax[i].r2.Z);

                        t2Size += 6;
                    }
                }

                if (hasScale[i])
                {
                    t1.WriteFloat(minmax[i].s.X);
                    t1.WriteFloat(minmax[i].s.Y);
                    t1.WriteFloat(minmax[i].s.Z);

                    if (!conScale[i])
                    {
                        minmax[i].s2.X -= minmax[i].s.X;
                        minmax[i].s2.Y -= minmax[i].s.Y;
                        minmax[i].s2.Z -= minmax[i].s.Z;

                        t1.WriteFloat(minmax[i].s2.X);
                        t1.WriteFloat(minmax[i].s2.Y);
                        t1.WriteFloat(minmax[i].s2.Z);

                        t2Size += 6;
                    }
                }
            }

            o.WriteIntAt(o.Size(), 0x18);

            o.WriteOutput(t1);

            o.WriteIntAt(o.Size(), 0x1C);

            // INTERPOLATION

            //a.setFrame(0);

            bool go = false;

            foreach (List <Bone> bonelist in Frames)
            {
                //a.nextFrame(vbn);
                int j = 0;
                foreach (Bone node in bonelist)
                {
                    //Bone node = getNodeId(vbn, nodeid[j]);

                    if (hasTrans[j] && !conTrans[j])
                    {
                        t2.WriteShort((int)(((node.pos.X - minmax[j].t.X) / minmax[j].t2.X) * 0xFFFF));
                        t2.WriteShort((int)(((node.pos.Y - minmax[j].t.Y) / minmax[j].t2.Y) * 0xFFFF));
                        t2.WriteShort((int)(((node.pos.Z - minmax[j].t.Z) / minmax[j].t2.Z) * 0xFFFF));
                    }

                    if (hasRot[j] && !conRot[j])
                    {
                        //					float[] fix = Node.fix360(node.nrx, node.nry, node.nrz);
                        //float[] f = CalculateRotation(node.nrx, node.nry, node.nrz);
                        Quaternion r = node.rot;

                        t2.WriteShort((int)(((r.X - minmax[j].r.X) / minmax[j].r2.X) * 0xFFFF));
                        t2.WriteShort((int)(((r.Y - minmax[j].r.Y) / minmax[j].r2.Y) * 0xFFFF));
                        t2.WriteShort((int)(((r.Z - minmax[j].r.Z) / minmax[j].r2.Z) * 0xFFFF));
                    }

                    if (hasScale[j] && !conScale[j])
                    {
                        t2.WriteShort((int)(((node.sca.X - minmax[j].s.X) / minmax[j].s2.X) * 0xFFFF));
                        t2.WriteShort((int)(((node.sca.Y - minmax[j].s.Y) / minmax[j].s2.Y) * 0xFFFF));
                        t2.WriteShort((int)(((node.sca.Z - minmax[j].s.Z) / minmax[j].s2.Z) * 0xFFFF));
                    }
                    j++;
                }
                if (!go)
                {
                    o.WriteShortAt(t2.Size(), 0x12);
                    go = true;
                }
            }

            o.WriteOutput(t2);
            return(o.GetBytes());
        }