usbloadergx/source/usbloader/GameList.cpp

384 lines
11 KiB
C++
Raw Normal View History

/****************************************************************************
2010-09-24 02:48:03 +02:00
* Copyright (C) 2010
* by Dimok
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any
* purpose, including commercial applications, and to alter it and
* redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you
* must not claim that you wrote the original software. If you use
* this software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and
* must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
***************************************************************************/
#include <algorithm>
#include <string>
#include <wctype.h>
#include <malloc.h>
#include "usbloader/wbfs.h"
#include "settings/newtitles.h"
#include "settings/CSettings.h"
#include "settings/CGameSettings.h"
#include "settings/CGameStatistics.h"
#include "settings/GameTitles.h"
#include "xml/xml.h"
#include "FreeTypeGX.h"
#include "GameList.h"
#include "memory/memory.h"
GameList gameList;
void GameList::clear()
{
GameFilter.clear();
AvailableSearchChars.clear();
FullGameList.clear();
GamePartitionList.clear();
FilteredList.clear();
//! Clear memory of the vector completely
2010-09-24 02:48:03 +02:00
std::vector<struct discHdr *>().swap(FilteredList);
std::vector<struct discHdr>().swap(FullGameList);
std::vector<int>().swap(GamePartitionList);
}
struct discHdr * GameList::GetDiscHeader(const char * gameID) const
{
if(!gameID) return NULL;
for (u32 i = 0; i < FilteredList.size(); ++i)
{
if(strncasecmp(gameID, (const char *) FilteredList[i]->id, 6) == 0)
return FilteredList[i];
}
return NULL;
}
int GameList::GetPartitionNumber(const u8 *gameID) const
{
if(!gameID) return -1;
for (u32 i = 0; i < FullGameList.size(); ++i)
{
if(strncasecmp((const char *) gameID, (const char *) FullGameList[i].id, 6) == 0)
return GamePartitionList[i];
}
return -1;
}
void GameList::RemovePartition(int part)
{
for(u32 i = 0; i < GamePartitionList.size(); ++i)
{
if(GamePartitionList[i] == part)
{
FullGameList.erase(FullGameList.begin()+i);
GamePartitionList.erase(GamePartitionList.begin()+i);
--i;
}
}
if(FullGameList.size() > 0)
{
wString filter(GameFilter);
FilterList(filter.c_str());
}
}
int GameList::InternalReadList(int part)
{
// Retrieve all stuff from WBFS
u32 cnt = 0;
int ret = WBFS_GetCount(part, &cnt);
2010-09-24 02:48:03 +02:00
if (ret < 0) return -1;
// We are done here if no games are there
if(cnt == 0)
return 0;
/* Buffer length */
2010-09-24 02:48:03 +02:00
u32 len = sizeof(struct discHdr) * cnt;
/* Allocate memory */
2010-09-24 02:48:03 +02:00
struct discHdr *buffer = (struct discHdr *) allocate_memory( len );
if (!buffer) return -1;
/* Clear buffer */
2010-09-24 02:48:03 +02:00
memset(buffer, 0, len);
/* Get header list */
ret = WBFS_GetHeaders(part, buffer, cnt, sizeof(struct discHdr));
2010-09-24 02:48:03 +02:00
if (ret < 0)
{
free(buffer);
return -1;
}
u32 oldSize = FullGameList.size();
std::vector<struct discHdr> PartGameList(cnt);
memcpy(&PartGameList[0], buffer, len);
free(buffer);
for (u32 i = 0; i < PartGameList.size(); i++)
{
for(u32 j = 0; j < FullGameList.size(); j++)
{
if(strncasecmp((const char *) PartGameList[i].id, (const char *) FullGameList[j].id, 6) == 0)
{
PartGameList.erase(PartGameList.begin()+i);
--i;
break;
}
}
}
FullGameList.resize(oldSize+PartGameList.size());
memcpy(&FullGameList[oldSize], &PartGameList[0], PartGameList.size()*sizeof(struct discHdr));
GamePartitionList.resize(oldSize+PartGameList.size());
for(u32 i = oldSize; i < GamePartitionList.size(); ++i)
GamePartitionList[i] = part;
return PartGameList.size();
}
int GameList::ReadGameList()
{
// Clear list
clear();
if(!Settings.MultiplePartitions)
{
int ret = InternalReadList(Settings.partition);
if(ret <= 0) return ret;
}
else
{
PartitionHandle * usbHandle = DeviceHandler::Instance()->GetUSBHandle();
int cnt = 0;
for(int part = 0; part < usbHandle->GetPartitionTotalCount(); ++part)
{
int ret = InternalReadList(part);
if(ret > 0) cnt += ret;
}
if(!cnt) return cnt;
}
return LoadUnfiltered();
}
static bool WCharSortCallback(const wchar_t char1, const wchar_t char2)
{
2010-09-24 02:48:03 +02:00
if (char2 == 0) return true;
if (char1 == 0) return false;
return char2 > char1;
}
2010-09-24 02:48:03 +02:00
int GameList::FilterList(const wchar_t * gameFilter)
{
2010-09-24 02:48:03 +02:00
if (FullGameList.size() == 0) ReadGameList();
if (gameFilter) GameFilter.assign(gameFilter);
FilteredList.clear();
AvailableSearchChars.clear();
2010-09-24 02:48:03 +02:00
for (u32 i = 0; i < FullGameList.size(); ++i)
{
struct discHdr *header = &FullGameList[i];
/* Register game */
2010-09-24 02:48:03 +02:00
NewTitles::Instance()->CheckGame(header->id);
/* Filters */
if (Settings.GameSort & SORT_FAVORITE)
{
GameStatus * GameStats = GameStatistics.GetGameStatus(header->id);
*Fixed display of partition size on WBFS partitions with a different wbfs sector size than 512bytes. *Made the ProgressWindow for game installation more accurate *Added displaying newly installed games (marked as new) on favorite list, so you don't have to change to full list when installing new games. (Thanks Cyan for the patch) *Lot's a small fixes *Added WDM Menu on game start. You can set it in the alternative DOL option (one new option there). The menu lists all DOLs on the disc and if a wdm file is provided in the WDM path (configurable in the settings) than the dol parameter and dol replacement name will be taken from the wdm. The DOLs that are not listed in the WDM but exist on the DISC will be listed at the end of the list. *Added avoid of multiple app cleanup when game fails to boot *Changed libfat to use FS info sector on FAT32 partitions. This speeds up the free space information getting to instant. For that the FS info sector has to have correct values. The values of all partitions where homebrews were writing to are currently incorrect because the official libfat does not support FS info sector (i submited a patch) (Windows does write it correct though). That is why there needs to be a synchronization of the FS info sector for partitions used with homebrews. For this purpose a new setting was added in the Loader Settings. You can synchronize all your FAT32 partitions on the USB with it once and you are done (if you don't write to that partition with current homebrews). After that you can enable free space display and it will be instant like on WBFS/NTFS/EXT partitions.
2011-01-16 14:12:07 +01:00
if (Settings.marknewtitles)
{
bool isNew = NewTitles::Instance()->IsNew(header->id);
if (!isNew && (!GameStats || GameStats->FavoriteRank == 0)) continue;
}
else
{
if (!GameStats || GameStats->FavoriteRank == 0) continue;
}
}
//ignore uLoader cfg "iso". i was told it is "__CFG_" but not confirmed
if (strncasecmp((char*) header->id, "__CFG_", 6) == 0)
continue;
GameCFG * GameConfig = GameSettings.GetGameCFG(header);
/* Rating based parental control method */
if (Settings.parentalcontrol != PARENTAL_LVL_ADULT && !Settings.godmode)
{
if (GameConfig && GameConfig->parentalcontrol > Settings.parentalcontrol)
continue;
// Check game rating in WiiTDB, since the default Wii parental control setting is enabled
int rating = GameTitles.GetParentalRating((char *) header->id);
if (rating > Settings.parentalcontrol)
continue;
}
//! Per game lock method
if(!Settings.godmode && GameConfig && GameConfig->Locked)
continue;
wchar_t *gameName = charToWideChar(GameTitles.GetTitle(header));
2010-09-24 02:48:03 +02:00
if (gameName && *GameFilter.c_str())
{
2010-09-24 02:48:03 +02:00
if (wcsnicmp(gameName, GameFilter.c_str(), GameFilter.size()) != 0)
{
delete [] gameName;
continue;
}
}
2010-09-24 02:48:03 +02:00
if (gameName)
{
if (wcslen(gameName) > GameFilter.size() &&
AvailableSearchChars.find(towupper(gameName[GameFilter.size()])) == std::string::npos &&
AvailableSearchChars.find(towlower(gameName[GameFilter.size()])) == std::string::npos)
{
AvailableSearchChars.push_back(gameName[GameFilter.size()]);
}
delete [] gameName;
}
2010-09-24 02:48:03 +02:00
FilteredList.push_back(header);
}
NewTitles::Instance()->Save();
2010-09-24 02:48:03 +02:00
AvailableSearchChars.push_back(L'\0');
if (FilteredList.size() < 2)
AvailableSearchChars.clear();
SortList();
return FilteredList.size();
}
int GameList::LoadUnfiltered()
{
2010-09-24 02:48:03 +02:00
if (FullGameList.size() == 0) ReadGameList();
GameFilter.clear();
AvailableSearchChars.clear();
FilteredList.clear();
2010-09-24 02:48:03 +02:00
for (u32 i = 0; i < FullGameList.size(); ++i)
{
struct discHdr *header = &FullGameList[i];
/* Register game */
2010-09-24 02:48:03 +02:00
NewTitles::Instance()->CheckGame(header->id);
wchar_t *gameName = charToWideChar(GameTitles.GetTitle(header));
2010-09-24 02:48:03 +02:00
if (gameName)
{
if (wcslen(gameName) > GameFilter.size() &&
AvailableSearchChars.find(towupper(gameName[GameFilter.size()])) == std::string::npos &&
AvailableSearchChars.find(towlower(gameName[GameFilter.size()])) == std::string::npos)
{
AvailableSearchChars.push_back(gameName[GameFilter.size()]);
}
delete [] gameName;
}
2010-09-24 02:48:03 +02:00
FilteredList.push_back(header);
}
NewTitles::Instance()->Save();
2010-09-24 02:48:03 +02:00
AvailableSearchChars.push_back(L'\0');
if (FilteredList.size() < 2)
AvailableSearchChars.clear();
SortList();
return FilteredList.size();
}
void GameList::SortList()
{
2010-09-24 02:48:03 +02:00
if (FilteredList.size() < 2) return;
if (Settings.GameSort & SORT_PLAYCOUNT)
{
2010-09-24 02:48:03 +02:00
std::sort(FilteredList.begin(), FilteredList.end(), PlaycountSortCallback);
}
else if(Settings.GameSort & SORT_RANKING)
{
std::sort(FilteredList.begin(), FilteredList.end(), RankingSortCallback);
}
else if(Settings.GameSort & SORT_PLAYERS)
{
std::sort(FilteredList.begin(), FilteredList.end(), PlayersSortCallback);
}
else
{
2010-09-24 02:48:03 +02:00
std::sort(FilteredList.begin(), FilteredList.end(), NameSortCallback);
}
if (AvailableSearchChars.size() > 1)
std::sort(AvailableSearchChars.begin(), AvailableSearchChars.end(), WCharSortCallback);
}
bool GameList::NameSortCallback(const struct discHdr *a, const struct discHdr *b)
{
return (strcasecmp(GameTitles.GetTitle((struct discHdr *) a), GameTitles.GetTitle((struct discHdr *) b)) < 0);
}
bool GameList::PlaycountSortCallback(const struct discHdr *a, const struct discHdr *b)
{
int count1 = GameStatistics.GetPlayCount(a->id);
int count2 = GameStatistics.GetPlayCount(b->id);
2010-09-24 02:48:03 +02:00
if (count1 == count2) return NameSortCallback(a, b);
return (count1 > count2);
}
bool GameList::RankingSortCallback(const struct discHdr *a, const struct discHdr *b)
{
int fav1 = GameStatistics.GetFavoriteRank(a->id);
int fav2 = GameStatistics.GetFavoriteRank(b->id);
2010-09-24 02:48:03 +02:00
if (fav1 == fav2) return NameSortCallback(a, b);
return (fav1 > fav2);
}
bool GameList::PlayersSortCallback(const struct discHdr *a, const struct discHdr *b)
{
int count1 = GameTitles.GetPlayersCount((const char *) a->id);
int count2 = GameTitles.GetPlayersCount((const char *) b->id);
if (count1 == count2) return NameSortCallback(a, b);
return (count1 > count2);
}