bool CMemcardManager::ReloadMemcard(const std::string& fileName, int card)
{
	if (memoryCard[card]) delete memoryCard[card];

	// TODO: add error checking and animate icons
	memoryCard[card] = new GCMemcard(fileName);

	if (!memoryCard[card]->IsValid())
		return false;

	int j;

	wxString wxTitle,
			 wxComment,
			 wxBlock,
			 wxFirstBlock,
			 wxLabel;


	m_MemcardList[card]->Hide();
	m_MemcardList[card]->ClearAll();

	m_MemcardList[card]->InsertColumn(COLUMN_BANNER, _("Banner"));
	m_MemcardList[card]->InsertColumn(COLUMN_TITLE, _("Title"));
	m_MemcardList[card]->InsertColumn(COLUMN_COMMENT, _("Comment"));
	m_MemcardList[card]->InsertColumn(COLUMN_ICON, _("Icon"));
	m_MemcardList[card]->InsertColumn(COLUMN_BLOCKS, _("Blocks"));
	m_MemcardList[card]->InsertColumn(COLUMN_FIRSTBLOCK, _("First Block"));

	wxImageList *list = m_MemcardList[card]->GetImageList(wxIMAGE_LIST_SMALL);
	list->RemoveAll();

	u8 nFiles = memoryCard[card]->GetNumFiles();
	int *images = new int[nFiles*2];

	for (u8 i = 0;i < nFiles;i++)
	{
		static u32 pxdata[96*32];
		static u8  animDelay[8];
		static u32 animData[32*32*8];

		u8 fileIndex = memoryCard[card]->GetFileIndex(i);
		int numFrames = memoryCard[card]->ReadAnimRGBA8(fileIndex, animData, animDelay);

		if (!memoryCard[card]->ReadBannerRGBA8(fileIndex, pxdata))
		{
			memset(pxdata,0,96*32*4);

			if (numFrames>0) // Just use the first one
			{
				u32 *icdata = animData;

				for (int y=0;y<32;y++)
				{
					for (int x=0;x<32;x++)
					{
						pxdata[y*96+x+32] = icdata[y*32+x];//  | 0xFF000000
					}
				}
			}
		}

		wxBitmap map = wxBitmapFromMemoryRGBA((u8*)pxdata,96,32);
		images[i*2] = list->Add(map);

		if (numFrames>0)
		{
			memset(pxdata,0,96*32*4);
			int frames=3;

			if (numFrames<frames)
				frames=numFrames;

			for (int f=0;f<frames;f++)
			{
				for (int y=0;y<32;y++)
				{
					for (int x=0;x<32;x++)
					{
						pxdata[y*96 + x + 32*f] = animData[f*32*32 + y*32 + x];
					}
				}
			}
			wxBitmap icon = wxBitmapFromMemoryRGBA((u8*)pxdata,96,32);
			images[i*2+1] = list->Add(icon);
		}
	}

	int pagesMax = (mcmSettings.usePages) ?
					(page[card] + 1) * itemsPerPage : 128;

	for (j = page[card] * itemsPerPage; (j < nFiles) && (j < pagesMax); j++)
	{
		u16 blocks;
		u16 firstblock;
		u8 fileIndex = memoryCard[card]->GetFileIndex(j);


		int index = m_MemcardList[card]->InsertItem(j, wxEmptyString);

		m_MemcardList[card]->SetItem(index, COLUMN_BANNER, wxEmptyString);

		std::string title = memoryCard[card]->GetSaveComment1(fileIndex);
		std::string comment = memoryCard[card]->GetSaveComment2(fileIndex);

		auto const string_decoder = memoryCard[card]->IsAsciiEncoding() ?
			CP1252ToUTF8 : SHIFTJISToUTF8;

		wxTitle = StrToWxStr(string_decoder(title));
		wxComment = StrToWxStr(string_decoder(comment));

		m_MemcardList[card]->SetItem(index, COLUMN_TITLE, wxTitle);
		m_MemcardList[card]->SetItem(index, COLUMN_COMMENT, wxComment);

		blocks = memoryCard[card]->DEntry_BlockCount(fileIndex);

		if (blocks == 0xFFFF)
			blocks = 0;

		wxBlock.Printf("%10d", blocks);
		m_MemcardList[card]->SetItem(index,COLUMN_BLOCKS, wxBlock);
		firstblock = memoryCard[card]->DEntry_FirstBlock(fileIndex);
		//if (firstblock == 0xFFFF) firstblock = 3; // to make firstblock -1
		wxFirstBlock.Printf("%15d", firstblock);
		m_MemcardList[card]->SetItem(index, COLUMN_FIRSTBLOCK, wxFirstBlock);
		m_MemcardList[card]->SetItem(index, COLUMN_ICON, wxEmptyString);

		if (images[j] >= 0)
		{
			m_MemcardList[card]->SetItemImage(index, images[j*2]);
			m_MemcardList[card]->SetItemColumnImage(index, COLUMN_ICON, images[j*2+1]);
		}
	}

	if (mcmSettings.usePages)
	{
		if (nFiles <= itemsPerPage)
		{
			m_PrevPage[card]->Disable();
			m_MemcardList[card]->prevPage = false;
		}
		if (j == nFiles)
		{
			m_NextPage[card]->Disable();
			m_MemcardList[card]->nextPage = false;
		}
		else
		{
			m_NextPage[card]->Enable();
			m_MemcardList[card]->nextPage = true;
		}
	}

	delete[] images;
	// Automatic column width and then show the list
	for (int i = COLUMN_BANNER; i <= COLUMN_FIRSTBLOCK; i++)
	{
		if (mcmSettings.column[i])
			m_MemcardList[card]->SetColumnWidth(i, wxLIST_AUTOSIZE);
		else
			m_MemcardList[card]->SetColumnWidth(i, 0);
	}

	m_MemcardList[card]->Show();
	wxLabel.Printf(_("%u Free Blocks; %u Free Dir Entries"),
		memoryCard[card]->GetFreeBlocks(), DIRLEN - nFiles);
	t_Status[card]->SetLabel(wxLabel);

	// Done so text doesn't overlap the UI.
	this->Fit();

	return true;
}
Esempio n. 2
0
bool CMemcardManager::ReloadMemcard(const char *fileName, int card)
{
	if (memoryCard[card]) delete memoryCard[card];

	// TODO: add error checking and animate icons
	memoryCard[card] = new GCMemcard(fileName);

	if (memoryCard[card]->fail) return false;

	int j;

	wxString wxTitle,
			 wxComment,
			 wxBlock,
			 wxFirstBlock,
			 wxLabel,
			 tString;


	m_MemcardList[card]->Hide();
	m_MemcardList[card]->ClearAll();

	m_MemcardList[card]->InsertColumn(COLUMN_BANNER, _("Banner"));
	m_MemcardList[card]->InsertColumn(COLUMN_TITLE, _("Title"));
	m_MemcardList[card]->InsertColumn(COLUMN_COMMENT, _("Comment"));
	m_MemcardList[card]->InsertColumn(COLUMN_ICON, _("Icon"));
	m_MemcardList[card]->InsertColumn(COLUMN_BLOCKS, _("Blocks"));
	m_MemcardList[card]->InsertColumn(COLUMN_FIRSTBLOCK, _("First Block"));

	wxImageList *list = m_MemcardList[card]->GetImageList(wxIMAGE_LIST_SMALL);
	list->RemoveAll();

	int nFiles = memoryCard[card]->GetNumFiles();
	int *images = new int[nFiles*2];

	for (int i = 0;i < nFiles;i++)
	{
		static u32 pxdata[96*32];
		static u8  animDelay[8];
		static u32 animData[32*32*8];

		int numFrames = memoryCard[card]->ReadAnimRGBA8(i,animData,animDelay);

		if (!memoryCard[card]->ReadBannerRGBA8(i,pxdata))
		{
			memset(pxdata,0,96*32*4);

			if (numFrames>0) // Just use the first one
			{
				u32 *icdata = animData;

				for (int y=0;y<32;y++)
				{
					for (int x=0;x<32;x++)
					{
						pxdata[y*96+x+32] = icdata[y*32+x];//  | 0xFF000000
					}
				}
			}
		}

		wxBitmap map = wxBitmapFromMemoryRGBA((u8*)pxdata,96,32);
		images[i*2] = list->Add(map);

		if (numFrames>0)
		{
			memset(pxdata,0,96*32*4);
			int frames=3;
			if (numFrames<frames) frames=numFrames;
			for (int f=0;f<frames;f++)
			{
				for (int y=0;y<32;y++)
				{
					for (int x=0;x<32;x++)
					{
						pxdata[y*96 + x + 32*f] = animData[f*32*32 + y*32 + x];
					}
				}
			}
			wxBitmap icon = wxBitmapFromMemoryRGBA((u8*)pxdata,96,32);
			images[i*2+1] = list->Add(icon);
		}
	}

	int	pagesMax = (mcmSettings.usePages) ?
					(page[card] + 1) * itemsPerPage : 128;

	for (j = page[card] * itemsPerPage; (j < nFiles) && (j < pagesMax); j++)
	{
		char title[DENTRY_STRLEN];
		char comment[DENTRY_STRLEN];
		u16 blocks;
		u16 firstblock;

		int index = m_MemcardList[card]->InsertItem(j, wxEmptyString);

		m_MemcardList[card]->SetItem(index, COLUMN_BANNER, wxEmptyString);

		if (!memoryCard[card]->DEntry_Comment1(j, title)) title[0]=0;
		if (!memoryCard[card]->DEntry_Comment2(j, comment)) comment[0]=0;

		bool ascii = memoryCard[card]->IsAsciiEncoding();
#ifdef _WIN32
		wxCSConv SJISConv(wxFontMapper::GetEncodingName(wxFONTENCODING_SHIFT_JIS));
#else
		wxCSConv SJISConv(wxFontMapper::GetEncodingName(wxFONTENCODING_EUC_JP));
#endif
		wxTitle  =  wxString(title, ascii ? *wxConvCurrent : SJISConv);
		wxComment = wxString(comment, ascii ? *wxConvCurrent : SJISConv);

		m_MemcardList[card]->SetItem(index, COLUMN_TITLE, wxTitle);
		m_MemcardList[card]->SetItem(index, COLUMN_COMMENT, wxComment);

		blocks = memoryCard[card]->DEntry_BlockCount(j);
		if (blocks == 0xFFFF) blocks = 0;
		wxBlock.Printf(wxT("%10d"), blocks);
		m_MemcardList[card]->SetItem(index,COLUMN_BLOCKS, wxBlock);
		firstblock = memoryCard[card]->DEntry_FirstBlock(j);
		if (firstblock == 0xFFFF) firstblock = 3;	// to make firstblock -1
		wxFirstBlock.Printf(wxT("%15d"), firstblock-4);
		m_MemcardList[card]->SetItem(index,COLUMN_FIRSTBLOCK, wxFirstBlock);
		m_MemcardList[card]->SetItem(index, COLUMN_ICON, wxEmptyString);

		if (images[j] >= 0)
		{
			m_MemcardList[card]->SetItemImage(index, images[j*2]);
			m_MemcardList[card]->SetItemColumnImage(index, COLUMN_ICON, images[j*2+1]);
		}
	}

	if (mcmSettings.usePages)
	{
		if (nFiles <= itemsPerPage)
		{
			m_PrevPage[card]->Disable();
			m_MemcardList[card]->prevPage = false;
		}
		if (j == nFiles)
		{
			m_NextPage[card]->Disable();
			m_MemcardList[card]->nextPage = false;
		}
		else
		{
			m_NextPage[card]->Enable();
			m_MemcardList[card]->nextPage = true;
		}
	}

	delete[] images;
	// Automatic column width and then show the list
	for (int i = COLUMN_BANNER; i <= COLUMN_FIRSTBLOCK; i++)
	{
		if (mcmSettings.column[i])
			m_MemcardList[card]->SetColumnWidth(i, wxLIST_AUTOSIZE);
		else
			m_MemcardList[card]->SetColumnWidth(i, 0);
	}

	m_MemcardList[card]->Show();
	wxLabel.Printf(_("%u Free Blocks; %u Free Dir Entries"),
		memoryCard[card]->GetFreeBlocks(), DIRLEN - nFiles);
	t_Status[card]->SetLabel(wxLabel);

	return true;
}