| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2009 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2014-02-21 01:47:53 +01:00
										 |  |  | #include <cctype>
 | 
					
						
							|  |  |  | #include <cstring>
 | 
					
						
							| 
									
										
										
										
											2015-12-06 23:15:51 -05:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | #include <unordered_set>
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 20:06:58 -05:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2014-06-05 19:29:54 -04:00
										 |  |  | #include "Common/Logging/LogManager.h"
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | #include "Common/StringUtil.h"
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | #include "Core/Boot/Boot.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Core/ConfigManager.h"
 | 
					
						
							|  |  |  | #include "Core/Core.h"
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-06 20:33:05 +02:00
										 |  |  | #include "DiscIO/Enums.h"
 | 
					
						
							| 
									
										
										
										
											2014-07-08 14:29:26 +02:00
										 |  |  | #include "DiscIO/FileMonitor.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-21 01:47:53 +01:00
										 |  |  | #include "DiscIO/Filesystem.h"
 | 
					
						
							|  |  |  | #include "DiscIO/Volume.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "DiscIO/VolumeCreator.h"
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FileMon | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-12-06 23:15:51 -05:00
										 |  |  | static std::unique_ptr<DiscIO::IVolume> s_open_iso; | 
					
						
							|  |  |  | static std::unique_ptr<DiscIO::IFileSystem> s_filesystem; | 
					
						
							| 
									
										
										
										
											2014-07-08 15:58:25 +02:00
										 |  |  | static std::string ISOFile = "", CurrentFile = ""; | 
					
						
							|  |  |  | static bool FileAccess = true; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Filtered files
 | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | bool IsSoundFile(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::string extension; | 
					
						
							|  |  |  |   SplitPath(filename, nullptr, nullptr, &extension); | 
					
						
							|  |  |  |   std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static std::unordered_set<std::string> extensions = { | 
					
						
							|  |  |  |       ".adp",    // 1080 Avalanche, Crash Bandicoot, etc.
 | 
					
						
							|  |  |  |       ".adx",    // Sonic Adventure 2 Battle, etc.
 | 
					
						
							|  |  |  |       ".afc",    // Zelda WW
 | 
					
						
							|  |  |  |       ".ast",    // Zelda TP, Mario Kart
 | 
					
						
							|  |  |  |       ".brstm",  // Wii Sports, Wario Land, etc.
 | 
					
						
							|  |  |  |       ".dsp",    // Metroid Prime
 | 
					
						
							|  |  |  |       ".hps",    // SSB Melee
 | 
					
						
							|  |  |  |       ".ogg",    // Tony Hawk's Underground 2
 | 
					
						
							|  |  |  |       ".sad",    // Disaster
 | 
					
						
							|  |  |  |       ".snd",    // Tales of Symphonia
 | 
					
						
							|  |  |  |       ".song",   // Tales of Symphonia
 | 
					
						
							|  |  |  |       ".ssm",    // Custom Robo, Kirby Air Ride, etc.
 | 
					
						
							|  |  |  |       ".str",    // Harry Potter & the Sorcerer's Stone
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return extensions.find(extension) != extensions.end(); | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 11:12:04 +01:00
										 |  |  | // Read the file system
 | 
					
						
							|  |  |  | void ReadFileSystem(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // Should have an actual Shutdown procedure or something
 | 
					
						
							|  |  |  |   s_open_iso.reset(); | 
					
						
							|  |  |  |   s_filesystem.reset(); | 
					
						
							| 
									
										
										
										
											2013-04-16 23:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   s_open_iso = DiscIO::CreateVolumeFromFilename(filename); | 
					
						
							|  |  |  |   if (!s_open_iso) | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-06 20:33:05 +02:00
										 |  |  |   if (s_open_iso->GetVolumeType() != DiscIO::Platform::WII_WAD) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   { | 
					
						
							|  |  |  |     s_filesystem = DiscIO::CreateFileSystem(s_open_iso.get()); | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     if (!s_filesystem) | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-12-25 11:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   FileAccess = true; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-27 20:13:31 +01:00
										 |  |  | // Logs a file if it passes a few checks
 | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | void CheckFile(const std::string& file, u64 size) | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // Don't do anything if the log is unselected
 | 
					
						
							|  |  |  |   if (!LogManager::GetInstance()->IsEnabled(LogTypes::FILEMON, LogTypes::LWARNING)) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   // Do nothing if we found the same file again
 | 
					
						
							|  |  |  |   if (CurrentFile == file) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (size > 0) | 
					
						
							|  |  |  |     size = (size / 1000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::string str = StringFromFormat("%s kB %s", ThousandSeparate(size, 7).c_str(), file.c_str()); | 
					
						
							|  |  |  |   if (IsSoundFile(file)) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     INFO_LOG(FILEMON, "%s", str.c_str()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     WARN_LOG(FILEMON, "%s", str.c_str()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Update the current file
 | 
					
						
							|  |  |  |   CurrentFile = file; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 11:12:04 +01:00
										 |  |  | // Find the filename
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | void FindFilename(u64 offset) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // Don't do anything if a game is not running
 | 
					
						
							|  |  |  |   if (Core::GetState() != Core::CORE_RUN) | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2013-04-16 23:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // Or if the log is unselected
 | 
					
						
							|  |  |  |   if (!LogManager::GetInstance()->IsEnabled(LogTypes::FILEMON, LogTypes::LWARNING)) | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2013-04-16 23:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // Or if we don't have file access
 | 
					
						
							|  |  |  |   if (!FileAccess) | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (!s_filesystem || ISOFile != SConfig::GetInstance().m_LastFilename) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     FileAccess = false; | 
					
						
							|  |  |  |     ReadFileSystem(SConfig::GetInstance().m_LastFilename); | 
					
						
							|  |  |  |     ISOFile = SConfig::GetInstance().m_LastFilename; | 
					
						
							|  |  |  |     INFO_LOG(FILEMON, "Opening '%s'", ISOFile.c_str()); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   const std::string filename = s_filesystem->GetFileName(offset); | 
					
						
							| 
									
										
										
										
											2014-03-29 11:05:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (filename.empty()) | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   CheckFile(filename, s_filesystem->GetFileSize(filename)); | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Close() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   s_open_iso.reset(); | 
					
						
							|  |  |  |   s_filesystem.reset(); | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   ISOFile = ""; | 
					
						
							|  |  |  |   CurrentFile = ""; | 
					
						
							|  |  |  |   FileAccess = true; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | }  // FileMon
 |