]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/filethread.cpp
Added logfile logging, ZIP file fishing
[virtualjaguar] / src / gui / filethread.cpp
1 //
2 // filethread.cpp - File discovery thread
3 //
4 // by James L. Hammons
5 // (C) 2010 Underground Software
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -------------------------------------------------------------
11 // JLH  01/28/2010  Created this file
12 // JLH  02/16/2010  Moved RomIdentifier stuff to its own file
13 //
14
15 #include "filethread.h"
16
17 #include <QtGui>
18 #include "crc32.h"
19 #include "file.h"
20 #include "filedb.h"
21 #include "settings.h"
22
23 FileThread::FileThread(QObject * parent/*= 0*/): QThread(parent), abort(false)
24 {
25 }
26
27 FileThread::~FileThread()
28 {
29         mutex.lock();
30         abort = true;
31         condition.wakeOne();
32         mutex.unlock();
33
34         wait();
35 }
36
37 void FileThread::Go(void)
38 {
39         QMutexLocker locker(&mutex);
40         start();
41 }
42
43 /*
44 Our strategy here is like so:
45 Look at the files in the directory pointed to by ROMPath.
46 For each file in the directory, take the CRC32 of it and compare it to the CRC
47 in the romList[]. If there's a match, put it in a list and note it's index value
48 in romList for future reference.
49
50 When constructing the list, use the index to pull up an image of the cart and
51 put that in the list. User picks from a graphical image of the cart.
52
53 Ideally, the label will go into the archive along with the ROM image, but that's
54 for the future...
55 Maybe box art, screenshots will go as well...
56 */
57
58 //
59 // Here's the thread's actual execution path...
60 //
61 void FileThread::run(void)
62 {
63         QDir romDir(vjs.ROMPath);
64 //      QDir romDir("../virtualjaguar/roms/rarities/");
65         QFileInfoList list = romDir.entryInfoList();
66
67 /*
68 Another thing we'll probably have to do here is check for compressed files and
69 decompress/fish around in them to find what we need. :-P
70 */
71
72         for(int i=0; i<list.size(); i++)
73         {
74                 if (abort)
75 #if 1
76 {
77 printf("FileThread: Aborting!!!\n");
78 #endif
79                         return;
80 #if 1
81 }
82 #endif
83
84                 QFileInfo fileInfo = list.at(i);
85
86                 if (fileInfo.suffix().compare("zip", Qt::CaseInsensitive) == 0)
87                 {
88                         uint8 * buffer = NULL;
89                         uint32 size = GetFileFromZIP(fileInfo.canonicalFilePath().toAscii(), FT_SOFTWARE, buffer);
90
91                         if (size > 0)
92                         {
93 //printf("FileThread: About to calc checksum on file with size %u... (buffer=%08X)\n", size, buffer);
94                                 uint32 crc = crc32_calcCheckSum(buffer, size);
95                                 uint32 index = FindCRCIndexInFileList(crc);
96                                 delete[] buffer;
97
98 // Mebbe we should pass a index AND a QImage here???
99                                 if (index != 0xFFFFFFFF && !(romList[index].flags & FF_BIOS))
100                                 {
101                                         QImage img;
102                                         size = GetFileFromZIP(fileInfo.canonicalFilePath().toAscii(), FT_LABEL, buffer);
103
104                                         if (size > 0)
105                                         {
106                                                 img.loadFromData(buffer, size);
107                                                 delete[] buffer;
108                                         }
109 //printf("FileThread: Attempted to load image. Size: %u x %u...\n", img.width(), img.height());
110
111                                         emit FoundAFile(index);
112                                 }
113                         }
114                 }
115                 else
116                 {
117                         QFile file(romDir.filePath(fileInfo.fileName()));
118
119                         if (file.open(QIODevice::ReadOnly))
120                         {
121                                 uint8 * buffer = new uint8[fileInfo.size()];
122                                 file.read((char *)buffer, fileInfo.size());
123                                 file.close();
124                                 uint32 crc = crc32_calcCheckSum(buffer, fileInfo.size());
125                                 uint32 index = FindCRCIndexInFileList(crc);
126                                 delete[] buffer;
127
128 // Mebbe we should pass a index AND a QImage here???
129                                 if (index != 0xFFFFFFFF && !(romList[index].flags & FF_BIOS))
130                                         emit FoundAFile(index);
131                         }
132                 }
133         }
134 }
135
136 //
137 // Find a CRC in the ROM list (simple brute force algorithm).
138 // If it's there, return the index, otherwise return $FFFFFFFF
139 //
140 uint32 FileThread::FindCRCIndexInFileList(uint32 crc)
141 {
142         for(int i=0; romList[i].crc32!=0xFFFFFFFF; i++)
143         {
144                 if (romList[i].crc32 == crc)
145                         return i;
146         }
147
148         return 0xFFFFFFFF;
149 }
150