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