public void SendPacket(IReadOnlyCollection <byte> payload)
        {
            if (!bluetoothCommunicator.IsConnected())
            {
                throw new InvalidOperationException("Fox is not connected");
            }

            if (payload.Count < MinPayloadLength || payload.Count > MaxPayloadLength)
            {
                throw new ArgumentException("Wrong payload size", nameof(payload));
            }

            var fullPacket = new List <byte>();

            // Length
            fullPacket.Add((byte)(payload.Count + AddToPacketLenght));

            // Payload
            fullPacket.AddRange(payload);

            // CRC32
            var crc = CRCGenerator.CalculateSTMCRC32(fullPacket);

            fullPacket.AddRange(BitsHelper.ConvertUInt32ToBytes(crc));

            bluetoothCommunicator.SendMessage(fullPacket);
        }
Exemple #2
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);
            }
        }
        /// <summary>
        /// Called when new raw packet received
        /// </summary>
        public void NewRawPacketReceived(IReadOnlyCollection <byte> packet)
        {
            var packetAsList = new List <byte>(packet);

            // Checking CRC
            var crcBuffer = packetAsList
                            .GetRange(packet.Count - 4, 4);

            var expectedCRC   = BitsHelper.ConvertBytesToUint32(crcBuffer.ToArray());
            var calculatedCRC = CRCGenerator.CalculateSTMCRC32(packetAsList
                                                               .GetRange(0, packetAsList.Count - 4));

            if (expectedCRC != calculatedCRC)
            {
                return;
            }

            var payloadLength = packetAsList[0] - AddToPacketLenght;

            var payload = packetAsList
                          .GetRange(1, payloadLength)
                          .AsReadOnly();

            OnPacketReceived(payload);
        }
Exemple #4
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);
        }
Exemple #5
0
        private List <BlitzGameFile> ParseBlitzFileList(string romLocation, BlitzGame gameInfo)
        {
            List <BlitzGameFile> midwayDecFileList = new List <BlitzGameFile>();

            using (var fs = new FileStream(romLocation,
                                           FileMode.Open,
                                           FileAccess.ReadWrite))
            {
                int  fileTableEntrySize = gameInfo.maxFileNameLenght + gameInfo.filePositionLength + gameInfo.decompressedLenght + gameInfo.compressedLenght;
                int  fileCount          = BitsHelper.GetNumberFromBytes(BitsHelper.ReadBytesFromFileStream(fs, gameInfo.FileSystemOffset + 12, 4).ToArray());
                long currentOffset      = BitsHelper.GetNumberFromBytes(BitsHelper.ReadBytesFromFileStream(fs, gameInfo.FileSystemOffset + 8, 4).ToArray()) + gameInfo.FileSystemOffset;
                for (int x = 0; x < fileCount; x++)
                {
                    int readLocation = 0;

                    // read file table entry
                    List <Byte> fileTableEntry = BitsHelper.ReadBytesFromFileStream(fs, currentOffset, fileTableEntrySize);

                    //Get File Name
                    string fileName = System.Text.Encoding.ASCII.GetString(fileTableEntry.ToArray(), readLocation, gameInfo.maxFileNameLenght).Replace("\0", "");

                    /// Get decompressed file size
                    readLocation += gameInfo.maxFileNameLenght;
                    byte[] decompressedSizeBytes = fileTableEntry.GetRange(readLocation, gameInfo.decompressedLenght).ToArray();
                    Array.Reverse(decompressedSizeBytes);
                    int decompressedSize = BitConverter.ToInt32(decompressedSizeBytes, 0);

                    //File Position
                    readLocation += gameInfo.decompressedLenght;
                    byte[] filePositionBytes = fileTableEntry.GetRange(readLocation, gameInfo.filePositionLength).ToArray();
                    Array.Reverse(filePositionBytes);
                    long filePosition = BitConverter.ToInt32(filePositionBytes, 0) + gameInfo.FileSystemOffset;

                    /// Get decompressed file size
                    readLocation += gameInfo.filePositionLength;
                    byte[] compressedSizeBytes = fileTableEntry.GetRange(readLocation, gameInfo.compressedLenght).ToArray();
                    Array.Reverse(compressedSizeBytes);
                    int compressedSize = BitConverter.ToInt32(compressedSizeBytes, 0);

                    midwayDecFileList.Add(new BlitzGameFile()
                    {
                        fileName            = fileName,
                        decompressedSize    = decompressedSize,
                        fileOffset          = filePosition,
                        compressedSize      = compressedSize,
                        fileTableEntryStart = currentOffset
                    }
                                          );
                    currentOffset += fileTableEntrySize;
                }
            }
            return(midwayDecFileList);
        }
