2019-11-01 20:00:28 +00:00
/*
Copyright ( c ) 2017 - 2018 Adubbz
Permission is hereby granted , free of charge , to any person obtaining a copy
of this software and associated documentation files ( the " Software " ) , to deal
in the Software without restriction , including without limitation the rights
to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
copies of the Software , and to permit persons to whom the Software is
furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE .
*/
2019-10-22 22:14:37 +00:00
# include <cstring>
# include <sstream>
2019-10-26 01:00:13 +00:00
# include <filesystem>
2019-11-03 20:02:15 +00:00
# include <ctime>
2019-10-30 01:58:53 +00:00
2019-10-22 22:14:37 +00:00
# include "install/install_nsp.hpp"
# include "nx/fs.hpp"
# include "util/file_util.hpp"
# include "util/title_util.hpp"
# include "util/error.hpp"
# include "ui/MainApplication.hpp"
2019-10-30 01:58:53 +00:00
# include "nspInstall.hpp"
2019-10-28 23:47:49 +00:00
# include "util/config.hpp"
# include "util/util.hpp"
2019-10-22 22:14:37 +00:00
namespace inst : : ui {
extern MainApplication * mainApp ;
2019-10-27 03:36:45 +00:00
void setTopInstInfoText ( std : : string ourText ) {
2019-10-24 19:53:54 +00:00
mainApp - > instpage - > pageInfoText - > SetText ( ourText ) ;
2019-10-22 22:14:37 +00:00
mainApp - > CallForRender ( ) ;
}
2019-10-27 03:36:45 +00:00
void setInstInfoText ( std : : string ourText ) {
mainApp - > instpage - > installInfoText - > SetText ( ourText ) ;
mainApp - > CallForRender ( ) ;
}
2019-10-25 01:33:41 +00:00
void setInstBarPerc ( double ourPercent ) {
mainApp - > instpage - > installBar - > SetVisible ( true ) ;
mainApp - > instpage - > installBar - > SetProgress ( ourPercent ) ;
mainApp - > CallForRender ( ) ;
}
2019-10-22 22:14:37 +00:00
void loadMainMenu ( ) {
mainApp - > LoadLayout ( mainApp - > mainPage ) ;
}
2019-10-24 19:53:54 +00:00
void loadInstallScreen ( ) {
mainApp - > instpage - > pageInfoText - > SetText ( " " ) ;
2019-10-27 03:36:45 +00:00
mainApp - > instpage - > installInfoText - > SetText ( " " ) ;
2019-10-25 01:33:41 +00:00
mainApp - > instpage - > installBar - > SetProgress ( 0 ) ;
mainApp - > instpage - > installBar - > SetVisible ( false ) ;
2019-10-31 15:56:16 +00:00
if ( inst : : config : : gayMode ) mainApp - > instpage - > awooImage - > SetVisible ( false ) ;
else mainApp - > instpage - > awooImage - > SetVisible ( true ) ;
2019-10-24 19:53:54 +00:00
mainApp - > LoadLayout ( mainApp - > instpage ) ;
mainApp - > CallForRender ( ) ;
}
2019-10-22 22:14:37 +00:00
}
namespace nspInstStuff {
2019-11-03 20:02:15 +00:00
std : : string finishedMessage ( ) {
std : : vector < std : : string > finishMessages = { " Enjoy your \" legal backups \" ! " , " I'm sure after you give the game a try you'll have tons of fun actually buying it! " , " You buy gamu right? Nintendo-san thanka-you for your purchase! " , " Bypassing DRM is great, isn't it? " , " You probably saved like six trees by not buying the game! All that plastic goes somewhere! " } ;
srand ( time ( NULL ) ) ;
return ( finishMessages [ rand ( ) % finishMessages . size ( ) ] ) ;
}
2019-11-02 00:12:00 +00:00
void installNspFromFile ( std : : vector < std : : filesystem : : path > ourNspList , int whereToInstall )
2019-10-22 22:14:37 +00:00
{
2019-11-03 21:16:52 +00:00
inst : : util : : initInstallServices ( ) ;
2019-10-26 01:00:13 +00:00
appletLockExit ( ) ;
2019-10-24 19:53:54 +00:00
inst : : ui : : loadInstallScreen ( ) ;
2019-11-02 00:12:00 +00:00
bool nspInstalled = true ;
2019-11-02 01:22:44 +00:00
FsStorageId m_destStorageId = FsStorageId_SdCard ;
2019-10-22 22:14:37 +00:00
2019-10-23 01:33:13 +00:00
if ( whereToInstall ) m_destStorageId = FsStorageId_NandUser ;
2019-11-02 00:12:00 +00:00
try
2019-10-22 22:14:37 +00:00
{
2019-11-02 00:12:00 +00:00
for ( unsigned int i = 0 ; i < ourNspList . size ( ) ; i + + ) {
2019-11-04 01:06:43 +00:00
inst : : ui : : setTopInstInfoText ( " Installing " + inst : : util : : shortenString ( ourNspList [ i ] . string ( ) . erase ( 0 , 6 ) , 48 , true ) ) ;
2019-11-03 18:18:42 +00:00
bool isNsz = false ;
if ( ourNspList [ i ] . extension ( ) = = " .nsz " ) {
std : : string newfilename = ourNspList [ i ] . string ( ) . substr ( 0 , ourNspList [ i ] . string ( ) . find_last_of ( ' . ' ) ) + " .nsp " ;
ourNspList [ i ] = newfilename ;
rename ( ourNspList [ i ] , newfilename ) ;
isNsz = true ;
}
2019-11-02 00:12:00 +00:00
std : : string path = " @Sdcard:// " + ourNspList [ i ] . string ( ) . erase ( 0 , 6 ) ;
2019-10-22 22:14:37 +00:00
2019-11-03 18:18:42 +00:00
nx : : fs : : IFileSystem fileSystem ;
fileSystem . OpenFileSystemWithId ( path , FsFileSystemType_ApplicationPackage , 0 ) ;
tin : : install : : nsp : : SimpleFileSystem simpleFS ( fileSystem , " / " , path + " / " ) ;
tin : : install : : nsp : : NSPInstallTask task ( simpleFS , m_destStorageId , inst : : config : : ignoreReqVers ) ;
2019-10-27 03:36:45 +00:00
2019-11-03 18:18:42 +00:00
printf ( " Preparing installation \n " ) ;
inst : : ui : : setInstInfoText ( " Preparing installation... " ) ;
task . Prepare ( ) ;
2019-10-22 22:14:37 +00:00
2019-11-03 18:18:42 +00:00
task . Begin ( ) ;
2019-10-22 22:14:37 +00:00
2019-11-03 18:18:42 +00:00
if ( isNsz & & ourNspList [ i ] . extension ( ) = = " .nsp " ) {
std : : string newfilename = ourNspList [ i ] . string ( ) . substr ( 0 , ourNspList [ i ] . string ( ) . find_last_of ( ' . ' ) ) + " .nsz " ;
ourNspList [ i ] = newfilename ;
rename ( ourNspList [ i ] , newfilename ) ;
}
2019-10-22 22:14:37 +00:00
}
}
2019-11-02 00:12:00 +00:00
catch ( std : : exception & e )
{
2019-11-03 18:18:42 +00:00
printf ( " Failed to install " ) ;
2019-11-02 00:12:00 +00:00
printf ( " %s " , e . what ( ) ) ;
fprintf ( stdout , " %s " , e . what ( ) ) ;
2019-11-04 00:13:10 +00:00
inst : : ui : : setInstInfoText ( " Failed to install " ) ;
inst : : ui : : setInstBarPerc ( 0 ) ;
2019-11-03 18:18:42 +00:00
inst : : ui : : mainApp - > CreateShowDialog ( " Failed to install! " , " Partially installed contents can be removed from the System Settings applet. \n \n " + ( std : : string ) e . what ( ) , { " OK " } , true ) ;
2019-11-02 00:12:00 +00:00
nspInstalled = false ;
}
2019-10-22 22:14:37 +00:00
2019-11-02 00:12:00 +00:00
if ( nspInstalled ) {
2019-11-04 00:13:10 +00:00
inst : : ui : : setInstInfoText ( " Install complete " ) ;
inst : : ui : : setInstBarPerc ( 100 ) ;
2019-11-02 00:12:00 +00:00
if ( ourNspList . size ( ) > 1 ) {
2019-11-03 20:02:15 +00:00
if ( inst : : ui : : mainApp - > CreateShowDialog ( " Selected files installed! Delete them from the SD card? " , nspInstStuff : : finishedMessage ( ) , { " No " , " Yes " } , false ) = = 1 ) {
2019-11-02 00:12:00 +00:00
for ( long unsigned int i = 0 ; i < ourNspList . size ( ) ; i + + ) {
std : : filesystem : : remove ( ourNspList [ i ] ) ;
}
}
} else {
2019-11-03 20:02:15 +00:00
if ( inst : : ui : : mainApp - > CreateShowDialog ( inst : : util : : shortenString ( ourNspList [ 0 ] . string ( ) . erase ( 0 , 6 ) , 64 , true ) + " installed! Delete it from the SD card? " , nspInstStuff : : finishedMessage ( ) , { " No " , " Yes " } , false ) = = 1 ) std : : filesystem : : remove ( ourNspList [ 0 ] ) ;
2019-11-02 00:12:00 +00:00
}
}
2019-10-26 01:00:13 +00:00
2019-10-22 22:14:37 +00:00
printf ( " Done " ) ;
2019-10-26 01:00:13 +00:00
appletUnlockExit ( ) ;
2019-10-23 01:33:13 +00:00
inst : : ui : : loadMainMenu ( ) ;
2019-11-03 21:16:52 +00:00
inst : : util : : deinitInstallServices ( ) ;
2019-10-24 19:53:54 +00:00
return ;
2019-10-22 22:14:37 +00:00
}
}