示例#1
0
        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);
            }
        }
示例#2
0
        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);
            }
        }