public string ExtractRom()
        {
            // Quiet down the console during the extraction valid rom check
            var consoleOutputStream = Console.Out;

            Console.SetOut(TextWriter.Null);
            if (this.IsValidRom())
            {
                Console.SetOut(consoleOutputStream);

                // Get the rom name from the dictionary
                romName = gbaDictionary.getRomName(romCode);

                if (String.IsNullOrEmpty(romName))
                {
                    int titleLength = 0;
                    while (titleLength < HEADER_TITLE_LENGTH)
                    {
                        if (gbaHeader[HEADER_TITLE_OFFSET + titleLength] == 0x00)
                        {
                            break;
                        }

                        titleLength++;
                    }

                    romName = Encoding.ASCII.GetString(gbaHeader, HEADER_TITLE_OFFSET, titleLength);

                    // If a rom name could not be determined from the dictionary or rom, prompt the user
                    if (String.IsNullOrEmpty(romName))
                    {
                        Console.WriteLine("Could not determine GBA rom name, please enter your desired filename:");
                        romName = Console.ReadLine();
                    }
                }

                Console.WriteLine("GBA Rom Code: " + romCode);
                Console.WriteLine("GBA Title: " + romName);

                extractedRomPath = romName + ".gba";

                if (File.Exists(extractedRomPath))
                {
                    File.Delete(extractedRomPath);
                }

                Console.WriteLine("Writing to " + extractedRomPath + "...");

                File.Move(psbFile.DecompressedPath, extractedRomPath);

                Console.WriteLine("GBA rom has been created successfully at " + extractedRomPath);

                return(extractedRomPath);
            }

            return("");
        }
