From 83c235b7cbde672a935aa94ef8422fba2d974375 Mon Sep 17 00:00:00 2001
From: "Admiral H. Curtiss" <pikachu025@gmail.com>
Date: Thu, 19 Nov 2020 03:19:56 +0100
Subject: [PATCH] WiiUtils: Create Wii Shop log files when installing a WAD.

Fixes https://bugs.dolphin-emu.org/issues/12269
---
 Source/Core/Core/WiiUtils.cpp | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/Source/Core/Core/WiiUtils.cpp b/Source/Core/Core/WiiUtils.cpp
index 667fe0e663..f8ff946abb 100644
--- a/Source/Core/Core/WiiUtils.cpp
+++ b/Source/Core/Core/WiiUtils.cpp
@@ -36,6 +36,7 @@
 #include "Core/IOS/ES/Formats.h"
 #include "Core/IOS/FS/FileSystem.h"
 #include "Core/IOS/IOS.h"
+#include "Core/IOS/Uids.h"
 #include "Core/SysConf.h"
 #include "DiscIO/DiscExtractor.h"
 #include "DiscIO/Enums.h"
@@ -57,6 +58,7 @@ static bool ImportWAD(IOS::HLE::Kernel& ios, const DiscIO::VolumeWAD& wad,
 
   const auto tmd = wad.GetTMD();
   const auto es = ios.GetES();
+  const auto fs = ios.GetFS();
 
   IOS::HLE::Device::ES::Context context;
   IOS::HLE::ReturnCode ret;
@@ -99,6 +101,38 @@ static bool ImportWAD(IOS::HLE::Kernel& ios, const DiscIO::VolumeWAD& wad,
     return false;
   }
 
+  // Under normal conditions, these two log files are created by the Wii Shop channel at some point
+  // during the process of downloading a game, and some games (eg. Mega Man 9) refuse to load DLC if
+  // they are not present. So ensure they exist and create them if they don't.
+  const bool shop_logs_exist = [&] {
+    const std::array<u8, 32> dummy_data{};
+    for (const std::string& path : {"/shared2/ec/shopsetu.log", "/shared2/succession/shop.log"})
+    {
+      constexpr IOS::HLE::FS::Mode rw_mode = IOS::HLE::FS::Mode::ReadWrite;
+      if (fs->CreateFullPath(IOS::SYSMENU_UID, IOS::SYSMENU_GID, path, 0,
+                             {rw_mode, rw_mode, rw_mode}) != IOS::HLE::FS::ResultCode::Success)
+      {
+        return false;
+      }
+
+      const auto old_handle = fs->OpenFile(IOS::SYSMENU_UID, IOS::SYSMENU_GID, path, rw_mode);
+      if (old_handle)
+        continue;
+
+      const auto new_handle = fs->CreateAndOpenFile(IOS::SYSMENU_UID, IOS::SYSMENU_GID, path,
+                                                    {rw_mode, rw_mode, rw_mode});
+      if (!new_handle || !new_handle->Write(dummy_data.data(), dummy_data.size()))
+        return false;
+    }
+    return true;
+  }();
+
+  if (!shop_logs_exist)
+  {
+    PanicAlertFmtT("WAD installation failed: Could not create Wii Shop log files.");
+    return false;
+  }
+
   return true;
 }