MemcardManager.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. // Copyright 2008 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include <string>
  6. #include <wx/bitmap.h>
  7. #include <wx/button.h>
  8. #include <wx/dialog.h>
  9. #include <wx/filedlg.h>
  10. #include <wx/filepicker.h>
  11. #include <wx/image.h>
  12. #include <wx/imaglist.h>
  13. #include <wx/listbase.h>
  14. #include <wx/menu.h>
  15. #include <wx/msgdlg.h>
  16. #include <wx/mstream.h>
  17. #include <wx/sizer.h>
  18. #include <wx/stattext.h>
  19. #include "Common/CommonTypes.h"
  20. #include "Common/FileUtil.h"
  21. #include "Common/IniFile.h"
  22. #include "Common/StringUtil.h"
  23. #include "Core/HW/GCMemcard.h"
  24. #include "DolphinWX/MemcardManager.h"
  25. #include "DolphinWX/WxUtils.h"
  26. #define ARROWS slot ? "" : ARROW[slot], slot ? ARROW[slot] : ""
  27. const u8 hdr[] = {
  28. 0x42, 0x4D,
  29. 0x38, 0x30, 0x00, 0x00,
  30. 0x00, 0x00, 0x00, 0x00,
  31. 0x36, 0x00, 0x00, 0x00,
  32. 0x28, 0x00, 0x00, 0x00,
  33. 0x20, 0x00, 0x00, 0x00, //W
  34. 0x20, 0x00, 0x00, 0x00, //H
  35. 0x01, 0x00,
  36. 0x20, 0x00,
  37. 0x00, 0x00, 0x00, 0x00,
  38. 0x02, 0x30, 0x00, 0x00, //data size
  39. 0x12, 0x0B, 0x00, 0x00,
  40. 0x12, 0x0B, 0x00, 0x00,
  41. 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x00, 0x00
  43. };
  44. static wxBitmap wxBitmapFromMemoryRGBA(const unsigned char* data, u32 width, u32 height)
  45. {
  46. u32 stride = (4*width);
  47. u32 bytes = (stride*height) + sizeof(hdr);
  48. bytes = (bytes + 3)&(~3);
  49. u8 *pdata = new u8[bytes];
  50. memcpy(pdata, hdr, sizeof(hdr));
  51. memset(pdata + sizeof(hdr), 0, bytes - sizeof(hdr));
  52. u8 *pixelData = pdata + sizeof(hdr);
  53. for (u32 y = 0; y < height; y++)
  54. {
  55. memcpy(pixelData + y*stride, data + (height - y - 1)*stride, stride);
  56. }
  57. *(u32*)(pdata + 18) = width;
  58. *(u32*)(pdata + 22) = height;
  59. *(u32*)(pdata + 34) = bytes - sizeof(hdr);
  60. wxMemoryInputStream is(pdata, bytes);
  61. wxBitmap map(wxImage(is, wxBITMAP_TYPE_BMP, -1), -1);
  62. delete[] pdata;
  63. return map;
  64. }
  65. BEGIN_EVENT_TABLE(CMemcardManager, wxDialog)
  66. EVT_BUTTON(ID_COPYFROM_A,CMemcardManager::CopyDeleteClick)
  67. EVT_BUTTON(ID_COPYFROM_B,CMemcardManager::CopyDeleteClick)
  68. EVT_BUTTON(ID_DELETE_A,CMemcardManager::CopyDeleteClick)
  69. EVT_BUTTON(ID_DELETE_B,CMemcardManager::CopyDeleteClick)
  70. EVT_BUTTON(ID_SAVEIMPORT_B,CMemcardManager::CopyDeleteClick)
  71. EVT_BUTTON(ID_SAVEEXPORT_B,CMemcardManager::CopyDeleteClick)
  72. EVT_BUTTON(ID_SAVEIMPORT_A,CMemcardManager::CopyDeleteClick)
  73. EVT_BUTTON(ID_SAVEEXPORT_A,CMemcardManager::CopyDeleteClick)
  74. EVT_BUTTON(ID_CONVERTTOGCI,CMemcardManager::CopyDeleteClick)
  75. EVT_BUTTON(ID_PREVPAGE_A, CMemcardManager::OnPageChange)
  76. EVT_BUTTON(ID_NEXTPAGE_A, CMemcardManager::OnPageChange)
  77. EVT_BUTTON(ID_PREVPAGE_B, CMemcardManager::OnPageChange)
  78. EVT_BUTTON(ID_NEXTPAGE_B, CMemcardManager::OnPageChange)
  79. EVT_FILEPICKER_CHANGED(ID_MEMCARDPATH_A,CMemcardManager::OnPathChange)
  80. EVT_FILEPICKER_CHANGED(ID_MEMCARDPATH_B,CMemcardManager::OnPathChange)
  81. EVT_MENU_RANGE(ID_MEMCARDPATH_A, ID_USEPAGES, CMemcardManager::OnMenuChange)
  82. EVT_MENU_RANGE(ID_COPYFROM_A, ID_CONVERTTOGCI, CMemcardManager::CopyDeleteClick)
  83. EVT_MENU_RANGE(ID_NEXTPAGE_A, ID_PREVPAGE_B, CMemcardManager::OnPageChange)
  84. EVT_MENU_RANGE(COLUMN_BANNER, NUMBER_OF_COLUMN, CMemcardManager::OnMenuChange)
  85. END_EVENT_TABLE()
  86. CMemcardManager::CMemcardManager(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& position, const wxSize& size, long style)
  87. : wxDialog(parent, id, title, position, size, style)
  88. {
  89. memoryCard[SLOT_A] = nullptr;
  90. memoryCard[SLOT_B] = nullptr;
  91. mcmSettings.twoCardsLoaded = false;
  92. if (!LoadSettings())
  93. {
  94. itemsPerPage = 16;
  95. mcmSettings.usePages = true;
  96. for (int i = COLUMN_BANNER; i < NUMBER_OF_COLUMN; i++)
  97. {
  98. mcmSettings.column[i] = (i <= COLUMN_FIRSTBLOCK) ? true : false;
  99. }
  100. }
  101. maxPages = (128 / itemsPerPage) - 1;
  102. CreateGUIControls();
  103. }
  104. CMemcardManager::~CMemcardManager()
  105. {
  106. if (memoryCard[SLOT_A])
  107. {
  108. delete memoryCard[SLOT_A];
  109. memoryCard[SLOT_A] = nullptr;
  110. }
  111. if (memoryCard[SLOT_B])
  112. {
  113. delete memoryCard[SLOT_B];
  114. memoryCard[SLOT_B] = nullptr;
  115. }
  116. SaveSettings();
  117. }
  118. bool CMemcardManager::LoadSettings()
  119. {
  120. if (MemcardManagerIni.Load(File::GetUserPath(F_DOLPHINCONFIG_IDX)))
  121. {
  122. iniMemcardSection = MemcardManagerIni.GetOrCreateSection("MemcardManager");
  123. iniMemcardSection->Get("Items per page", &itemsPerPage, 16);
  124. iniMemcardSection->Get("DefaultMemcardA", &(DefaultMemcard[SLOT_A]), "");
  125. iniMemcardSection->Get("DefaultMemcardB", &(DefaultMemcard[SLOT_B]), "");
  126. iniMemcardSection->Get("DefaultIOFolder", &DefaultIOPath, "/Users/GC");
  127. iniMemcardSection->Get("Use Pages", &mcmSettings.usePages, true);
  128. iniMemcardSection->Get("cBanner", &mcmSettings.column[COLUMN_BANNER], true);
  129. iniMemcardSection->Get("cTitle", &mcmSettings.column[COLUMN_TITLE], true);
  130. iniMemcardSection->Get("cComment", &mcmSettings.column[COLUMN_COMMENT], true);
  131. iniMemcardSection->Get("cIcon", &mcmSettings.column[COLUMN_ICON], true);
  132. iniMemcardSection->Get("cBlocks", &mcmSettings.column[COLUMN_BLOCKS], true);
  133. iniMemcardSection->Get("cFirst Block", &mcmSettings.column[COLUMN_FIRSTBLOCK], true);
  134. mcmSettings.column[NUMBER_OF_COLUMN] = false;
  135. for (int i = COLUMN_GAMECODE; i < NUMBER_OF_COLUMN; i++)
  136. {
  137. mcmSettings.column[i] = mcmSettings.column[NUMBER_OF_COLUMN];
  138. }
  139. return true;
  140. }
  141. return false;
  142. }
  143. bool CMemcardManager::SaveSettings()
  144. {
  145. MemcardManagerIni.Load(File::GetUserPath(F_DOLPHINCONFIG_IDX));
  146. iniMemcardSection = MemcardManagerIni.GetOrCreateSection("MemcardManager");
  147. iniMemcardSection->Set("Items per page", itemsPerPage, 16);
  148. iniMemcardSection->Set("DefaultMemcardA", DefaultMemcard[SLOT_A], "");
  149. iniMemcardSection->Set("DefaultMemcardB", DefaultMemcard[SLOT_B], "");
  150. iniMemcardSection->Set("Use Pages", mcmSettings.usePages, true);
  151. iniMemcardSection->Set("cBanner", mcmSettings.column[COLUMN_BANNER], true);
  152. iniMemcardSection->Set("cTitle", mcmSettings.column[COLUMN_TITLE], true);
  153. iniMemcardSection->Set("cComment", mcmSettings.column[COLUMN_COMMENT], true);
  154. iniMemcardSection->Set("cIcon", mcmSettings.column[COLUMN_ICON], true);
  155. iniMemcardSection->Set("cBlocks", mcmSettings.column[COLUMN_BLOCKS], true);
  156. iniMemcardSection->Set("cFirst Block", mcmSettings.column[COLUMN_FIRSTBLOCK], true);
  157. return MemcardManagerIni.Save(File::GetUserPath(F_DOLPHINCONFIG_IDX));
  158. }
  159. void CMemcardManager::CreateGUIControls()
  160. {
  161. // Create the controls for both memcards
  162. const char* ARROW[2] = { "<-", "->" };
  163. m_ConvertToGci = new wxButton(this, ID_CONVERTTOGCI, _("Convert to GCI"));
  164. wxStaticBoxSizer *sMemcard[2];
  165. for (int slot = SLOT_A; slot <= SLOT_B; slot++)
  166. {
  167. m_CopyFrom[slot] = new wxButton(this, ID_COPYFROM_A + slot,
  168. wxString::Format(_("%1$sCopy%1$s"), ARROW[slot ? 0 : 1]));
  169. m_SaveImport[slot] = new wxButton(this, ID_SAVEIMPORT_A + slot,
  170. wxString::Format(_("%sImport GCI%s"), ARROWS));
  171. m_SaveExport[slot] = new wxButton(this, ID_SAVEEXPORT_A + slot,
  172. wxString::Format(_("%sExport GCI%s"), ARROWS));
  173. m_Delete[slot] = new wxButton(this, ID_DELETE_A + slot,
  174. wxString::Format(_("%sDelete%s"), ARROWS));
  175. m_PrevPage[slot] = new wxButton(this, ID_PREVPAGE_A + slot, _("Prev Page"));
  176. m_NextPage[slot] = new wxButton(this, ID_NEXTPAGE_A + slot, _("Next Page"));
  177. t_Status[slot] = new wxStaticText(this, 0, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxEmptyString);
  178. wxBoxSizer * const sPages = new wxBoxSizer(wxHORIZONTAL);
  179. sPages->Add(m_PrevPage[slot], 0, wxEXPAND | wxALL, 1);
  180. sPages->Add(t_Status[slot], 0, wxEXPAND | wxALL, 5);
  181. sPages->Add(0, 0, 1, wxEXPAND|wxALL, 0);
  182. sPages->Add(m_NextPage[slot], 0, wxEXPAND | wxALL, 1);
  183. m_MemcardPath[slot] = new wxFilePickerCtrl(this, ID_MEMCARDPATH_A + slot,
  184. StrToWxStr(File::GetUserPath(D_GCUSER_IDX)), _("Choose a memory card:"),
  185. _("GameCube Memory Cards (*.raw,*.gcp)") + wxString("|*.raw;*.gcp"), wxDefaultPosition, wxDefaultSize, wxFLP_USE_TEXTCTRL | wxFLP_OPEN);
  186. m_MemcardList[slot] = new CMemcardListCtrl(this, ID_MEMCARDLIST_A + slot, wxDefaultPosition, wxSize(350, 400),
  187. wxLC_REPORT | wxSUNKEN_BORDER | wxLC_ALIGN_LEFT | wxLC_SINGLE_SEL, mcmSettings);
  188. m_MemcardList[slot]->AssignImageList(new wxImageList(96, 32), wxIMAGE_LIST_SMALL);
  189. sMemcard[slot] = new wxStaticBoxSizer(wxVERTICAL, this, _("Memory Card") + wxString::Format(" %c", 'A' + slot));
  190. sMemcard[slot]->Add(m_MemcardPath[slot], 0, wxEXPAND | wxALL, 5);
  191. sMemcard[slot]->Add(m_MemcardList[slot], 1, wxEXPAND | wxALL, 5);
  192. sMemcard[slot]->Add(sPages, 0, wxEXPAND | wxALL, 1);
  193. }
  194. wxBoxSizer * const sButtons = new wxBoxSizer(wxVERTICAL);
  195. sButtons->AddStretchSpacer(2);
  196. sButtons->Add(m_CopyFrom[SLOT_B], 0, wxEXPAND, 5);
  197. sButtons->Add(m_CopyFrom[SLOT_A], 0, wxEXPAND, 5);
  198. sButtons->AddStretchSpacer(1);
  199. sButtons->Add(m_SaveImport[SLOT_A], 0, wxEXPAND, 5);
  200. sButtons->Add(m_SaveExport[SLOT_A], 0, wxEXPAND, 5);
  201. sButtons->AddStretchSpacer(1);
  202. sButtons->Add(m_ConvertToGci, 0, wxEXPAND, 5);
  203. sButtons->AddStretchSpacer(1);
  204. sButtons->Add(m_SaveImport[SLOT_B], 0, wxEXPAND, 5);
  205. sButtons->Add(m_SaveExport[SLOT_B], 0, wxEXPAND, 5);
  206. sButtons->AddStretchSpacer(1);
  207. sButtons->Add(m_Delete[SLOT_A], 0, wxEXPAND, 5);
  208. sButtons->Add(m_Delete[SLOT_B], 0, wxEXPAND, 5);
  209. sButtons->AddStretchSpacer();
  210. sButtons->Add(new wxButton(this, wxID_OK, _("Close")), 0, wxEXPAND, 5);
  211. sButtons->AddStretchSpacer();
  212. wxBoxSizer * const sMain = new wxBoxSizer(wxHORIZONTAL);
  213. sMain->Add(sMemcard[SLOT_A], 1, wxEXPAND | wxALL, 5);
  214. sMain->Add(sButtons, 0, wxEXPAND, 0);
  215. sMain->Add(sMemcard[SLOT_B], 1, wxEXPAND | wxALL, 5);
  216. SetSizerAndFit(sMain);
  217. Center();
  218. for (int i = SLOT_A; i <= SLOT_B; i++)
  219. {
  220. m_PrevPage[i]->Disable();
  221. m_NextPage[i]->Disable();
  222. m_CopyFrom[i]->Disable();
  223. m_SaveImport[i]->Disable();
  224. m_SaveExport[i]->Disable();
  225. m_Delete[i]->Disable();
  226. if (DefaultMemcard[i].length())
  227. {
  228. m_MemcardPath[i]->SetPath(StrToWxStr(DefaultMemcard[i]));
  229. ChangePath(i);
  230. }
  231. }
  232. }
  233. void CMemcardManager::OnPathChange(wxFileDirPickerEvent& event)
  234. {
  235. ChangePath(event.GetId() - ID_MEMCARDPATH_A);
  236. }
  237. void CMemcardManager::ChangePath(int slot)
  238. {
  239. int slot2 = (slot == SLOT_A) ? SLOT_B : SLOT_A;
  240. page[slot] = FIRSTPAGE;
  241. if (mcmSettings.usePages && m_PrevPage[slot]->IsEnabled())
  242. {
  243. m_PrevPage[slot]->Disable();
  244. m_MemcardList[slot]->prevPage = false;
  245. }
  246. if (!m_MemcardPath[SLOT_A]->GetPath().CmpNoCase(m_MemcardPath[SLOT_B]->GetPath()))
  247. {
  248. if (m_MemcardPath[slot]->GetPath().length())
  249. wxMessageBox(_("Memcard already opened"));
  250. }
  251. else
  252. {
  253. if (m_MemcardPath[slot]->GetPath().length() && ReloadMemcard(WxStrToStr(m_MemcardPath[slot]->GetPath()), slot))
  254. {
  255. if (memoryCard[slot2])
  256. {
  257. mcmSettings.twoCardsLoaded = true;
  258. }
  259. m_SaveImport[slot]->Enable();
  260. m_SaveExport[slot]->Enable();
  261. m_Delete[slot]->Enable();
  262. }
  263. else
  264. {
  265. if (memoryCard[slot])
  266. {
  267. delete memoryCard[slot];
  268. memoryCard[slot] = nullptr;
  269. }
  270. mcmSettings.twoCardsLoaded = false;
  271. m_MemcardPath[slot]->SetPath(wxEmptyString);
  272. m_MemcardList[slot]->ClearAll();
  273. t_Status[slot]->SetLabel(wxEmptyString);
  274. m_SaveImport[slot]->Disable();
  275. m_SaveExport[slot]->Disable();
  276. m_Delete[slot]->Disable();
  277. if (mcmSettings.usePages)
  278. {
  279. m_PrevPage[slot]->Disable();
  280. m_NextPage[slot]->Disable();
  281. }
  282. }
  283. }
  284. m_CopyFrom[SLOT_A]->Enable(mcmSettings.twoCardsLoaded);
  285. m_CopyFrom[SLOT_B]->Enable(mcmSettings.twoCardsLoaded);
  286. }
  287. void CMemcardManager::OnPageChange(wxCommandEvent& event)
  288. {
  289. int slot = SLOT_B;
  290. switch (event.GetId())
  291. {
  292. case ID_NEXTPAGE_A:
  293. slot = SLOT_A;
  294. case ID_NEXTPAGE_B:
  295. if (!m_PrevPage[slot]->IsEnabled())
  296. {
  297. m_PrevPage[slot]->Enable();
  298. m_MemcardList[slot]->prevPage = true;
  299. }
  300. page[slot]++;
  301. if (page[slot] == maxPages)
  302. {
  303. m_NextPage[slot]->Disable();
  304. m_MemcardList[slot]->nextPage = false;
  305. }
  306. ReloadMemcard(WxStrToStr(m_MemcardPath[slot]->GetPath()), slot);
  307. break;
  308. case ID_PREVPAGE_A:
  309. slot = SLOT_A;
  310. case ID_PREVPAGE_B:
  311. if (!m_NextPage[slot]->IsEnabled())
  312. {
  313. m_NextPage[slot]->Enable();
  314. m_MemcardList[slot]->nextPage = true;
  315. }
  316. page[slot]--;
  317. if (!page[slot])
  318. {
  319. m_PrevPage[slot]->Disable();
  320. m_MemcardList[slot]->prevPage = false;
  321. }
  322. ReloadMemcard(WxStrToStr(m_MemcardPath[slot]->GetPath()), slot);
  323. break;
  324. }
  325. }
  326. void CMemcardManager::OnMenuChange(wxCommandEvent& event)
  327. {
  328. int _id = event.GetId();
  329. switch (_id)
  330. {
  331. case ID_MEMCARDPATH_A:
  332. case ID_MEMCARDPATH_B:
  333. DefaultMemcard[_id - ID_MEMCARDPATH_A] = WxStrToStr(m_MemcardPath[_id - ID_MEMCARDPATH_A]->GetPath());
  334. return;
  335. case ID_USEPAGES:
  336. mcmSettings.usePages = !mcmSettings.usePages;
  337. if (!mcmSettings.usePages)
  338. {
  339. m_PrevPage[SLOT_A]->Disable();
  340. m_PrevPage[SLOT_B]->Disable();
  341. m_NextPage[SLOT_A]->Disable();
  342. m_NextPage[SLOT_B]->Disable();
  343. m_MemcardList[SLOT_A]->prevPage =
  344. m_MemcardList[SLOT_B]->prevPage = false;
  345. page[SLOT_A] =
  346. page[SLOT_B] = FIRSTPAGE;
  347. }
  348. break;
  349. case NUMBER_OF_COLUMN:
  350. for (int i = COLUMN_GAMECODE; i <= NUMBER_OF_COLUMN; i++)
  351. {
  352. mcmSettings.column[i] = !mcmSettings.column[i];
  353. }
  354. break;
  355. default:
  356. mcmSettings.column[_id] = !mcmSettings.column[_id];
  357. break;
  358. }
  359. if (memoryCard[SLOT_A]) ReloadMemcard(WxStrToStr(m_MemcardPath[SLOT_A]->GetPath()), SLOT_A);
  360. if (memoryCard[SLOT_B]) ReloadMemcard(WxStrToStr(m_MemcardPath[SLOT_B]->GetPath()), SLOT_B);
  361. }
  362. bool CMemcardManager::CopyDeleteSwitch(u32 error, int slot)
  363. {
  364. switch (error)
  365. {
  366. case GCS:
  367. wxMessageBox(_("File converted to .gci"));
  368. break;
  369. case SUCCESS:
  370. if (slot != -1)
  371. {
  372. memoryCard[slot]->FixChecksums();
  373. if (!memoryCard[slot]->Save()) PanicAlertT("File write failed");
  374. page[slot] = FIRSTPAGE;
  375. ReloadMemcard(WxStrToStr(m_MemcardPath[slot]->GetPath()), slot);
  376. }
  377. break;
  378. case NOMEMCARD:
  379. WxUtils::ShowErrorDialog(_("File is not recognized as a memcard"));
  380. break;
  381. case OPENFAIL:
  382. WxUtils::ShowErrorDialog(_("File could not be opened\nor does not have a valid extension"));
  383. break;
  384. case OUTOFBLOCKS:
  385. if (slot == -1)
  386. {
  387. WxUtils::ShowErrorDialog(_("Unknown memory card error"));
  388. break;
  389. }
  390. wxMessageBox(wxString::Format(_("Only %d blocks available"), memoryCard[slot]->GetFreeBlocks()));
  391. break;
  392. case OUTOFDIRENTRIES:
  393. WxUtils::ShowErrorDialog(_("No free directory index entries."));
  394. break;
  395. case LENGTHFAIL:
  396. WxUtils::ShowErrorDialog(_("Imported file has invalid length."));
  397. break;
  398. case INVALIDFILESIZE:
  399. WxUtils::ShowErrorDialog(_("The save you are trying to copy has an invalid file size."));
  400. break;
  401. case TITLEPRESENT:
  402. WxUtils::ShowErrorDialog(_("Memcard already has a save for this title."));
  403. break;
  404. case SAVFAIL:
  405. WxUtils::ShowErrorDialog(_("Imported file has sav extension\nbut does not have a correct header."));
  406. break;
  407. case GCSFAIL:
  408. WxUtils::ShowErrorDialog(_("Imported file has gsc extension\nbut does not have a correct header."));
  409. break;
  410. case FAIL:
  411. if (slot == -1)
  412. {
  413. WxUtils::ShowErrorDialog(_("Export failed"));
  414. return false;
  415. }
  416. WxUtils::ShowErrorDialog(_("Invalid bat.map or dir entry."));
  417. break;
  418. case WRITEFAIL:
  419. WxUtils::ShowErrorDialog(_("File write failed"));
  420. break;
  421. case DELETE_FAIL:
  422. WxUtils::ShowErrorDialog(_("Order of files in the File Directory do not match the block order\n"
  423. "Right click and export all of the saves,\nand import the saves to a new memcard\n"));
  424. break;
  425. default:
  426. WxUtils::ShowErrorDialog(_("Unknown memory card error"));
  427. break;
  428. }
  429. SetFocus();
  430. return true;
  431. }
  432. void CMemcardManager::CopyDeleteClick(wxCommandEvent& event)
  433. {
  434. int index_A = m_MemcardList[SLOT_A]->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
  435. int index_B = m_MemcardList[SLOT_B]->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
  436. int slot = SLOT_B;
  437. int slot2 = SLOT_A;
  438. std::string fileName2("");
  439. if (index_A != wxNOT_FOUND && page[SLOT_A]) index_A += itemsPerPage * page[SLOT_A];
  440. if (index_B != wxNOT_FOUND && page[SLOT_B]) index_B += itemsPerPage * page[SLOT_B];
  441. int index = index_B;
  442. switch (event.GetId())
  443. {
  444. case ID_COPYFROM_B:
  445. slot = SLOT_A;
  446. slot2 = SLOT_B;
  447. case ID_COPYFROM_A:
  448. index = slot2 ? index_B : index_A;
  449. index = memoryCard[slot2]->GetFileIndex(index);
  450. if ((index != wxNOT_FOUND))
  451. {
  452. CopyDeleteSwitch(memoryCard[slot]->CopyFrom(*memoryCard[slot2], index), slot);
  453. }
  454. break;
  455. case ID_FIXCHECKSUM_A:
  456. slot = SLOT_A;
  457. case ID_FIXCHECKSUM_B:
  458. if (memoryCard[slot]->FixChecksums() && memoryCard[slot]->Save())
  459. {
  460. wxMessageBox(_("The checksum was successfully fixed."));
  461. }
  462. else
  463. {
  464. WxUtils::ShowErrorDialog(_("File write failed"));
  465. }
  466. break;
  467. case ID_CONVERTTOGCI:
  468. fileName2 = "convert";
  469. case ID_SAVEIMPORT_A:
  470. slot = SLOT_A;
  471. case ID_SAVEIMPORT_B:
  472. {
  473. wxString fileName = wxFileSelector(
  474. _("Select a save file to import"),
  475. (strcmp(DefaultIOPath.c_str(), "/Users/GC") == 0)
  476. ? StrToWxStr("")
  477. : StrToWxStr(DefaultIOPath),
  478. wxEmptyString, wxEmptyString,
  479. _("GameCube Savegame files(*.gci;*.gcs;*.sav)") + wxString("|*.gci;*.gcs;*.sav|") +
  480. _("Native GCI files(*.gci)") + wxString("|*.gci|") +
  481. _("MadCatz Gameshark files(*.gcs)") + wxString("|*.gcs|") +
  482. _("Datel MaxDrive/Pro files(*.sav)") + wxString("|*.sav"),
  483. wxFD_OPEN | wxFD_FILE_MUST_EXIST, this);
  484. if (!fileName.empty() && !fileName2.empty())
  485. {
  486. wxString temp2 = wxFileSelector(_("Save GCI as..."),
  487. wxEmptyString, wxEmptyString, ".gci",
  488. _("GCI File(*.gci)") + wxString("|*.gci"),
  489. wxFD_OVERWRITE_PROMPT | wxFD_SAVE, this);
  490. if (temp2.empty())
  491. break;
  492. fileName2 = WxStrToStr(temp2);
  493. }
  494. if (fileName.length() > 0)
  495. {
  496. CopyDeleteSwitch(memoryCard[slot]->ImportGci(WxStrToStr(fileName), fileName2), slot);
  497. }
  498. }
  499. break;
  500. case ID_SAVEEXPORT_A:
  501. slot = SLOT_A;
  502. index = index_A;
  503. case ID_SAVEEXPORT_B:
  504. index = memoryCard[slot]->GetFileIndex(index);
  505. if (index != wxNOT_FOUND)
  506. {
  507. std::string gciFilename;
  508. if (!memoryCard[slot]->GCI_FileName(index, gciFilename))
  509. {
  510. wxMessageBox(_("Invalid index"), _("Error"));
  511. return;
  512. }
  513. wxString fileName = wxFileSelector(
  514. _("Export save as..."),
  515. StrToWxStr(DefaultIOPath),
  516. StrToWxStr(gciFilename), ".gci",
  517. _("Native GCI files(*.gci)") + wxString("|*.gci|") +
  518. _("MadCatz Gameshark files(*.gcs)") + wxString("|*.gcs|") +
  519. _("Datel MaxDrive/Pro files(*.sav)") + wxString("|*.sav"),
  520. wxFD_OVERWRITE_PROMPT | wxFD_SAVE, this);
  521. if (fileName.length() > 0)
  522. {
  523. if (!CopyDeleteSwitch(memoryCard[slot]->ExportGci(index, WxStrToStr(fileName), ""), -1))
  524. {
  525. File::Delete(WxStrToStr(fileName));
  526. }
  527. }
  528. }
  529. break;
  530. case ID_EXPORTALL_A:
  531. slot = SLOT_A;
  532. case ID_EXPORTALL_B:
  533. {
  534. std::string path1, path2, mpath;
  535. mpath = WxStrToStr(m_MemcardPath[slot]->GetPath());
  536. SplitPath(mpath, &path1, &path2, nullptr);
  537. path1 += path2;
  538. File::CreateDir(path1);
  539. int answer = wxMessageBox(wxString::Format(_("Warning: This will overwrite any existing saves that are in the folder:\n"
  540. "%s\nand have the same name as a file on your memcard\nContinue?"), path1.c_str()), _("Warning"), wxYES_NO);
  541. if (answer == wxYES)
  542. {
  543. for (int i = 0; i < DIRLEN; i++)
  544. {
  545. CopyDeleteSwitch(memoryCard[slot]->ExportGci(i, "", path1), -1);
  546. }
  547. }
  548. break;
  549. }
  550. case ID_DELETE_A:
  551. slot = SLOT_A;
  552. index = index_A;
  553. case ID_DELETE_B:
  554. index = memoryCard[slot]->GetFileIndex(index);
  555. if (index != wxNOT_FOUND)
  556. {
  557. CopyDeleteSwitch(memoryCard[slot]->RemoveFile(index), slot);
  558. }
  559. break;
  560. }
  561. }
  562. bool CMemcardManager::ReloadMemcard(const std::string& fileName, int card)
  563. {
  564. if (memoryCard[card]) delete memoryCard[card];
  565. // TODO: add error checking and animate icons
  566. memoryCard[card] = new GCMemcard(fileName);
  567. if (!memoryCard[card]->IsValid())
  568. return false;
  569. int j;
  570. wxString wxTitle,
  571. wxComment,
  572. wxBlock,
  573. wxFirstBlock,
  574. wxLabel;
  575. m_MemcardList[card]->Hide();
  576. m_MemcardList[card]->ClearAll();
  577. m_MemcardList[card]->InsertColumn(COLUMN_BANNER, _("Banner"));
  578. m_MemcardList[card]->InsertColumn(COLUMN_TITLE, _("Title"));
  579. m_MemcardList[card]->InsertColumn(COLUMN_COMMENT, _("Comment"));
  580. m_MemcardList[card]->InsertColumn(COLUMN_ICON, _("Icon"));
  581. m_MemcardList[card]->InsertColumn(COLUMN_BLOCKS, _("Blocks"));
  582. m_MemcardList[card]->InsertColumn(COLUMN_FIRSTBLOCK, _("First Block"));
  583. wxImageList *list = m_MemcardList[card]->GetImageList(wxIMAGE_LIST_SMALL);
  584. list->RemoveAll();
  585. u8 nFiles = memoryCard[card]->GetNumFiles();
  586. int *images = new int[nFiles * 2];
  587. for (u8 i = 0; i < nFiles; i++)
  588. {
  589. static u32 pxdata[96*32];
  590. static u8 animDelay[8];
  591. static u32 animData[32*32*8];
  592. u8 fileIndex = memoryCard[card]->GetFileIndex(i);
  593. int numFrames = memoryCard[card]->ReadAnimRGBA8(fileIndex, animData, animDelay);
  594. if (!memoryCard[card]->ReadBannerRGBA8(fileIndex, pxdata))
  595. {
  596. memset(pxdata, 0, 96*32*4);
  597. if (numFrames > 0) // Just use the first one
  598. {
  599. u32 *icdata = animData;
  600. for (int y = 0; y < 32; y++)
  601. {
  602. for (int x = 0; x < 32; x++)
  603. {
  604. pxdata[y*96 + x + 32] = icdata[y*32 + x];// | 0xFF000000
  605. }
  606. }
  607. }
  608. }
  609. wxBitmap map = wxBitmapFromMemoryRGBA((u8*)pxdata, 96, 32);
  610. images[i*2] = list->Add(map);
  611. if (numFrames > 0)
  612. {
  613. memset(pxdata, 0, 96*32*4);
  614. int frames = 3;
  615. if (numFrames < frames)
  616. frames = numFrames;
  617. for (int f = 0; f < frames; f++)
  618. {
  619. for (int y = 0; y < 32; y++)
  620. {
  621. for (int x = 0; x < 32; x++)
  622. {
  623. pxdata[y*96 + x + 32*f] = animData[f*32*32 + y*32 + x];
  624. }
  625. }
  626. }
  627. wxBitmap icon = wxBitmapFromMemoryRGBA((u8*)pxdata, 96, 32);
  628. images[i*2 + 1] = list->Add(icon);
  629. }
  630. }
  631. int pagesMax = (mcmSettings.usePages) ?
  632. (page[card] + 1) * itemsPerPage : 128;
  633. for (j = page[card] * itemsPerPage; (j < nFiles) && (j < pagesMax); j++)
  634. {
  635. u16 blocks;
  636. u16 firstblock;
  637. u8 fileIndex = memoryCard[card]->GetFileIndex(j);
  638. int index = m_MemcardList[card]->InsertItem(j, wxEmptyString);
  639. m_MemcardList[card]->SetItem(index, COLUMN_BANNER, wxEmptyString);
  640. std::string title = memoryCard[card]->GetSaveComment1(fileIndex);
  641. std::string comment = memoryCard[card]->GetSaveComment2(fileIndex);
  642. auto const string_decoder = memoryCard[card]->IsAsciiEncoding() ?
  643. CP1252ToUTF8 : SHIFTJISToUTF8;
  644. wxTitle = StrToWxStr(string_decoder(title));
  645. wxComment = StrToWxStr(string_decoder(comment));
  646. m_MemcardList[card]->SetItem(index, COLUMN_TITLE, wxTitle);
  647. m_MemcardList[card]->SetItem(index, COLUMN_COMMENT, wxComment);
  648. blocks = memoryCard[card]->DEntry_BlockCount(fileIndex);
  649. if (blocks == 0xFFFF)
  650. blocks = 0;
  651. wxBlock.Printf("%10d", blocks);
  652. m_MemcardList[card]->SetItem(index, COLUMN_BLOCKS, wxBlock);
  653. firstblock = memoryCard[card]->DEntry_FirstBlock(fileIndex);
  654. //if (firstblock == 0xFFFF) firstblock = 3; // to make firstblock -1
  655. wxFirstBlock.Printf("%15d", firstblock);
  656. m_MemcardList[card]->SetItem(index, COLUMN_FIRSTBLOCK, wxFirstBlock);
  657. m_MemcardList[card]->SetItem(index, COLUMN_ICON, wxEmptyString);
  658. if (images[j] >= 0)
  659. {
  660. m_MemcardList[card]->SetItemImage(index, images[j*2]);
  661. m_MemcardList[card]->SetItemColumnImage(index, COLUMN_ICON, images[j*2 + 1]);
  662. }
  663. }
  664. if (mcmSettings.usePages)
  665. {
  666. if (nFiles <= itemsPerPage)
  667. {
  668. m_PrevPage[card]->Disable();
  669. m_MemcardList[card]->prevPage = false;
  670. }
  671. if (j == nFiles)
  672. {
  673. m_NextPage[card]->Disable();
  674. m_MemcardList[card]->nextPage = false;
  675. }
  676. else
  677. {
  678. m_NextPage[card]->Enable();
  679. m_MemcardList[card]->nextPage = true;
  680. }
  681. }
  682. delete[] images;
  683. // Automatic column width and then show the list
  684. for (int i = COLUMN_BANNER; i <= COLUMN_FIRSTBLOCK; i++)
  685. {
  686. if (mcmSettings.column[i])
  687. m_MemcardList[card]->SetColumnWidth(i, wxLIST_AUTOSIZE);
  688. else
  689. m_MemcardList[card]->SetColumnWidth(i, 0);
  690. }
  691. m_MemcardList[card]->Show();
  692. wxLabel.Printf(_("%u Free Blocks; %u Free Dir Entries"),
  693. memoryCard[card]->GetFreeBlocks(), DIRLEN - nFiles);
  694. t_Status[card]->SetLabel(wxLabel);
  695. // Done so text doesn't overlap the UI.
  696. this->Fit();
  697. return true;
  698. }
  699. void CMemcardManager::CMemcardListCtrl::OnRightClick(wxMouseEvent& event)
  700. {
  701. int flags;
  702. long item = HitTest(event.GetPosition(), flags);
  703. wxMenu popupMenu;
  704. if (item != wxNOT_FOUND)
  705. {
  706. if (GetItemState(item, wxLIST_STATE_SELECTED) != wxLIST_STATE_SELECTED)
  707. {
  708. SetItemState(item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
  709. }
  710. SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
  711. int slot = GetId() - ID_MEMCARDLIST_A;
  712. popupMenu.Append(ID_COPYFROM_A + slot, wxString::Format(_("Copy to Memcard %c"), 'B' - slot));
  713. popupMenu.Append(ID_DELETE_A + slot, _("Delete Save"));
  714. popupMenu.Append(ID_SAVEIMPORT_A + slot, _("Import Save"));
  715. popupMenu.Append(ID_SAVEEXPORT_A + slot, _("Export Save"));
  716. popupMenu.Append(ID_EXPORTALL_A + slot, _("Export all saves"));
  717. popupMenu.FindItem(ID_COPYFROM_A + slot)->Enable(__mcmSettings.twoCardsLoaded);
  718. popupMenu.AppendSeparator();
  719. popupMenu.Append(ID_FIXCHECKSUM_A + slot, _("Fix Checksums"));
  720. popupMenu.Append(ID_PREVPAGE_A + slot, _("Previous Page"));
  721. popupMenu.Append(ID_NEXTPAGE_A + slot, _("Next Page"));
  722. popupMenu.Append(ID_MEMCARDPATH_A + slot, wxString::Format(_("Set as default Memcard %c"), 'A' + slot));
  723. popupMenu.AppendCheckItem(ID_USEPAGES, _("Enable pages"));
  724. popupMenu.FindItem(ID_PREVPAGE_A + slot)->Enable(prevPage && __mcmSettings.usePages);
  725. popupMenu.FindItem(ID_NEXTPAGE_A + slot)->Enable(nextPage && __mcmSettings.usePages);
  726. popupMenu.FindItem(ID_USEPAGES)->Check(__mcmSettings.usePages);
  727. popupMenu.AppendSeparator();
  728. // popupMenu->AppendCheckItem(COLUMN_BANNER, _("Show save banner"));
  729. popupMenu.AppendCheckItem(COLUMN_TITLE, _("Show save title"));
  730. popupMenu.AppendCheckItem(COLUMN_COMMENT, _("Show save comment"));
  731. popupMenu.AppendCheckItem(COLUMN_ICON, _("Show save icon"));
  732. popupMenu.AppendCheckItem(COLUMN_BLOCKS, _("Show save blocks"));
  733. popupMenu.AppendCheckItem(COLUMN_FIRSTBLOCK, _("Show first block"));
  734. // for (int i = COLUMN_BANNER; i <= COLUMN_FIRSTBLOCK; i++)
  735. for (int i = COLUMN_TITLE; i <= COLUMN_FIRSTBLOCK; i++)
  736. {
  737. popupMenu.FindItem(i)->Check(__mcmSettings.column[i]);
  738. }
  739. }
  740. PopupMenu(&popupMenu);
  741. }