Esempio n. 2
0
        public string ExtractRom()
        {
            // Quiet down the console during the extraction valid rom check
            var consoleOutputStream = Console.Out;

            Console.SetOut(TextWriter.Null);
            if (this.IsValidRom())
            {
                Console.SetOut(consoleOutputStream);

                // Browse to the romPosition in the file and look for the WUP
                // string 16 bytes before
                using (FileStream fs = new FileStream(rpxFile.DecompressedPath,
                                                      FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader br = new BinaryReader(fs, new ASCIIEncoding()))
                    {
                        br.BaseStream.Seek(vcNamePosition, SeekOrigin.Begin);

                        // read in the VC rom name
                        vcName  = Encoding.ASCII.GetString(br.ReadBytes(VC_NAME_LENGTH));
                        romName = nesDictionary.getRomName(vcName);

                        // If a rom name could not be determined, prompt the user
                        if (String.IsNullOrEmpty(romName))
                        {
                            Console.WriteLine("Could not determine rom name, " +
                                              "please enter your desired filename:");
                            romName = Console.ReadLine();
                        }

                        Console.WriteLine("Virtual Console Title: " + vcName);
                        Console.WriteLine("FDS Title: " + romName);

                        extractedRomPath = romName + ".fds";

                        br.ReadBytes(VC_NAME_PADDING);

                        // We are currently at the FDS header's position again,
                        // read past it
                        br.ReadBytes(FDS_HEADER_LENGTH);

                        // Determine the rom's size - find number of disk sides
                        //
                        // All FDS disk sides are 65500 bytes (0xFFDC bytes)
                        //
                        // These are in QuickDisk format, which are either
                        // 0x10000, 0x20000, 0x30000, or 0x40000 bytes in length, depending on number of sides
                        using (FileStream fsDskChk = new FileStream(rpxFile.DecompressedPath,
                                                                    FileMode.Open, FileAccess.Read))
                        {
                            using (BinaryReader brDskChk = new BinaryReader(fsDskChk, new ASCIIEncoding()))
                            {
                                // Bool to account for correct header
                                bool headerValid = true;

                                // First side is known to exist, so seek ahead to next side
                                brDskChk.BaseStream.Seek(vcNamePosition, SeekOrigin.Begin);
                                brDskChk.ReadBytes(VC_NAME_LENGTH);
                                brDskChk.ReadBytes(VC_NAME_PADDING);
                                // OK, currently at beginning of first side
                                // Now, read to the next side
                                brDskChk.ReadBytes(qdDiskSize);

                                // Check for Nintendo header until it doesn't match
                                while (headerValid)
                                {
                                    // Check header
                                    // Ensure the rest of the header is valid, except final byte (manufacturer code)
                                    // Read in 2nd disk header
                                    byte[] headerBuffer = brDskChk.ReadBytes(FDS_HEADER_LENGTH);

                                    // Iterate through buffer and header
                                    for (int i = 0; i < FDS_HEADER_CHECK.Length && headerValid; i++)
                                    {
                                        // Compare byte at buffer position to corresponding byte in header
                                        if (headerBuffer[i] != FDS_HEADER_CHECK[i])
                                        {
                                            // If they don't match, header is wrong - end loops
                                            headerValid = false;
                                        }
                                    }

                                    // If the header is valid, increment side count and continue
                                    if (headerValid)
                                    {
                                        numberOfSides++;
                                        // Now, read to the next side - account for header already read
                                        brDskChk.ReadBytes(qdDiskSize - FDS_HEADER_LENGTH);
                                    }
                                }
                            }
                        }

                        // Set size of full QD and FDS game using number of disks
                        fullGameDataQD  = new byte[qdDiskSize * numberOfSides];
                        fullGameDataFDS = new byte[fdsDiskSize * numberOfSides];

                        Console.WriteLine("Number of Disks: " + numberOfSides);
                        Console.WriteLine("Getting rom data...");

                        // From the position at the end of the header, read the rest of the rom
                        qdRomData = br.ReadBytes(-FDS_HEADER_LENGTH + qdDiskSize * numberOfSides);

                        // Copy the FDS header (determined by IsValidRom) and the rom data to a full-game byte array
                        Buffer.BlockCopy(fdsRomHeader, 0, fullGameDataQD, 0, fdsRomHeader.Length);
                        Buffer.BlockCopy(qdRomData, 0, fullGameDataQD, fdsRomHeader.Length, qdRomData.Length);

                        Console.WriteLine("Writing to " + extractedRomPath + "...");

                        using (BinaryWriter bw = new BinaryWriter(File.Open(
                                                                      extractedRomPath, FileMode.Create)))
                        {
                            // Einstein95's qd2fds.py
                            // Convert QD to FDS
                            //
                            // Convert each side of disk, then insert each into FDS output game data array
                            for (int disk = 0; disk < numberOfSides; disk++)
                            {
                                // Get current disk data
                                byte[] currentDisk = new byte[qdDiskSize];
                                Buffer.BlockCopy(fullGameDataQD, disk * qdDiskSize, currentDisk, 0, qdDiskSize);

                                // Remove bytes at offsets 0x38 and 0x39
                                for (int i = 0x38; i + 2 < currentDisk.Length; i++)
                                {
                                    currentDisk[i]     = currentDisk[i + 2];
                                    currentDisk[i + 2] = 0;
                                }

                                int position = 0x3A;

                                try
                                {
                                    while (currentDisk[position + 2] == 3)
                                    {
                                        // Delete 2 bytes
                                        for (int i = position; i + 2 < currentDisk.Length; i++)
                                        {
                                            currentDisk[i]     = currentDisk[i + 2];
                                            currentDisk[i + 2] = 0;
                                        }

                                        int    end2         = currentDisk[position + 0xD];
                                        int    end1         = currentDisk[position + 0xE];
                                        string fileSizeText = end1.ToString("X2") + end2.ToString("X2");
                                        int    fileSize     = int.Parse(fileSizeText, System.Globalization.NumberStyles.HexNumber);

                                        // Delete 2 bytes
                                        for (int i = position + 0x10; i + 2 < currentDisk.Length; i++)
                                        {
                                            currentDisk[i]     = currentDisk[i + 2];
                                            currentDisk[i + 2] = 0;
                                        }

                                        position += 0x11 + fileSize;
                                    }
                                }
                                catch (IndexOutOfRangeException)
                                {
                                }

                                // Delete 2 bytes
                                for (int i = position; i + 2 < currentDisk.Length; i++)
                                {
                                    currentDisk[i]     = currentDisk[i + 2];
                                    currentDisk[i + 2] = 0;
                                }

                                // Copy current disk data to the full FDS game data array at the correct position for the disk
                                Buffer.BlockCopy(currentDisk, 0, fullGameDataFDS, disk * fdsDiskSize, fdsDiskSize);
                            }

                            Console.WriteLine("Total FDS rom size: " + fullGameDataFDS.Length + " Bytes");

                            Console.WriteLine("Writing rom data...");
                            bw.Write(fullGameDataFDS);
                        }

                        Console.WriteLine("Famicom Disk System rom has been " +
                                          "created successfully at " + extractedRomPath);
                    }
                }
            }

            return(extractedRomPath);
        }
