| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // 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>
 | 
					
						
							| 
									
										
										
										
											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-02-21 01:47:53 +01:00
										 |  |  | #include "Common/StringUtil.h"
 | 
					
						
							| 
									
										
										
										
											2014-06-05 19:29:54 -04:00
										 |  |  | #include "Common/Logging/LogManager.h"
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Core/ConfigManager.h"
 | 
					
						
							|  |  |  | #include "Core/Core.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-21 01:47:53 +01:00
										 |  |  | #include "Core/Boot/Boot.h"
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-08 15:58:25 +02:00
										 |  |  | static DiscIO::IVolume *OpenISO = nullptr; | 
					
						
							|  |  |  | static DiscIO::IFileSystem *pFileSystem = nullptr; | 
					
						
							| 
									
										
										
										
											2014-12-25 11:12:04 +01:00
										 |  |  | static std::vector<const DiscIO::SFileInfo *> DiscFiles; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 	std::string extension; | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	SplitPath(filename, nullptr, nullptr, &extension); | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 	std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-25 19:56:03 -04:00
										 |  |  | 	static std::unordered_set<std::string> extensions = { | 
					
						
							| 
									
										
										
										
											2014-03-04 08:57:07 -05:00
										 |  |  | 		".adp",   // 1080 Avalanche, Crash Bandicoot, etc.
 | 
					
						
							|  |  |  | 		".adx",   // Sonic Adventure 2 Battle, etc.
 | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 		".afc",   // Zelda WW
 | 
					
						
							|  |  |  | 		".ast",   // Zelda TP, Mario Kart
 | 
					
						
							| 
									
										
										
										
											2014-03-04 08:57:07 -05:00
										 |  |  | 		".brstm", // Wii Sports, Wario Land, etc.
 | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 		".dsp",   // Metroid Prime
 | 
					
						
							|  |  |  | 		".hps",   // SSB Melee
 | 
					
						
							| 
									
										
										
										
											2014-03-04 08:57:07 -05:00
										 |  |  | 		".ogg",   // Tony Hawk's Underground 2
 | 
					
						
							| 
									
										
										
										
											2014-03-09 12:46:01 -04:00
										 |  |  | 		".sad",   // Disaster
 | 
					
						
							|  |  |  | 		".snd",   // Tales of Symphonia
 | 
					
						
							|  |  |  | 		".song",  // Tales of Symphonia
 | 
					
						
							|  |  |  | 		".ssm",   // Custom Robo, Kirby Air Ride, etc.
 | 
					
						
							|  |  |  | 		".str",   // Harry Potter & the Sorcerer's Stone
 | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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
										 |  |  | { | 
					
						
							|  |  |  | 	// Should have an actual Shutdown procedure or something
 | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	if (OpenISO != nullptr) | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		delete OpenISO; | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 		OpenISO = nullptr; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	if (pFileSystem != nullptr) | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		delete pFileSystem; | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 		pFileSystem = nullptr; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-16 23:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 11:12:04 +01:00
										 |  |  | 	// DiscFiles' pointers are no longer valid after pFileSystem is cleared
 | 
					
						
							|  |  |  | 	DiscFiles.clear(); | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 	OpenISO = DiscIO::CreateVolumeFromFilename(filename); | 
					
						
							|  |  |  | 	if (!OpenISO) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-17 13:21:02 +01:00
										 |  |  | 	if (!OpenISO->IsWadFile()) | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		pFileSystem = DiscIO::CreateFileSystem(OpenISO); | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (!pFileSystem) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 11:12:04 +01:00
										 |  |  | 		pFileSystem->GetFileList(DiscFiles); | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-25 11:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	FileAccess = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							|  |  |  | 	// Don't do anything if the log is unselected
 | 
					
						
							| 
									
										
										
										
											2011-04-01 07:43:02 +00:00
										 |  |  | 	if (!LogManager::GetInstance()->IsEnabled(LogTypes::FILEMON)) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	// Do nothing if we found the same file again
 | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 	if (CurrentFile == file) | 
					
						
							| 
									
										
										
										
											2011-04-01 07:43:02 +00:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 	if (size > 0) | 
					
						
							|  |  |  | 		size = (size / 1000); | 
					
						
							| 
									
										
										
										
											2013-04-16 23:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 	std::string str = StringFromFormat("%s kB %s", ThousandSeparate(size, 7).c_str(), file.c_str()); | 
					
						
							|  |  |  | 	if (IsSoundFile(file)) | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 		INFO_LOG(FILEMON, "%s", str.c_str()); | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 		WARN_LOG(FILEMON, "%s", str.c_str()); | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Update the current file
 | 
					
						
							| 
									
										
										
										
											2014-03-04 08:39:25 -05:00
										 |  |  | 	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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// Don't do anything if a game is not running
 | 
					
						
							| 
									
										
										
										
											2013-04-16 23:14:36 -04:00
										 |  |  | 	if (Core::GetState() != Core::CORE_RUN) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	// Or if the log is unselected
 | 
					
						
							| 
									
										
										
										
											2013-04-16 23:14:36 -04:00
										 |  |  | 	if (!LogManager::GetInstance()->IsEnabled(LogTypes::FILEMON)) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Or if we don't have file access
 | 
					
						
							|  |  |  | 	if (!FileAccess) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!pFileSystem || ISOFile != SConfig::GetInstance().m_LastFilename) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		FileAccess = false; | 
					
						
							| 
									
										
										
										
											2014-12-25 11:12:04 +01:00
										 |  |  | 		ReadFileSystem(SConfig::GetInstance().m_LastFilename); | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 		ISOFile = SConfig::GetInstance().m_LastFilename; | 
					
						
							| 
									
										
										
										
											2011-02-02 18:21:20 +00:00
										 |  |  | 		INFO_LOG(FILEMON, "Opening '%s'", ISOFile.c_str()); | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-14 23:38:14 -04:00
										 |  |  | 	const std::string filename = pFileSystem->GetFileName(offset); | 
					
						
							| 
									
										
										
										
											2014-03-29 11:05:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-23 22:29:30 -05:00
										 |  |  | 	if (filename.empty()) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-14 23:38:14 -04:00
										 |  |  | 	CheckFile(filename, pFileSystem->GetFileSize(filename)); | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Close() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	if (OpenISO != nullptr) | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		delete OpenISO; | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 		OpenISO = nullptr; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	if (pFileSystem != nullptr) | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		delete pFileSystem; | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 		pFileSystem = nullptr; | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 11:12:04 +01:00
										 |  |  | 	// DiscFiles' pointers are no longer valid after pFileSystem is cleared
 | 
					
						
							|  |  |  | 	DiscFiles.clear(); | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ISOFile = ""; | 
					
						
							|  |  |  | 	CurrentFile = ""; | 
					
						
							|  |  |  | 	FileAccess = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // FileMon
 |