2014-06-25 16:29:58 +00:00
# include "FileData.h"
2017-06-12 16:38:59 +00:00
# include "FileSorts.h"
# include "views/ViewController.h"
2014-06-25 16:29:58 +00:00
# include "SystemData.h"
2017-06-12 16:38:59 +00:00
# include "Log.h"
# include "AudioManager.h"
# include "VolumeControl.h"
# include "Util.h"
2014-06-25 16:29:58 +00:00
namespace fs = boost : : filesystem ;
2017-06-12 16:38:59 +00:00
FileData : : FileData ( FileType type , const fs : : path & path , SystemEnvironmentData * envData , SystemData * system )
: mType ( type ) , mPath ( path ) , mSystem ( system ) , mEnvData ( envData ) , mSourceFileData ( NULL ) , mParent ( NULL ) , metadata ( type = = GAME ? GAME_METADATA : FOLDER_METADATA ) // metadata is REALLY set in the constructor!
2014-06-25 16:29:58 +00:00
{
// metadata needs at least a name field (since that's what getName() will return)
if ( metadata . get ( " name " ) . empty ( ) )
2016-03-29 15:33:19 +00:00
metadata . set ( " name " , getDisplayName ( ) ) ;
2017-06-12 16:38:59 +00:00
mSystemName = system - > getName ( ) ;
2014-06-25 16:29:58 +00:00
}
FileData : : ~ FileData ( )
{
if ( mParent )
mParent - > removeChild ( this ) ;
2017-07-18 09:45:50 +00:00
if ( mType = = GAME )
mSystem - > getIndex ( ) - > removeFromIndex ( this ) ;
2017-06-12 16:38:59 +00:00
mChildren . clear ( ) ;
2014-06-25 16:29:58 +00:00
}
2016-03-29 15:33:19 +00:00
std : : string FileData : : getDisplayName ( ) const
2014-06-25 16:29:58 +00:00
{
std : : string stem = mPath . stem ( ) . generic_string ( ) ;
if ( mSystem & & mSystem - > hasPlatformId ( PlatformIds : : ARCADE ) | | mSystem - > hasPlatformId ( PlatformIds : : NEOGEO ) )
stem = PlatformIds : : getCleanMameName ( stem . c_str ( ) ) ;
2015-09-19 01:25:31 +00:00
return stem ;
2014-06-25 16:29:58 +00:00
}
2016-03-29 15:33:19 +00:00
std : : string FileData : : getCleanName ( ) const
{
return removeParenthesis ( this - > getDisplayName ( ) ) ;
}
2014-06-25 16:29:58 +00:00
const std : : string & FileData : : getThumbnailPath ( ) const
{
if ( ! metadata . get ( " thumbnail " ) . empty ( ) )
return metadata . get ( " thumbnail " ) ;
else
return metadata . get ( " image " ) ;
}
2017-06-12 16:38:59 +00:00
const std : : string & FileData : : getName ( )
{
return metadata . get ( " name " ) ;
}
2017-03-18 17:54:39 +00:00
const std : : vector < FileData * > & FileData : : getChildrenListToDisplay ( ) {
2017-05-18 10:16:57 +00:00
2017-07-18 09:45:50 +00:00
FileFilterIndex * idx = CollectionSystemManager : : get ( ) - > getSystemToView ( mSystem ) - > getIndex ( ) ;
2017-03-18 17:54:39 +00:00
if ( idx - > isFiltered ( ) ) {
mFilteredChildren . clear ( ) ;
for ( auto it = mChildren . begin ( ) ; it ! = mChildren . end ( ) ; it + + )
{
if ( idx - > showFile ( ( * it ) ) ) {
mFilteredChildren . push_back ( * it ) ;
}
}
return mFilteredChildren ;
}
2017-05-18 10:16:57 +00:00
else
2017-03-18 17:54:39 +00:00
{
return mChildren ;
}
}
2016-12-04 23:47:34 +00:00
const std : : string & FileData : : getVideoPath ( ) const
{
2017-03-17 15:33:46 +00:00
return metadata . get ( " video " ) ;
2016-12-04 23:47:34 +00:00
}
const std : : string & FileData : : getMarqueePath ( ) const
{
2017-03-17 15:33:46 +00:00
return metadata . get ( " marquee " ) ;
2016-12-04 23:47:34 +00:00
}
2017-03-18 17:54:39 +00:00
std : : vector < FileData * > FileData : : getFilesRecursive ( unsigned int typeMask , bool displayedOnly ) const
2014-06-25 16:29:58 +00:00
{
std : : vector < FileData * > out ;
2017-03-18 17:54:39 +00:00
FileFilterIndex * idx = mSystem - > getIndex ( ) ;
2014-06-25 16:29:58 +00:00
for ( auto it = mChildren . begin ( ) ; it ! = mChildren . end ( ) ; it + + )
{
if ( ( * it ) - > getType ( ) & typeMask )
2017-03-18 17:54:39 +00:00
{
if ( ! displayedOnly | | ! idx - > isFiltered ( ) | | idx - > showFile ( * it ) )
out . push_back ( * it ) ;
}
2017-05-18 10:16:57 +00:00
2014-06-25 16:29:58 +00:00
if ( ( * it ) - > getChildren ( ) . size ( ) > 0 )
{
2017-03-18 17:54:39 +00:00
std : : vector < FileData * > subchildren = ( * it ) - > getFilesRecursive ( typeMask , displayedOnly ) ;
2014-06-25 16:29:58 +00:00
out . insert ( out . end ( ) , subchildren . cbegin ( ) , subchildren . cend ( ) ) ;
}
}
return out ;
}
2017-06-12 16:38:59 +00:00
std : : string FileData : : getKey ( ) {
return getFileName ( ) ;
}
FileData * FileData : : getSourceFileData ( )
{
return this ;
}
2014-06-25 16:29:58 +00:00
void FileData : : addChild ( FileData * file )
{
assert ( mType = = FOLDER ) ;
assert ( file - > getParent ( ) = = NULL ) ;
2017-06-12 16:38:59 +00:00
const std : : string key = file - > getKey ( ) ;
2016-08-09 20:26:30 +00:00
if ( mChildrenByFilename . find ( key ) = = mChildrenByFilename . end ( ) )
{
mChildrenByFilename [ key ] = file ;
mChildren . push_back ( file ) ;
file - > mParent = this ;
}
2014-06-25 16:29:58 +00:00
}
void FileData : : removeChild ( FileData * file )
{
assert ( mType = = FOLDER ) ;
assert ( file - > getParent ( ) = = this ) ;
2017-06-12 16:38:59 +00:00
mChildrenByFilename . erase ( file - > getKey ( ) ) ;
2014-06-25 16:29:58 +00:00
for ( auto it = mChildren . begin ( ) ; it ! = mChildren . end ( ) ; it + + )
{
if ( * it = = file )
{
2017-07-18 09:45:50 +00:00
file - > mParent = NULL ;
2014-06-25 16:29:58 +00:00
mChildren . erase ( it ) ;
return ;
}
}
// File somehow wasn't in our children.
assert ( false ) ;
2016-08-09 20:26:30 +00:00
2014-06-25 16:29:58 +00:00
}
void FileData : : sort ( ComparisonFunction & comparator , bool ascending )
{
2017-06-12 16:38:59 +00:00
std : : stable_sort ( mChildren . begin ( ) , mChildren . end ( ) , comparator ) ;
2014-06-25 16:29:58 +00:00
for ( auto it = mChildren . begin ( ) ; it ! = mChildren . end ( ) ; it + + )
{
if ( ( * it ) - > getChildren ( ) . size ( ) > 0 )
( * it ) - > sort ( comparator , ascending ) ;
}
if ( ! ascending )
std : : reverse ( mChildren . begin ( ) , mChildren . end ( ) ) ;
}
void FileData : : sort ( const SortType & type )
{
sort ( * type . comparisonFunction , type . ascending ) ;
}
2017-06-12 16:38:59 +00:00
void FileData : : launchGame ( Window * window )
{
LOG ( LogInfo ) < < " Attempting to launch game... " ;
AudioManager : : getInstance ( ) - > deinit ( ) ;
VolumeControl : : getInstance ( ) - > deinit ( ) ;
window - > deinit ( ) ;
std : : string command = mEnvData - > mLaunchCommand ;
const std : : string rom = escapePath ( getPath ( ) ) ;
const std : : string basename = getPath ( ) . stem ( ) . string ( ) ;
const std : : string rom_raw = fs : : path ( getPath ( ) ) . make_preferred ( ) . string ( ) ;
command = strreplace ( command , " %ROM% " , rom ) ;
command = strreplace ( command , " %BASENAME% " , basename ) ;
command = strreplace ( command , " %ROM_RAW% " , rom_raw ) ;
LOG ( LogInfo ) < < " " < < command ;
int exitCode = runSystemCommand ( command ) ;
if ( exitCode ! = 0 )
{
LOG ( LogWarning ) < < " ...launch terminated with nonzero exit code " < < exitCode < < " ! " ;
}
window - > init ( ) ;
VolumeControl : : getInstance ( ) - > init ( ) ;
AudioManager : : getInstance ( ) - > init ( ) ;
window - > normalizeNextUpdate ( ) ;
//update number of times the game has been launched
FileData * gameToUpdate = getSourceFileData ( ) ;
int timesPlayed = gameToUpdate - > metadata . getInt ( " playcount " ) + 1 ;
gameToUpdate - > metadata . set ( " playcount " , std : : to_string ( static_cast < long long > ( timesPlayed ) ) ) ;
//update last played time
boost : : posix_time : : ptime time = boost : : posix_time : : second_clock : : universal_time ( ) ;
gameToUpdate - > metadata . setTime ( " lastplayed " , time ) ;
2017-07-18 09:45:50 +00:00
CollectionSystemManager : : get ( ) - > refreshCollectionSystems ( gameToUpdate ) ;
2017-06-12 16:38:59 +00:00
}
CollectionFileData : : CollectionFileData ( FileData * file , SystemData * system )
2017-07-18 09:45:50 +00:00
: FileData ( file - > getSourceFileData ( ) - > getType ( ) , file - > getSourceFileData ( ) - > getPath ( ) , file - > getSourceFileData ( ) - > getSystemEnvData ( ) , system )
2017-06-12 16:38:59 +00:00
{
// we use this constructor to create a clone of the filedata, and change its system
mSourceFileData = file - > getSourceFileData ( ) ;
refreshMetadata ( ) ;
mParent = NULL ;
metadata = mSourceFileData - > metadata ;
mSystemName = mSourceFileData - > getSystem ( ) - > getName ( ) ;
}
CollectionFileData : : ~ CollectionFileData ( )
{
// need to remove collection file data at the collection object destructor
if ( mParent )
mParent - > removeChild ( this ) ;
mParent = NULL ;
}
std : : string CollectionFileData : : getKey ( ) {
return getFullPath ( ) ;
}
FileData * CollectionFileData : : getSourceFileData ( )
{
return mSourceFileData ;
}
void CollectionFileData : : refreshMetadata ( )
{
metadata = mSourceFileData - > metadata ;
mDirty = true ;
}
const std : : string & CollectionFileData : : getName ( )
{
if ( mDirty ) {
mCollectionFileName = removeParenthesis ( mSourceFileData - > metadata . get ( " name " ) ) ;
boost : : trim ( mCollectionFileName ) ;
mCollectionFileName + = " [ " + strToUpper ( mSourceFileData - > getSystem ( ) - > getName ( ) ) + " ] " ;
mDirty = false ;
}
return mCollectionFileName ;
2017-07-18 09:45:50 +00:00
}
// returns Sort Type based on a string description
FileData : : SortType getSortTypeFromString ( std : : string desc ) {
std : : vector < FileData : : SortType > SortTypes = FileSorts : : SortTypes ;
// find it
for ( unsigned int i = 0 ; i < FileSorts : : SortTypes . size ( ) ; i + + )
{
const FileData : : SortType & sort = FileSorts : : SortTypes . at ( i ) ;
if ( sort . description = = desc )
{
return sort ;
}
}
// if not found default to name, ascending
return FileSorts : : SortTypes . at ( 0 ) ;
2017-06-12 16:38:59 +00:00
}