Esempio n. 3
0
        public string ExtractRom()
        {
            // Quiet down the console during the extraction valid rom check
            var consoleOutputStream = Console.Out;

            Console.SetOut(TextWriter.Null);
            if (this.IsValidRom())
            {
                Console.SetOut(consoleOutputStream);

                // Browse to the romPosition in the file and look for the WUP string 16 bytes before
                using (FileStream fs = new FileStream(rpxFile.DecompressedPath, FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader br = new BinaryReader(fs, new ASCIIEncoding()))
                    {
                        br.BaseStream.Seek(vcNamePosition, SeekOrigin.Begin);

                        // read in the VC rom name
                        vcName  = Encoding.ASCII.GetString(br.ReadBytes(VC_NAME_LENGTH));
                        romName = nesDictionary.getRomName(vcName);

                        // If a rom name could not be determined, prompt the user
                        if (String.IsNullOrEmpty(romName))
                        {
                            Console.WriteLine("Could not determine NES rom name, please enter your desired filename:");
                            romName = Console.ReadLine();
                        }

                        Console.WriteLine("Virtual Console Title: " + vcName);
                        Console.WriteLine("NES Title: " + romName);

                        extractedRomPath = romName + ".nes";

                        br.ReadBytes(VC_NAME_PADDING);

                        // We are currently at the NES header's position again, read past it
                        br.ReadBytes(NES_HEADER_LENGTH);

                        // Determine the NES rom's size
                        Console.WriteLine("Getting number of PRG and CHR pages...");

                        byte prgPages = nesRomHeader[PRG_PAGE_OFFSET];
                        byte chrPages = nesRomHeader[CHR_PAGE_OFFSET];

                        Console.WriteLine("PRG Pages: " + prgPages);
                        Console.WriteLine("CHR Pages: " + chrPages);

                        int prgPageSize = prgPages * PRG_PAGE_SIZE;
                        int chrPageSize = chrPages * CHR_PAGE_SIZE;

                        int romSize = prgPageSize + chrPageSize + NES_HEADER_LENGTH;
                        Console.WriteLine("Total NES rom size: " + romSize + " Bytes");

                        // Fix the NES header
                        Console.WriteLine("Fixing VC NES Header...");
                        nesRomHeader[BROKEN_NES_HEADER_OFFSET] = CHARACTER_BREAK;

                        Console.WriteLine("Getting rom data...");
                        nesRomData = br.ReadBytes(romSize - NES_HEADER_LENGTH);

                        Console.WriteLine("Writing to " + extractedRomPath + "...");

                        using (BinaryWriter bw = new BinaryWriter(File.Open(extractedRomPath, FileMode.Create)))
                        {
                            Console.WriteLine("Writing NES rom header...");
                            bw.Write(nesRomHeader, 0, NES_HEADER_LENGTH);
                            Console.WriteLine("Writing NES rom data...");
                            bw.Write(nesRomData);
                        }

                        Console.WriteLine("NES rom has been created successfully at " + extractedRomPath);
                    }
                }
            }

            return(extractedRomPath);
        }
        public string ExtractRom()
        {
            // Quiet down the console during the extraction valid rom check
            var consoleOutputStream = Console.Out;

            Console.SetOut(TextWriter.Null);
            if (this.IsValidRom())
            {
                Console.SetOut(consoleOutputStream);

                byte[] header = new byte[SNES_HEADER_LENGTH];

                switch (headerType)
                {
                case SnesHeaderType.HiROM:
                    header = snesHiRomHeader;
                    break;

                case SnesHeaderType.LoROM:
                    header = snesLoRomHeader;
                    break;
                }

                // Attempt to get the game title from the dictionary
                romName = snesDictionary.getRomName(vcName);

                if (String.IsNullOrEmpty(romName))
                {
                    romName = GetRomName(header);

                    // If a rom name could not be determined from the dictionary or rom, prompt the user
                    if (String.IsNullOrEmpty(romName))
                    {
                        Console.WriteLine("Could not determine SNES rom name, please enter your desired filename:");
                        romName = Console.ReadLine();
                    }
                }

                Console.WriteLine("Virtual Console Title: " + vcName);
                Console.WriteLine("SNES Title: " + romName);

                extractedRomPath = romName + ".smc";

                Console.WriteLine("Getting size of rom...");
                int romSize = GetRomSize(header[HEADER_ROM_SIZE_OFFSET]);

                Console.WriteLine("Total SNES rom size: " + romSize + " Bytes");

                Console.WriteLine("Getting rom data...");

                byte[] pcmData;

                // Browse to the romPosition in the file
                using (FileStream fs = new FileStream(rpxFile.DecompressedPath, FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader br = new BinaryReader(fs, new ASCIIEncoding()))
                    {
                        br.BaseStream.Seek(romPosition, SeekOrigin.Begin);

                        snesRomData = br.ReadBytes(romSize);
                        pcmData     = br.ReadBytes((int)(br.BaseStream.Length - romPosition + romSize));
                    }
                }

                SnesPcmExtractor pcmExtract = new SnesPcmExtractor(snesRomData, pcmData);
                snesRomData = pcmExtract.ExtractPcmData();

                Console.WriteLine("Writing to " + extractedRomPath + "...");

                using (BinaryWriter bw = new BinaryWriter(File.Open(extractedRomPath, FileMode.Create)))
                {
                    Console.WriteLine("Writing SNES rom data...");
                    bw.Write(snesRomData);
                }

                Console.WriteLine("SNES rom has been created successfully at " + extractedRomPath);
            }

            return(extractedRomPath);
        }
        public string ExtractRom()
        {
            // Quiet down the console during the extraction valid rom check
            var consoleOutputStream = Console.Out;

            Console.SetOut(TextWriter.Null);
            if (this.IsValidRom())
            {
                Console.SetOut(consoleOutputStream);

                // Get the rom name from the dictionary
                romName = gbaDictionary.getRomName(romCode);

                if (String.IsNullOrEmpty(romName))
                {
                    int titleLength = 0;
                    while (titleLength < HEADER_TITLE_LENGTH)
                    {
                        if (gbaHeader[HEADER_TITLE_OFFSET + titleLength] == 0x00)
                        {
                            break;
                        }

                        titleLength++;
                    }

                    romName = Encoding.ASCII.GetString(gbaHeader, HEADER_TITLE_OFFSET, titleLength);

                    // If a rom name could not be determined from the dictionary or rom, prompt the user
                    if (String.IsNullOrEmpty(romName))
                    {
                        Console.WriteLine("Could not determine GBA rom name, please enter your desired filename:");
                        romName = Console.ReadLine();
                    }
                }

                Console.WriteLine("GBA Rom Code: " + romCode);
                Console.WriteLine("GBA Title: " + romName);

                extractedRomPath = romName + ".gba";

                if (File.Exists(extractedRomPath))
                {
                    File.Delete(extractedRomPath);
                }

                Console.WriteLine("Writing to " + extractedRomPath + "...");

                File.Move(psbFile.DecompressedPath, extractedRomPath);

                Console.WriteLine("GBA rom has been created successfully at " + extractedRomPath);

                // Per issue https://github.com/wheatevo/wiiu-vc-extractor/issues/44 this should be unnecessary.
                // Leaving the code in for now to allow for it to be enabled via an option in the future if desired.
                // ensure the first 4 bytes of the rom file are 0x2E0000EA and then replace them if they are not
                // FixEntryPoint(extractedRomPath);

                return(extractedRomPath);
            }

            return("");
        }