コード例 #1
0
ファイル: HaloImage.xaml.cs プロジェクト: Nibre/Assembly
        private void btnInjectImage_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _blf = new PureBLF(_blfLocation);
                var ofd = new OpenFileDialog
                {
                    Title = "Open an image to be injected",
                    Filter = "JPEG Image (*.jpg,*.jpeg,)|*.jpg;*.jpeg|PNG Image [H3/ODST]|*.png|BMP Image [H3/ODST]|*.bmp"
                };

                if (!((bool)ofd.ShowDialog()))
                {
                    Close();
                    return;
                }
                byte[] newImage = File.ReadAllBytes(ofd.FileName);
                var stream = new EndianStream(new MemoryStream(newImage), Endian.BigEndian);

                // Check if it's a supported image
                stream.SeekTo(0x0);
                ushort imageMagic = stream.ReadUInt16();
                if (imageMagic != 0xFFD8 && imageMagic != 0x8950 && imageMagic != 0x424D)
                    throw new Exception("Invalid image type. Only JPEG, PNG, and BMP are supported.");

                // Check for size and dimension differences
                var imageSize = new FileInfo(ofd.FileName).Length;
                var image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = new MemoryStream(newImage);
                image.EndInit();
                string sizeMessage = "";
                string dimensionMessage = "";

                if (new FileInfo(ofd.FileName).Length >= 0x1C000)
                    sizeMessage = String.Format("- The size of the new image (0x{0}) exceeds Halo 3/ODST's modified limit of 0x1C000. This image will not display in those games as a result. Can be ignored otherwise.\n",
                        imageSize.ToString("X"));

                if (image.PixelWidth != ((BitmapImage)imgBLF.Source).PixelWidth ||
                    image.PixelHeight != ((BitmapImage)imgBLF.Source).PixelHeight)
                    dimensionMessage = String.Format("- The dimensions of the new image ({0}x{1}) are not the same as the dimensions of the original image ({2}x{3}). This blf may appear stretched or not appear at all as a result.\n",
                        image.PixelWidth, image.PixelHeight, ((BitmapImage)imgBLF.Source).PixelWidth, ((BitmapImage)imgBLF.Source).PixelHeight);

                if (dimensionMessage != "" || sizeMessage != "")
                    if (MetroMessageBox.Show("Warning",
                        "There were some potential issue(s) found with your new image;\n\n" + String.Format("{0}{1}",
                        sizeMessage, dimensionMessage) + "\nInject anyway?",
                        MetroMessageBox.MessageBoxButtons.OkCancel) != MetroMessageBox.MessageBoxResult.OK)
                        {
                            Close();
                            return;
                        }

                // It's the right everything! Let's inject

                var newImageChunkData = new List<byte>();
                newImageChunkData.AddRange(new byte[] { 0x00, 0x00, 0x00, 0x00 });
                byte[] imageLength = BitConverter.GetBytes(newImage.Length);
                Array.Reverse(imageLength);
                newImageChunkData.AddRange(imageLength);
                newImageChunkData.AddRange(newImage);

                // Write data to chunk file
                _blf.BLFChunks[1].ChunkData = newImageChunkData.ToArray<byte>();

                _blf.RefreshRelativeChunkData();
                _blf.UpdateChunkTable();

                // Update eof offset value
                var eofstream = new EndianStream(new MemoryStream(_blf.BLFChunks[2].ChunkData), Endian.BigEndian);

                uint eofFixup = (uint)_blf.BLFStream.Length - 0x111; //real cheap but hey it works and is always the same in all games

                eofstream.SeekTo(0);
                eofstream.WriteUInt32(eofFixup);

                _blf.RefreshRelativeChunkData();
                _blf.UpdateChunkTable();

                Close();
                MetroMessageBox.Show("Injected!", "The BLF Image has been injected. This image tab will now close.");
                App.AssemblyStorage.AssemblySettings.HomeWindow.ExternalTabClose(_tab);
            }
            catch (Exception ex)
            {
                Close();
                MetroMessageBox.Show("Inject Failed!", "The BLF Image failed to be injected: \n " + ex.Message);
            }
        }
コード例 #2
0
ファイル: HaloImage.xaml.cs プロジェクト: ChadSki/Assembly
        private void btnInjectImage_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _blf = new PureBLF(_blfLocation);
                var ofd = new OpenFileDialog
                {
                    Title = "Opem an image to be injected",
                    Filter = "JPEG Image (*.jpg)|*.jpg|JPEG Image (*.jpeg)|*.jpeg"
                };

                if (!((bool)ofd.ShowDialog()))
                {
                    Close();
                    return;
                }
                byte[] newImage = File.ReadAllBytes(ofd.FileName);
                var stream = new EndianStream(new MemoryStream(newImage), Endian.BigEndian);

                // To-do: Allow PNGs

                // Check if it's a JIFI
                stream.SeekTo(0x0);
                ushort imageMagic = stream.ReadUInt16();
                if (imageMagic != 0xFFD8)
                    throw new Exception("Invalid image type, it has to be a JPEG (JFIF in the header).");

                // Check if it's the right size
                var image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = new MemoryStream(newImage);
                image.EndInit();

                if (image.PixelWidth != ((BitmapImage)imgBLF.Source).PixelWidth ||
                    image.PixelHeight != ((BitmapImage)imgBLF.Source).PixelHeight)
                    throw new Exception(string.Format("Image isn't the right size. It must be {0}x{1}",
                        ((BitmapImage)imgBLF.Source).PixelWidth, ((BitmapImage)imgBLF.Source).PixelHeight));

                // It's the right everything! Let's inject

                var newImageChunkData = new List<byte>();
                newImageChunkData.AddRange(new byte[] { 0x00, 0x00, 0x00, 0x00 });
                byte[] imageLength = BitConverter.GetBytes(newImage.Length);
                Array.Reverse(imageLength);
                newImageChunkData.AddRange(imageLength);
                newImageChunkData.AddRange(newImage);

                // Write data to chunk file
                _blf.BLFChunks[1].ChunkData = newImageChunkData.ToArray<byte>();

                _blf.RefreshRelativeChunkData();
                _blf.UpdateChunkTable();

                imgBLF.Source = image;

                MetroMessageBox.Show("Injected!", "The BLF Image has been injected.");
                Close();
            }
            catch (Exception ex)
            {
                Close();
                MetroMessageBox.Show("Inject Failed!", "The BLF Image failed to be injected: \n " + ex.Message);
            }
        }