| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2008 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-09-06 04:36:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <cstdio>
 | 
					
						
							| 
									
										
										
										
											2010-12-22 00:48:59 +00:00
										 |  |  | #include <fstream>
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <utility>
 | 
					
						
							| 
									
										
										
										
											2010-09-06 12:14:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-17 20:59:14 -05:00
										 |  |  | #include "Common/CommonPaths.h"
 | 
					
						
							| 
									
										
										
										
											2014-09-07 20:06:58 -05:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Common/FileUtil.h"
 | 
					
						
							|  |  |  | #include "Common/NandPaths.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include "Common/StringUtil.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | namespace Common | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-10-01 12:38:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-17 20:59:14 -05:00
										 |  |  | static std::string s_temp_wii_root; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void InitializeWiiRoot(bool use_dummy) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ShutdownWiiRoot(); | 
					
						
							|  |  |  | 	if (use_dummy) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		s_temp_wii_root = File::CreateTempDir(); | 
					
						
							|  |  |  | 		if (s_temp_wii_root.empty()) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			ERROR_LOG(WII_IPC_FILEIO, "Could not create temporary directory"); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		File::CopyDir(File::GetSysDirectory() + WII_USER_DIR, s_temp_wii_root); | 
					
						
							|  |  |  | 		WARN_LOG(WII_IPC_FILEIO, "Using temporary directory %s for minimal Wii FS", s_temp_wii_root.c_str()); | 
					
						
							|  |  |  | 		static bool s_registered; | 
					
						
							|  |  |  | 		if (!s_registered) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			s_registered = true; | 
					
						
							|  |  |  | 			atexit(ShutdownWiiRoot); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		File::SetUserPath(D_SESSION_WIIROOT_IDX, s_temp_wii_root); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		File::SetUserPath(D_SESSION_WIIROOT_IDX, File::GetUserPath(D_WIIROOT_IDX)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShutdownWiiRoot() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!s_temp_wii_root.empty()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		File::DeleteDirRecursively(s_temp_wii_root); | 
					
						
							|  |  |  | 		s_temp_wii_root.clear(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | std::string GetTicketFileName(u64 _titleID) | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-17 20:59:14 -05:00
										 |  |  | 	return StringFromFormat("%s/ticket/%08x/%08x.tik", | 
					
						
							|  |  |  | 			File::GetUserPath(D_SESSION_WIIROOT_IDX).c_str(), | 
					
						
							| 
									
										
										
										
											2014-02-08 14:23:34 +13:00
										 |  |  | 			(u32)(_titleID >> 32), (u32)_titleID); | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | std::string GetTitleDataPath(u64 _titleID) | 
					
						
							| 
									
										
										
										
											2010-09-07 06:06:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-17 20:59:14 -05:00
										 |  |  | 	return StringFromFormat("%s/title/%08x/%08x/data/", | 
					
						
							|  |  |  | 			File::GetUserPath(D_SESSION_WIIROOT_IDX).c_str(), | 
					
						
							| 
									
										
										
										
											2014-02-08 14:23:34 +13:00
										 |  |  | 			(u32)(_titleID >> 32), (u32)_titleID); | 
					
						
							| 
									
										
										
										
											2010-09-07 06:06:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | std::string GetTMDFileName(u64 _titleID) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return GetTitleContentPath(_titleID) + "title.tmd"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | std::string GetTitleContentPath(u64 _titleID) | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-17 20:59:14 -05:00
										 |  |  | 	return StringFromFormat("%s/title/%08x/%08x/content/", | 
					
						
							|  |  |  | 			File::GetUserPath(D_SESSION_WIIROOT_IDX).c_str(), | 
					
						
							| 
									
										
										
										
											2014-02-08 14:23:34 +13:00
										 |  |  | 			(u32)(_titleID >> 32), (u32)_titleID); | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-07 06:06:08 +00:00
										 |  |  | bool CheckTitleTMD(u64 _titleID) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	const std::string TitlePath = GetTMDFileName(_titleID); | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (File::Exists(TitlePath)) | 
					
						
							| 
									
										
										
										
											2010-09-07 06:06:08 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 		File::IOFile pTMDFile(TitlePath, "rb"); | 
					
						
							|  |  |  | 		u64 TitleID = 0; | 
					
						
							|  |  |  | 		pTMDFile.Seek(0x18C, SEEK_SET); | 
					
						
							|  |  |  | 		if (pTMDFile.ReadArray(&TitleID, 1) && _titleID == Common::swap64(TitleID)) | 
					
						
							|  |  |  | 			return true; | 
					
						
							| 
									
										
										
										
											2010-09-07 06:06:08 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	INFO_LOG(DISCIO, "Invalid or no tmd for title %08x %08x", (u32)(_titleID >> 32), (u32)(_titleID & 0xFFFFFFFF)); | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool CheckTitleTIK(u64 _titleID) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 	const std::string ticketFileName = Common::GetTicketFileName(_titleID); | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 	if (File::Exists(ticketFileName)) | 
					
						
							| 
									
										
										
										
											2010-09-07 06:06:08 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | 		File::IOFile pTIKFile(ticketFileName, "rb"); | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 		u64 TitleID = 0; | 
					
						
							|  |  |  | 		pTIKFile.Seek(0x1dC, SEEK_SET); | 
					
						
							|  |  |  | 		if (pTIKFile.ReadArray(&TitleID, 1) && _titleID == Common::swap64(TitleID)) | 
					
						
							|  |  |  | 			return true; | 
					
						
							| 
									
										
										
										
											2010-09-07 06:06:08 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	INFO_LOG(DISCIO, "Invalid or no tik for title %08x %08x", (u32)(_titleID >> 32), (u32)(_titleID & 0xFFFFFFFF)); | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-10-01 12:38:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-22 00:48:59 +00:00
										 |  |  | static void CreateReplacementFile(std::string &filename) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-02-28 19:33:39 -06:00
										 |  |  | 	std::ofstream replace; | 
					
						
							|  |  |  | 	OpenFStream(replace, filename, std::ios_base::out); | 
					
						
							| 
									
										
										
										
											2010-12-22 00:48:59 +00:00
										 |  |  | 	replace <<"\" __22__\n"; | 
					
						
							|  |  |  | 	replace << "* __2a__\n"; | 
					
						
							|  |  |  | 	//replace << "/ __2f__\n";
 | 
					
						
							|  |  |  | 	replace << ": __3a__\n"; | 
					
						
							|  |  |  | 	replace << "< __3c__\n"; | 
					
						
							|  |  |  | 	replace << "> __3e__\n"; | 
					
						
							|  |  |  | 	replace << "? __3f__\n"; | 
					
						
							|  |  |  | 	//replace <<"\\ __5c__\n";
 | 
					
						
							|  |  |  | 	replace << "| __7c__\n"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ReadReplacements(replace_v& replacements) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	replacements.clear(); | 
					
						
							|  |  |  | 	const std::string replace_fname = "/sys/replace"; | 
					
						
							| 
									
										
										
										
											2014-11-17 20:59:14 -05:00
										 |  |  | 	std::string filename = File::GetUserPath(D_SESSION_WIIROOT_IDX) + replace_fname; | 
					
						
							| 
									
										
										
										
											2010-12-22 00:48:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (!File::Exists(filename)) | 
					
						
							| 
									
										
										
										
											2010-12-22 00:48:59 +00:00
										 |  |  | 		CreateReplacementFile(filename); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 19:33:39 -06:00
										 |  |  | 	std::ifstream f; | 
					
						
							|  |  |  | 	OpenFStream(f, filename, std::ios_base::in); | 
					
						
							| 
									
										
										
										
											2010-12-22 00:48:59 +00:00
										 |  |  | 	char letter; | 
					
						
							|  |  |  | 	std::string replacement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (f >> letter >> replacement && replacement.size()) | 
					
						
							| 
									
										
										
										
											2015-02-04 10:36:42 -05:00
										 |  |  | 		replacements.emplace_back(letter, replacement); | 
					
						
							| 
									
										
										
										
											2010-12-22 00:48:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-11 13:00:40 -04:00
										 |  |  | } |