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  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(), 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, *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")
199  .arg(RomPath, 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 
302  QString lastrom;
303  QString firstname;
304  QString basename;
305 
306  for (const auto & GameType : qAsConst(updatelist))
307  {
308  LOG(VB_GENERAL, LOG_NOTICE,
309  LOC + QString("Update gametype %1").arg(GameType));
310 
311  query.prepare("SELECT romname,`system`,spandisks,gamename FROM "
312  "gamemetadata,gameplayers WHERE "
313  "gamemetadata.gametype = :GAMETYPE AND "
314  "playername = `system` ORDER BY romname");
315 
316  query.bindValue(":GAMETYPE",GameType);
317 
318  if (query.exec())
319  {
320  while (query.next())
321  {
322  QString RomName = query.value(0).toString();
323  QString System = query.value(1).toString();
324  int spandisks = query.value(2).toInt();
325  QString GameName = query.value(3).toString();
326 
327  basename = RomName;
328 
329  if (spandisks)
330  {
331  int diskcount = 0;
332  int extlength = 0;
333  int pos = RomName.lastIndexOf(".");
334  if (pos > 1)
335  {
336  extlength = RomName.length() - pos;
337  pos--;
338 
339  basename = RomName.mid(pos,1);
340  }
341 
342  if (basename.contains(multiDiskRGXP))
343  {
344  pos = (RomName.length() - extlength) - 1;
345  basename = RomName.left(pos);
346 
347  if (basename.right(1) == ".")
348  basename = RomName.left(pos - 1);
349  }
350  else
351  basename = GameName;
352 
353  if (basename == lastrom)
354  {
355  updateDisplayRom(RomName,0,System);
356  diskcount++;
357  if (diskcount > 1)
358  updateDiskCount(firstname,diskcount,GameType);
359  }
360  else
361  {
362  firstname = RomName;
363  lastrom = basename;
364  }
365 
366  if (basename != GameName)
367  updateGameName(RomName,basename,System);
368  }
369  else
370  {
371  if (basename == lastrom)
372  updateDisplayRom(RomName,0,System);
373  else
374  lastrom = basename;
375  }
376  }
377  }
378  }
379 }
380 
382 {
383  int counter = 0;
384  MSqlQuery query(MSqlQuery::InitCon());
385 
386  //: %1 is the system name, %2 is the game type
387  QString message = tr("Updating %1 (%2) ROM database")
388  .arg(handler->SystemName(), handler->GameType());
389 
390  CreateProgress(message);
391 
392  if (m_progressDlg)
394 
395  QString GameName;
396  QString Genre;
397  QString Country;
398  QString CRC32;
399  QString Year;
400  QString Plot;
401  QString Publisher;
402  QString Version;
403  QString Fanart;
404  QString Boxart;
405  QString ScreenShot;
406 
407  int removalprompt = gCoreContext->GetSetting("GameRemovalPrompt").toInt();
408  int indepth = gCoreContext->GetSetting("GameDeepScan").toInt();
409  QString screenShotPath = gCoreContext->GetSetting("mythgame.screenshotdir");
410 
411  for (const auto & game : qAsConst(m_gameMap))
412  {
413 
414  if (game.FoundLoc() == inFileSystem)
415  {
416  if (indepth)
417  {
418  GetMetadata(handler, game.RomFullPath(), &Genre, &Year, &Country, &CRC32, &GameName,
419  &Plot, &Publisher, &Version, &Fanart, &Boxart);
420  }
421  else
422  {
423  /*: %1 is the game type, when we don't know the genre we use the
424  * game type */
425  Genre = tr("Unknown %1", "Unknown genre").arg(handler->GameType());
426  Country = tr("Unknown", "Unknown country");
427  CRC32.clear();
428  Year = tr("19xx", "Default game year");
429  GameName = tr("Unknown", "Unknown game name");
430  Plot = tr("Unknown", "Unknown plot");
431  Publisher = tr("Unknown", "Unknown publisher");
432  Version = tr("0", "Default game version");
433  Fanart.clear();
434  Boxart.clear();
435  }
436 
437  if (GameName == tr("Unknown", "Unknown game name"))
438  GameName = game.GameName();
439 
440  int suffixPos = game.Rom().lastIndexOf(QChar('.'));
441  QString baseName = game.Rom();
442 
443  if (suffixPos > 0)
444  baseName = game.Rom().left(suffixPos);
445 
446  baseName = screenShotPath + "/" + baseName;
447 
448  if (QFile(baseName + ".png").exists())
449  ScreenShot = baseName + ".png";
450  else if (QFile(baseName + ".jpg").exists())
451  ScreenShot = baseName + ".jpg";
452  else if (QFile(baseName + ".gif").exists())
453  ScreenShot = baseName + ".gif";
454  else
455  ScreenShot.clear();
456 
457 #if 0
458  LOG(VB_GENERAL, LOG_INFO, QString("file %1 - genre %2 ")
459  .arg(iter.data().Rom()).arg(Genre));
460  LOG(VB_GENERAL, LOG_INFO, QString("screenshot %1").arg(ScreenShot));
461 #endif
462 
463  query.prepare("INSERT INTO gamemetadata "
464  "(`system`, romname, gamename, genre, year, gametype, "
465  "rompath, country, crc_value, diskcount, display, plot, "
466  "publisher, version, fanart, boxart, screenshot) "
467  "VALUES (:SYSTEM, :ROMNAME, :GAMENAME, :GENRE, :YEAR, "
468  ":GAMETYPE, :ROMPATH, :COUNTRY, :CRC32, '1', '1', :PLOT, :PUBLISHER, :VERSION, "
469  ":FANART, :BOXART, :SCREENSHOT)");
470 
471  query.bindValueNoNull(":SYSTEM",handler->SystemName());
472  query.bindValueNoNull(":ROMNAME",game.Rom());
473  query.bindValueNoNull(":GAMENAME",GameName);
474  query.bindValueNoNull(":GENRE",Genre);
475  query.bindValueNoNull(":YEAR",Year);
476  query.bindValueNoNull(":GAMETYPE",handler->GameType());
477  query.bindValueNoNull(":ROMPATH",game.RomPath());
478  query.bindValueNoNull(":COUNTRY",Country);
479  query.bindValueNoNull(":CRC32", CRC32);
480  query.bindValueNoNull(":PLOT", Plot);
481  query.bindValueNoNull(":PUBLISHER", Publisher);
482  query.bindValueNoNull(":VERSION", Version);
483  query.bindValueNoNull(":FANART", Fanart);
484  query.bindValueNoNull(":BOXART", Boxart);
485  query.bindValueNoNull(":SCREENSHOT", ScreenShot);
486 
487  if (!query.exec())
488  MythDB::DBError("GameHandler::UpdateGameDB - "
489  "insert gamemetadata", query);
490  }
491  else if ((game.FoundLoc() == inDatabase) && (removalprompt))
492  {
493 
494  promptForRemoval( game );
495  }
496 
497  if (m_progressDlg)
498  m_progressDlg->SetProgress(++counter);
499  }
500 
501  if (m_progressDlg)
502  {
503  m_progressDlg->Close();
504  m_progressDlg = nullptr;
505 }
506 }
507 
509 {
510  int counter = 0;
511 
512  MSqlQuery query(MSqlQuery::InitCon());
513  query.prepare("SELECT romname,rompath,gamename FROM gamemetadata "
514  "WHERE `system` = :SYSTEM");
515 
516  query.bindValue(":SYSTEM",handler->SystemName());
517 
518  if (!query.exec())
519  MythDB::DBError("GameHandler::VerifyGameDB - "
520  "select", query);
521 
522  //: %1 is the system name
523  QString message = tr("Verifying %1 files...").arg(handler->SystemName());
524 
525  CreateProgress(message);
526 
527  if (m_progressDlg)
528  m_progressDlg->SetTotal(query.size());
529 
530  // For every file we know about, check to see if it still exists.
531  while (query.next())
532  {
533  QString RomName = query.value(0).toString();
534  QString RomPath = query.value(1).toString();
535  QString GameName = query.value(2).toString();
536  if (!RomName.isEmpty())
537  {
538 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
539  auto iter = m_gameMap.find(RomName);
540 #else
541  auto iter = m_gameMap.constFind(RomName);
542 #endif
543  if (iter != m_gameMap.end())
544  {
545  // If it's both on disk and in the database we're done with it.
546  m_gameMap.erase(iter);
547  }
548  else
549  {
550  // If it's only in the database add it to our list and mark it for
551  // removal.
552  m_gameMap[RomName] = GameScan(RomName,RomPath + "/" + RomName,inDatabase,
553  GameName,RomPath);
554  }
555  }
556  if (m_progressDlg)
557  m_progressDlg->SetProgress(++counter);
558  }
559 
560  if (m_progressDlg)
561  {
562  m_progressDlg->Close();
563  m_progressDlg = nullptr;
564  }
565 }
566 
567 // Recurse through the directory and gather a count on how many files there are to process.
568 // This is used for the progressbar info.
569 int GameHandler::buildFileCount(const QString& directory, GameHandler *handler)
570 {
571  int filecount = 0;
572  QDir RomDir(directory);
573 
574  // If we can't read it's contents move on
575  if (!RomDir.isReadable())
576  return 0;
577 
578  RomDir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
579  QFileInfoList List = RomDir.entryInfoList();
580  for (const auto & Info : qAsConst(List))
581  {
582  if (Info.isDir())
583  {
584  filecount += buildFileCount(Info.filePath(), handler);
585  continue;
586  }
587 
588  if (handler->m_validextensions.count() > 0)
589  {
590  QRegularExpression r {
591  "^" + Info.suffix() + "$",
592  QRegularExpression::CaseInsensitiveOption };
593  QStringList result;
594  QStringList& exts = handler->m_validextensions;
595  std::copy_if(exts.cbegin(), exts.cend(), std::back_inserter(result),
596  [&r](const QString& extension){ return extension.contains(r); } );
597  if (result.isEmpty())
598  continue;
599  }
600 
601  filecount++;
602  }
603 
604  return filecount;
605 }
606 
608 {
609  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
610  auto *clearPopup = new MythDialogBox(
611  tr("This will clear all game metadata from the database. Are you sure "
612  "you want to do this?"), popupStack, "clearAllPopup");
613 
614  if (clearPopup->Create())
615  {
616  clearPopup->SetReturnEvent(this, "clearAllPopup");
617  clearPopup->AddButton(tr("No"));
618  clearPopup->AddButton(tr("Yes"));
619  popupStack->AddScreen(clearPopup);
620  }
621  else
622  delete clearPopup;
623 }
624 
625 void GameHandler::buildFileList(const QString& directory, GameHandler *handler,
626  int* filecount)
627 {
628  QDir RomDir(directory);
629 
630  // If we can't read its contents move on
631  if (!RomDir.isReadable())
632  return;
633 
634  RomDir.setSorting( QDir:: DirsFirst | QDir::Name );
635  RomDir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
636  QFileInfoList List = RomDir.entryInfoList();
637  for (const auto & Info : qAsConst(List))
638  {
639  QString RomName = Info.fileName();
640  QString GameName = Info.completeBaseName();
641 
642  if (Info.isDir())
643  {
644  buildFileList(Info.filePath(), handler, filecount);
645  continue;
646  }
647 
648  if (handler->m_validextensions.count() > 0)
649  {
650  QRegularExpression r {
651  "^" + Info.suffix() + "$",
652  QRegularExpression::CaseInsensitiveOption };
653  QStringList result;
654  QStringList& exts = handler->m_validextensions;
655  std::copy_if(exts.cbegin(), exts.cend(), std::back_inserter(result),
656  [&r](const QString& extension){ return extension.contains(r); } );
657  if (result.isEmpty())
658  continue;
659  }
660 
661  m_gameMap[RomName] = GameScan(RomName,Info.filePath(),inFileSystem,
662  GameName, Info.absoluteDir().path());
663 
664  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Found ROM : (%1) - %2")
665  .arg(handler->SystemName(), RomName));
666 
667  *filecount = *filecount + 1;
668  if (m_progressDlg)
669  m_progressDlg->SetProgress(*filecount);
670  }
671 }
672 
674 {
675  int maxcount = 0;
676  MSqlQuery query(MSqlQuery::InitCon());
677 
678  if ((!handler->SystemRomPath().isEmpty()) && (handler->GameType() != "PC"))
679  {
680  QDir d(handler->SystemRomPath());
681  if (d.exists())
682  maxcount = buildFileCount(handler->SystemRomPath(),handler);
683  else
684  {
685  LOG(VB_GENERAL, LOG_ERR, LOC +
686  QString("ROM Path does not exist: %1")
687  .arg(handler->SystemRomPath()));
688  return;
689  }
690  }
691  else
692  maxcount = 100;
693 
694  if (handler->GameType() == "PC")
695  {
696  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
697 
698  //: %1 is the system name
699  QString message = tr("Scanning for %1 games...")
700  .arg(handler->SystemName());
701  auto *busyDialog = new MythUIBusyDialog(message, popupStack,
702  "gamescanbusy");
703 
704  if (busyDialog->Create())
705  popupStack->AddScreen(busyDialog, false);
706  else
707  {
708  delete busyDialog;
709  busyDialog = nullptr;
710  }
711 
712  m_gameMap[handler->SystemCmdLine()] =
713  GameScan(handler->SystemCmdLine(),
714  handler->SystemCmdLine(),
715  inFileSystem,
716  handler->SystemName(),
717  handler->SystemCmdLine().left(handler->SystemCmdLine().lastIndexOf("/")));
718 
719  if (busyDialog)
720  busyDialog->Close();
721 
722  LOG(VB_GENERAL, LOG_INFO, LOC +
723  QString("PC Game %1").arg(handler->SystemName()));
724  }
725  else
726  {
727  QString message = tr("Scanning for %1 games...")
728  .arg(handler->SystemName());
729  CreateProgress(message);
730 
731  if (m_progressDlg)
732  m_progressDlg->SetTotal(maxcount);
733 
734  int filecount = 0;
735  buildFileList(handler->SystemRomPath(), handler, &filecount);
736 
737  if (m_progressDlg)
738  {
739  m_progressDlg->Close();
740  m_progressDlg = nullptr;
741  }
742  }
743 
744  VerifyGameDB(handler);
745 
746  // If we still have some games in the list then update the database
747  if (!m_gameMap.empty())
748  {
749  InitMetaDataMap(handler->GameType());
750 
751  UpdateGameDB(handler);
752 
753  m_romDB.clear();
754  handler->setRebuild(true);
755  }
756  else
757  handler->setRebuild(false);
758 }
759 
761 {
762  checkHandlers();
763  QStringList updatelist;
764 
765  for (auto *handler : qAsConst(*handlers))
766  {
767  if (handler)
768  {
769  updateSettings(handler);
770  handler->processGames(handler);
771 
772  if (handler->needRebuild())
773  updatelist.append(handler->GameType());
774  }
775  }
776 
777  if (!updatelist.isEmpty())
778  UpdateGameCounts(updatelist);
779 }
780 
782 {
783  if (!rominfo)
784  return nullptr;
785 
786  for (auto *handler : qAsConst(*handlers))
787  {
788  if (handler)
789  {
790  if (rominfo->System() == handler->SystemName())
791  return handler;
792  }
793  }
794 
795  return nullptr;
796 }
797 
798 GameHandler* GameHandler::GetHandlerByName(const QString& systemname)
799 {
800  if (systemname.isEmpty())
801  return nullptr;
802 
803  for (auto *handler : qAsConst(*handlers))
804  {
805  if (handler)
806  {
807  if (handler->SystemName() == systemname)
808  return handler;
809  }
810  }
811 
812  return nullptr;
813 }
814 
815 void GameHandler::Launchgame(RomInfo *romdata, const QString& systemname)
816 {
817  GameHandler *handler = nullptr;
818 
819  if (!systemname.isEmpty())
820  {
821  handler = GetHandlerByName(systemname);
822  }
823  else if (!(handler = GetHandler(romdata)))
824  {
825  // Couldn't get handler so abort.
826  return;
827  }
828  QString exec = handler->SystemCmdLine();
829 
830  if (exec.isEmpty())
831  return;
832 
833  if (handler->GameType() != "PC")
834  {
835  QString arg = "\"" + romdata->Rompath() +
836  "/" + romdata->Romname() + "\"";
837 
838  // If they specified a %s in the commandline place the romname
839  // in that location, otherwise tack it on to the end of
840  // the command.
841  if (exec.contains("%s") || handler->SpanDisks())
842  {
843  exec = exec.replace("%s",arg);
844 
845  if (handler->SpanDisks())
846  {
847  QRegularExpression rxp { "%d[0-4]" };
848 
849  if (exec.contains(rxp))
850  {
851  if (romdata->DiskCount() > 1)
852  {
853  // Chop off the extension, . and last character of the name which we are assuming is the disk #
854  QString basename = romdata->Romname().left(romdata->Romname().length() - (romdata->getExtension().length() + 2));
855  QString extension = romdata->getExtension();
856  QString rom;
857  std::array<QString,7> diskid { "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6" };
858 
859  for (int disk = 1; disk <= romdata->DiskCount(); disk++)
860  {
861  rom = QString("\"%1/%2%3.%4\"")
862  .arg(romdata->Rompath(), basename,
863  QString::number(disk), extension);
864  exec = exec.replace(diskid[disk],rom);
865  }
866  } else
867  { // If there is only one disk make sure we replace %d1 just like %s
868  exec = exec.replace("%d1",arg);
869  }
870  }
871  }
872  }
873  else
874  {
875  exec = exec + " \"" +
876  romdata->Rompath() + "/" +
877  romdata->Romname() + "\"";
878  }
879  }
880 
881  QString savedir = QDir::current().path();
882  QDir d;
883  if (!handler->SystemWorkingPath().isEmpty())
884  {
885  if (!d.cd(handler->SystemWorkingPath()))
886  {
887  LOG(VB_GENERAL, LOG_ERR, LOC +
888  QString("Failed to change to specified Working Directory: %1")
889  .arg(handler->SystemWorkingPath()));
890  }
891  }
892  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Launching Game : %1 : %2")
893  .arg(handler->SystemName(), exec));
894 
895  GetMythUI()->AddCurrentLocation(QString("MythGame %1 ( %2 )")
896  .arg(handler->SystemName(), exec));
897 
898  QStringList cmdlist = exec.split(";");
899  if (cmdlist.count() > 0)
900  {
901  for (const auto & cmd : qAsConst(cmdlist))
902  {
903  LOG(VB_GENERAL, LOG_INFO, LOC +
904  QString("Executing : %1").arg(cmd));
906  }
907  }
908  else
909  {
910  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Executing : %1").arg(exec));
912  }
913 
915 
916  (void)d.cd(savedir);
917 }
918 
920 {
921  if (!parent || !GetHandler(parent))
922  return nullptr;
923 
924  return new RomInfo(*parent);
925 }
926 
928 {
929  handlers->append(handler);
930 }
931 
932 void GameHandler::customEvent(QEvent *event)
933 {
934  if (auto *dce = dynamic_cast<DialogCompletionEvent*>(event))
935  {
936  QString resultid = dce->GetId();
937 // QString resulttext = dce->GetResultText();
938 
939  if (resultid == "removalPopup")
940  {
941  int buttonNum = dce->GetResult();
942  auto scan = dce->GetData().value<GameScan>();
943  switch (buttonNum)
944  {
945  case 1:
946  m_keepAll = true;
947  break;
948  case 2:
949  purgeGameDB(scan.Rom() , scan.RomFullPath());
950  break;
951  case 3:
952  m_removeAll = true;
953  purgeGameDB(scan.Rom() , scan.RomFullPath());
954  break;
955  default:
956  break;
957  };
958  }
959  else if (resultid == "clearAllPopup")
960  {
961  int buttonNum = dce->GetResult();
962  switch (buttonNum)
963  {
964  case 1:
966  break;
967  default:
968  break;
969  }
970  }
971  }
972 }
973 
975 {
976  MSqlQuery query(MSqlQuery::InitCon());
977  if (!query.exec("DELETE FROM gamemetadata;"))
978  MythDB::DBError("GameHandler::clearAllGameData - "
979  "delete gamemetadata", query);
980 }
981 
982 void GameHandler::CreateProgress(const QString& message)
983 {
984  if (m_progressDlg)
985  return;
986 
987  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
988 
989  m_progressDlg = new MythUIProgressDialog(message, popupStack,
990  "gameprogress");
991 
992  if (m_progressDlg->Create())
993  {
994  popupStack->AddScreen(m_progressDlg, false);
995  }
996  else
997  {
998  delete m_progressDlg;
999  m_progressDlg = nullptr;
1000  }
1001 }
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:806
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
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:212
MSqlQuery::bindValueNoNull
void bindValueNoNull(const QString &placeholder, const QVariant &val)
Add a single binding, taking care not to set a NULL value.
Definition: mythdbcon.cpp:887
MythUILocation::RemoveCurrentLocation
QString RemoveCurrentLocation()
Definition: mythuilocation.cpp:12
GameHandler::CreateProgress
void CreateProgress(const QString &message)
Definition: gamehandler.cpp:982
mythdb.h
GameHandler::setRebuild
void setRebuild(bool setrebuild)
Definition: gamehandler.h:96
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:386
RomPath
Definition: gamesettings.cpp:262
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:607
GameHandler
Definition: gamehandler.h:60
mythdialogbox.h
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:202
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:927
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:607
GameHandler::CreateRomInfo
static RomInfo * CreateRomInfo(RomInfo *parent)
Definition: gamehandler.cpp:919
GameHandler::customEvent
void customEvent(QEvent *event) override
Definition: gamehandler.cpp:932
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
GameHandler::UpdateGameDB
void UpdateGameDB(GameHandler *handler)
Definition: gamehandler.cpp:381
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:506
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:974
mythsystemlegacy.h
GameHandler::GetHandlerByName
static GameHandler * GetHandlerByName(const QString &systemname)
Definition: gamehandler.cpp:798
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:539
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:227
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:760
GameHandler::GameType
QString GameType() const
Definition: gamehandler.h:107
GameHandler::processGames
void processGames(GameHandler *handler)
Definition: gamehandler.cpp:673
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:144
inFileSystem
@ inFileSystem
Definition: gamehandler.h:23
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:61
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:781
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
crcinfo
QString crcinfo(const QString &romname, const QString &GameType, QString *key, RomDBMap *romDB)
Definition: rom_metadata.cpp:56
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:508
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:882
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:625
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:102
GameHandler::Launchgame
static void Launchgame(RomInfo *romdata, const QString &systemname)
Definition: gamehandler.cpp:815
purgeGameDB
static void purgeGameDB(const QString &filename, const QString &RomPath)
Definition: gamehandler.cpp:196
MythMainWindow::GetStack
MythScreenStack * GetStack(const QString &Stackname)
Definition: mythmainwindow.cpp:320
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:242
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:49
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:569
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:924
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:831
GameHandler::InitMetaDataMap
void InitMetaDataMap(const QString &GameType)
Definition: gamehandler.cpp:103
GameHandler::SystemWorkingPath
QString SystemWorkingPath() const
Definition: gamehandler.h:104