// ---------------------------------------------------------------------------- // Load // ---------------------------------------------------------------------------- bool bios_Load(std::string filename) { if(filename.empty( ) || filename.length( ) == 0) { logger_LogError("Bios filename is invalid.", BIOS_SOURCE); return false; } bios_Release( ); logger_LogInfo("Opening bios file " + filename + ".", BIOS_SOURCE); bios_size = archive_GetUncompressedFileSize(filename); if(bios_size == 0) { FILE* file = fopen(filename.c_str( ), "rb"); if(file == NULL) { #ifndef WII logger_LogError("Failed to open the bios file " + filename + " for reading.", BIOS_SOURCE); #endif return false; } if(fseek(file, 0, SEEK_END)) { fclose(file); logger_LogError("Failed to find the end of the bios file.", BIOS_SOURCE); return false; } bios_size = ftell(file); if(fseek(file, 0, SEEK_SET)) { fclose(file); logger_LogError("Failed to find the size of the bios file.", BIOS_SOURCE); return false; } bios_data = new byte[bios_size]; if(fread(bios_data, 1, bios_size, file) != bios_size && ferror(file)) { fclose(file); logger_LogError("Failed to read the bios data.", BIOS_SOURCE); bios_Release( ); return false; } fclose(file); } else { bios_data = new byte[bios_size]; archive_Uncompress(filename, bios_data, bios_size); } bios_filename = filename; return true; }
// ---------------------------------------------------------------------------- // GetUncompressedFileSize // ---------------------------------------------------------------------------- uint archive_GetUncompressedFileSize(std::string filename) { if(filename.empty( ) || filename.size( ) == 0) { logger_LogError("Zip filename is invalid.", ARCHIVE_SOURCE); return 0; } unzFile file = unzOpen(filename.c_str( )); if(file == NULL) { logger_LogInfo("Filename " + filename + " is not a valid zip file.", ARCHIVE_SOURCE); return 0; } int result = unzGoToFirstFile(file); if(result != UNZ_OK) { logger_LogInfo("Failed to find the first file within the zip file.", ARCHIVE_SOURCE); logger_LogInfo("Result: " + result, ARCHIVE_SOURCE); unzClose(file); return 0; } unz_file_info_s zipInfo = {0}; char buffer[_MAX_PATH] = {0}; result = unzGetCurrentFileInfo(file, &zipInfo, buffer, _MAX_PATH, NULL, 0, NULL, 0); if(result != UNZ_OK) { logger_LogInfo("Failed to retrieve the current zipped file info.", ARCHIVE_SOURCE); logger_LogInfo("Result: " + result, ARCHIVE_SOURCE); unzClose(file); return 0; } uint size = zipInfo.uncompressed_size; unzClose(file); return size; }
// ---------------------------------------------------------------------------- // Load // ---------------------------------------------------------------------------- bool database_Load(std::string digest) { if(database_enabled) { logger_LogInfo("Accessing database " + database_filename + ".", DATABASE_SOURCE); FILE* file = fopen(database_filename.c_str( ), "r"); if(file == NULL) { logger_LogError("Failed to open the database for reading.", DATABASE_SOURCE); return false; } char buffer[256]; while(fgets(buffer, 256, file) != NULL) { std::string line = buffer; if(line.compare(1, 32, digest.c_str( )) == 0) { std::string entry[11]; for(int index = 0; index < 11; index++) { fgets(buffer, 256, file); entry[index] = common_Remove(buffer, '\n'); entry[index] = common_Remove(entry[index], '\r'); } cartridge_title = database_GetValue(entry[0]); cartridge_type = common_ParseByte(database_GetValue(entry[1])); cartridge_pokey = common_ParseBool(database_GetValue(entry[2])); cartridge_controller[0] = common_ParseByte(database_GetValue(entry[3])); cartridge_controller[1] = common_ParseByte(database_GetValue(entry[4])); cartridge_region = common_ParseByte(database_GetValue(entry[5])); cartridge_flags = common_ParseUint(database_GetValue(entry[6])); // Optionally load the lightgun crosshair offsets, hblank, dual analog for(int index = 7; index < 11; index++) { if(entry[index].find("crossx") != std::string::npos) { cartridge_crosshair_x = common_ParseInt(database_GetValue(entry[index])); } if(entry[index].find("crossy") != std::string::npos) { cartridge_crosshair_y = common_ParseInt(database_GetValue(entry[index])); } if(entry[index].find("hblank") != std::string::npos) { cartridge_hblank = common_ParseInt(database_GetValue(entry[index])); } if(entry[index].find("dualanalog") != std::string::npos) { cartridge_dualanalog = common_ParseBool(database_GetValue(entry[index])); } } break; } } fclose(file); } return true; }
// ---------------------------------------------------------------------------- // Uncompress // ---------------------------------------------------------------------------- bool archive_Uncompress(std::string filename, byte* data, uint size) { if(filename.empty( ) || filename.size( ) == 0) { logger_LogError("Zip filename is invalid.", ARCHIVE_SOURCE); return false; } if(data == NULL) { logger_LogError("Data parameter is invalid.", ARCHIVE_SOURCE); return false; } unzFile file = unzOpen(filename.c_str( )); if(file == NULL) { logger_LogInfo("Filename " + filename + " is not a valid zip file.", ARCHIVE_SOURCE); return false; } int result = unzGoToFirstFile(file); if(result != UNZ_OK) { logger_LogInfo("Failed to find the first file within the zip file " + filename + ".", ARCHIVE_SOURCE); logger_LogInfo("Result: " + result, ARCHIVE_SOURCE); unzClose(file); return false; } result = unzOpenCurrentFile(file); if(result != UNZ_OK) { logger_LogInfo("Failed to open the first file within the zip file " + filename + ".", ARCHIVE_SOURCE); logger_LogInfo("Result: " + result, ARCHIVE_SOURCE); unzClose(file); return false; } result = unzReadCurrentFile(file, data, size); if(result != size) { logger_LogInfo("Failed to read first file data within the zip file " + filename + ".", ARCHIVE_SOURCE); logger_LogInfo("Result: " + result, ARCHIVE_SOURCE); unzCloseCurrentFile(file); unzClose(file); return false; } unzCloseCurrentFile(file); unzClose(file); return true; }
// ---------------------------------------------------------------------------- // Compress // ---------------------------------------------------------------------------- bool archive_Compress(std::string zipFilename, std::string filename, const byte* data, uint size) { if(zipFilename.empty( ) || zipFilename.size( ) == 0) { logger_LogError("Zip filename is invalid.", ARCHIVE_SOURCE); return false; } if(filename.empty( ) || filename.size( ) == 0) { logger_LogError("Filename is invalid.", ARCHIVE_SOURCE); return false; } if(data == NULL) { logger_LogError("Data parameter is invalid.", ARCHIVE_SOURCE); return false; } zipFile file = zipOpen(zipFilename.c_str( ), APPEND_STATUS_CREATE); if(file == NULL) { logger_LogInfo("Failed to create the zip file " + zipFilename + ".", ARCHIVE_SOURCE); return false; } zip_fileinfo fileInfo = {0}; fileInfo.dosDate = 1; int result = zipOpenNewFileInZip(file, filename.c_str( ), &fileInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION); if(result != ZIP_OK) { logger_LogInfo("Failed to open a new file within the zip file " + filename + ".", ARCHIVE_SOURCE); logger_LogInfo("Result: " + result, ARCHIVE_SOURCE); zipClose(file, "Failed to compress."); return false; } result = zipWriteInFileInZip(file, data, size); if(result != ZIP_OK) { logger_LogInfo("Failed to write data to the zip file " + filename + ".", ARCHIVE_SOURCE); zipCloseFileInZip(file); zipClose(file, "Failed to compress."); return false; } zipCloseFileInZip(file); zipClose(file, "Comment"); return true; }
// ---------------------------------------------------------------------------- // Load // ---------------------------------------------------------------------------- bool palette_Load(std::string filename) { if(filename.empty( ) || filename.length( ) == 0) { logger_LogError("Palette filename is invalid.", PALETTE_SOURCE); return false; } logger_LogInfo("Opening palette file " + filename + "."); FILE* file = fopen(filename.c_str( ), "rb"); if(file == NULL) { logger_LogError("Failed to open the palette file " + filename + " for reading.", PALETTE_SOURCE); return false; } if(fread(palette_data, 1, PALETTE_SIZE, file) != PALETTE_SIZE) { fclose(file); logger_LogError("Failed to read the palette data.", PALETTE_SOURCE); return false; } fclose(file); palette_filename = filename; return true; }
// ---------------------------------------------------------------------------- // Load // ---------------------------------------------------------------------------- bool bios_Load(char* filename) { if(strlen(filename) == 0) { #if 0 logger_LogError("Bios filename is invalid.", BIOS_SOURCE); #endif return false; } bios_Release( ); #if 0 logger_LogInfo("Opening bios file " + filename + "."); #endif bios_size = archive_GetUncompressedFileSize(filename); if(bios_size == 0) { FILE* file = fopen(filename, "rb"); if(file == NULL) { #if 0 logger_LogError("Failed to open the bios file " + filename + " for reading.", BIOS_SOURCE); #endif return false; } fseek(file, 0, SEEK_END); #if 0 if(fseek(file, 0, SEEK_END)) { fclose(file); logger_LogError("Failed to find the end of the bios file.", BIOS_SOURCE); return false; } #endif bios_size = ftell(file); fseek(file, 0, SEEK_SET); #if 0 if(fseek(file, 0, SEEK_SET)) { fclose(file); logger_LogError("Failed to find the size of the bios file.", BIOS_SOURCE); return false; } #endif bios_data = (char *) malloc(bios_size); fread(bios_data, 1, bios_size, file); #if 0 if(fread(bios_data, 1, bios_size, file) != bios_size && ferror(file)) { fclose(file); logger_LogError("Failed to read the bios data.", BIOS_SOURCE); bios_Release( ); return false; } #endif fclose(file); } else { bios_data = (char *) malloc(bios_size); archive_Uncompress(filename, bios_data, bios_size); } strcpy(bios_filename,filename); return true; }