2 // filethread.cpp - File discovery thread
5 // (C) 2010 Underground Software
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- -------------------------------------------------------------
11 // JLH 01/28/2010 Created this file
12 // JLH 02/16/2010 Moved RomIdentifier stuff to its own file
13 // JLH 03/02/2010 Added .ZIP file fishing
16 #include "filethread.h"
24 FileThread::FileThread(QObject * parent/*= 0*/): QThread(parent), abort(false)
28 FileThread::~FileThread()
38 void FileThread::Go(void)
40 QMutexLocker locker(&mutex);
45 Our strategy here is like so:
46 Look at the files in the directory pointed to by ROMPath.
47 For each file in the directory, take the CRC32 of it and compare it to the CRC
48 in the romList[]. If there's a match, put it in a list and note it's index value
49 in romList for future reference.
51 When constructing the list, use the index to pull up an image of the cart and
52 put that in the list. User picks from a graphical image of the cart.
54 Ideally, the label will go into the archive along with the ROM image, but that's
56 Maybe box art, screenshots will go as well...
60 // Here's the thread's actual execution path...
62 void FileThread::run(void)
64 QDir romDir(vjs.ROMPath);
65 // QDir romDir("../virtualjaguar/roms/rarities/");
66 QFileInfoList list = romDir.entryInfoList();
69 Another thing we'll probably have to do here is check for compressed files and
70 decompress/fish around in them to find what we need. :-P
73 for(int i=0; i<list.size(); i++)
78 printf("FileThread: Aborting!!!\n");
85 QFileInfo fileInfo = list.at(i);
87 // ZIP files are special: They contain more than just the software now... ;-)
88 // So now we fish around inside them to pull out the stuff we want.
89 // Probably also need more stringent error checking as well... :-O
90 if (fileInfo.suffix().compare("zip", Qt::CaseInsensitive) == 0)
92 uint8 * buffer = NULL;
93 uint32 size = GetFileFromZIP(fileInfo.canonicalFilePath().toAscii(), FT_SOFTWARE, buffer);
97 //printf("FileThread: About to calc checksum on file with size %u... (buffer=%08X)\n", size, buffer);
98 uint32 crc = crc32_calcCheckSum(buffer, size);
99 uint32 index = FindCRCIndexInFileList(crc);
102 // Mebbe we should pass a index AND a QImage here???
104 Let's think about this... What *do* we need to send out?
105 we need the filename for sure. image file if it exists.
106 do we need the index? I think we're only using it to pull the label from the subdir...
107 we might need it if we want to pull ROM flags from the fileDB...
109 if (index != 0xFFFFFFFF && !(romList[index].flags & FF_BIOS))
112 size = GetFileFromZIP(fileInfo.canonicalFilePath().toAscii(), FT_LABEL, buffer);
116 img.loadFromData(buffer, size);
119 //printf("FileThread: Attempted to load image. Size: %u x %u...\n", img.width(), img.height());
121 // emit FoundAFile(index);
122 emit FoundAFile2(index, fileInfo.canonicalFilePath(), &img);
128 QFile file(romDir.filePath(fileInfo.fileName()));
130 if (file.open(QIODevice::ReadOnly))
132 uint8 * buffer = new uint8[fileInfo.size()];
133 file.read((char *)buffer, fileInfo.size());
135 uint32 crc = crc32_calcCheckSum(buffer, fileInfo.size());
136 uint32 index = FindCRCIndexInFileList(crc);
139 // Mebbe we should pass a index AND a QImage here???
140 if (index != 0xFFFFFFFF && !(romList[index].flags & FF_BIOS))
141 // emit FoundAFile(index);
142 emit FoundAFile2(index, fileInfo.canonicalFilePath(), 0);
149 // Find a CRC in the ROM list (simple brute force algorithm).
150 // If it's there, return the index, otherwise return $FFFFFFFF
152 uint32 FileThread::FindCRCIndexInFileList(uint32 crc)
154 for(int i=0; romList[i].crc32!=0xFFFFFFFF; i++)
156 if (romList[i].crc32 == crc)