/** * detectFormat_fopen(): Detect the format of a given ROM file. * @param filename Filename of the ROM file. * @return ROMType. */ ROMType ROM::detectFormat_fopen(const char* filename) { Compressor *cmp; list<CompressedFile> *files; ROMType rtype; // Open the ROM file using the compressor functions. cmp = new Compressor(filename); if (!cmp->isFileLoaded()) { // Cannot load the file. delete cmp; return (ROMType)0; } // Get the file information. files = cmp->getFileInfo(); // Check how many files are available. if (!files || files->empty()) { // No files. delete files; delete cmp; return (ROMType)0; } // Get the first file in the archive. // TODO: Store the compressed filename in ROM history. unsigned char detectBuf[2048]; cmp->getFile(&(*files->begin()), detectBuf, sizeof(detectBuf)); rtype = detectFormat(detectBuf); // Return the ROM type. delete files; delete cmp; return rtype; }
/** * loadROM(): Load a ROM file. * @param filename Filename of the ROM file. * @param interleaved If non-zero, the ROM is interleaved. * @return Pointer to Rom struct with the ROM information. */ ROMType ROM::loadROM(const char* filename, ROM_t** retROM) { Compressor *cmp; list<CompressedFile> *files; CompressedFile* selFile; ROMType rtype; // Set up the compressor. cmp = new Compressor(filename, true); if (!cmp->isFileLoaded()) { // Error loading the file. delete cmp; Game = NULL; *retROM = NULL; return (ROMType)0; } // Get the file information. files = cmp->getFileInfo(); // Check how many files are available. if (!files || files->empty()) { // No files in the archive. // TODO: For 7z, suggest setting the 7z binary filename. GensUI::msgBox("No files were detected in this archive.", "No Files Detected"); if (files) delete files; if (cmp) delete cmp; files = NULL; cmp = NULL; Game = NULL; *retROM = NULL; return (ROMType)0; } else if (files->size() == 1) { // One file is in the archive. Load it. selFile = &(*files->begin()); } else { #if !defined(GENS_UI_OPENEMU) // More than one file is in the archive. Load it. // TODO: Improve this! #if defined(GENS_UI_GTK) ZipSelectDialog *zip = new ZipSelectDialog(GTK_WINDOW(gens_window)); #elif defined(GENS_UI_WIN32) ZipSelectDialog *zip = new ZipSelectDialog(Gens_hWnd); #else #error Can't determine UI. #endif selFile = zip->getFile(files); delete zip; #endif } if (!selFile) { // No file was selected and/or Cancel was clicked. delete files; delete cmp; Game = NULL; *retROM = NULL; return (ROMType)0; } // Determine the ROM type. unsigned char detectBuf[2048]; cmp->getFile(&(*selFile), detectBuf, sizeof(detectBuf)); rtype = detectFormat(detectBuf); if (rtype < MD_ROM || rtype >= SegaCD_Image) { // Unknown ROM type, or this is a SegaCD image. delete files; delete cmp; Game = NULL; *retROM = NULL; return rtype; } // If the ROM is larger than 6MB (+512 bytes for SMD interleaving), don't load it. if (selFile->filesize > ((6 * 1024 * 1024) + 512)) { GensUI::msgBox("ROM files larger than 6 MB are not supported.", "ROM File Error"); delete files; delete cmp; Game = NULL; *retROM = NULL; return (ROMType)0; } myROM = (ROM_t*)malloc(sizeof(ROM_t)); if (!myROM) { // Memory allocation error delete files; delete cmp; Game = NULL; *retROM = NULL; return (ROMType)0; } //fseek(ROM_File, 0, SEEK_SET); // Clear the ROM buffer and load the ROM. memset(Rom_Data, 0, 6 * 1024 * 1024); int loadedSize = cmp->getFile(&(*selFile), Rom_Data, selFile->filesize); if (loadedSize != selFile->filesize) { // Incorrect filesize. GensUI::msgBox("Error loading the ROM file.", "ROM File Error"); free(myROM); delete files; delete cmp; myROM = NULL; Game = NULL; *retROM = NULL; return (ROMType)0; } //fclose(ROM_File); updateROMName(filename); Rom_Size = selFile->filesize; // Delete the compression objects. delete files; delete cmp; // Deinterleave the ROM, if necessary. if (rtype == MD_ROM_Interleaved || rtype == _32X_ROM_Interleaved) deinterleaveSMD(); fillROMInfo(); *retROM = myROM; return rtype; }