Exemple #6
0
        /// <summary>
        /// Fill in bit vectors in the ITEM_IS_EXIST_TABLE according to a last positions in sequences.
        /// </summary>
        private void FillBitVectors()
        {
            uint itemOrder = 0;

            foreach (var item in _sequenceDatabase)
            {
                var itemBitmaps8 = item.Bitmaps8;
                for (int sid = 0; sid < item.Sequences8Count; sid++)
                {
                    SetBitVectorForItem(_bitVectors8, itemOrder, sid, BitsHelper.LastSetBit(itemBitmaps8[sid]));
                }

                var itemBitmaps16 = item.Bitmaps16;
                for (int sid = 0; sid < item.Sequences16Count; sid++)
                {
                    SetBitVectorForItem(_bitVectors16, itemOrder, sid, BitsHelper.LastSetBit(itemBitmaps16[sid]));
                }

                var itemBitmaps32 = item.Bitmaps32;
                for (int sid = 0; sid < item.Sequences32Count; sid++)
                {
                    SetBitVectorForItem(_bitVectors32, itemOrder, sid, BitsHelper.LastSetBit(itemBitmaps32[sid]));
                }

                var itemBitmaps64 = item.Bitmaps64;
                for (int sid = 0; sid < item.Sequences64Count; sid++)
                {
                    SetBitVectorForItem(_bitVectors64, itemOrder, sid, BitsHelper.LastSetBit(itemBitmaps64[sid]));
                }

                var itemBitmaps128 = item.Bitmaps128;
                for (int sid = 0; sid < item.Sequences128Count; sid++)
                {
                    SetBitVectorForItem(_bitVectors128, itemOrder, sid, BitsHelper.LastSetBit(itemBitmaps128[sid]));
                }

                itemOrder++;
            }
        }
        public static byte[] CreateNFLBlitz2000Header(int width, int height, bool containsAlpha, byte imageType, int irx = 0, int iry = 0)
        {
            List <byte> header = new List <byte>(new byte[headerSize]);

            header[typeIndex]     = type[0];
            header[typeIndex + 1] = type[1];
            header[versionIndex]  = (byte)version;
            if (containsAlpha)
            {
                header[flagIndex] = 0x04;
            }
            header[imageTypeIndex]   = imageType;
            header[widthHeaderIndex] = (byte)width;
            for (int x = widthHeaderIndex; x < widthHeaderIndex + 4; x++)
            {
                header[x] = BitsHelper.intToByteArray(width)[x - widthHeaderIndex];
            }
            for (int x = heightHeaderIndex; x < heightHeaderIndex + 4; x++)
            {
                header[x] = BitsHelper.intToByteArray(height)[x - heightHeaderIndex];
            }
            if (irx != 0)
            {
                for (int x = irxIndex; x < irxIndex + 4; x++)
                {
                    header[x] = BitsHelper.intToByteArray(irx)[x - irxIndex];
                }
            }
            if (iry != 0)
            {
                for (int x = iryIndex; x < iryIndex + 4; x++)
                {
                    header[x] = BitsHelper.intToByteArray(iry)[x - iryIndex];
                }
            }
            return(header.ToArray());
        }
Exemple #8
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);
            }
        }