77#include  " SIO/Memcard/MemcardPS2.h" 
88#include  " SIO/Memcard/MemcardPS1.h" 
99#include  " SIO/Memcard/MemcardNotConnected.h" 
10+ #include  " SIO/Memcard/MemcardHostBase.h" 
1011#include  " SIO/Memcard/MemcardHostFile.h" 
1112#include  " SIO/Memcard/MemcardHostFolder.h" 
1213
1718
1819namespace  Memcard 
1920{
21+ 	static  std::array<std::unique_ptr<MemcardHostBase>, Memcard::MAX_SLOTS> s_memcardHosts;
2022	static  std::array<std::unique_ptr<MemcardBase>, Memcard::MAX_SLOTS> s_memcards;
2123} //  namespace Memcard
2224
@@ -29,28 +31,41 @@ bool Memcard::Initialize()
2931		if  (fileName.empty ())
3032		{
3133			s_memcards.at (i) = std::make_unique<MemcardNotConnected>(i);
34+ 			s_memcardHosts.at (i) = nullptr ;
3235		}
3336		else 
3437		{
3538			const  std::string fullPath = Path::Combine (EmuFolders::MemoryCards, fileName);
3639
37- 			if  (fullPath.ends_with (" .ps2"  ))
40+ 			//  First, determine if the host is a file or folder, and set up a host object for it.
41+ 			if  (FileSystem::FileExists (fullPath.c_str ()))
3842			{
39- 				s_memcards .at (i) = std::make_unique<MemcardPS2>(i,  fullPath);
43+ 				s_memcardHosts .at (i) = std::make_unique<MemcardHostFile>( fullPath);
4044			}
41- 			else  if  (fullPath.ends_with ( " .mcd "  ))
45+ 			else  if  (FileSystem::DirectoryExists ( fullPath.c_str () ))
4246			{
43- 				s_memcards.at (i) = std::make_unique<MemcardPS1>(i, fullPath);
47+ 				s_memcardHosts.at (i) = std::make_unique<MemcardHostFolder>(fullPath);
48+ 			}
49+ 			else 
50+ 			{
51+ 				s_memcardHosts.at (i) = nullptr ;
52+ 				s_memcards.at (i) = std::make_unique<MemcardNotConnected>(i);
53+ 
54+ 				Host::ReportFormattedErrorAsync (" Memory Card"  , 
55+ 					" Memory card not found: \n\n %s\n\n " 
56+ 					" PCSX2 will treat this memory card as ejected for this session."  ,
57+ 					fullPath.c_str ()
58+ 				);
59+ 				
60+ 				continue ;
4461			}
4562
46- 			//  If a host was not built because no such file or folder existed,
47- 			//  or a file card failed to open, then ditch the card
48- 			if  (!s_memcards.at (i)->GetHost () || !s_memcards.at (i)->GetHost ()->IsOpened ())
63+ 			//  If the host failed to open, then ditch the card
64+ 			if  (!s_memcardHosts.at (i)->IsOpened ())
4965			{
66+ 				s_memcardHosts.at (i) = nullptr ;
5067				s_memcards.at (i) = std::make_unique<MemcardNotConnected>(i);
5168
52- 				//  Translation note: detailed description should mention that the memory card will be disabled
53- 				//  for the duration of this session.
5469				Host::ReportFormattedErrorAsync (" Memory Card"  , 
5570					" Access denied to memory card: \n\n %s\n\n " 
5671					" PCSX2 will treat this memory card as ejected for this session. Another instance of PCSX2 may be using this memory card. Close any other instances of PCSX2, or restart your computer.%s"  ,
@@ -61,6 +76,48 @@ bool Memcard::Initialize()
6176					" " 
6277#endif 
6378				);
79+ 
80+ 				continue ;
81+ 			}
82+ 
83+ 			//  Now that we know the host is fine, determine what type of emulated memory card we are creating.
84+ 			if  (fullPath.ends_with (" .ps2"  ))
85+ 			{
86+ 				s_memcards.at (i) = std::make_unique<MemcardPS2>(i);
87+ 			}
88+ 			else  if  (fullPath.ends_with (" .mcd"  ))
89+ 			{
90+ 				s_memcards.at (i) = std::make_unique<MemcardPS1>(i);
91+ 			}
92+ 			else 
93+ 			{
94+ 				s_memcardHosts.at (i) = nullptr ;
95+ 				s_memcards.at (i) = std::make_unique<MemcardNotConnected>(i);
96+ 				
97+ 				Host::ReportFormattedErrorAsync (" Memory Card"  , 
98+ 					" Unrecognized file extension: \n\n %s\n\n " 
99+ 					" Please check your memory card settings and insert a valid memory card."  ,
100+ 					fullPath.c_str ()
101+ 				);
102+ 
103+ 				continue ;
104+ 			}
105+ 
106+ 			//  Validate that the capacity is okay for the type of emulated card
107+ 			MemcardBase* memcard = s_memcards.at (i).get ();
108+ 
109+ 			if  (!memcard->ValidateCapacity ())
110+ 			{
111+ 				s_memcardHosts.at (i) = nullptr ;
112+ 				s_memcards.at (i) = std::make_unique<MemcardNotConnected>(i);
113+ 				
114+ 				Host::ReportFormattedErrorAsync (" Memory Card"  , 
115+ 					" Malformed memory card: \n\n %s\n\n " 
116+ 					" File size does not match a known memory card size. Please check your memory card settings and insert a valid memory card."  ,
117+ 					fullPath.c_str ()
118+ 				);
119+ 
120+ 				continue ;
64121			}
65122		}
66123	}
@@ -78,11 +135,22 @@ void Memcard::Shutdown()
78135
79136MemcardBase* Memcard::GetMemcard (const  u32  unifiedSlot)
80137{
81- 	return  s_memcards[ unifiedSlot] .get ();
138+ 	return  s_memcards. at ( unifiedSlot) .get ();
82139}
83140
84141MemcardBase* Memcard::GetMemcard (const  u32  port, const  u32  slot)
85142{
86143	const  u32  unifiedSlot = sioConvertPortAndSlotToPad (port, slot);
87- 	return  s_memcards[unifiedSlot].get ();
144+ 	return  s_memcards.at (unifiedSlot).get ();
145+ }
146+ 
147+ MemcardHostBase* Memcard::GetMemcardHost (const  u32  unifiedSlot)
148+ {
149+ 	return  s_memcardHosts.at (unifiedSlot).get ();
150+ }
151+ 
152+ MemcardHostBase* Memcard::GetMemcardHost (const  u32  port, const  u32  slot)
153+ {
154+ 	const  u32  unifiedSlot = sioConvertPortAndSlotToPad (port, slot);
155+ 	return  s_memcardHosts.at (unifiedSlot).get ();
88156}
0 commit comments