diff --git a/src/common/FilePaths.hpp b/src/common/FilePaths.hpp index e6a9d4cec..51173c057 100644 --- a/src/common/FilePaths.hpp +++ b/src/common/FilePaths.hpp @@ -46,21 +46,22 @@ static inline void CxbxResolveHostToFullPath(std::string& file_path, std::string void CxbxrInitFilePaths() { if (g_Settings) { - std::string dataLoc = g_Settings->GetDataLocation(); - std::strncpy(szFolder_CxbxReloadedData, dataLoc.c_str(), dataLoc.length() + 1); + g_DataFilePath = g_Settings->GetDataLocation(); } else { - g_EmuShared->GetDataLocation(szFolder_CxbxReloadedData); + char dataLoc[MAX_PATH]; + g_EmuShared->GetDataLocation(dataLoc); + g_DataFilePath = dataLoc; } // Make sure our data folder exists : - bool result = std::filesystem::exists(szFolder_CxbxReloadedData); - if (!result && !std::filesystem::create_directory(szFolder_CxbxReloadedData)) { + bool result = std::filesystem::exists(g_DataFilePath); + if (!result && !std::filesystem::create_directory(g_DataFilePath)) { CxbxKrnlCleanup("%s : Couldn't create Cxbx-Reloaded's data folder!", __func__); } // Make sure the EmuDisk folder exists - g_DiskBasePath = std::string(szFolder_CxbxReloadedData) + std::string("\\EmuDisk"); + g_DiskBasePath = g_DataFilePath + "\\EmuDisk"; result = std::filesystem::exists(g_DiskBasePath); if (!result && !std::filesystem::create_directory(g_DiskBasePath)) { CxbxKrnlCleanup("%s : Couldn't create Cxbx-Reloaded EmuDisk folder!", __func__); @@ -69,7 +70,7 @@ void CxbxrInitFilePaths() g_DiskBasePath = std::filesystem::path(g_DiskBasePath).append("").string(); // Make sure the EmuDMu folder exists - g_MuBasePath = std::string(szFolder_CxbxReloadedData) + std::string("\\EmuMu"); + g_MuBasePath = g_DataFilePath + "\\EmuMu"; result = std::filesystem::exists(g_MuBasePath); if (!result && !std::filesystem::create_directory(g_MuBasePath)) { CxbxKrnlCleanup("%s : Couldn't create Cxbx-Reloaded EmuMu folder!", __func__); @@ -77,17 +78,52 @@ void CxbxrInitFilePaths() CxbxResolveHostToFullPath(g_MuBasePath, "Cxbx-Reloaded's EmuMu directory"); g_MuBasePath = std::filesystem::path(g_MuBasePath).append("").string(); - snprintf(szFilePath_EEPROM_bin, MAX_PATH, "%s\\EEPROM.bin", szFolder_CxbxReloadedData); + snprintf(szFilePath_EEPROM_bin, MAX_PATH, "%s\\EEPROM.bin", g_DataFilePath.c_str()); GetModuleFileName(GetModuleHandle(nullptr), szFilePath_CxbxReloaded_Exe, MAX_PATH); } +// Loads a keys.bin file as generated by dump-xbox +// See https://github.com/JayFoxRox/xqemu-tools/blob/master/dump-xbox.c +static void LoadXboxKeys() +{ + std::string keys_path = g_DataFilePath + "\\keys.bin"; + + // Attempt to open Keys.bin + FILE* fp = fopen(keys_path.c_str(), "rb"); + + if (fp != nullptr) { + // Determine size of Keys.bin + xbox::XBOX_KEY_DATA keys[2]; + fseek(fp, 0, SEEK_END); + long size = ftell(fp); + rewind(fp); + + // If the size of Keys.bin is correct (two keys), read it + if (size == xbox::XBOX_KEY_LENGTH * 2) { + fread(keys, xbox::XBOX_KEY_LENGTH, 2, fp); + + memcpy(xbox::XboxEEPROMKey, &keys[0], xbox::XBOX_KEY_LENGTH); + memcpy(xbox::XboxCertificateKey, &keys[1], xbox::XBOX_KEY_LENGTH); + } + else { + EmuLog(LOG_LEVEL::WARNING, "Keys.bin has an incorrect filesize. Should be %d bytes", xbox::XBOX_KEY_LENGTH * 2); + } + + fclose(fp); + return; + } + + // If we didn't already exit the function, keys.bin could not be loaded + EmuLog(LOG_LEVEL::WARNING, "Failed to load Keys.bin. Cxbx-Reloaded will be unable to read Save Data from a real Xbox"); +} + static HANDLE hMapDataHash = nullptr; static bool CxbxLockFilePath() { std::stringstream filePathHash("Local\\"); - uint64_t hashValue = XXH3_64bits(szFolder_CxbxReloadedData, strlen(szFolder_CxbxReloadedData) + 1); + uint64_t hashValue = XXH3_64bits(g_DataFilePath.c_str(), g_DataFilePath.length() + 1); if (!hashValue) { CxbxKrnlCleanup("%s : Couldn't generate Cxbx-Reloaded's data folder hash!", __func__); } diff --git a/src/core/hle/Intercept.cpp b/src/core/hle/Intercept.cpp index 4c28dfeb5..35dd38c7d 100644 --- a/src/core/hle/Intercept.cpp +++ b/src/core/hle/Intercept.cpp @@ -381,7 +381,7 @@ void EmuHLEIntercept(Xbe::Header *pXbeHeader) << std::endl; // Make sure the Symbol Cache directory exists - std::string cachePath = std::string(szFolder_CxbxReloadedData) + "\\SymbolCache\\"; + std::string cachePath = g_DataFilePath + "\\SymbolCache\\"; if (!std::filesystem::exists(cachePath) && !std::filesystem::create_directory(cachePath)) { CxbxKrnlCleanup("Couldn't create Cxbx-Reloaded SymbolCache folder!"); } diff --git a/src/core/kernel/init/CxbxKrnl.cpp b/src/core/kernel/init/CxbxKrnl.cpp index 1a213291f..1e0bfdce9 100644 --- a/src/core/kernel/init/CxbxKrnl.cpp +++ b/src/core/kernel/init/CxbxKrnl.cpp @@ -94,7 +94,7 @@ Xbe::Certificate *g_pCertificate = NULL; static std::vector g_hThreads; char szFilePath_CxbxReloaded_Exe[MAX_PATH] = { 0 }; -char szFolder_CxbxReloadedData[MAX_PATH] = { 0 }; +std::string g_DataFilePath; char szFilePath_EEPROM_bin[MAX_PATH] = { 0 }; char szFilePath_Xbe[xbox::max_path*2] = { 0 }; // NOTE: LAUNCH_DATA_HEADER's szLaunchPath is xbox::max_path*2 = 520 @@ -223,9 +223,6 @@ void RestoreExeImageHeader() ExeOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] = NewOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS]; } -// Forward declaration to avoid moving the definition of LoadXboxKeys -void LoadXboxKeys(std::string path); - // Returns the Win32 error in string format. Returns an empty string if there is no error. std::string CxbxGetErrorCodeAsString(DWORD errorCode) { @@ -801,8 +798,8 @@ void CxbxKrnlEmulate(unsigned int reserved_systems, blocks_reserved_t blocks_res RestoreExeImageHeader(); } - // Load Per-Xbe Keys from the Cxbx-Reloaded AppData directory - LoadXboxKeys(szFolder_CxbxReloadedData); + // Load Xbox Keys from the Cxbx-Reloaded AppData directory + LoadXboxKeys(); EEPROM = CxbxRestoreEEPROM(szFilePath_EEPROM_bin); if (EEPROM == nullptr) @@ -844,7 +841,7 @@ void CxbxKrnlEmulate(unsigned int reserved_systems, blocks_reserved_t blocks_res // which means we cannot rely on that alone. if (BootFlags == BOOT_NONE && std::filesystem::exists(xbeDirectory / "boot.id")) { - std::string chihiroMediaBoardRom = std::string(szFolder_CxbxReloadedData) + std::string("/EmuDisk/") + MediaBoardRomFile; + std::string chihiroMediaBoardRom = g_DataFilePath + "/EmuDisk/" + MediaBoardRomFile; if (!std::filesystem::exists(chihiroMediaBoardRom)) { CxbxKrnlCleanup("Chihiro Media Board ROM (fpr21042_m29w160et.bin) could not be found"); } @@ -866,8 +863,8 @@ void CxbxKrnlEmulate(unsigned int reserved_systems, blocks_reserved_t blocks_res // Extract SEGABOOT_OLD.XBE and SEGABOOT.XBE from Media Rom // We only do this if SEGABOOT_OLD and SEGABOOT.XBE are *not* already present - std::string chihiroSegaBootOld = std::string(szFolder_CxbxReloadedData) + std::string("/EmuDisk/") + MediaBoardSegaBoot0; - std::string chihiroSegaBootNew = std::string(szFolder_CxbxReloadedData) + std::string("/EmuDisk/") + MediaBoardSegaBoot1; + std::string chihiroSegaBootOld = g_DataFilePath + "/EmuDisk/" + MediaBoardSegaBoot0; + std::string chihiroSegaBootNew = g_DataFilePath + "/EmuDisk/" + MediaBoardSegaBoot1; if (!std::filesystem::exists(chihiroSegaBootOld) || !std::filesystem::exists(chihiroSegaBootNew)) { FILE* fpSegaBootOld = fopen(chihiroSegaBootOld.c_str(), "wb"); FILE* fpSegaBootNew = fopen(chihiroSegaBootNew.c_str(), "wb"); @@ -1090,41 +1087,6 @@ void CxbxKrnlEmulate(unsigned int reserved_systems, blocks_reserved_t blocks_res } #pragma optimize("", on) -// Loads a keys.bin file as generated by dump-xbox -// See https://github.com/JayFoxRox/xqemu-tools/blob/master/dump-xbox.c -void LoadXboxKeys(std::string path) -{ - std::string keys_path = path + "\\keys.bin"; - - // Attempt to open Keys.bin - FILE* fp = fopen(keys_path.c_str(), "rb"); - - if (fp != nullptr) { - // Determine size of Keys.bin - xbox::XBOX_KEY_DATA keys[2]; - fseek(fp, 0, SEEK_END); - long size = ftell(fp); - rewind(fp); - - // If the size of Keys.bin is correct (two keys), read it - if (size == xbox::XBOX_KEY_LENGTH * 2) { - fread(keys, xbox::XBOX_KEY_LENGTH, 2, fp); - - memcpy(xbox::XboxEEPROMKey, &keys[0], xbox::XBOX_KEY_LENGTH); - memcpy(xbox::XboxCertificateKey, &keys[1], xbox::XBOX_KEY_LENGTH); - } - else { - EmuLog(LOG_LEVEL::WARNING, "Keys.bin has an incorrect filesize. Should be %d bytes", xbox::XBOX_KEY_LENGTH * 2); - } - - fclose(fp); - return; - } - - // If we didn't already exit the function, keys.bin could not be loaded - EmuLog(LOG_LEVEL::WARNING, "Failed to load Keys.bin. Cxbx-Reloaded will be unable to read Save Data from a real Xbox"); -} - __declspec(noreturn) void CxbxKrnlInit ( void *pTLSData, diff --git a/src/core/kernel/init/CxbxKrnl.h b/src/core/kernel/init/CxbxKrnl.h index 5321df00a..53106640a 100644 --- a/src/core/kernel/init/CxbxKrnl.h +++ b/src/core/kernel/init/CxbxKrnl.h @@ -216,7 +216,7 @@ extern std::string CxbxKrnl_DebugFileName; /*! file paths */ extern char szFilePath_CxbxReloaded_Exe[MAX_PATH]; -extern char szFolder_CxbxReloadedData[MAX_PATH]; +extern std::string g_DataFilePath; extern char szFilePath_EEPROM_bin[MAX_PATH]; extern char szFilePath_Xbe[xbox::max_path*2]; diff --git a/src/gui/DlgEepromConfig.cpp b/src/gui/DlgEepromConfig.cpp index 06e56d13c..17bc59d58 100644 --- a/src/gui/DlgEepromConfig.cpp +++ b/src/gui/DlgEepromConfig.cpp @@ -198,7 +198,7 @@ void WriteEepromInMemory(HWND hDlg) // Finally, recalculate the hash and the checksums uint8_t EepromKey[16] = { 0 }; - std::basic_ifstream EepromFile(std::string(szFolder_CxbxReloadedData) + "\\keys.bin", std::ios::binary); + std::basic_ifstream EepromFile(g_DataFilePath + "\\keys.bin", std::ios::binary); if (EepromFile.is_open()) { EepromFile.read(EepromKey, 16); EepromFile.close();