private void btnLoadReplacementFile_Click(object sender, RoutedEventArgs e) { OpenFileDialog openDialog = new OpenFileDialog(); Nullable <bool> result = openDialog.ShowDialog(); if (result == true) { byte[] replacementFile; if (openDialog.FileName.ToLower().Contains(".png") || openDialog.FileName.ToLower().Contains(".bmp")) { ImageCoder imageCoder = new ImageCoder(); imageCoder.Convert(new Bitmap(openDialog.FileName)); List <byte> convertedImage = new List <byte>(); // testing out using part of existing header TODO BlitzGameFile selectedGame = (BlitzGameFile)lbGameFiles.SelectedItem; BlitzGraphic currentGraphic = GetBlitzGraphic(selectedGame); convertedImage.AddRange(Blitz2000Header.CreateNFLBlitz2000Header(imageCoder.Width, imageCoder.Height, imageCoder.HasAlpha, (byte)imageCoder.n64ImageType, currentGraphic.IRX, currentGraphic.IRY)); convertedImage.AddRange(imageCoder.Data); if (imageCoder.Palette != null) { convertedImage.AddRange(imageCoder.Palette); } replacementFile = convertedImage.ToArray(); openDialog.FileName = openDialog.FileName.Split('.')[0] + ".wms"; File.WriteAllBytes(openDialog.FileName, convertedImage.ToArray()); } replacementFile = File.ReadAllBytes(openDialog.FileName); InsertReplacementFile(replacementFile); } }
/// <summary> /// When a user chnages the selected n64 graphics type, we convert it to that type. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cbImageType_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (lbGameFiles.SelectedValue == null) { return; } BlitzGraphic currentGraphic = GetBlitzGraphic((BlitzGameFile)lbGameFiles.SelectedItem); if (cbImageType.IsDropDownOpen && (N64ImageType)Enum.Parse(typeof(N64ImageType), currentGraphic.ImageType) != (N64ImageType)cbImageType.SelectedValue) { ImageCoder imageCoder = new ImageCoder(); imageCoder.ConvertTo(currentGraphic.BlitzImage, (N64ImageType)cbImageType.SelectedValue); List <byte> convertedImage = new List <byte>(); convertedImage.AddRange(Blitz2000Header.CreateNFLBlitz2000Header(imageCoder.Width, imageCoder.Height, imageCoder.HasAlpha, (byte)imageCoder.n64ImageType, currentGraphic.IRX, currentGraphic.IRY)); convertedImage.AddRange(imageCoder.Data); InsertReplacementFile(convertedImage.ToArray()); } }
private void btnUpdateImageValues_Click(object sender, RoutedEventArgs e) { BlitzGameFile file = (BlitzGameFile)lbGameFiles.SelectedItem; byte[] fileBytes = GetFileBytes(file).Skip(32).ToArray(); List <byte> newHeaderFileBytes = new List <byte>(); newHeaderFileBytes.AddRange( Blitz2000Header.CreateNFLBlitz2000Header (selectedGraphic.Width, selectedGraphic.Height, true, (byte)((N64ImageType)Enum.Parse(typeof(N64ImageType), selectedGraphic.ImageType)), int.Parse(tbIRX.Text), int.Parse(tbIRY.Text) )); newHeaderFileBytes.AddRange(fileBytes); InsertReplacementFile(newHeaderFileBytes.ToArray()); }
private void btnInsertNewFile_Click(object sender, RoutedEventArgs e) { OpenFileDialog openDialog = new OpenFileDialog(); Nullable <bool> result = openDialog.ShowDialog(); if (result == true) { byte[] replacementFile; if (openDialog.FileName.ToLower().Contains(".png") || openDialog.FileName.ToLower().Contains(".bmp")) { ImageCoder imageCoder = new ImageCoder(); imageCoder.Convert(new Bitmap(openDialog.FileName)); List <byte> convertedImage = new List <byte>(); convertedImage.AddRange(Blitz2000Header.CreateNFLBlitz2000Header(imageCoder.Width, imageCoder.Height, imageCoder.HasAlpha, (byte)imageCoder.n64ImageType)); convertedImage.AddRange(imageCoder.Data); if (imageCoder.Palette != null) { convertedImage.AddRange(imageCoder.Palette); } replacementFile = convertedImage.ToArray(); openDialog.FileName = openDialog.FileName.Split('.')[0] + ".wms"; File.WriteAllBytes(openDialog.FileName, convertedImage.ToArray()); } replacementFile = File.ReadAllBytes(openDialog.FileName); //byte[] compressedFileBytes = MiniLZO.MiniLZO.Compress(replacementFile); byte[] compressedFileBytes = MiniLZO.MiniLZO.CompressWithPrecomp2(openDialog.FileName); int compressedSize = compressedFileBytes.Length; if ((compressedSize & 1) != 0) { List <byte> tempList = compressedFileBytes.ToList(); tempList.Add(00); compressedFileBytes = tempList.ToArray(); } long currentTableOffset; int fileTableEntrySize = gameInfo.maxFileNameLenght + gameInfo.filePositionLength + gameInfo.decompressedLenght + gameInfo.compressedLenght; int fileCount; using (var fs = new FileStream(romLocation, FileMode.Open, FileAccess.ReadWrite)) { //Update tableOffsetLocation currentTableOffset = BitsHelper.GetNumberFromBytes(BitsHelper.ReadBytesFromFileStream(fs, gameInfo.FileSystemOffset + 8, 4).ToArray()); long newTableOffset = currentTableOffset + compressedFileBytes.Length; byte[] newTableOffsetBytes = BitConverter.GetBytes((Int32)newTableOffset); Array.Reverse(newTableOffsetBytes); BitsHelper.WritBytesToFileStream(newTableOffsetBytes, fs, gameInfo.FileSystemOffset + 8); fileCount = BitsHelper.GetNumberFromBytes(BitsHelper.ReadBytesFromFileStream(fs, gameInfo.FileSystemOffset + 12, 4).ToArray()); byte[] newfileCountBytes = BitConverter.GetBytes((Int32)(fileCount + 1)); Array.Reverse(newfileCountBytes); BitsHelper.WritBytesToFileStream(newfileCountBytes, fs, gameInfo.FileSystemOffset + 12); } //move the files that are after this entry byte[] fullRom = File.ReadAllBytes(romLocation); byte[] fileTable = fullRom.ToList().GetRange((int)(currentTableOffset + gameInfo.FileSystemOffset), fileTableEntrySize * fileCount).ToArray(); //Write new file BlitzGameFile newGameFile = new BlitzGameFile() { fileName = "~" + openDialog.FileName.Split('\\').Last(), compressedSize = compressedSize, decompressedSize = replacementFile.Length, fileOffset = currentTableOffset, fileTableEntryStart = currentTableOffset + gameInfo.FileSystemOffset + compressedFileBytes.Length + (fileTableEntrySize * (fileCount)) }; RomEditor.ByteArrayToFile(romLocation, compressedFileBytes, (int)newGameFile.fileOffset); // fix/write to file table RomEditor.ByteArrayToFile(romLocation, fileTable, (int)(currentTableOffset + gameInfo.FileSystemOffset) + compressedFileBytes.Length); WriteNewFileTableToRom(newGameFile); LoadRom(romLocation); } }