From 1e5c83b3a95c667eff32ef7bdd47f546307d9be9 Mon Sep 17 00:00:00 2001
From: JosJuice <josjuice@gmail.com>
Date: Sat, 3 Jun 2017 14:41:09 +0200
Subject: [PATCH] Use std::optional for UpdateRunningGameMetadata

---
 Source/Core/Core/HW/DVD/DVDInterface.cpp | 15 ++-----------
 Source/Core/Core/HW/DVD/DVDInterface.h   | 12 +++++------
 Source/Core/Core/HW/DVD/DVDThread.cpp    | 27 +++++++++---------------
 Source/Core/Core/HW/DVD/DVDThread.h      | 13 ++++++------
 4 files changed, 25 insertions(+), 42 deletions(-)

diff --git a/Source/Core/Core/HW/DVD/DVDInterface.cpp b/Source/Core/Core/HW/DVD/DVDInterface.cpp
index 0e9f31992f..ac509c8be2 100644
--- a/Source/Core/Core/HW/DVD/DVDInterface.cpp
+++ b/Source/Core/Core/HW/DVD/DVDInterface.cpp
@@ -5,6 +5,7 @@
 #include <algorithm>
 #include <cinttypes>
 #include <memory>
+#include <optional>
 #include <string>
 
 #include "AudioCommon/AudioCommon.h"
@@ -506,7 +507,7 @@ void SetLidOpen()
     GenerateDIInterrupt(INT_CVRINT);
 }
 
-bool UpdateRunningGameMetadata(u64 title_id)
+bool UpdateRunningGameMetadata(std::optional<u64> title_id)
 {
   if (!DVDThread::HasDisc())
     return false;
@@ -518,18 +519,6 @@ bool UpdateRunningGameMetadata(u64 title_id)
   return DVDThread::UpdateRunningGameMetadata(partition, title_id);
 }
 
-bool UpdateRunningGameMetadata()
-{
-  if (!DVDThread::HasDisc())
-    return false;
-
-  const DiscIO::Partition& partition = DVDThread::GetDiscType() == DiscIO::Platform::WII_DISC ?
-                                           s_current_partition :
-                                           DiscIO::PARTITION_NONE;
-
-  return DVDThread::UpdateRunningGameMetadata(partition);
-}
-
 void ChangePartition(const DiscIO::Partition& partition)
 {
   s_current_partition = partition;
diff --git a/Source/Core/Core/HW/DVD/DVDInterface.h b/Source/Core/Core/HW/DVD/DVDInterface.h
index d3c8ba5ad8..bb6bb3f7a3 100644
--- a/Source/Core/Core/HW/DVD/DVDInterface.h
+++ b/Source/Core/Core/HW/DVD/DVDInterface.h
@@ -4,6 +4,7 @@
 
 #pragma once
 
+#include <optional>
 #include <string>
 #include <vector>
 
@@ -114,12 +115,11 @@ bool IsDiscInside();
 void ChangeDiscAsHost(const std::string& new_path);  // Can only be called by the host thread
 void ChangeDiscAsCPU(const std::string& new_path);   // Can only be called by the CPU thread
 
-// If a disc is inserted and its title ID is equal to the title_id argument, returns true and
-// calls SConfig::SetRunningGameMetadata(IVolume&, Partition&). Otherwise, returns false.
-bool UpdateRunningGameMetadata(u64 title_id);
-// If a disc is inserted, returns true and calls
-// SConfig::SetRunningGameMetadata(IVolume&, Partition&). Otherwise, returns false.
-bool UpdateRunningGameMetadata();
+// This function returns true and calls SConfig::SetRunningGameMetadata(IVolume&, Partition&)
+// if both of the following conditions are true:
+// - A disc is inserted
+// - The title_id argument doesn't contain a value, or its value matches the disc's title ID
+bool UpdateRunningGameMetadata(std::optional<u64> title_id = {});
 
 // Direct access to DI for IOS HLE (simpler to implement than how real IOS accesses DI,
 // and lets us skip encrypting/decrypting in some cases)
diff --git a/Source/Core/Core/HW/DVD/DVDThread.cpp b/Source/Core/Core/HW/DVD/DVDThread.cpp
index bc7bf60447..a55abae4a9 100644
--- a/Source/Core/Core/HW/DVD/DVDThread.cpp
+++ b/Source/Core/Core/HW/DVD/DVDThread.cpp
@@ -6,6 +6,7 @@
 #include <map>
 #include <memory>
 #include <mutex>
+#include <optional>
 #include <thread>
 #include <utility>
 #include <vector>
@@ -209,30 +210,22 @@ IOS::ES::TicketReader GetTicket(const DiscIO::Partition& partition)
   return s_disc->GetTicket(partition);
 }
 
-bool UpdateRunningGameMetadata(const DiscIO::Partition& partition, u64 title_id)
+bool UpdateRunningGameMetadata(const DiscIO::Partition& partition, std::optional<u64> title_id)
 {
   if (!s_disc)
     return false;
 
   WaitUntilIdle();
 
-  u64 volume_title_id;
-  if (!s_disc->GetTitleID(&volume_title_id, partition))
-    return false;
+  if (title_id)
+  {
+    u64 volume_title_id;
+    if (!s_disc->GetTitleID(&volume_title_id, partition))
+      return false;
 
-  if (volume_title_id != title_id)
-    return false;
-
-  SConfig::GetInstance().SetRunningGameMetadata(*s_disc, partition);
-  return true;
-}
-
-bool UpdateRunningGameMetadata(const DiscIO::Partition& partition)
-{
-  if (!s_disc)
-    return false;
-
-  DVDThread::WaitUntilIdle();
+    if (volume_title_id != *title_id)
+      return false;
+  }
 
   SConfig::GetInstance().SetRunningGameMetadata(*s_disc, partition);
   return true;
diff --git a/Source/Core/Core/HW/DVD/DVDThread.h b/Source/Core/Core/HW/DVD/DVDThread.h
index cc10480e92..76874004c5 100644
--- a/Source/Core/Core/HW/DVD/DVDThread.h
+++ b/Source/Core/Core/HW/DVD/DVDThread.h
@@ -5,6 +5,7 @@
 #pragma once
 
 #include <memory>
+#include <optional>
 #include <vector>
 
 #include "Common/CommonTypes.h"
@@ -44,12 +45,12 @@ bool HasDisc();
 DiscIO::Platform GetDiscType();
 IOS::ES::TMDReader GetTMD(const DiscIO::Partition& partition);
 IOS::ES::TicketReader GetTicket(const DiscIO::Partition& partition);
-// If a disc is inserted and its title ID is equal to the title_id argument, returns true and
-// calls SConfig::SetRunningGameMetadata(IVolume&, Partition&). Otherwise, returns false.
-bool UpdateRunningGameMetadata(const DiscIO::Partition& partition, u64 title_id);
-// If a disc is inserted, returns true and calls
-// SConfig::SetRunningGameMetadata(IVolume&, Partition&). Otherwise, returns false.
-bool UpdateRunningGameMetadata(const DiscIO::Partition& partition);
+// This function returns true and calls SConfig::SetRunningGameMetadata(IVolume&, Partition&)
+// if both of the following conditions are true:
+// - A disc is inserted
+// - The title_id argument doesn't contain a value, or its value matches the disc's title ID
+bool UpdateRunningGameMetadata(const DiscIO::Partition& partition,
+                               std::optional<u64> title_id = {});
 
 void StartRead(u64 dvd_offset, u32 length, const DiscIO::Partition& partition,
                DVDInterface::ReplyType reply_type, s64 ticks_until_completion);