MythTV  master
gamehandler.cpp
Go to the documentation of this file.
1 
2 #include "gamehandler.h"
3 #include "rominfo.h"
4 #include "rom_metadata.h"
5 
6 #include <QDir>
7 #include <QList>
8 #include <QRegularExpression>
9 #include <utility>
10 
11 #include <mythdb.h>
12 #include <mythdbcon.h>
13 #include <mythsystemlegacy.h>
14 #include <mythcontext.h>
15 #include <mythuihelper.h>
16 #include <mythdialogbox.h>
17 #include <mythmainwindow.h>
18 #include <mythprogressdialog.h>
19 
20 #define LOC_ERR QString("MythGame:GAMEHANDLER Error: ")
21 #define LOC QString("MythGame:GAMEHANDLER: ")
22 
23 static QList<GameHandler*> *handlers = nullptr;
24 
25 static void checkHandlers(void)
26 {
27  // If a handlers list doesn't currently exist create one. Otherwise
28  // clear the existing list so that we can regenerate a new one.
29  if (!handlers)
30  handlers = new QList<GameHandler*>;
31  else
32  {
33  while (!handlers->isEmpty())
34  delete handlers->takeFirst();
35  handlers->clear();
36  }
37 
39  if (!query.exec("SELECT DISTINCT playername FROM gameplayers "
40  "WHERE playername <> '';"))
41  MythDB::DBError("checkHandlers - selecting playername", query);
42 
43  while (query.next())
44  {
45  QString name = query.value(0).toString();
47  }
48 }
49 
51 {
52  return handlers->at(i);
53 }
54 
56 {
58 
59  query.prepare("SELECT rompath, workingpath, commandline, screenshots, "
60  "gameplayerid, gametype, extensions, spandisks "
61  "FROM gameplayers WHERE playername = :SYSTEM ");
62 
63  query.bindValue(":SYSTEM", handler->SystemName());
64 
65  if (query.exec() && query.next())
66  {
67  handler->m_rompath = query.value(0).toString();
68  handler->m_workingpath = query.value(1).toString();
69  handler->m_commandline = query.value(2).toString();
70  handler->m_screenshots = query.value(3).toString();
71  handler->m_gameplayerid = query.value(4).toInt();
72  handler->m_gametype = query.value(5).toString();
73 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
74  handler->m_validextensions = query.value(6).toString().trimmed()
75  .remove(" ").split(",", QString::SkipEmptyParts);
76 #else
77  handler->m_validextensions = query.value(6).toString().trimmed()
78  .remove(" ").split(",", Qt::SkipEmptyParts);
79 #endif
80  handler->m_spandisks = query.value(7).toBool();
81  }
82 }
83 
85 
87 {
88  s_newInstance = new GameHandler();
89  s_newInstance->m_systemname = std::move(name);
90 
92 
93  return s_newInstance;
94 }
95 
96 // Creates/rebuilds the handler list and then returns the count.
98 {
99  checkHandlers();
100  return handlers->count();
101 }
102 
104 {
105  QString key;
106 
107  MSqlQuery query(MSqlQuery::InitCon());
108  query.prepare("SELECT crc, category, year, country, name, "
109  "description, publisher, platform, version, "
110  "binfile FROM romdb WHERE platform = :GAMETYPE;");
111 
112  query.bindValue(":GAMETYPE",GameType);
113 
114  if (query.exec())
115  {
116  while (query.next())
117  {
118  key = QString("%1:%2")
119  .arg(query.value(0).toString())
120  .arg(query.value(9).toString());
121  m_romDB[key] = RomData(
122  query.value(1).toString(),
123  query.value(2).toString(),
124  query.value(3).toString(),
125  query.value(4).toString(),
126  query.value(5).toString(),
127  query.value(6).toString(),
128  query.value(7).toString(),
129  query.value(8).toString());
130  }
131  }
132 
133  if (m_romDB.count() == 0)
134  {
135  LOG(VB_GENERAL, LOG_ERR, LOC + QString("No romDB data read from "
136  "database for gametype %1 . Not imported?").arg(GameType));
137  }
138  else
139  {
140  LOG(VB_GENERAL, LOG_INFO, LOC +
141  QString("Loaded %1 items from romDB Database") .arg(m_romDB.count()));
142  }
143 }
144 
145 void GameHandler::GetMetadata(GameHandler *handler, const QString& rom, QString* Genre, QString* Year,
146  QString* Country, QString* CRC32, QString* GameName,
147  QString *Plot, QString *Publisher, QString *Version,
148  QString* Fanart, QString* Boxart)
149 {
150  QString key;
151 
152  *CRC32 = crcinfo(rom, handler->GameType(), &key, &m_romDB);
153 
154 #if 0
155  LOG(VB_GENERAL, LOG_DEBUG, "Key = " + key);
156 #endif
157 
158  // Set our default values
159  *Year = tr("19xx", "Default game year");
160  *Country = tr("Unknown", "Unknown country");
161  *GameName = tr("Unknown", "Unknown game name");
162  *Genre = tr("Unknown", "Unknown genre");
163  *Plot = tr("Unknown", "Unknown plot");
164  *Publisher = tr("Unknown", "Unknown publisher");
165  *Version = tr("0", "Default game version");
166  (*Fanart).clear();
167  (*Boxart).clear();
168 
169  if (!(*CRC32).isEmpty())
170  {
171  if (m_romDB.contains(key))
172  {
173  LOG(VB_GENERAL, LOG_INFO, LOC + QString("ROMDB FOUND for %1 - %2")
174  .arg(m_romDB[key].GameName()).arg(key));
175  *Year = m_romDB[key].Year();
176  *Country = m_romDB[key].Country();
177  *Genre = m_romDB[key].Genre();
178  *Publisher = m_romDB[key].Publisher();
179  *GameName = m_romDB[key].GameName();
180  *Version = m_romDB[key].Version();
181  }
182  else
183  {
184  LOG(VB_GENERAL, LOG_ERR, LOC + QString("NO ROMDB FOUND for %1 (%2)")
185  .arg(rom).arg(*CRC32));
186  }
187 
188  };
189 
190  if ((*Genre == tr("Unknown", "Unknown genre")) || (*Genre).isEmpty())
191  *Genre = tr("Unknown %1", "Unknown genre")
192  .arg( handler->GameType() );
193 
194 }
195 
196 static void purgeGameDB(const QString& filename, const QString& RomPath)
197 {
198  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Purging %1 - %2").arg(RomPath)
199  .arg(filename));
200 
201  MSqlQuery query(MSqlQuery::InitCon());
202 
203  // This should have the added benefit of removing the rom from
204  // other games of the same gametype so we wont be asked to remove it
205  // more than once.
206  query.prepare("DELETE FROM gamemetadata WHERE "
207  "romname = :ROMNAME AND "
208  "rompath = :ROMPATH ");
209 
210  query.bindValue(":ROMNAME",filename);
211  query.bindValue(":ROMPATH",RomPath);
212 
213  if (!query.exec())
214  MythDB::DBError("purgeGameDB", query);
215 
216 }
217 
219 {
220  QString filename = scan.Rom();
221  QString RomPath = scan.RomFullPath();
222 
223  if (m_removeAll)
225 
226  if (m_keepAll || m_removeAll)
227  return;
228 
229  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
230  auto *removalPopup = new MythDialogBox(
231  //: %1 is the file name
232  tr("%1 appears to be missing.\n"
233  "Remove it from the database?")
234  .arg(filename), popupStack, "chooseSystemPopup");
235 
236  if (removalPopup->Create())
237  {
238  removalPopup->SetReturnEvent(this, "removalPopup");
239 
240  removalPopup->AddButton(tr("No"));
241  removalPopup->AddButton(tr("No to all"));
242  removalPopup->AddButtonV(tr("Yes"), QVariant::fromValue(scan));
243  removalPopup->AddButtonV(tr("Yes to all"), QVariant::fromValue(scan));
244  popupStack->AddScreen(removalPopup);
245 }
246  else
247  delete removalPopup;
248 }
249 
250 static void updateDisplayRom(const QString& romname, int display, const QString& Systemname)
251 {
252  MSqlQuery query(MSqlQuery::InitCon());
253  query.prepare("UPDATE gamemetadata SET display = :DISPLAY "
254  "WHERE romname = :ROMNAME AND `system` = :SYSTEM");
255 
256  query.bindValue(":DISPLAY", display);
257  query.bindValue(":ROMNAME", romname);
258  query.bindValue(":SYSTEM", Systemname);
259 
260  if (!query.exec())
261  MythDB::DBError("updateDisplayRom", query);
262 
263 }
264 
265 static void updateDiskCount(const QString& romname, int diskcount, const QString& GameType)
266 {
267  MSqlQuery query(MSqlQuery::InitCon());
268  query.prepare("UPDATE gamemetadata SET diskcount = :DISKCOUNT "
269  "WHERE romname = :ROMNAME AND gametype = :GAMETYPE ");
270 
271  query.bindValue(":DISKCOUNT",diskcount);
272  query.bindValue(":ROMNAME", romname);
273  query.bindValue(":GAMETYPE",GameType);
274 
275  if (!query.exec())
276  MythDB::DBError("updateDiskCount", query);
277 
278 }
279 
280 static void updateGameName(const QString& romname, const QString& GameName, const QString& Systemname)
281 {
282  MSqlQuery query(MSqlQuery::InitCon());
283  query.prepare("UPDATE gamemetadata SET GameName = :GAMENAME "
284  "WHERE romname = :ROMNAME AND `system` = :SYSTEM ");
285 
286  query.bindValue(":GAMENAME", GameName);
287  query.bindValue(":ROMNAME", romname);
288  query.bindValue(":SYSTEM", Systemname);
289 
290  if (!query.exec())
291  MythDB::DBError("updateGameName", query);
292 
293 }
294 
295 
296 static void UpdateGameCounts(const QStringList& updatelist)
297 {
298  MSqlQuery query(MSqlQuery::InitCon());
299 
300  static const QRegularExpression multiDiskRGXP { "[0-4]$" };
301  int pos = 0;
302 
303  QString lastrom;
304  QString firstname;
305  QString basename;
306 
307  for (const auto & GameType : qAsConst(updatelist))
308  {
309  LOG(VB_GENERAL, LOG_NOTICE,
310  LOC + QString("Update gametype %1").arg(GameType));
311 
312  query.prepare("SELECT romname,`system`,spandisks,gamename FROM "
313  "gamemetadata,gameplayers WHERE "
314  "gamemetadata.gametype = :GAMETYPE AND "
315  "playername = `system` ORDER BY romname");
316 
317  query.bindValue(":GAMETYPE",GameType);
318 
319  if (query.exec())
320  {
321  while (query.next())
322  {
323  QString RomName = query.value(0).toString();
324  QString System = query.value(1).toString();
325  int spandisks = query.value(2).toInt();
326  QString GameName = query.value(3).toString();
327 
328  basename = RomName;
329 
330  if (spandisks)
331  {
332  int diskcount = 0;
333  int extlength = 0;
334  pos = RomName.lastIndexOf(".");
335  if (pos > 1)
336  {
337  extlength = RomName.length() - pos;
338  pos--;
339 
340  basename = RomName.mid(pos,1);
341  }
342 
343  if (basename.contains(multiDiskRGXP))
344  {
345  pos = (RomName.length() - extlength) - 1;
346  basename = RomName.left(pos);
347 
348  if (basename.right(1) == ".")
349  basename = RomName.left(pos - 1);
350  }
351  else
352  basename = GameName;
353 
354  if (basename == lastrom)
355  {
356  updateDisplayRom(RomName,0,System);
357  diskcount++;
358  if (diskcount > 1)
359  updateDiskCount(firstname,diskcount,GameType);
360  }
361  else
362  {
363  firstname = RomName;
364  lastrom = basename;
365  }
366 
367  if (basename != GameName)
368  updateGameName(RomName,basename,System);
369  }
370  else
371  {
372  if (basename == lastrom)
373  updateDisplayRom(RomName,0,System);
374  else
375  lastrom = basename;
376  }
377  }
378  }
379  }
380 }
381 
383 {
384  int counter = 0;
385  MSqlQuery query(MSqlQuery::InitCon());
386 
387  //: %1 is the system name, %2 is the game type
388  QString message = tr("Updating %1 (%2) ROM database")
389  .arg(handler->SystemName())
390  .arg(handler->GameType());
391 
392  CreateProgress(message);
393 
394  if (m_progressDlg)
396 
397  QString GameName;
398  QString Genre;
399  QString Country;
400  QString CRC32;
401  QString Year;
402  QString Plot;
403  QString Publisher;
404  QString Version;
405  QString Fanart;
406  QString Boxart;
407  QString ScreenShot;
408 
409  int removalprompt = gCoreContext->GetSetting("GameRemovalPrompt").toInt();
410  int indepth = gCoreContext->GetSetting("GameDeepScan").toInt();
411  QString screenShotPath = gCoreContext->GetSetting("mythgame.screenshotdir");
412 
413  for (const auto & game : qAsConst(m_gameMap))
414  {
415 
416  if (game.FoundLoc() == inFileSystem)
417  {
418  if (indepth)
419  {
420  GetMetadata(handler, game.RomFullPath(), &Genre, &Year, &Country, &CRC32, &GameName,
421  &Plot, &Publisher, &Version, &Fanart, &Boxart);
422  }
423  else
424  {
425  /*: %1 is the game type, when we don't know the genre we use the
426  * game type */
427  Genre = tr("Unknown %1", "Unknown genre").arg(handler->GameType());
428  Country = tr("Unknown", "Unknown country");
429  CRC32.clear();
430  Year = tr("19xx", "Default game year");
431  GameName = tr("Unknown", "Unknown game name");
432  Plot = tr("Unknown", "Unknown plot");
433  Publisher = tr("Unknown", "Unknown publisher");
434  Version = tr("0", "Default game version");
435  Fanart.clear();
436  Boxart.clear();
437  }
438 
439  if (GameName == tr("Unknown", "Unknown game name"))
440  GameName = game.GameName();
441 
442  int suffixPos = game.Rom().lastIndexOf(QChar('.'));
443  QString baseName = game.Rom();
444 
445  if (suffixPos > 0)
446  baseName = game.Rom().left(suffixPos);
447 
448  baseName = screenShotPath + "/" + baseName;
449 
450  if (QFile(baseName + ".png").exists())
451  ScreenShot = baseName + ".png";
452  else if (QFile(baseName + ".jpg").exists())
453  ScreenShot = baseName + ".jpg";
454  else if (QFile(baseName + ".gif").exists())
455  ScreenShot = baseName + ".gif";
456  else
457  ScreenShot.clear();
458 
459 #if 0
460  LOG(VB_GENERAL, LOG_INFO, QString("file %1 - genre %2 ")
461  .arg(iter.data().Rom()).arg(Genre));
462  LOG(VB_GENERAL, LOG_INFO, QString("screenshot %1").arg(ScreenShot));
463 #endif
464 
465  query.prepare("INSERT INTO gamemetadata "
466  "(`system`, romname, gamename, genre, year, gametype, "
467  "rompath, country, crc_value, diskcount, display, plot, "
468  "publisher, version, fanart, boxart, screenshot) "
469  "VALUES (:SYSTEM, :ROMNAME, :GAMENAME, :GENRE, :YEAR, "
470  ":GAMETYPE, :ROMPATH, :COUNTRY, :CRC32, '1', '1', :PLOT, :PUBLISHER, :VERSION, "
471  ":FANART, :BOXART, :SCREENSHOT)");
472 
473  query.bindValue(":SYSTEM",handler->SystemName());
474  query.bindValue(":ROMNAME",game.Rom());
475  query.bindValue(":GAMENAME",GameName);
476  query.bindValue(":GENRE",Genre);
477  query.bindValue(":YEAR",Year);
478  query.bindValue(":GAMETYPE",handler->GameType());
479  query.bindValue(":ROMPATH",game.RomPath());
480  query.bindValue(":COUNTRY",Country);
481  query.bindValue(":CRC32", CRC32);
482  query.bindValue(":PLOT", Plot);
483  query.bindValue(":PUBLISHER", Publisher);
484  query.bindValue(":VERSION", Version);
485  query.bindValue(":FANART", Fanart);
486  query.bindValue(":BOXART", Boxart);
487  query.bindValue(":SCREENSHOT", ScreenShot);
488 
489  if (!query.exec())
490  MythDB::DBError("GameHandler::UpdateGameDB - "
491  "insert gamemetadata", query);
492  }
493  else if ((game.FoundLoc() == inDatabase) && (removalprompt))
494  {
495 
496  promptForRemoval( game );
497  }
498 
499  if (m_progressDlg)
500  m_progressDlg->SetProgress(++counter);
501  }
502 
503  if (m_progressDlg)
504  {
505  m_progressDlg->Close();
506  m_progressDlg = nullptr;
507 }
508 }
509 
511 {
512  int counter = 0;
513  GameScanMap::Iterator iter;
514 
515  MSqlQuery query(MSqlQuery::InitCon());
516  query.prepare("SELECT romname,rompath,gamename FROM gamemetadata "
517  "WHERE `system` = :SYSTEM");
518 
519  query.bindValue(":SYSTEM",handler->SystemName());
520 
521  if (!query.exec())
522  MythDB::DBError("GameHandler::VerifyGameDB - "
523  "select", query);
524 
525  //: %1 is the system name
526  QString message = tr("Verifying %1 files...").arg(handler->SystemName());
527 
528  CreateProgress(message);
529 
530  if (m_progressDlg)
531  m_progressDlg->SetTotal(query.size());
532 
533  // For every file we know about, check to see if it still exists.
534  while (query.next())
535  {
536  QString RomName = query.value(0).toString();
537  QString RomPath = query.value(1).toString();
538  QString GameName = query.value(2).toString();
539  if (!RomName.isEmpty())
540  {
541  if ((iter = m_gameMap.find(RomName)) != m_gameMap.end())
542  {
543  // If it's both on disk and in the database we're done with it.
544  m_gameMap.erase(iter);
545  }
546  else
547  {
548  // If it's only in the database add it to our list and mark it for
549  // removal.
550  m_gameMap[RomName] = GameScan(RomName,RomPath + "/" + RomName,inDatabase,
551  GameName,RomPath);
552  }
553  }
554  if (m_progressDlg)
555  m_progressDlg->SetProgress(++counter);
556  }
557 
558  if (m_progressDlg)
559  {
560  m_progressDlg->Close();
561  m_progressDlg = nullptr;
562  }
563 }
564 
565 // Recurse through the directory and gather a count on how many files there are to process.
566 // This is used for the progressbar info.
567 int GameHandler::buildFileCount(const QString& directory, GameHandler *handler)
568 {
569  int filecount = 0;
570  QDir RomDir(directory);
571 
572  // If we can't read it's contents move on
573  if (!RomDir.isReadable())
574  return 0;
575 
576  RomDir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
577  QFileInfoList List = RomDir.entryInfoList();
578  for (const auto & Info : qAsConst(List))
579  {
580  if (Info.isDir())
581  {
582  filecount += buildFileCount(Info.filePath(), handler);
583  continue;
584  }
585 
586  if (handler->m_validextensions.count() > 0)
587  {
588  QRegularExpression r {
589  "^" + Info.suffix() + "$",
590  QRegularExpression::CaseInsensitiveOption };
591  QStringList result;
592  for (int x = 0; x < handler->m_validextensions.size(); x++)
593  {
594  QString extension = handler->m_validextensions.at(x);
595  if (extension.contains(r))
596  result.append(extension);
597  }
598  if (result.isEmpty())
599  continue;
600  }
601 
602  filecount++;
603  }
604 
605  return filecount;
606 }
607 
609 {
610  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
611  auto *clearPopup = new MythDialogBox(
612  tr("This will clear all game metadata from the database. Are you sure "
613  "you want to do this?"), popupStack, "clearAllPopup");
614 
615  if (clearPopup->Create())
616  {
617  clearPopup->SetReturnEvent(this, "clearAllPopup");
618  clearPopup->AddButton(tr("No"));
619  clearPopup->AddButton(tr("Yes"));
620  popupStack->AddScreen(clearPopup);
621  }
622  else
623  delete clearPopup;
624 }
625 
626 void GameHandler::buildFileList(const QString& directory, GameHandler *handler,
627  int* filecount)
628 {
629  QDir RomDir(directory);
630 
631  // If we can't read its contents move on
632  if (!RomDir.isReadable())
633  return;
634 
635  RomDir.setSorting( QDir:: DirsFirst | QDir::Name );
636  RomDir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
637  QFileInfoList List = RomDir.entryInfoList();
638  for (const auto & Info : qAsConst(List))
639  {
640  QString RomName = Info.fileName();
641  QString GameName = Info.completeBaseName();
642 
643  if (Info.isDir())
644  {
645  buildFileList(Info.filePath(), handler, filecount);
646  continue;
647  }
648 
649  if (handler->m_validextensions.count() > 0)
650  {
651  QRegularExpression r {
652  "^" + Info.suffix() + "$",
653  QRegularExpression::CaseInsensitiveOption };
654  QStringList result;
655  for (int x = 0; x < handler->m_validextensions.size(); x++)
656  {
657  QString extension = handler->m_validextensions.at(x);
658  if (extension.contains(r))
659  result.append(extension);
660  }
661 
662  if (result.isEmpty())
663  continue;
664  }
665 
666  m_gameMap[RomName] = GameScan(RomName,Info.filePath(),inFileSystem,
667  GameName, Info.absoluteDir().path());
668 
669  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Found ROM : (%1) - %2")
670  .arg(handler->SystemName()).arg(RomName));
671 
672  *filecount = *filecount + 1;
673  if (m_progressDlg)
674  m_progressDlg->SetProgress(*filecount);
675  }
676 }
677 
679 {
680  int maxcount = 0;
681  MSqlQuery query(MSqlQuery::InitCon());
682 
683  if ((!handler->SystemRomPath().isEmpty()) && (handler->GameType() != "PC"))
684  {
685  QDir d(handler->SystemRomPath());
686  if (d.exists())
687  maxcount = buildFileCount(handler->SystemRomPath(),handler);
688  else
689  {
690  LOG(VB_GENERAL, LOG_ERR, LOC +
691  QString("ROM Path does not exist: %1")
692  .arg(handler->SystemRomPath()));
693  return;
694  }
695  }
696  else
697  maxcount = 100;
698 
699  if (handler->GameType() == "PC")
700  {
701  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
702 
703  //: %1 is the system name
704  QString message = tr("Scanning for %1 games...")
705  .arg(handler->SystemName());
706  auto *busyDialog = new MythUIBusyDialog(message, popupStack,
707  "gamescanbusy");
708 
709  if (busyDialog->Create())
710  popupStack->AddScreen(busyDialog, false);
711  else
712  {
713  delete busyDialog;
714  busyDialog = nullptr;
715  }
716 
717  m_gameMap[handler->SystemCmdLine()] =
718  GameScan(handler->SystemCmdLine(),
719  handler->SystemCmdLine(),
720  inFileSystem,
721  handler->SystemName(),
722  handler->SystemCmdLine().left(handler->SystemCmdLine().lastIndexOf("/")));
723 
724  if (busyDialog)
725  busyDialog->Close();
726 
727  LOG(VB_GENERAL, LOG_INFO, LOC +
728  QString("PC Game %1").arg(handler->SystemName()));
729  }
730  else
731  {
732  QString message = tr("Scanning for %1 games...")
733  .arg(handler->SystemName());
734  CreateProgress(message);
735 
736  if (m_progressDlg)
737  m_progressDlg->SetTotal(maxcount);
738 
739  int filecount = 0;
740  buildFileList(handler->SystemRomPath(), handler, &filecount);
741 
742  if (m_progressDlg)
743  {
744  m_progressDlg->Close();
745  m_progressDlg = nullptr;
746  }
747  }
748 
749  VerifyGameDB(handler);
750 
751  // If we still have some games in the list then update the database
752  if (!m_gameMap.empty())
753  {
754  InitMetaDataMap(handler->GameType());
755 
756  UpdateGameDB(handler);
757 
758  m_romDB.clear();
759  handler->setRebuild(true);
760  }
761  else
762  handler->setRebuild(false);
763 }
764 
766 {
767  checkHandlers();
768  QStringList updatelist;
769 
770  for (auto *handler : qAsConst(*handlers))
771  {
772  if (handler)
773  {
774  updateSettings(handler);
775  handler->processGames(handler);
776 
777  if (handler->needRebuild())
778  updatelist.append(handler->GameType());
779  }
780  }
781 
782  if (!updatelist.isEmpty())
783  UpdateGameCounts(updatelist);
784 }
785 
787 {
788  if (!rominfo)
789  return nullptr;
790 
791  for (auto *handler : qAsConst(*handlers))
792  {
793  if (handler)
794  {
795  if (rominfo->System() == handler->SystemName())
796  return handler;
797  }
798  }
799 
800  return nullptr;
801 }
802 
803 GameHandler* GameHandler::GetHandlerByName(const QString& systemname)
804 {
805  if (systemname.isEmpty() || systemname.isNull())
806  return nullptr;
807 
808  for (auto *handler : qAsConst(*handlers))
809  {
810  if (handler)
811  {
812  if (handler->SystemName() == systemname)
813  return handler;
814  }
815  }
816 
817  return nullptr;
818 }
819 
820 void GameHandler::Launchgame(RomInfo *romdata, const QString& systemname)
821 {
822  GameHandler *handler = nullptr;
823 
824  if (!systemname.isEmpty() && !systemname.isNull())
825  {
826  handler = GetHandlerByName(systemname);
827  }
828  else if (!(handler = GetHandler(romdata)))
829  {
830  // Couldn't get handler so abort.
831  return;
832  }
833  QString exec = handler->SystemCmdLine();
834 
835  if (exec.isEmpty())
836  return;
837 
838  if (handler->GameType() != "PC")
839  {
840  QString arg = "\"" + romdata->Rompath() +
841  "/" + romdata->Romname() + "\"";
842 
843  // If they specified a %s in the commandline place the romname
844  // in that location, otherwise tack it on to the end of
845  // the command.
846  if (exec.contains("%s") || handler->SpanDisks())
847  {
848  exec = exec.replace("%s",arg);
849 
850  if (handler->SpanDisks())
851  {
852  QRegularExpression rxp { "%d[0-4]" };
853 
854  if (exec.contains(rxp))
855  {
856  if (romdata->DiskCount() > 1)
857  {
858  // Chop off the extension, . and last character of the name which we are assuming is the disk #
859  QString basename = romdata->Romname().left(romdata->Romname().length() - (romdata->getExtension().length() + 2));
860  QString extension = romdata->getExtension();
861  QString rom;
862  std::array<QString,7> diskid { "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6" };
863 
864  for (int disk = 1; disk <= romdata->DiskCount(); disk++)
865  {
866  rom = QString("\"%1/%2%3.%4\"")
867  .arg(romdata->Rompath())
868  .arg(basename)
869  .arg(disk)
870  .arg(extension);
871  exec = exec.replace(diskid[disk],rom);
872  }
873  } else
874  { // If there is only one disk make sure we replace %d1 just like %s
875  exec = exec.replace("%d1",arg);
876  }
877  }
878  }
879  }
880  else
881  {
882  exec = exec + " \"" +
883  romdata->Rompath() + "/" +
884  romdata->Romname() + "\"";
885  }
886  }
887 
888  QString savedir = QDir::current().path();
889  QDir d;
890  if (!handler->SystemWorkingPath().isEmpty())
891  {
892  if (!d.cd(handler->SystemWorkingPath()))
893  {
894  LOG(VB_GENERAL, LOG_ERR, LOC +
895  QString("Failed to change to specified Working Directory: %1")
896  .arg(handler->SystemWorkingPath()));
897  }
898  }
899  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Launching Game : %1 : %2")
900  .arg(handler->SystemName())
901  .arg(exec));
902 
903  GetMythUI()->AddCurrentLocation(QString("MythGame %1 ( %2 )").arg(handler->SystemName()).arg(exec));
904 
905  QStringList cmdlist = exec.split(";");
906  if (cmdlist.count() > 0)
907  {
908  for (const auto & cmd : qAsConst(cmdlist))
909  {
910  LOG(VB_GENERAL, LOG_INFO, LOC +
911  QString("Executing : %1").arg(cmd));
913  }
914  }
915  else
916  {
917  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Executing : %1").arg(exec));
919  }
920 
922 
923  (void)d.cd(savedir);
924 }
925 
927 {
928  if (!parent || !GetHandler(parent))
929  return nullptr;
930 
931  return new RomInfo(*parent);
932 }
933 
935 {
936  handlers->append(handler);
937 }
938 
939 void GameHandler::customEvent(QEvent *event)
940 {
941  if (auto *dce = dynamic_cast<DialogCompletionEvent*>(event))
942  {
943  QString resultid = dce->GetId();
944 // QString resulttext = dce->GetResultText();
945 
946  if (resultid == "removalPopup")
947  {
948  int buttonNum = dce->GetResult();
949  auto scan = dce->GetData().value<GameScan>();
950  switch (buttonNum)
951  {
952  case 1:
953  m_keepAll = true;
954  break;
955  case 2:
956  purgeGameDB(scan.Rom() , scan.RomFullPath());
957  break;
958  case 3:
959  m_removeAll = true;
960  purgeGameDB(scan.Rom() , scan.RomFullPath());
961  break;
962  default:
963  break;
964  };
965  }
966  else if (resultid == "clearAllPopup")
967  {
968  int buttonNum = dce->GetResult();
969  switch (buttonNum)
970  {
971  case 1:
973  break;
974  default:
975  break;
976  }
977  }
978  }
979 }
980 
982 {
983  MSqlQuery query(MSqlQuery::InitCon());
984  if (!query.exec("DELETE FROM gamemetadata;"))
985  MythDB::DBError("GameHandler::clearAllGameData - "
986  "delete gamemetadata", query);
987 }
988 
989 void GameHandler::CreateProgress(const QString& message)
990 {
991  if (m_progressDlg)
992  return;
993 
994  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
995 
996  m_progressDlg = new MythUIProgressDialog(message, popupStack,
997  "gameprogress");
998 
999  if (m_progressDlg->Create())
1000  {
1001  popupStack->AddScreen(m_progressDlg, false);
1002  }
1003  else
1004  {
1005  delete m_progressDlg;
1006  m_progressDlg = nullptr;
1007  }
1008 }
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:807
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:124
MythUIProgressDialog::Create
bool Create(void) override
Definition: mythprogressdialog.cpp:130
updateDisplayRom
static void updateDisplayRom(const QString &romname, int display, const QString &Systemname)
Definition: gamehandler.cpp:250
MSqlQuery::size
int size(void) const
Definition: mythdbcon.h:207
MythUILocation::RemoveCurrentLocation
QString RemoveCurrentLocation()
Definition: mythuilocation.cpp:12
GameHandler::CreateProgress
void CreateProgress(const QString &message)
Definition: gamehandler.cpp:989
mythdb.h
GameHandler::setRebuild
void setRebuild(bool setrebuild)
Definition: gamehandler.h:96
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:385
RomPath
Definition: gamesettings.cpp:260
GameHandler::GetMetadata
void GetMetadata(GameHandler *handler, const QString &rom, QString *Genre, QString *Year, QString *Country, QString *CRC32, QString *GameName, QString *Plot, QString *Publisher, QString *Version, QString *Fanart, QString *Boxart)
Definition: gamehandler.cpp:145
GameHandler::clearAllGameData
void clearAllGameData(void)
Definition: gamehandler.cpp:608
GameHandler
Definition: gamehandler.h:60
mythdialogbox.h
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:197
MythScreenStack
Definition: mythscreenstack.h:16
GameHandler::SpanDisks
bool SpanDisks() const
Definition: gamehandler.h:100
mythdbcon.h
GameHandler::registerHandler
static void registerHandler(GameHandler *handler)
Definition: gamehandler.cpp:934
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:604
GameHandler::CreateRomInfo
static RomInfo * CreateRomInfo(RomInfo *parent)
Definition: gamehandler.cpp:926
GameHandler::customEvent
void customEvent(QEvent *event) override
Definition: gamehandler.cpp:939
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
GameHandler::UpdateGameDB
void UpdateGameDB(GameHandler *handler)
Definition: gamehandler.cpp:382
RomInfo::System
QString System() const
Definition: rominfo.h:73
myth_system
uint myth_system(const QString &command, uint flags, std::chrono::seconds timeout)
Definition: mythsystemlegacy.cpp:502
hardwareprofile.scan.scan
def scan(profile, smoonURL, gate)
Definition: scan.py:57
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
mythprogressdialog.h
GameHandler::clearAllMetadata
static void clearAllMetadata(void)
Definition: gamehandler.cpp:981
mythsystemlegacy.h
GameHandler::GetHandlerByName
static GameHandler * GetHandlerByName(const QString &systemname)
Definition: gamehandler.cpp:803
GameHandler::m_progressDlg
MythUIProgressDialog * m_progressDlg
Definition: gamehandler.h:139
UpdateGameCounts
static void UpdateGameCounts(const QStringList &updatelist)
Definition: gamehandler.cpp:296
LOC
#define LOC
Definition: gamehandler.cpp:21
MythUIProgressDialog
Definition: mythprogressdialog.h:59
GameScan
Definition: gamehandler.h:28
RomInfo::Rompath
QString Rompath() const
Definition: rominfo.h:58
MythDialogBox
Basic menu dialog, message and a list of options.
Definition: mythdialogbox.h:166
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:536
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:194
MythUIProgressDialog::SetProgress
void SetProgress(uint count)
Definition: mythprogressdialog.cpp:199
GameHandler::getHandler
static GameHandler * getHandler(uint i)
Definition: gamehandler.cpp:50
updateDiskCount
static void updateDiskCount(const QString &romname, int diskcount, const QString &GameType)
Definition: gamehandler.cpp:265
GameHandler::promptForRemoval
void promptForRemoval(const GameScan &scan)
Definition: gamehandler.cpp:218
GameHandler::processAllGames
static void processAllGames(void)
Definition: gamehandler.cpp:765
GameHandler::GameType
QString GameType() const
Definition: gamehandler.h:107
GameHandler::processGames
void processGames(GameHandler *handler)
Definition: gamehandler.cpp:678
rominfo.h
handlers
static QList< GameHandler * > * handlers
Definition: gamehandler.cpp:23
MythUIBusyDialog
Definition: mythprogressdialog.h:36
GameHandler::m_systemname
QString m_systemname
Definition: gamehandler.h:120
checkHandlers
static void checkHandlers(void)
Definition: gamehandler.cpp:25
uint
unsigned int uint
Definition: compat.h:140
inFileSystem
@ inFileSystem
Definition: gamehandler.h:23
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
inDatabase
@ inDatabase
Definition: gamehandler.h:24
GameHandler::m_screenshots
QString m_screenshots
Definition: gamehandler.h:124
GameHandler::newHandler
static GameHandler * newHandler(QString name)
Definition: gamehandler.cpp:86
GameHandler::m_keepAll
bool m_keepAll
Definition: gamehandler.h:133
GameHandler::GetHandler
static GameHandler * GetHandler(RomInfo *rominfo)
Definition: gamehandler.cpp:786
RomInfo
Definition: rominfo.h:12
mythuihelper.h
RomInfo::DiskCount
int DiskCount() const
Definition: rominfo.h:94
GameHandler::SystemCmdLine
QString SystemCmdLine() const
Definition: gamehandler.h:102
CRC32
#define CRC32(c, b)
CRC32 routine.
Definition: unzip.cpp:177
crcinfo
QString crcinfo(const QString &romname, const QString &GameType, QString *key, RomDBMap *romDB)
Definition: rom_metadata.cpp:51
GameHandler::m_gameplayerid
uint m_gameplayerid
Definition: gamehandler.h:125
GameHandler::SystemName
QString SystemName() const
Definition: gamehandler.h:101
GameHandler::m_commandline
QString m_commandline
Definition: gamehandler.h:122
GameHandler::updateSettings
static void updateSettings(GameHandler *handler)
Definition: gamehandler.cpp:55
kMSProcessEvents
@ kMSProcessEvents
process events while waiting
Definition: mythsystem.h:39
GameHandler::VerifyGameDB
void VerifyGameDB(GameHandler *handler)
Definition: gamehandler.cpp:510
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:888
MythUIProgressDialog::SetTotal
void SetTotal(uint total)
Definition: mythprogressdialog.cpp:193
RomInfo::getExtension
QString getExtension() const
Definition: rominfo.cpp:231
DialogCompletionEvent
Event dispatched from MythUI modal dialogs to a listening class containing a result of some form.
Definition: mythdialogbox.h:41
mythcontext.h
GameHandler::buildFileList
void buildFileList(const QString &directory, GameHandler *handler, int *filecount)
Definition: gamehandler.cpp:626
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:104
GameHandler::Launchgame
static void Launchgame(RomInfo *romdata, const QString &systemname)
Definition: gamehandler.cpp:820
purgeGameDB
static void purgeGameDB(const QString &filename, const QString &RomPath)
Definition: gamehandler.cpp:196
MythMainWindow::GetStack
MythScreenStack * GetStack(const QString &Stackname)
Definition: mythmainwindow.cpp:322
GameHandler::m_gameMap
GameScanMap m_gameMap
Definition: gamehandler.h:130
GameHandler::s_newInstance
static GameHandler * s_newInstance
Definition: gamehandler.h:137
GameType
Definition: gamesettings.cpp:240
d
static const iso6937table * d
Definition: iso6937tables.cpp:1025
MythUILocation::AddCurrentLocation
void AddCurrentLocation(const QString &Location)
Definition: mythuilocation.cpp:5
GetMythUI
MythUIHelper * GetMythUI()
Definition: mythuihelper.cpp:66
RomData
Definition: rom_metadata.h:9
rom_metadata.h
build_compdb.filename
filename
Definition: build_compdb.py:21
mythmainwindow.h
GameHandler::m_rompath
QString m_rompath
Definition: gamehandler.h:121
GameHandler::m_romDB
RomDBMap m_romDB
Definition: gamehandler.h:129
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:50
GameHandler::m_validextensions
QStringList m_validextensions
Definition: gamehandler.h:127
GameHandler::GameHandler
GameHandler()=default
GameHandler::m_spandisks
bool m_spandisks
Definition: gamehandler.h:119
updateGameName
static void updateGameName(const QString &romname, const QString &GameName, const QString &Systemname)
Definition: gamehandler.cpp:280
gamehandler.h
GameHandler::SystemRomPath
QString SystemRomPath() const
Definition: gamehandler.h:103
GameHandler::m_gametype
QString m_gametype
Definition: gamehandler.h:126
GameHandler::buildFileCount
static int buildFileCount(const QString &directory, GameHandler *handler)
Definition: gamehandler.cpp:567
GameHandler::m_removeAll
bool m_removeAll
Definition: gamehandler.h:132
GameHandler::m_workingpath
QString m_workingpath
Definition: gamehandler.h:123
RomInfo::Romname
QString Romname() const
Definition: rominfo.h:70
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:923
GameHandler::count
static uint count(void)
Definition: gamehandler.cpp:97
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:832
GameHandler::InitMetaDataMap
void InitMetaDataMap(const QString &GameType)
Definition: gamehandler.cpp:103
GameHandler::SystemWorkingPath
QString SystemWorkingPath() const
Definition: gamehandler.h:104