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
14 // JLH 06/28/2011 Cleanup in the file parsing/fishing code, to make it easier
15 // to follow the flow of the logic
18 #include "filethread.h"
26 #define VERBOSE_LOGGING
28 FileThread::FileThread(QObject * parent/*= 0*/): QThread(parent), abort(false)
32 FileThread::~FileThread()
42 void FileThread::Go(void)
44 QMutexLocker locker(&mutex);
49 Our strategy here is like so:
50 Look at the files in the directory pointed to by ROMPath.
51 For each file in the directory, take the CRC32 of it and compare it to the CRC
52 in the romList[]. If there's a match, put it in a list and note it's index value
53 in romList for future reference.
55 When constructing the list, use the index to pull up an image of the cart and
56 put that in the list. User picks from a graphical image of the cart.
58 Ideally, the label will go into the archive along with the ROM image, but that's
60 Maybe box art, screenshots will go as well...
61 The future is NOW! :-)
65 // Here's the thread's actual execution path...
67 void FileThread::run(void)
69 QDir romDir(vjs.ROMPath);
70 QFileInfoList list = romDir.entryInfoList();
72 for(int i=0; i<list.size(); i++)
75 #ifdef VERBOSE_LOGGING
77 printf("FileThread: Aborting!!!\n");
80 #ifdef VERBOSE_LOGGING
84 HandleFile(list.at(i));
89 // This handles file identification and ZIP extraction.
91 void FileThread::HandleFile(QFileInfo fileInfo)
93 bool haveZIPFile = (fileInfo.suffix().compare("zip", Qt::CaseInsensitive) == 0
95 uint32_t fileSize = 0;
96 uint8 * buffer = NULL;
100 // ZIP files are special: They contain more than just the software now... ;-)
101 // So now we fish around inside them to pull out the stuff we want.
102 // Probably also need more stringent error checking as well... :-O
103 fileSize = GetFileFromZIP(fileInfo.filePath().toAscii(), FT_SOFTWARE, buffer);
110 QFile file(fileInfo.filePath());
112 if (!file.open(QIODevice::ReadOnly))
115 fileSize = fileInfo.size();
120 buffer = new uint8[fileSize];
121 file.read((char *)buffer, fileSize);
125 // Try to divine the file type by size & header
126 int fileType = ParseFileType(buffer[0], buffer[1], fileSize);
128 // Check for Alpine ROM w/Universal Header
129 bool foundUniversalHeader = HasUniversalHeader(buffer, fileSize);
132 //printf("FileThread: About to calc checksum on file with size %u... (buffer=%08X)\n", size, buffer);
133 if (foundUniversalHeader)
134 crc = crc32_calcCheckSum(buffer + 8192, fileSize - 8192);
136 crc = crc32_calcCheckSum(buffer, fileSize);
138 uint32 index = FindCRCIndexInFileList(crc);
141 if ((index == 0xFFFFFFFF) || (romList[index].flags & FF_BIOS))
142 return; // CRC wasn't found, so bail...
144 //Here's a little problem. When we create the image here and pass it off to FilePicker,
145 //we can clobber this image before we have a chance to copy it out in the FilePicker function
146 //because we can be back here before FilePicker can respond.
147 // So now we create the image on the heap, problem solved. :-)
150 // See if we can fish out a label. :-)
153 uint32 size = GetFileFromZIP(fileInfo.filePath().toAscii(), FT_LABEL, buffer);
154 //printf("FT: Label size = %u bytes.\n", size);
159 bool successful = label.loadFromData(buffer, size);
161 *img = label.scaled(365, 168, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
162 //printf("FT: Label %s: %ux%u.\n", (successful ? "succeeded" : "did not succeed"), img->width(), img->height());
165 //printf("FileThread: Attempted to load image. Size: %u x %u...\n", img.width(), img.height());
168 emit FoundAFile2(index, fileInfo.canonicalFilePath(), img, fileSize);
172 // Find a CRC in the ROM list (simple brute force algorithm).
173 // If it's there, return the index, otherwise return $FFFFFFFF
175 uint32 FileThread::FindCRCIndexInFileList(uint32 crc)
177 // Instead of a simple brute-force search, we should probably do a binary
178 // partition search instead, since the CRCs are sorted numerically.
179 #warning "!!! Should do binary partition search here !!!"
180 for(int i=0; romList[i].crc32!=0xFFFFFFFF; i++)
182 if (romList[i].crc32 == crc)