Beispiel #1
0
        private void WriteNewFileTableToRom(BlitzGameFile newGameFile)
        {
            using (var fs = new FileStream(romLocation, FileMode.Open, FileAccess.ReadWrite))
            {
                int writeLocation = (int)newGameFile.fileTableEntryStart;
                int addBlanks     = gameInfo.maxFileNameLenght - newGameFile.fileName.Length;
                for (int z = 0; z <= addBlanks; z++)
                {
                    newGameFile.fileName += "\0";
                }
                BitsHelper.WriteStringToFileStream(newGameFile.fileName, fs, writeLocation);

                /// Set decompressed file size
                writeLocation += gameInfo.maxFileNameLenght;
                byte[] decompressedSizeBytes = BitConverter.GetBytes((Int32)newGameFile.decompressedSize);
                Array.Reverse(decompressedSizeBytes);
                BitsHelper.WritBytesToFileStream(decompressedSizeBytes, fs, writeLocation);

                //Set File Offset
                writeLocation += gameInfo.decompressedLenght;
                byte[] filePositionBytes = BitConverter.GetBytes((Int32)(newGameFile.fileOffset - gameInfo.FileSystemOffset));
                Array.Reverse(filePositionBytes);
                BitsHelper.WritBytesToFileStream(filePositionBytes, fs, writeLocation);

                /// Get decompressed file size
                writeLocation += gameInfo.filePositionLength;
                byte[] compressedSizeBytes = BitConverter.GetBytes((Int32)newGameFile.compressedSize);
                Array.Reverse(compressedSizeBytes);
                BitsHelper.WritBytesToFileStream(compressedSizeBytes, fs, writeLocation);
            }
        }
Beispiel #2
0
        private void InsertReplacementFile(byte[] replacementFile)
        {
            File.WriteAllBytes("temp.wms", replacementFile);
            byte[] compressedFileBytes = MiniLZO.MiniLZO.Compress(replacementFile);
            int    compressedSize      = compressedFileBytes.Length;

            if ((compressedSize & 1) != 0)
            {
                List <byte> tempList = compressedFileBytes.ToList();
                tempList.Add(0x00);
                compressedFileBytes = tempList.ToArray();
            }

            int           differenceInSize;
            BlitzGameFile selectedGame            = (BlitzGameFile)lbGameFiles.SelectedItem;
            int           indexOfFileInSortedList = filesSortedByOffset.FindIndex(x => x.fileOffset == selectedGame.fileOffset);
            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());// +gameInfo.FileSystemOffset;
                differenceInSize   = (int)(compressedFileBytes.Length - selectedGame.compressedSize);
                //helps prevent writing to off addresses TODO: rewrite this logic
                if ((differenceInSize & 1) != 0)
                {
                    differenceInSize++;
                }
                long   newTableOffset      = currentTableOffset + differenceInSize;
                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());
            }

            //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();
            int    filesAfterStart = (int)filesSortedByOffset[indexOfFileInSortedList + 1].fileOffset;

            byte[] filesAfterNewFile = fullRom.ToList().GetRange(filesAfterStart, (int)(currentTableOffset + gameInfo.FileSystemOffset) - filesAfterStart - 1).ToArray();

            RomEditor.ByteArrayToFile(romLocation, filesAfterNewFile, filesAfterStart + differenceInSize);
            //Write new file
            RomEditor.ByteArrayToFile(romLocation, compressedFileBytes, (int)selectedGame.fileOffset);
            filesSortedByOffset[indexOfFileInSortedList].compressedSize   = compressedSize;
            filesSortedByOffset[indexOfFileInSortedList].decompressedSize = replacementFile.Length;
            // fix/write to file table
            RomEditor.ByteArrayToFile(romLocation, fileTable, (int)(currentTableOffset + gameInfo.FileSystemOffset) + differenceInSize);
            AdjustFileTable(indexOfFileInSortedList + 1, differenceInSize);
            WritewFileTableToRom(differenceInSize);
            LoadRom(romLocation);
        }
Beispiel #3
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);
            }
        }