diff --git a/Core/Console.cpp b/Core/Console.cpp index e38a305b..bee2ddc8 100644 --- a/Core/Console.cpp +++ b/Core/Console.cpp @@ -4,7 +4,6 @@ #include "../Utilities/Timer.h" #include "../Utilities/FolderUtilities.h" #include "../Utilities/ConfigManager.h" -#include "../Utilities/CRC32.h" Console* Console::Instance = nullptr; IMessageManager* Console::MessageManager = nullptr; @@ -77,17 +76,20 @@ void Console::LoadROM(wstring filename) bool Console::AttemptLoadROM(wstring filename, uint32_t crc32Hash) { if(Instance) { - if(CRC32::GetCRC(Instance->_romFilepath) == crc32Hash) { + if(ROMLoader::GetCRC32(Instance->_romFilepath) == crc32Hash) { //Current game matches, no need to do anything return true; } } vector romFiles = FolderUtilities::GetFilesInFolder(ConfigManager::GetValue(Config::LastGameFolder), L"*.nes", true); + for(wstring zipFile : FolderUtilities::GetFilesInFolder(ConfigManager::GetValue(Config::LastGameFolder), L"*.zip", true)) { + romFiles.push_back(zipFile); + } for(wstring romFile : romFiles) { //Quick search by filename if(FolderUtilities::GetFilename(romFile, true).compare(filename) == 0) { - if(CRC32::GetCRC(romFile) == crc32Hash) { + if(ROMLoader::GetCRC32(romFile) == crc32Hash) { //Matching ROM found Console::LoadROM(romFile); return true; @@ -97,7 +99,7 @@ bool Console::AttemptLoadROM(wstring filename, uint32_t crc32Hash) for(wstring romFile : romFiles) { //Slower search by CRC value - if(CRC32::GetCRC(romFile) == crc32Hash) { + if(ROMLoader::GetCRC32(romFile) == crc32Hash) { //Matching ROM found Console::LoadROM(romFile); return true; diff --git a/Core/GameInformationMessage.h b/Core/GameInformationMessage.h index 4bbd7b21..f2e92c48 100644 --- a/Core/GameInformationMessage.h +++ b/Core/GameInformationMessage.h @@ -2,7 +2,7 @@ #include "stdafx.h" #include "NetMessage.h" #include "Console.h" -#include "../Utilities/CRC32.h" +#include "ROMLoader.h" #include "../Utilities/FolderUtilities.h" class GameInformationMessage : public NetMessage @@ -39,7 +39,7 @@ public: { memset(ROMFilename, 0, sizeof(ROMFilename)); wcscpy_s(ROMFilename, FolderUtilities::GetFilename(filepath, true).c_str()); - CRC32Hash = CRC32::GetCRC(filepath); + CRC32Hash = ROMLoader::GetCRC32(filepath); ControllerPort = port; Paused = paused; } diff --git a/Core/ROMLoader.h b/Core/ROMLoader.h index a5b4aef9..d51ec38e 100644 --- a/Core/ROMLoader.h +++ b/Core/ROMLoader.h @@ -3,6 +3,7 @@ #include "stdafx.h" #include "../Utilities/FolderUtilities.h" #include "../Utilities/ZIPReader.h" +#include "../Utilities/CRC32.h" enum class MirroringType { @@ -56,6 +57,7 @@ class ROMLoader wstring _filename; uint8_t* _prgRAM = nullptr; uint8_t* _chrRAM = nullptr; + uint32_t _crc32; bool LoadFromZIP(ifstream &zipFile) { @@ -119,6 +121,7 @@ class ROMLoader bool LoadFromMemory(uint8_t* buffer, uint32_t length) { + _crc32 = CRC32::GetCRC(buffer, length); if(memcmp(buffer, "NES", 3) == 0) { memcpy((char*)&_header, buffer, sizeof(NESHeader)); @@ -216,5 +219,16 @@ class ROMLoader { return _filename; } + + static uint32_t GetCRC32(wstring filename) + { + ROMLoader loader; + uint32_t crc = 0; + if(loader.LoadFile(filename)) { + crc = loader._crc32; + loader.FreeMemory(); + } + return crc; + } }; diff --git a/Utilities/CRC32.h b/Utilities/CRC32.h index f23ef19e..53b976c0 100644 --- a/Utilities/CRC32.h +++ b/Utilities/CRC32.h @@ -1,6 +1,6 @@ //From: http://tdistler.com/2011/06/22/crc32-a-simple-c-class //"You are free to use and adapt this code however you likec that goes for anyone." - +#pragma once #include "stdafx.h" static const uint32_t kCrc32Table[256] = {