2010-09-19 01:16:05 +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.
|
|
|
|
***************************************************************************/
|
2010-09-18 13:46:25 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include "usbloader/wbfs.h"
|
|
|
|
#include "settings/newtitles.h"
|
|
|
|
#include "settings/cfg.h"
|
|
|
|
#include "xml/xml.h"
|
|
|
|
#include "FreeTypeGX.h"
|
|
|
|
#include "GameList.h"
|
|
|
|
#include "memory.h"
|
|
|
|
|
|
|
|
GameList gameList;
|
|
|
|
|
|
|
|
GameList::GameList()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameList::clear()
|
|
|
|
{
|
|
|
|
FullGameList.clear();
|
|
|
|
FilteredList.clear();
|
|
|
|
//! Clear memory of the vector completely
|
2010-09-19 01:16:05 +02:00
|
|
|
std::vector<struct discHdr *>().swap( FilteredList );
|
|
|
|
std::vector<struct discHdr>().swap( FullGameList );
|
2010-09-18 13:46:25 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
struct discHdr * GameList::at( int i )
|
2010-09-18 13:46:25 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( i < 0 || i >= ( int ) FilteredList.size() )
|
2010-09-18 13:46:25 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return FilteredList[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
int GameList::ReadGameList()
|
|
|
|
{
|
|
|
|
FullGameList.clear();
|
|
|
|
FilteredList.clear();
|
|
|
|
|
|
|
|
// Retrieve all stuff from WBFS
|
|
|
|
u32 cnt;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int ret = WBFS_GetCount( &cnt );
|
|
|
|
if ( ret < 0 )
|
2010-09-18 13:46:25 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Buffer length */
|
2010-09-19 01:16:05 +02:00
|
|
|
u32 len = sizeof( struct discHdr ) * cnt;
|
2010-09-18 13:46:25 +02:00
|
|
|
|
|
|
|
/* Allocate memory */
|
2010-09-19 01:16:05 +02:00
|
|
|
struct discHdr *buffer = ( struct discHdr * ) allocate_memory( len );
|
|
|
|
if ( !buffer )
|
2010-09-18 13:46:25 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Clear buffer */
|
2010-09-19 01:16:05 +02:00
|
|
|
memset( buffer, 0, len );
|
2010-09-18 13:46:25 +02:00
|
|
|
|
|
|
|
/* Get header list */
|
2010-09-19 01:16:05 +02:00
|
|
|
ret = WBFS_GetHeaders( buffer, cnt, sizeof( struct discHdr ) );
|
|
|
|
if ( ret < 0 )
|
2010-09-18 13:46:25 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( buffer )
|
|
|
|
free( buffer );
|
2010-09-18 13:46:25 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
FullGameList.resize( cnt );
|
|
|
|
memcpy( &FullGameList[0], buffer, len );
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
free( buffer );
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
return LoadUnfiltered();
|
2010-09-18 13:46:25 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
static bool WCharSortCallback( const wchar_t char1, const wchar_t char2 )
|
2010-09-18 13:46:25 +02:00
|
|
|
{
|
2010-09-19 03:16:04 +02:00
|
|
|
if( char2 == 0 )return true;
|
|
|
|
if( char1 == 0 )return false;
|
2010-09-18 13:46:25 +02:00
|
|
|
return char2 > char1;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int GameList::FilterList( const wchar_t * gameFilter )
|
2010-09-18 13:46:25 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( FullGameList.size() == 0 )
|
2010-09-18 13:46:25 +02:00
|
|
|
ReadGameList();
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( gameFilter )
|
|
|
|
GameFilter.assign( gameFilter );
|
2010-09-18 13:46:25 +02:00
|
|
|
|
|
|
|
FilteredList.clear();
|
|
|
|
AvailableSearchChars.clear();
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
for ( u32 i = 0; i < FullGameList.size(); ++i )
|
2010-09-18 13:46:25 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
struct discHdr *header = &FullGameList[i];
|
|
|
|
|
|
|
|
/* Register game */
|
|
|
|
NewTitles::Instance()->CheckGame( header->id );
|
|
|
|
|
|
|
|
/* Filters */
|
|
|
|
if ( Settings.fave )
|
|
|
|
{
|
|
|
|
struct Game_NUM* game_num = CFG_get_game_num( header->id );
|
|
|
|
if ( !game_num || game_num->favorite == 0 )
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//ignore uLoader cfg "iso". i was told it is "__CFG_" but not confirmed
|
|
|
|
if ( strncasecmp( ( char* )header->id, "__CFG_", 6 ) == 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( Settings.parentalcontrol && !Settings.godmode )
|
|
|
|
if ( get_block( header ) >= Settings.parentalcontrol )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Other parental control method */
|
|
|
|
if ( Settings.parentalcontrol == 0 && Settings.godmode == 0 && Settings.parental.enabled == 1 )
|
|
|
|
{
|
|
|
|
// Check game rating in WiiTDB, since the default Wii parental control setting is enabled
|
|
|
|
s32 rating = GetRatingForGame( ( char * ) header->id );
|
|
|
|
if ( ( rating != -1 && rating > Settings.parental.rating ) ||
|
|
|
|
( rating == -1 && get_pegi_block( header ) > Settings.parental.rating ) )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t *gameName = charToWideChar( get_title( header ) );
|
|
|
|
|
|
|
|
if ( gameName && *GameFilter.c_str() )
|
|
|
|
{
|
|
|
|
if ( wcsnicmp( gameName, GameFilter.c_str(), GameFilter.size() ) != 0 )
|
|
|
|
{
|
|
|
|
delete [] gameName;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( gameName )
|
|
|
|
{
|
|
|
|
if ( wcslen( gameName ) > GameFilter.size() && AvailableSearchChars.find( gameName[GameFilter.size()] ) == std::string::npos )
|
|
|
|
AvailableSearchChars.push_back( gameName[GameFilter.size()] );
|
2010-09-18 13:46:25 +02:00
|
|
|
delete [] gameName;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
FilteredList.push_back( header );
|
|
|
|
}
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
NewTitles::Instance()->Save();
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
AvailableSearchChars.push_back( L'\0' );
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( FilteredList.size() < 2 )
|
2010-09-18 13:46:25 +02:00
|
|
|
AvailableSearchChars.clear();
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
SortList();
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
return FilteredList.size();
|
2010-09-18 13:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int GameList::LoadUnfiltered()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( FullGameList.size() == 0 )
|
2010-09-18 13:46:25 +02:00
|
|
|
ReadGameList();
|
|
|
|
|
|
|
|
GameFilter.clear();
|
|
|
|
AvailableSearchChars.clear();
|
|
|
|
FilteredList.clear();
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
for ( u32 i = 0; i < FullGameList.size(); ++i )
|
2010-09-18 13:46:25 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
struct discHdr *header = &FullGameList[i];
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
/* Register game */
|
|
|
|
NewTitles::Instance()->CheckGame( header->id );
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
wchar_t *gameName = charToWideChar( get_title( header ) );
|
|
|
|
if ( gameName )
|
|
|
|
{
|
|
|
|
if ( wcslen( gameName ) > GameFilter.size() && AvailableSearchChars.find( gameName[GameFilter.size()] ) == std::string::npos )
|
|
|
|
AvailableSearchChars.push_back( gameName[GameFilter.size()] );
|
2010-09-18 13:46:25 +02:00
|
|
|
delete [] gameName;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
FilteredList.push_back( header );
|
|
|
|
}
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
NewTitles::Instance()->Save();
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
AvailableSearchChars.push_back( L'\0' );
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( FilteredList.size() < 2 )
|
2010-09-18 13:46:25 +02:00
|
|
|
AvailableSearchChars.clear();
|
|
|
|
|
|
|
|
SortList();
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
return FilteredList.size();
|
2010-09-18 13:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameList::SortList()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( FilteredList.size() < 2 )
|
2010-09-18 13:46:25 +02:00
|
|
|
return;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( Settings.sort == PLAYCOUNT )
|
|
|
|
{
|
|
|
|
std::sort( FilteredList.begin(), FilteredList.end(), PlaycountSortCallback );
|
|
|
|
}
|
|
|
|
else if ( Settings.fave )
|
|
|
|
{
|
|
|
|
std::sort( FilteredList.begin(), FilteredList.end(), FavoriteSortCallback );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::sort( FilteredList.begin(), FilteredList.end(), NameSortCallback );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( AvailableSearchChars.size() > 1 )
|
|
|
|
std::sort( AvailableSearchChars.begin(), AvailableSearchChars.end(), WCharSortCallback );
|
2010-09-18 13:46:25 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
bool GameList::NameSortCallback( const struct discHdr *a, const struct discHdr *b )
|
2010-09-18 13:46:25 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
return ( strcasecmp( get_title( ( struct discHdr * ) a ), get_title( ( struct discHdr * ) b ) ) < 0 );
|
2010-09-18 13:46:25 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
bool GameList::PlaycountSortCallback( const struct discHdr *a, const struct discHdr *b )
|
2010-09-18 13:46:25 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
struct Game_NUM* game_num1 = CFG_get_game_num( a->id );
|
|
|
|
struct Game_NUM* game_num2 = CFG_get_game_num( b->id );
|
2010-09-18 13:46:25 +02:00
|
|
|
int count1 = 0, count2 = 0;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( game_num1 )
|
2010-09-18 13:46:25 +02:00
|
|
|
count1 = game_num1->count;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( game_num2 )
|
2010-09-18 13:46:25 +02:00
|
|
|
count2 = game_num2->count;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( count1 == count2 )
|
|
|
|
return NameSortCallback( a, b );
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
return ( count1 > count2 );
|
2010-09-18 13:46:25 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
bool GameList::FavoriteSortCallback( const struct discHdr *a, const struct discHdr *b )
|
2010-09-18 13:46:25 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
struct Game_NUM* game_num1 = CFG_get_game_num( a->id );
|
|
|
|
struct Game_NUM* game_num2 = CFG_get_game_num( b->id );
|
2010-09-18 13:46:25 +02:00
|
|
|
int fav1 = 0, fav2 = 0;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( game_num1 )
|
2010-09-18 13:46:25 +02:00
|
|
|
fav1 = game_num1->favorite;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( game_num2 )
|
2010-09-18 13:46:25 +02:00
|
|
|
fav2 = game_num2->favorite;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( fav1 == fav2 );
|
|
|
|
return NameSortCallback( a, b );
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
return ( fav1 > fav2 );
|
2010-09-18 13:46:25 +02:00
|
|
|
}
|