2019-12-22 03:49:51 +01:00
using Gtk ;
2020-01-05 12:49:44 +01:00
using LibHac ;
2020-03-25 18:09:38 +01:00
using LibHac.Account ;
2020-02-08 19:22:45 +01:00
using LibHac.Common ;
2020-01-05 12:49:44 +01:00
using LibHac.Fs ;
2020-09-01 22:08:59 +02:00
using LibHac.Fs.Fsa ;
2020-01-05 12:49:44 +01:00
using LibHac.Fs.Shim ;
2020-02-08 19:22:45 +01:00
using LibHac.FsSystem ;
using LibHac.FsSystem.NcaUtils ;
2020-01-05 12:49:44 +01:00
using LibHac.Ncm ;
2020-03-25 18:09:38 +01:00
using LibHac.Ns ;
2020-04-23 21:21:32 +02:00
using Ryujinx.Common.Configuration ;
2020-02-08 19:22:45 +01:00
using Ryujinx.Common.Logging ;
2020-04-30 14:07:41 +02:00
using Ryujinx.Common.Utilities ;
2019-12-22 03:49:51 +01:00
using Ryujinx.HLE.FileSystem ;
2020-09-21 05:45:30 +02:00
using Ryujinx.HLE.HOS ;
2019-12-22 03:49:51 +01:00
using System ;
2020-02-08 19:22:45 +01:00
using System.Buffers ;
2020-08-13 19:31:13 +02:00
using System.Collections.Generic ;
2019-12-22 03:49:51 +01:00
using System.Diagnostics ;
2020-01-05 12:49:44 +01:00
using System.Globalization ;
2019-12-22 03:49:51 +01:00
using System.IO ;
using System.Reflection ;
2020-02-08 19:22:45 +01:00
using System.Threading ;
2020-06-23 02:32:07 +02:00
2020-03-25 18:09:38 +01:00
using static LibHac . Fs . ApplicationSaveDataManagement ;
2019-12-22 03:49:51 +01:00
namespace Ryujinx.Ui
{
public class GameTableContextMenu : Menu
{
2020-06-23 02:32:07 +02:00
private readonly ListStore _gameTableStore ;
private readonly TreeIter _rowIter ;
private readonly VirtualFileSystem _virtualFileSystem ;
2019-12-22 03:49:51 +01:00
2020-06-23 02:32:07 +02:00
private readonly BlitStruct < ApplicationControlProperty > _controlData ;
2019-12-22 03:49:51 +01:00
2020-06-23 02:32:07 +02:00
private MessageDialog _dialog ;
private bool _cancel ;
2019-12-22 03:49:51 +01:00
2020-06-23 02:32:07 +02:00
public GameTableContextMenu ( ListStore gameTableStore , BlitStruct < ApplicationControlProperty > controlData , TreeIter rowIter , VirtualFileSystem virtualFileSystem )
{
2020-01-24 17:01:21 +01:00
_gameTableStore = gameTableStore ;
_rowIter = rowIter ;
_virtualFileSystem = virtualFileSystem ;
2020-03-25 18:09:38 +01:00
_controlData = controlData ;
2019-12-22 03:49:51 +01:00
2020-06-23 02:32:07 +02:00
MenuItem openSaveUserDir = new MenuItem ( "Open User Save Directory" )
{
2020-09-01 22:08:59 +02:00
Sensitive = ! Utilities . IsEmpty ( controlData . ByteSpan ) & & controlData . Value . UserAccountSaveDataSize > 0 ,
2020-07-09 06:31:15 +02:00
TooltipText = "Open the directory which contains Application's User Saves."
2020-06-23 02:32:07 +02:00
} ;
2020-04-12 23:02:37 +02:00
2020-06-23 02:32:07 +02:00
MenuItem openSaveDeviceDir = new MenuItem ( "Open Device Save Directory" )
{
2020-09-01 22:08:59 +02:00
Sensitive = ! Utilities . IsEmpty ( controlData . ByteSpan ) & & controlData . Value . DeviceSaveDataSize > 0 ,
2020-07-09 06:31:15 +02:00
TooltipText = "Open the directory which contains Application's Device Saves."
2020-06-23 02:32:07 +02:00
} ;
2020-04-12 23:02:37 +02:00
2020-06-23 02:32:07 +02:00
MenuItem openSaveBcatDir = new MenuItem ( "Open BCAT Save Directory" )
2019-12-22 03:49:51 +01:00
{
2020-09-01 22:08:59 +02:00
Sensitive = ! Utilities . IsEmpty ( controlData . ByteSpan ) & & controlData . Value . BcatDeliveryCacheStorageSize > 0 ,
2020-07-09 06:31:15 +02:00
TooltipText = "Open the directory which contains Application's BCAT Saves."
2020-06-23 02:32:07 +02:00
} ;
MenuItem manageTitleUpdates = new MenuItem ( "Manage Title Updates" )
{
2020-07-09 06:31:15 +02:00
TooltipText = "Open the Title Update management window"
2020-06-23 02:32:07 +02:00
} ;
MenuItem manageDlc = new MenuItem ( "Manage DLC" )
{
TooltipText = "Open the DLC management window"
} ;
2020-07-09 06:31:15 +02:00
MenuItem openTitleModDir = new MenuItem ( "Open Mods Directory" )
{
TooltipText = "Open the directory which contains Application's Mods."
} ;
2020-06-23 02:32:07 +02:00
string ext = System . IO . Path . GetExtension ( _gameTableStore . GetValue ( _rowIter , 9 ) . ToString ( ) ) . ToLower ( ) ;
bool hasNca = ext = = ".nca" | | ext = = ".nsp" | | ext = = ".pfs0" | | ext = = ".xci" ;
2020-07-04 01:04:04 +02:00
MenuItem extractMenu = new MenuItem ( "Extract Data" ) ;
MenuItem extractRomFs = new MenuItem ( "RomFS" )
2020-06-23 02:32:07 +02:00
{
Sensitive = hasNca ,
2020-07-09 06:31:15 +02:00
TooltipText = "Extract the RomFS section from Application's current config (including updates)."
2020-06-23 02:32:07 +02:00
} ;
2020-07-04 01:04:04 +02:00
MenuItem extractExeFs = new MenuItem ( "ExeFS" )
2020-06-23 02:32:07 +02:00
{
Sensitive = hasNca ,
2020-07-09 06:31:15 +02:00
TooltipText = "Extract the ExeFS section from Application's current config (including updates)."
2020-06-23 02:32:07 +02:00
} ;
2020-07-04 01:04:04 +02:00
MenuItem extractLogo = new MenuItem ( "Logo" )
2020-06-23 02:32:07 +02:00
{
Sensitive = hasNca ,
2020-07-09 06:31:15 +02:00
TooltipText = "Extract the Logo section from Application's current config (including updates)."
2020-07-04 01:04:04 +02:00
} ;
Menu extractSubMenu = new Menu ( ) ;
extractSubMenu . Append ( extractExeFs ) ;
extractSubMenu . Append ( extractRomFs ) ;
extractSubMenu . Append ( extractLogo ) ;
extractMenu . Submenu = extractSubMenu ;
MenuItem managePtcMenu = new MenuItem ( "Cache Management" ) ;
2020-07-09 06:31:15 +02:00
MenuItem purgePtcCache = new MenuItem ( "Purge PPTC cache" )
2020-07-04 01:04:04 +02:00
{
2020-07-09 06:31:15 +02:00
TooltipText = "Delete the Application's PPTC cache."
2020-07-04 01:04:04 +02:00
} ;
2020-07-09 06:31:15 +02:00
MenuItem openPtcDir = new MenuItem ( "Open PPTC directory" )
2020-07-04 01:04:04 +02:00
{
2020-07-09 06:31:15 +02:00
TooltipText = "Open the directory which contains Application's PPTC cache."
2020-06-23 02:32:07 +02:00
} ;
2020-07-04 01:04:04 +02:00
Menu managePtcSubMenu = new Menu ( ) ;
managePtcSubMenu . Append ( purgePtcCache ) ;
managePtcSubMenu . Append ( openPtcDir ) ;
managePtcMenu . Submenu = managePtcSubMenu ;
2020-06-23 02:32:07 +02:00
openSaveUserDir . Activated + = OpenSaveUserDir_Clicked ;
openSaveDeviceDir . Activated + = OpenSaveDeviceDir_Clicked ;
openSaveBcatDir . Activated + = OpenSaveBcatDir_Clicked ;
manageTitleUpdates . Activated + = ManageTitleUpdates_Clicked ;
manageDlc . Activated + = ManageDlc_Clicked ;
2020-07-09 06:31:15 +02:00
openTitleModDir . Activated + = OpenTitleModDir_Clicked ;
2020-06-23 02:32:07 +02:00
extractRomFs . Activated + = ExtractRomFs_Clicked ;
extractExeFs . Activated + = ExtractExeFs_Clicked ;
extractLogo . Activated + = ExtractLogo_Clicked ;
2020-07-04 01:04:04 +02:00
purgePtcCache . Activated + = PurgePtcCache_Clicked ;
openPtcDir . Activated + = OpenPtcDir_Clicked ;
2020-06-23 02:32:07 +02:00
this . Add ( openSaveUserDir ) ;
this . Add ( openSaveDeviceDir ) ;
this . Add ( openSaveBcatDir ) ;
this . Add ( new SeparatorMenuItem ( ) ) ;
this . Add ( manageTitleUpdates ) ;
this . Add ( manageDlc ) ;
2020-07-09 06:31:15 +02:00
this . Add ( openTitleModDir ) ;
2020-06-23 02:32:07 +02:00
this . Add ( new SeparatorMenuItem ( ) ) ;
2020-07-04 01:04:04 +02:00
this . Add ( managePtcMenu ) ;
this . Add ( extractMenu ) ;
2020-01-05 12:49:44 +01:00
}
2020-03-25 18:09:38 +01:00
private bool TryFindSaveData ( string titleName , ulong titleId , BlitStruct < ApplicationControlProperty > controlHolder , SaveDataFilter filter , out ulong saveDataId )
2020-01-05 12:49:44 +01:00
{
saveDataId = default ;
2020-01-24 17:01:21 +01:00
Result result = _virtualFileSystem . FsClient . FindSaveDataWithFilter ( out SaveDataInfo saveDataInfo , SaveDataSpaceId . User , ref filter ) ;
2020-01-05 12:49:44 +01:00
2020-03-03 15:07:06 +01:00
if ( ResultFs . TargetNotFound . Includes ( result ) )
2020-01-05 12:49:44 +01:00
{
// Savedata was not found. Ask the user if they want to create it
using MessageDialog messageDialog = new MessageDialog ( null , DialogFlags . Modal , MessageType . Question , ButtonsType . YesNo , null )
2019-12-22 03:49:51 +01:00
{
2020-04-12 23:02:37 +02:00
Title = "Ryujinx" ,
Icon = new Gdk . Pixbuf ( Assembly . GetExecutingAssembly ( ) , "Ryujinx.Ui.assets.Icon.png" ) ,
Text = $"There is no savedata for {titleName} [{titleId:x16}]" ,
SecondaryText = "Would you like to create savedata for this game?" ,
2019-12-22 03:49:51 +01:00
WindowPosition = WindowPosition . Center
} ;
2020-01-05 12:49:44 +01:00
if ( messageDialog . Run ( ) ! = ( int ) ResponseType . Yes )
2019-12-22 03:49:51 +01:00
{
2020-01-05 12:49:44 +01:00
return false ;
2019-12-22 03:49:51 +01:00
}
2020-01-05 12:49:44 +01:00
2020-03-25 18:09:38 +01:00
ref ApplicationControlProperty control = ref controlHolder . Value ;
2020-09-01 22:08:59 +02:00
if ( LibHac . Utilities . IsEmpty ( controlHolder . ByteSpan ) )
2020-03-25 18:09:38 +01:00
{
// If the current application doesn't have a loaded control property, create a dummy one
// and set the savedata sizes so a user savedata will be created.
control = ref new BlitStruct < ApplicationControlProperty > ( 1 ) . Value ;
// The set sizes don't actually matter as long as they're non-zero because we use directory savedata.
2020-04-12 23:02:37 +02:00
control . UserAccountSaveDataSize = 0x4000 ;
2020-03-25 18:09:38 +01:00
control . UserAccountSaveDataJournalSize = 0x4000 ;
2020-08-04 01:32:53 +02:00
Logger . Warning ? . Print ( LogClass . Application ,
2020-03-25 18:09:38 +01:00
"No control file was found for this game. Using a dummy one instead. This may cause inaccuracies in some games." ) ;
}
Uid user = new Uid ( 1 , 0 ) ;
2020-09-01 22:08:59 +02:00
result = EnsureApplicationSaveData ( _virtualFileSystem . FsClient , out _ , new LibHac . Ncm . ApplicationId ( titleId ) , ref control , ref user ) ;
2020-01-05 12:49:44 +01:00
if ( result . IsFailure ( ) )
2019-12-22 03:49:51 +01:00
{
2020-01-05 12:49:44 +01:00
GtkDialog . CreateErrorDialog ( $"There was an error creating the specified savedata: {result.ToStringWithName()}" ) ;
2019-12-22 03:49:51 +01:00
2020-01-05 12:49:44 +01:00
return false ;
2019-12-22 03:49:51 +01:00
}
2020-01-05 12:49:44 +01:00
// Try to find the savedata again after creating it
2020-01-24 17:01:21 +01:00
result = _virtualFileSystem . FsClient . FindSaveDataWithFilter ( out saveDataInfo , SaveDataSpaceId . User , ref filter ) ;
2019-12-22 03:49:51 +01:00
}
2020-01-05 12:49:44 +01:00
if ( result . IsSuccess ( ) )
2019-12-22 03:49:51 +01:00
{
2020-01-05 12:49:44 +01:00
saveDataId = saveDataInfo . SaveDataId ;
return true ;
}
GtkDialog . CreateErrorDialog ( $"There was an error finding the specified savedata: {result.ToStringWithName()}" ) ;
return false ;
}
private string GetSaveDataDirectory ( ulong saveDataId )
{
2020-01-24 17:01:21 +01:00
string saveRootPath = System . IO . Path . Combine ( _virtualFileSystem . GetNandPath ( ) , $"user/save/{saveDataId:x16}" ) ;
2020-01-05 12:49:44 +01:00
if ( ! Directory . Exists ( saveRootPath ) )
{
// Inconsistent state. Create the directory
Directory . CreateDirectory ( saveRootPath ) ;
}
string committedPath = System . IO . Path . Combine ( saveRootPath , "0" ) ;
2020-02-08 19:22:45 +01:00
string workingPath = System . IO . Path . Combine ( saveRootPath , "1" ) ;
2020-01-05 12:49:44 +01:00
// If the committed directory exists, that path will be loaded the next time the savedata is mounted
if ( Directory . Exists ( committedPath ) )
{
return committedPath ;
}
// If the working directory exists and the committed directory doesn't,
// the working directory will be loaded the next time the savedata is mounted
if ( ! Directory . Exists ( workingPath ) )
{
Directory . CreateDirectory ( workingPath ) ;
}
return workingPath ;
2019-12-22 03:49:51 +01:00
}
2020-02-08 19:22:45 +01:00
2020-09-21 05:45:30 +02:00
private void ExtractSection ( NcaSectionType ncaSectionType , int programIndex = 0 )
2020-02-08 19:22:45 +01:00
{
FileChooserDialog fileChooser = new FileChooserDialog ( "Choose the folder to extract into" , null , FileChooserAction . SelectFolder , "Cancel" , ResponseType . Cancel , "Extract" , ResponseType . Accept ) ;
fileChooser . SetPosition ( WindowPosition . Center ) ;
int response = fileChooser . Run ( ) ;
string destination = fileChooser . Filename ;
fileChooser . Dispose ( ) ;
if ( response = = ( int ) ResponseType . Accept )
{
Thread extractorThread = new Thread ( ( ) = >
{
string sourceFile = _gameTableStore . GetValue ( _rowIter , 9 ) . ToString ( ) ;
Gtk . Application . Invoke ( delegate
{
_dialog = new MessageDialog ( null , DialogFlags . DestroyWithParent , MessageType . Info , ButtonsType . Cancel , null )
{
Title = "Ryujinx - NCA Section Extractor" ,
Icon = new Gdk . Pixbuf ( Assembly . GetExecutingAssembly ( ) , "Ryujinx.Ui.assets.Icon.png" ) ,
SecondaryText = $"Extracting {ncaSectionType} section from {System.IO.Path.GetFileName(sourceFile)}..." ,
WindowPosition = WindowPosition . Center
} ;
2020-04-23 21:21:32 +02:00
2020-02-08 19:22:45 +01:00
int dialogResponse = _dialog . Run ( ) ;
if ( dialogResponse = = ( int ) ResponseType . Cancel | | dialogResponse = = ( int ) ResponseType . DeleteEvent )
{
_cancel = true ;
_dialog . Dispose ( ) ;
}
} ) ;
using ( FileStream file = new FileStream ( sourceFile , FileMode . Open , FileAccess . Read ) )
{
Nca mainNca = null ;
Nca patchNca = null ;
if ( ( System . IO . Path . GetExtension ( sourceFile ) . ToLower ( ) = = ".nsp" ) | |
( System . IO . Path . GetExtension ( sourceFile ) . ToLower ( ) = = ".pfs0" ) | |
( System . IO . Path . GetExtension ( sourceFile ) . ToLower ( ) = = ".xci" ) )
{
PartitionFileSystem pfs ;
if ( System . IO . Path . GetExtension ( sourceFile ) = = ".xci" )
{
Xci xci = new Xci ( _virtualFileSystem . KeySet , file . AsStorage ( ) ) ;
pfs = xci . OpenPartition ( XciPartitionType . Secure ) ;
}
else
{
pfs = new PartitionFileSystem ( file . AsStorage ( ) ) ;
}
foreach ( DirectoryEntryEx fileEntry in pfs . EnumerateEntries ( "/" , "*.nca" ) )
{
2020-03-25 09:14:35 +01:00
pfs . OpenFile ( out IFile ncaFile , fileEntry . FullPath . ToU8Span ( ) , OpenMode . Read ) . ThrowIfFailure ( ) ;
2020-02-08 19:22:45 +01:00
Nca nca = new Nca ( _virtualFileSystem . KeySet , ncaFile . AsStorage ( ) ) ;
if ( nca . Header . ContentType = = NcaContentType . Program )
{
int dataIndex = Nca . GetSectionIndexFromType ( NcaSectionType . Data , NcaContentType . Program ) ;
if ( nca . Header . GetFsHeader ( dataIndex ) . IsPatchSection ( ) )
{
patchNca = nca ;
}
else
{
mainNca = nca ;
}
}
}
}
else if ( System . IO . Path . GetExtension ( sourceFile ) . ToLower ( ) = = ".nca" )
{
mainNca = new Nca ( _virtualFileSystem . KeySet , file . AsStorage ( ) ) ;
}
if ( mainNca = = null )
{
2020-08-04 01:32:53 +02:00
Logger . Error ? . Print ( LogClass . Application , "Extraction failed. The main NCA was not present in the selected file." ) ;
2020-02-08 19:22:45 +01:00
Gtk . Application . Invoke ( delegate
{
GtkDialog . CreateErrorDialog ( "Extraction failed. The main NCA was not present in the selected file." ) ;
} ) ;
return ;
}
2020-04-23 21:21:32 +02:00
2020-09-21 05:45:30 +02:00
( Nca updatePatchNca , _ ) = ApplicationLoader . GetGameUpdateData ( _virtualFileSystem , mainNca . Header . TitleId . ToString ( "x16" ) , programIndex , out _ ) ;
2020-04-23 21:21:32 +02:00
2020-09-21 05:45:30 +02:00
if ( updatePatchNca ! = null )
{
patchNca = updatePatchNca ;
2020-04-23 21:21:32 +02:00
}
2020-02-08 19:22:45 +01:00
int index = Nca . GetSectionIndexFromType ( ncaSectionType , mainNca . Header . ContentType ) ;
IFileSystem ncaFileSystem = patchNca ! = null ? mainNca . OpenFileSystemWithPatch ( patchNca , index , IntegrityCheckLevel . ErrorOnInvalid )
: mainNca . OpenFileSystem ( index , IntegrityCheckLevel . ErrorOnInvalid ) ;
FileSystemClient fsClient = _virtualFileSystem . FsClient ;
string source = DateTime . Now . ToFileTime ( ) . ToString ( ) . Substring ( 10 ) ;
string output = DateTime . Now . ToFileTime ( ) . ToString ( ) . Substring ( 10 ) ;
fsClient . Register ( source . ToU8Span ( ) , ncaFileSystem ) ;
fsClient . Register ( output . ToU8Span ( ) , new LocalFileSystem ( destination ) ) ;
( Result ? resultCode , bool canceled ) = CopyDirectory ( fsClient , $"{source}:/" , $"{output}:/" ) ;
if ( ! canceled )
{
if ( resultCode . Value . IsFailure ( ) )
{
2020-08-04 01:32:53 +02:00
Logger . Error ? . Print ( LogClass . Application , $"LibHac returned error code: {resultCode.Value.ErrorCode}" ) ;
2020-02-08 19:22:45 +01:00
Gtk . Application . Invoke ( delegate
{
_dialog ? . Dispose ( ) ;
GtkDialog . CreateErrorDialog ( "Extraction failed. Read the log file for further information." ) ;
} ) ;
}
else if ( resultCode . Value . IsSuccess ( ) )
{
Gtk . Application . Invoke ( delegate
{
_dialog ? . Dispose ( ) ;
MessageDialog dialog = new MessageDialog ( null , DialogFlags . DestroyWithParent , MessageType . Info , ButtonsType . Ok , null )
{
Title = "Ryujinx - NCA Section Extractor" ,
Icon = new Gdk . Pixbuf ( Assembly . GetExecutingAssembly ( ) , "Ryujinx.Ui.assets.Icon.png" ) ,
SecondaryText = "Extraction has completed successfully." ,
WindowPosition = WindowPosition . Center
} ;
dialog . Run ( ) ;
dialog . Dispose ( ) ;
} ) ;
}
}
2020-03-25 09:14:35 +01:00
fsClient . Unmount ( source . ToU8Span ( ) ) ;
fsClient . Unmount ( output . ToU8Span ( ) ) ;
2020-02-08 19:22:45 +01:00
}
} ) ;
extractorThread . Name = "GUI.NcaSectionExtractorThread" ;
extractorThread . IsBackground = true ;
extractorThread . Start ( ) ;
}
}
private ( Result ? result , bool canceled ) CopyDirectory ( FileSystemClient fs , string sourcePath , string destPath )
{
2020-03-25 09:14:35 +01:00
Result rc = fs . OpenDirectory ( out DirectoryHandle sourceHandle , sourcePath . ToU8Span ( ) , OpenDirectoryMode . All ) ;
2020-02-08 19:22:45 +01:00
if ( rc . IsFailure ( ) ) return ( rc , false ) ;
using ( sourceHandle )
{
foreach ( DirectoryEntryEx entry in fs . EnumerateEntries ( sourcePath , "*" , SearchOptions . Default ) )
{
if ( _cancel )
{
return ( null , true ) ;
}
string subSrcPath = PathTools . Normalize ( PathTools . Combine ( sourcePath , entry . Name ) ) ;
string subDstPath = PathTools . Normalize ( PathTools . Combine ( destPath , entry . Name ) ) ;
if ( entry . Type = = DirectoryEntryType . Directory )
{
fs . EnsureDirectoryExists ( subDstPath ) ;
( Result ? result , bool canceled ) = CopyDirectory ( fs , subSrcPath , subDstPath ) ;
if ( canceled | | result . Value . IsFailure ( ) )
{
return ( result , canceled ) ;
}
}
if ( entry . Type = = DirectoryEntryType . File )
{
fs . CreateOrOverwriteFile ( subDstPath , entry . Size ) ;
rc = CopyFile ( fs , subSrcPath , subDstPath ) ;
if ( rc . IsFailure ( ) ) return ( rc , false ) ;
}
}
}
return ( Result . Success , false ) ;
}
public Result CopyFile ( FileSystemClient fs , string sourcePath , string destPath )
{
2020-03-25 09:14:35 +01:00
Result rc = fs . OpenFile ( out FileHandle sourceHandle , sourcePath . ToU8Span ( ) , OpenMode . Read ) ;
2020-02-08 19:22:45 +01:00
if ( rc . IsFailure ( ) ) return rc ;
using ( sourceHandle )
{
2020-03-25 09:14:35 +01:00
rc = fs . OpenFile ( out FileHandle destHandle , destPath . ToU8Span ( ) , OpenMode . Write | OpenMode . AllowAppend ) ;
2020-02-08 19:22:45 +01:00
if ( rc . IsFailure ( ) ) return rc ;
using ( destHandle )
{
const int maxBufferSize = 1024 * 1024 ;
rc = fs . GetFileSize ( out long fileSize , sourceHandle ) ;
if ( rc . IsFailure ( ) ) return rc ;
int bufferSize = ( int ) Math . Min ( maxBufferSize , fileSize ) ;
byte [ ] buffer = ArrayPool < byte > . Shared . Rent ( bufferSize ) ;
try
{
for ( long offset = 0 ; offset < fileSize ; offset + = bufferSize )
{
int toRead = ( int ) Math . Min ( fileSize - offset , bufferSize ) ;
Span < byte > buf = buffer . AsSpan ( 0 , toRead ) ;
rc = fs . ReadFile ( out long _ , sourceHandle , offset , buf ) ;
if ( rc . IsFailure ( ) ) return rc ;
rc = fs . WriteFile ( destHandle , offset , buf ) ;
if ( rc . IsFailure ( ) ) return rc ;
}
}
finally
{
ArrayPool < byte > . Shared . Return ( buffer ) ;
}
rc = fs . FlushFile ( destHandle ) ;
if ( rc . IsFailure ( ) ) return rc ;
}
}
return Result . Success ;
}
// Events
2020-03-25 18:09:38 +01:00
private void OpenSaveUserDir_Clicked ( object sender , EventArgs args )
2020-02-08 19:22:45 +01:00
{
string titleName = _gameTableStore . GetValue ( _rowIter , 2 ) . ToString ( ) . Split ( "\n" ) [ 0 ] ;
2020-04-12 23:02:37 +02:00
string titleId = _gameTableStore . GetValue ( _rowIter , 2 ) . ToString ( ) . Split ( "\n" ) [ 1 ] . ToLower ( ) ;
2020-02-08 19:22:45 +01:00
2020-03-25 18:09:38 +01:00
if ( ! ulong . TryParse ( titleId , NumberStyles . HexNumber , CultureInfo . InvariantCulture , out ulong titleIdNumber ) )
{
GtkDialog . CreateErrorDialog ( "UI error: The selected game did not have a valid title ID" ) ;
return ;
}
SaveDataFilter filter = new SaveDataFilter ( ) ;
filter . SetUserId ( new UserId ( 1 , 0 ) ) ;
OpenSaveDir ( titleName , titleIdNumber , filter ) ;
}
private void OpenSaveDir ( string titleName , ulong titleId , SaveDataFilter filter )
{
2020-09-01 22:08:59 +02:00
filter . SetProgramId ( new ProgramId ( titleId ) ) ;
2020-03-25 18:09:38 +01:00
if ( ! TryFindSaveData ( titleName , titleId , _controlData , filter , out ulong saveDataId ) )
2020-02-08 19:22:45 +01:00
{
return ;
}
string saveDir = GetSaveDataDirectory ( saveDataId ) ;
2020-06-23 02:32:07 +02:00
Process . Start ( new ProcessStartInfo
2020-02-08 19:22:45 +01:00
{
FileName = saveDir ,
UseShellExecute = true ,
Verb = "open"
} ) ;
}
2020-03-25 18:09:38 +01:00
private void OpenSaveDeviceDir_Clicked ( object sender , EventArgs args )
{
string titleName = _gameTableStore . GetValue ( _rowIter , 2 ) . ToString ( ) . Split ( "\n" ) [ 0 ] ;
2020-04-12 23:02:37 +02:00
string titleId = _gameTableStore . GetValue ( _rowIter , 2 ) . ToString ( ) . Split ( "\n" ) [ 1 ] . ToLower ( ) ;
2020-03-25 18:09:38 +01:00
if ( ! ulong . TryParse ( titleId , NumberStyles . HexNumber , CultureInfo . InvariantCulture , out ulong titleIdNumber ) )
{
GtkDialog . CreateErrorDialog ( "UI error: The selected game did not have a valid title ID" ) ;
return ;
}
SaveDataFilter filter = new SaveDataFilter ( ) ;
filter . SetSaveDataType ( SaveDataType . Device ) ;
OpenSaveDir ( titleName , titleIdNumber , filter ) ;
}
2020-04-30 06:58:19 +02:00
private void OpenSaveBcatDir_Clicked ( object sender , EventArgs args )
{
string titleName = _gameTableStore . GetValue ( _rowIter , 2 ) . ToString ( ) . Split ( "\n" ) [ 0 ] ;
string titleId = _gameTableStore . GetValue ( _rowIter , 2 ) . ToString ( ) . Split ( "\n" ) [ 1 ] . ToLower ( ) ;
if ( ! ulong . TryParse ( titleId , NumberStyles . HexNumber , CultureInfo . InvariantCulture , out ulong titleIdNumber ) )
{
GtkDialog . CreateErrorDialog ( "UI error: The selected game did not have a valid title ID" ) ;
return ;
}
SaveDataFilter filter = new SaveDataFilter ( ) ;
filter . SetSaveDataType ( SaveDataType . Bcat ) ;
OpenSaveDir ( titleName , titleIdNumber , filter ) ;
}
2020-04-12 23:02:37 +02:00
private void ManageTitleUpdates_Clicked ( object sender , EventArgs args )
{
string titleName = _gameTableStore . GetValue ( _rowIter , 2 ) . ToString ( ) . Split ( "\n" ) [ 0 ] ;
string titleId = _gameTableStore . GetValue ( _rowIter , 2 ) . ToString ( ) . Split ( "\n" ) [ 1 ] . ToLower ( ) ;
TitleUpdateWindow titleUpdateWindow = new TitleUpdateWindow ( titleId , titleName , _virtualFileSystem ) ;
titleUpdateWindow . Show ( ) ;
}
2020-06-23 02:32:07 +02:00
private void ManageDlc_Clicked ( object sender , EventArgs args )
{
string titleName = _gameTableStore . GetValue ( _rowIter , 2 ) . ToString ( ) . Split ( "\n" ) [ 0 ] ;
string titleId = _gameTableStore . GetValue ( _rowIter , 2 ) . ToString ( ) . Split ( "\n" ) [ 1 ] . ToLower ( ) ;
DlcWindow dlcWindow = new DlcWindow ( titleId , titleName , _virtualFileSystem ) ;
dlcWindow . Show ( ) ;
}
2020-07-09 06:31:15 +02:00
private void OpenTitleModDir_Clicked ( object sender , EventArgs args )
{
string titleId = _gameTableStore . GetValue ( _rowIter , 2 ) . ToString ( ) . Split ( "\n" ) [ 1 ] . ToLower ( ) ;
2020-08-30 18:51:53 +02:00
var modsBasePath = _virtualFileSystem . ModLoader . GetModsBasePath ( ) ;
2020-07-09 06:31:15 +02:00
var titleModsPath = _virtualFileSystem . ModLoader . GetTitleDir ( modsBasePath , titleId ) ;
Process . Start ( new ProcessStartInfo
{
FileName = titleModsPath ,
UseShellExecute = true ,
Verb = "open"
} ) ;
}
2020-02-08 19:22:45 +01:00
private void ExtractRomFs_Clicked ( object sender , EventArgs args )
{
ExtractSection ( NcaSectionType . Data ) ;
}
private void ExtractExeFs_Clicked ( object sender , EventArgs args )
{
ExtractSection ( NcaSectionType . Code ) ;
}
private void ExtractLogo_Clicked ( object sender , EventArgs args )
{
ExtractSection ( NcaSectionType . Logo ) ;
}
2020-07-04 01:04:04 +02:00
private void OpenPtcDir_Clicked ( object sender , EventArgs args )
{
string titleId = _gameTableStore . GetValue ( _rowIter , 2 ) . ToString ( ) . Split ( "\n" ) [ 1 ] . ToLower ( ) ;
2020-08-30 18:51:53 +02:00
string ptcDir = System . IO . Path . Combine ( AppDataManager . GamesDirPath , titleId , "cache" , "cpu" ) ;
2020-07-04 01:04:04 +02:00
string mainPath = System . IO . Path . Combine ( ptcDir , "0" ) ;
string backupPath = System . IO . Path . Combine ( ptcDir , "1" ) ;
if ( ! Directory . Exists ( ptcDir ) )
{
Directory . CreateDirectory ( ptcDir ) ;
Directory . CreateDirectory ( mainPath ) ;
Directory . CreateDirectory ( backupPath ) ;
}
Process . Start ( new ProcessStartInfo
{
FileName = ptcDir ,
UseShellExecute = true ,
Verb = "open"
} ) ;
}
private void PurgePtcCache_Clicked ( object sender , EventArgs args )
{
2020-08-13 19:31:13 +02:00
string [ ] tableEntry = _gameTableStore . GetValue ( _rowIter , 2 ) . ToString ( ) . Split ( "\n" ) ;
string titleId = tableEntry [ 1 ] . ToLower ( ) ;
2020-07-04 01:04:04 +02:00
2020-08-30 18:51:53 +02:00
DirectoryInfo mainDir = new DirectoryInfo ( System . IO . Path . Combine ( AppDataManager . GamesDirPath , titleId , "cache" , "cpu" , "0" ) ) ;
DirectoryInfo backupDir = new DirectoryInfo ( System . IO . Path . Combine ( AppDataManager . GamesDirPath , titleId , "cache" , "cpu" , "1" ) ) ;
2020-07-04 01:04:04 +02:00
MessageDialog warningDialog = new MessageDialog ( null , DialogFlags . Modal , MessageType . Warning , ButtonsType . YesNo , null )
{
Title = "Ryujinx - Warning" ,
2020-08-13 19:31:13 +02:00
Text = $"You are about to delete the PPTC cache for '{tableEntry[0]}'. Are you sure you want to proceed?" ,
2020-07-04 01:04:04 +02:00
WindowPosition = WindowPosition . Center
} ;
2020-08-13 19:31:13 +02:00
List < FileInfo > cacheFiles = new List < FileInfo > ( ) ;
if ( mainDir . Exists ) { cacheFiles . AddRange ( mainDir . EnumerateFiles ( "*.cache" ) ) ; }
if ( backupDir . Exists ) { cacheFiles . AddRange ( backupDir . EnumerateFiles ( "*.cache" ) ) ; }
if ( cacheFiles . Count > 0 & & warningDialog . Run ( ) = = ( int ) ResponseType . Yes )
2020-07-04 01:04:04 +02:00
{
2020-08-13 19:31:13 +02:00
foreach ( FileInfo file in cacheFiles )
2020-07-04 01:04:04 +02:00
{
2020-08-13 19:31:13 +02:00
try
{
file . Delete ( ) ;
}
catch ( Exception e )
{
Logger . Error ? . Print ( LogClass . Application , $"Error purging PPTC cache {file.Name}: {e}" ) ;
}
2020-07-04 01:04:04 +02:00
}
}
2020-08-13 19:31:13 +02:00
2020-07-04 01:04:04 +02:00
warningDialog . Dispose ( ) ;
}
2019-12-22 03:49:51 +01:00
}
}