Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            UInt32 FileOffset = (UInt32)numericUpDown1.Value;

            FileOffset = FileOffset - (UInt32)GlobalData.Instance.ftable_arr[(int)numericUpDown2.Value];
            FileOffset = FileOffset - 0x2008;

            UInt32 TableOffset = (UInt32)GlobalData.Instance.ftable_arr[(int)numericUpDown2.Value] + 0x10;

            while (ByteTools.Read4Bytes(GlobalData.Instance.ROM, TableOffset) != FileOffset)
            {
                TableOffset += 0x08;
            }
            TableOffset = TableOffset - (UInt32)GlobalData.Instance.ftable_arr[(int)numericUpDown2.Value] - 0x10;
            TableOffset = TableOffset / 0x04 / 0x02;
            MessageBox.Show(TableOffset.ToString());
        }
Пример #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            int           p = 1;
            int           f5;
            int           SizeX        = 32;
            int           SizeY        = 32;
            UInt32        DataOffset   = 0;
            List <int>    imageOffsets = new List <int>();
            List <string> imageNames   = new List <string>();


            OpenFileDialog open = new OpenFileDialog();

            open.Filter = "bin file|*.bin";
            DialogResult         res       = open.ShowDialog();
            CommonOpenFileDialog FolSelect = new CommonOpenFileDialog();

            FolSelect.IsFolderPicker = true;
            if (res == DialogResult.OK && FolSelect.ShowDialog() == CommonFileDialogResult.Ok)
            {
                try
                {
                    //OpenFileDialog open = new OpenFileDialog();
                    //open.Filter = "BIN file|*.bin";
                    //DialogResult res = open.ShowDialog();
                    //if (res == DialogResult.OK)
                    {
                        foreach (PictureBox b in LoadedImgs)
                        {
                            b.Dispose();
                        }
                        foreach (Label l in Labels)
                        {
                            l.Dispose();
                        }
                        //vScrollBar1.Maximum = 0;
                        //LoadedImgs.Clear();
                        byte[] Data = File.ReadAllBytes(open.FileName);
                        //detect if header contains offsets or DL's contain offsets

                        /*List<string> HeaderData = new List<string>();
                         * HeaderData.AddRange(new HeaderReader().ReadHeader(Data).Split('\n'));
                         * HeaderData.RemoveAll(x => !x.Contains("Offset:"));
                         */
                        int[] Offsets = new HeaderReader().ReadOffsets(Data);

                        bool HeaderIsDL = true;
                        for (int i = 0; i < Offsets.Length / 2; i++)
                        {
                            if (Data[Offsets[i]] != 4)
                            {
                                HeaderIsDL = false;
                            }
                        }

                        if (HeaderIsDL)
                        {
                            f5 = 0;
                            int CurOffset = Offsets[0];
                            while (CurOffset < Data.Length)
                            {
                                if (Data[CurOffset] == 0x04 || Data[CurOffset] == 0xB8)
                                {
                                    f5 = 0;
                                }
                                //count f5 third instance = pallette
                                if (Data[CurOffset] == 0xF5)
                                {
                                    f5++;
                                    int OffsetPrevFD = CurOffset;
                                    while (Data[OffsetPrevFD] != 0xFD)
                                    {
                                        OffsetPrevFD -= 1;
                                    }
                                    if (OffsetPrevFD >= CurOffset - 15)
                                    {
                                        //texture command
                                        //get sizes

                                        switch (Data[CurOffset + 6])
                                        {
                                        case (0x40):
                                        {
                                            SizeY = 32;
                                            break;
                                        }

                                        case (0x80):
                                        {
                                            SizeY = 64;
                                            break;
                                        }
                                        }
                                        switch (Data[CurOffset + 7])
                                        {
                                        case (0x50):
                                        {
                                            SizeX = 32;
                                            break;
                                        }

                                        case (0x60):
                                        {
                                            SizeX = 64;
                                            break;
                                        }
                                        }
                                        byte[] FixedData = new byte[4];
                                        Array.Copy(Data, OffsetPrevFD + 4, FixedData, 0, 4);
                                        FixedData[0] = 0;
                                        DataOffset   = ByteTools.Read4Bytes(FixedData, 0); //UInt32
                                        //store offset at this point
                                        imageOffsets.Add((int)DataOffset);
                                    }
                                }
                                if (f5 == 3)
                                {
                                    Bitmap b = new Bitmap(SizeX, SizeY, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                                    Graphics g = Graphics.FromImage(b);



                                    byte[] Palette;
                                    int    colval;
                                    //string ColourType = Convert.ToString(Data[CurOffset + 1], 2);
                                    //ColourType += Convert.ToString(Data[CurOffset + 2], 2);
                                    //ColourType = ColourType.Substring(4, 2);
                                    colval = (Data[CurOffset + 1]);
                                    colval = (colval & 24);

                                    colval = colval >> 3;
                                    //Convert.ToInt32(ColourType, 2);
                                    switch (colval)
                                    {
                                    //ci4
                                    case (0):
                                    {
                                        Palette = new byte[32];
                                        int PalOff = (int)DataOffset + 0x200;
                                        Array.Copy(Data, PalOff, Palette, 0, 32);
                                        N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.CI4, N64IMode.AlphaBinary);
                                        break;
                                    }

                                    //rgb16
                                    case (1):
                                    {
                                        Palette = new byte[512];
                                        int arrayoffset = (int)DataOffset + 0x400;
                                        Array.Copy(Data, arrayoffset, Palette, 0, 512);
                                        N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.CI8, N64IMode.AlphaCopyIntensity);
                                        break;
                                    }

                                    //rgb16
                                    case (2):
                                    {
                                        Palette = new byte[32];
                                        int PalOff = (int)DataOffset + 0x200;
                                        Array.Copy(Data, PalOff, Palette, 0, 32);
                                        N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.RGBA16, N64IMode.AlphaBinary);
                                        break;
                                    }
                                    }

                                    PictureBox box = new PictureBox();
                                    box.Image  = b;
                                    box.Width  = SizeX;
                                    box.Height = SizeY;
                                    LoadedImgs.Add(box);
                                    Label l = new Label();
                                    l.Text = DataOffset.ToString("X");
                                    Labels.Add(l);
                                    b.RotateFlip(RotateFlipType.RotateNoneFlipY);
                                    string filepath = @"\textures\" + Path.GetFileName(open.FileName.Substring(0, open.FileName.Length - 4) + "-" + p + ".bmp");
                                    b.Save(FolSelect.FileName + filepath);
                                    imageNames.Add(FolSelect.FileName + filepath);
                                    p++;
                                    f5 = 0;
                                }

                                CurOffset += 8;
                            }
                        }
                        else
                        {
                            /*
                             * foreach(int off in Offsets)
                             * {
                             *  byte[] pal = new byte[32];
                             *
                             *  Array.Copy(Data, off, pal, 0, 32);
                             *  Bitmap b = new Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                             *  Graphics g = Graphics.FromImage(b);
                             *  N64GraphicsCoding.RenderTexture(g, Data, pal, off, 32,32,1,N64Codec.CI4, N64IMode.AlphaBinary);
                             *
                             *  PictureBox box = new PictureBox();
                             *  box.Image = b;
                             *  box.Width = 32;
                             *  box.Height = 32;
                             *  LoadedImgs.Add(box);
                             *
                             *  pal = new byte[256];
                             *
                             *  Array.Copy(Data, off, pal, 0, 256);
                             *  b = new Bitmap(256, 256, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                             *  g = Graphics.FromImage(b);
                             *  N64GraphicsCoding.RenderTexture(g, Data, pal, off, 64, 64, 1, N64Codec.CI8, N64IMode.AlphaBinary);
                             *
                             *  box = new PictureBox();
                             *  box.Image = b;
                             *  box.Width = 32;
                             *  box.Height = 32;
                             *  LoadedImgs.Add(box);
                             * }
                             * byte[] Pal = new byte[32];
                             *
                             */
                        }
                        int CurPosY = BaseBox.Location.Y;
                        int CurPosX = BaseBox.Location.X;

                        /*foreach (PictureBox b in LoadedImgs)
                         * {
                         *  b.Location = new System.Drawing.Point(CurPosX, CurPosY);
                         *  CurPosY += b.Image.Height + 10;
                         *  vScrollBar1.Maximum += b.Image.Height + 10;
                         *  this.Controls.Add(b);
                         *  OrigY.Add(CurPosY);
                         * }*/
                        for (int i = 0; i < LoadedImgs.Count; i++)
                        {
                            LoadedImgs[i].Location = new System.Drawing.Point(CurPosX, CurPosY);
                            CurPosY += LoadedImgs[i].Image.Height + 10;
                            //vScrollBar1.Maximum += LoadedImgs[i].Image.Height + 10;
                            this.Controls.Add(LoadedImgs[i]);
                            OrigY.Add(CurPosY);
                            Labels[i].Location = new System.Drawing.Point(CurPosX + LoadedImgs[i].Width + 32, CurPosY);
                            this.Controls.Add(Labels[i]);
                        }
                        BaseBox.Visible = false;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                try
                {
                    SaveFileDialog mtl = new SaveFileDialog();
                    mtl.Filter = "material file|*.mtl";
                    res        = mtl.ShowDialog();
                    if (res == DialogResult.OK)
                    {
                        //string[] OutObj = generateMtl(mtl.FileName, imageNames, RList);
                        //File.WriteAllLines(mtl.FileName, OutObj);
                    }

                    SaveFileDialog obj = new SaveFileDialog();
                    obj.Filter = "object file|*.obj";
                    res        = obj.ShowDialog();

                    if (res == DialogResult.OK)
                    {
                        string[] OutObj = new DLParser().GetParsedObject2(File.ReadAllBytes(open.FileName), imageOffsets, imageNames, mtl.FileName.Substring(0, mtl.FileName.Length - 4));
                        File.WriteAllLines(obj.FileName, OutObj);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Пример #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "BIN file|*.bin";
                DialogResult res = open.ShowDialog();
                if (res == DialogResult.OK)
                {
                    foreach (PictureBox b in LoadedImgs)
                    {
                        b.Dispose();
                    }
                    foreach (Label l in Labels)
                    {
                        l.Dispose();
                    }
                    vScrollBar1.Maximum = 0;
                    LoadedImgs.Clear();
                    byte[] Data = File.ReadAllBytes(open.FileName);
                    //detect if header contains offsets or DL's contain offsets

                    /*List<string> HeaderData = new List<string>();
                     * HeaderData.AddRange(new HeaderReader().ReadHeader(Data).Split('\n'));
                     * HeaderData.RemoveAll(x => !x.Contains("Offset:"));
                     */
                    int[] Offsets = new HeaderReader().ReadOffsets(Data);

                    bool HeaderIsDL = true;
                    for (int i = 0; i < Offsets.Length / 2; i++)
                    {
                        if (Data[Offsets[i]] != 4)
                        {
                            HeaderIsDL = false;
                        }
                    }

                    if (HeaderIsDL)
                    {
                        int CurOffset = Offsets[0];
                        while (CurOffset < Data.Length)
                        {
                            if (Data[CurOffset] == 0xF5)
                            {
                                int OffsetPrevFD = CurOffset;
                                while (Data[OffsetPrevFD] != 0xFD)
                                {
                                    OffsetPrevFD -= 1;
                                }
                                if (OffsetPrevFD > CurOffset - 15)
                                {
                                    //texture command
                                    //get sizes
                                    int SizeX = 32;
                                    int SizeY = 32;
                                    switch (Data[CurOffset + 6])
                                    {
                                    case (0x40):
                                    {
                                        SizeY = 32;
                                        break;
                                    }

                                    case (0x80):
                                    {
                                        SizeY = 64;
                                        break;
                                    }
                                    }
                                    switch (Data[CurOffset + 7])
                                    {
                                    case (0x50):
                                    {
                                        SizeX = 32;
                                        break;
                                    }

                                    case (0x60):
                                    {
                                        SizeX = 64;
                                        break;
                                    }
                                    }
                                    byte[] FixedData = new byte[4];
                                    Array.Copy(Data, OffsetPrevFD + 4, FixedData, 0, 4);
                                    FixedData[0] = 0;
                                    UInt32   DataOffset = ByteTools.Read4Bytes(FixedData, 0);
                                    Bitmap   b          = new Bitmap(SizeX, SizeY, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                                    Graphics g          = Graphics.FromImage(b);

                                    byte[] Palette;

                                    string ColourType = Convert.ToString(Data[CurOffset + 1], 2);
                                    ColourType += Convert.ToString(Data[CurOffset + 2], 2);
                                    ColourType  = ColourType.Substring(4, 2);

                                    switch (Convert.ToInt32(ColourType, 2))
                                    {
                                    case (0):
                                    {
                                        Palette = new byte[32];
                                        int PalOff = (int)DataOffset + 0x200;
                                        Array.Copy(Data, PalOff, Palette, 0, 32);
                                        N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.CI4, N64IMode.AlphaBinary);
                                        break;
                                    }

                                    case (1):
                                    {
                                        Palette = new byte[512];
                                        Array.Copy(Data, (int)DataOffset + 0x1000, Palette, 0, 512);
                                        N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.CI8, N64IMode.AlphaBinary);
                                        break;
                                    }
                                    }

                                    PictureBox box = new PictureBox();
                                    box.Image  = b;
                                    box.Width  = SizeX;
                                    box.Height = SizeY;
                                    LoadedImgs.Add(box);
                                    Label l = new Label();
                                    l.Text = DataOffset.ToString("X");
                                    Labels.Add(l);
                                }
                            }


                            CurOffset += 8;
                        }
                    }
                    else
                    {
                        /*
                         * foreach(int off in Offsets)
                         * {
                         *  byte[] pal = new byte[32];
                         *
                         *  Array.Copy(Data, off, pal, 0, 32);
                         *  Bitmap b = new Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                         *  Graphics g = Graphics.FromImage(b);
                         *  N64GraphicsCoding.RenderTexture(g, Data, pal, off, 32,32,1,N64Codec.CI4, N64IMode.AlphaBinary);
                         *
                         *  PictureBox box = new PictureBox();
                         *  box.Image = b;
                         *  box.Width = 32;
                         *  box.Height = 32;
                         *  LoadedImgs.Add(box);
                         *
                         *  pal = new byte[256];
                         *
                         *  Array.Copy(Data, off, pal, 0, 256);
                         *  b = new Bitmap(256, 256, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                         *  g = Graphics.FromImage(b);
                         *  N64GraphicsCoding.RenderTexture(g, Data, pal, off, 64, 64, 1, N64Codec.CI8, N64IMode.AlphaBinary);
                         *
                         *  box = new PictureBox();
                         *  box.Image = b;
                         *  box.Width = 32;
                         *  box.Height = 32;
                         *  LoadedImgs.Add(box);
                         * }
                         * byte[] Pal = new byte[32];
                         *
                         */
                    }
                    int CurPosY = BaseBox.Location.Y;
                    int CurPosX = BaseBox.Location.X;

                    /*foreach (PictureBox b in LoadedImgs)
                     * {
                     *  b.Location = new System.Drawing.Point(CurPosX, CurPosY);
                     *  CurPosY += b.Image.Height + 10;
                     *  vScrollBar1.Maximum += b.Image.Height + 10;
                     *  this.Controls.Add(b);
                     *  OrigY.Add(CurPosY);
                     * }*/
                    for (int i = 0; i < LoadedImgs.Count; i++)
                    {
                        LoadedImgs[i].Location = new System.Drawing.Point(CurPosX, CurPosY);
                        CurPosY             += LoadedImgs[i].Image.Height + 10;
                        vScrollBar1.Maximum += LoadedImgs[i].Image.Height + 10;
                        this.Controls.Add(LoadedImgs[i]);
                        OrigY.Add(CurPosY);
                        Labels[i].Location = new System.Drawing.Point(CurPosX + LoadedImgs[i].Width + 32, CurPosY);
                        this.Controls.Add(Labels[i]);
                    }
                    BaseBox.Visible = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Пример #4
0
        /// <summary>
        /// injects bytes in the globaldata ROM
        /// </summary>
        /// <param name="TableID">The filetable to instert into</param>
        /// <param name="FileID">The FileID</param>
        /// <param name="Data">Uncompressed filedata to insert</param>
        /// <returns>Returns ROM with custom file</returns>
        public byte[] InjectIntoROM(int TableID, int FileID, byte[] Data)
        {
            byte[] ROM             = GlobalData.Instance.ROM;
            int    FileTableOffset = GlobalData.Instance.ftable_arr[TableID];

            byte[] CompressedData = Compression.Compression.CompressInflate(Data);
            int[]  TableOffsets   = GlobalData.Instance.ftable_arr;

            int Offset = TableOffsets[TableID] + 0x10;

            Offset += FileID * 8;

            UInt32 FileOffset = ByteTools.Read4Bytes(ROM, (UInt32)Offset) + (UInt32)TableOffsets[TableID] + 0x2008;

            int CompressedSize   = CompressedData.Length - 1;
            int UnCompressedSize = Data.Length - 1;

            //check if compressed data fits within ROM, if not, expand

            /*
             #region CheckIfFit
             * int LastOffset = GlobalData.Instance.ftable_arr[GlobalData.Instance.ftable_arr.Length - 1] + 0x10;
             * UInt32 BiggestOffset = 0;
             * while (ByteTools.Read4Bytes(ROM, ROM[LastOffset]) != 0xFFFFFFFF)
             * {
             *  UInt32 CurOffset = ByteTools.Read4Bytes(ROM, ROM[LastOffset]);
             *  if (CurOffset > BiggestOffset)
             *  {
             *      BiggestOffset = CurOffset;
             *  }
             *  LastOffset += 8;
             * }
             * BiggestOffset += (UInt32)GlobalData.Instance.ftable_arr[GlobalData.Instance.ftable_arr.Length - 1];
             * byte[] RomBuf = new byte[BiggestOffset];
             * Array.Copy(ROM, 0, RomBuf, 0, BiggestOffset);
             * if(RomBuf.Length - 1 + CompressedSize > ROM.Length - 1)
             * {
             *  ROM = ByteTools.Extend4MB(ROM);
             * }
             #endregion
             */

            //going to assume last file = final offset

            UInt32 OrigCompSize    = ByteTools.Read4Bytes(ROM, (uint)Offset + 4);
            UInt32 LastIndex       = 0x301B48;
            UInt32 LastCompression = ByteTools.Read4Bytes(ROM, LastIndex + 4);
            UInt32 LastUsedAddr    = ByteTools.Read4Bytes(ROM, LastIndex) + LastCompression;

            if (LastUsedAddr + Math.Abs(OrigCompSize - CompressedSize) > ROM.Length)
            {
                ROM = ByteTools.Extend4MB(ROM);
            }

            List <byte> RomL = new List <byte>();

            RomL.AddRange(ROM);

            //get original compressed size and take out old data
            CompressedSize += 4; //compensation for first 4 bytes

            //get rid of original data
            RomL.RemoveRange((int)FileOffset, (int)OrigCompSize);

            //insert new data
            byte[] buf = BitConverter.GetBytes(UnCompressedSize);
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(buf);
            }

            RomL.InsertRange((int)FileOffset, buf);
            RomL.InsertRange((int)FileOffset + 4, CompressedData);

            //other offsets are still not correct
            //fix overwritten file sizes
            buf = BitConverter.GetBytes(CompressedSize);
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(buf);
            }

            RomL.RemoveRange(Offset + 4, 4);
            RomL.InsertRange(Offset + 4, buf);

            //fix all other offsets
            int BufOffset = Offset + 8;

            while (ByteTools.Read4Bytes(ROM, (uint)BufOffset) != 0xFFFFFFFF)
            {
                int OffsetToChange = (int)OrigCompSize - ((int)CompressedSize + 1);
                int ChangedOffset  = (int)ByteTools.Read4Bytes(ROM, (uint)BufOffset) - OffsetToChange;
                RomL.RemoveRange(BufOffset, 4);
                buf = BitConverter.GetBytes(ChangedOffset);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(buf);
                }
                RomL.InsertRange(BufOffset, buf);
                BufOffset += 8;
            }

            while (RomL.Count - 1 < 0x7FFFFF)
            {
                RomL.Add(0x00);
            }

            //trim ROM
            while ((RomL.Count - 1) % 0x400000 != 0x3FFFFF)
            {
                RomL.RemoveAt(RomL.Count - 1);
            }

            return(RomL.ToArray());
        }