2016-02-02 19:38:53 +01:00
package de.mas.jnustool ;
2016-02-01 20:54:01 +01:00
import java.io.File ;
import java.io.IOException ;
2016-02-05 16:40:26 +01:00
import java.util.ArrayList ;
import java.util.List ;
2016-02-06 18:25:08 +01:00
import java.util.concurrent.Callable ;
import java.util.concurrent.ExecutionException ;
import java.util.concurrent.ForkJoinPool ;
import java.util.concurrent.ForkJoinTask ;
2016-04-11 12:48:21 +02:00
import java.util.concurrent.atomic.AtomicInteger ;
2016-02-01 20:54:01 +01:00
2016-02-02 19:38:53 +01:00
import de.mas.jnustool.gui.NUSGUI ;
2016-02-05 16:40:26 +01:00
import de.mas.jnustool.gui.UpdateChooser ;
import de.mas.jnustool.util.NUSTitleInformation ;
2016-10-31 15:08:14 +01:00
import de.mas.jnustool.util.Settings ;
import de.mas.jnustool.util.UpdateListManager ;
2016-02-02 19:38:53 +01:00
import de.mas.jnustool.util.Util ;
2016-02-01 20:54:01 +01:00
public class Starter {
2016-03-02 19:48:04 +01:00
public static void main ( String [ ] args ) {
2016-12-06 20:06:24 +01:00
Logger . log ( " JNUSTool 0.3b - by Maschell " ) ;
2016-02-05 16:40:26 +01:00
Logger . log ( " " ) ;
2016-02-01 20:54:01 +01:00
try {
2016-10-31 15:08:14 +01:00
Settings . readConfig ( ) ;
2016-02-01 20:54:01 +01:00
} catch ( IOException e ) {
System . err . println ( " Error while reading config! Needs to be: " ) ;
System . err . println ( " DOWNLOAD URL BASE " ) ;
System . err . println ( " COMMONKEY " ) ;
2016-04-11 12:48:21 +02:00
System . err . println ( " updateinfos.csv " ) ;
2016-10-31 15:08:14 +01:00
System . err . println ( " UPDATELIST VERSION URL " ) ;
System . err . println ( " UPDATELIST URL PATTERN " ) ;
2016-02-01 20:54:01 +01:00
return ;
}
2016-02-05 16:40:26 +01:00
long titleID = 0 ;
String key = null ;
if ( args . length ! = 0 ) {
2016-04-24 13:36:54 +02:00
titleID = Util . StringToLong ( args [ 0 ] ) ;
int version = - 1 ;
2016-02-01 20:54:01 +01:00
if ( args . length > 1 & & args [ 1 ] . length ( ) = = 32 ) {
key = args [ 1 ] . substring ( 0 , 32 ) ;
}
2016-04-24 13:36:54 +02:00
if ( titleID ! = 0 ) {
2016-10-21 20:09:52 +02:00
String path = " " ;
2016-04-24 13:36:54 +02:00
boolean dl_encrypted = false ;
2016-10-21 20:09:52 +02:00
boolean download_file = false ;
2016-04-24 13:36:54 +02:00
for ( int i = 0 ; i < args . length ; i + + ) {
if ( args [ i ] . startsWith ( " v " ) ) {
version = Integer . parseInt ( ( args [ i ] . substring ( 1 ) ) ) ;
}
if ( args [ i ] . equals ( " -dlEncrypted " ) ) {
dl_encrypted = true ;
2016-10-21 20:09:52 +02:00
}
if ( args [ i ] . equals ( " -file " ) ) {
if ( args . length > i ) {
i + + ;
path = args [ i ] ;
}
download_file = true ;
}
2016-04-24 13:36:54 +02:00
}
if ( dl_encrypted ) {
NUSTitle title = new NUSTitle ( titleID , version , key ) ;
try {
title . downloadEncryptedFiles ( null ) ;
} catch ( IOException e ) {
2016-08-24 21:27:35 +02:00
e . printStackTrace ( ) ;
2016-04-24 13:36:54 +02:00
}
System . exit ( 0 ) ;
2016-10-21 20:09:52 +02:00
} else if ( download_file ) {
NUSTitle title = new NUSTitle ( titleID , version , key ) ;
title . decryptFEntries ( title . getFst ( ) . getFileEntriesByFilePath ( path ) , null ) ;
System . exit ( 0 ) ;
}
2016-04-24 13:44:39 +02:00
NUSGUI m = new NUSGUI ( new NUSTitle ( titleID , version , key ) ) ;
2016-02-06 18:25:08 +01:00
m . setVisible ( true ) ;
}
2016-02-01 20:54:01 +01:00
} else {
2016-10-31 15:08:14 +01:00
List < NUSTitleInformation > updatelist = UpdateListManager . getTitles ( ) ;
List < NUSTitleInformation > result = new ArrayList < > ( ) ;
if ( updatelist ! = null ) {
UpdateChooser . createAndShowGUI ( updatelist , result ) ;
synchronized ( result ) {
try {
result . wait ( ) ;
} catch ( InterruptedException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
}
}
for ( final NUSTitleInformation nus : result ) {
2016-02-06 18:25:08 +01:00
final long tID = nus . getTitleID ( ) ;
new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
2016-03-02 19:48:04 +01:00
NUSGUI m = new NUSGUI ( new NUSTitle ( tID , nus . getSelectedVersion ( ) , null ) ) ;
m . setVisible ( true ) ;
2016-02-06 18:25:08 +01:00
}
} ) . start ( ) ; ;
2016-03-02 19:48:04 +01:00
}
2016-02-01 20:54:01 +01:00
}
}
2016-02-06 18:25:08 +01:00
2016-10-23 06:28:16 +02:00
public static void downloadMeta ( List < NUSTitleInformation > output_ , final Progress totalProgress ) {
2016-02-06 18:25:08 +01:00
ForkJoinPool pool = ForkJoinPool . commonPool ( ) ;
List < ForkJoinTask < Boolean > > list = new ArrayList < > ( ) ;
2016-03-02 19:48:04 +01:00
2016-10-23 06:28:16 +02:00
for ( final NUSTitleInformation nus : output_ ) {
2016-02-06 18:25:08 +01:00
final long tID = nus . getTitleID ( ) ;
list . add ( pool . submit ( new Callable < Boolean > ( ) {
@Override
2016-03-02 19:48:04 +01:00
public Boolean call ( ) throws Exception {
NUSTitle nusa = new NUSTitle ( tID , nus . getSelectedVersion ( ) , Util . ByteArrayToString ( nus . getKey ( ) ) ) ;
2016-10-31 15:08:14 +01:00
Progress childProgress = new Progress ( ) ;
2016-02-06 18:25:08 +01:00
totalProgress . add ( childProgress ) ;
2016-10-31 15:08:14 +01:00
Util . deleteFolder ( new File ( nusa . getLongNameFolder ( ) + " /updates " ) ) ;
2016-03-02 19:48:04 +01:00
nusa . setTargetPath ( nusa . getLongNameFolder ( ) ) ;
nusa . decryptFEntries ( nusa . getFst ( ) . getMetaFolder ( ) , childProgress ) ;
2016-02-06 18:25:08 +01:00
return true ;
}
} ) ) ;
}
for ( ForkJoinTask < Boolean > task : list ) {
try {
task . get ( ) ;
} catch ( InterruptedException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
} catch ( ExecutionException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
}
}
2016-10-23 06:28:16 +02:00
public static void downloadEncrypted ( List < NUSTitleInformation > output_ , final Progress progress ) {
2016-03-02 19:48:04 +01:00
ForkJoinPool pool = ForkJoinPool . commonPool ( ) ;
List < ForkJoinTask < Boolean > > list = new ArrayList < > ( ) ;
2016-10-23 06:28:16 +02:00
for ( final NUSTitleInformation nus : output_ ) {
2016-03-02 19:48:04 +01:00
final long tID = nus . getTitleID ( ) ;
list . add ( pool . submit ( new Callable < Boolean > ( ) {
@Override
public Boolean call ( ) throws Exception {
NUSTitle nusa = new NUSTitle ( tID , nus . getSelectedVersion ( ) , Util . ByteArrayToString ( nus . getKey ( ) ) ) ;
Progress childProgress = new Progress ( ) ;
progress . add ( childProgress ) ;
nusa . downloadEncryptedFiles ( progress ) ;
return true ;
}
} ) ) ;
}
for ( ForkJoinTask < Boolean > task : list ) {
try {
task . get ( ) ;
} catch ( InterruptedException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
} catch ( ExecutionException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
}
}
2016-04-11 12:48:21 +02:00
public static AtomicInteger finished = new AtomicInteger ( ) ;
2016-10-23 06:28:16 +02:00
public static void downloadEncryptedAllVersions ( List < NUSTitleInformation > output_ , final Progress progress ) {
2016-04-11 12:48:21 +02:00
ForkJoinPool pool = new ForkJoinPool ( 25 ) ;
List < ForkJoinTask < Boolean > > list = new ArrayList < > ( ) ;
2016-10-23 06:28:16 +02:00
final int outputsize = output_ . size ( ) ;
for ( final NUSTitleInformation nus : output_ ) {
2016-04-11 12:48:21 +02:00
final long tID = nus . getTitleID ( ) ;
list . add ( pool . submit ( new Callable < Boolean > ( ) {
@Override
public Boolean call ( ) throws Exception {
int count = 1 ;
for ( Integer i : nus . getAllVersions ( ) ) {
NUSTitle nusa = new NUSTitle ( tID , i , Util . ByteArrayToString ( nus . getKey ( ) ) ) ;
Progress childProgress = new Progress ( ) ;
progress . add ( childProgress ) ;
nusa . downloadEncryptedFiles ( progress ) ;
System . out . println ( " Update download progress " + " ( " + nus . getLongnameEN ( ) + " ) version " + i + " complete! This was " + count + " of " + nus . getAllVersions ( ) . size ( ) + " ! " ) ;
count + + ;
}
2016-10-23 06:28:16 +02:00
System . out . println ( " Update download complete " + " ( " + nus . getLongnameEN ( ) + " ) " + " ! Loaded updates for " + nus . getAllVersions ( ) . size ( ) + " version. Now are " + finished . incrementAndGet ( ) + " of " + outputsize + " done! " ) ;
2016-04-11 12:48:21 +02:00
return true ;
}
} ) ) ;
}
for ( ForkJoinTask < Boolean > task : list ) {
try {
task . get ( ) ;
} catch ( InterruptedException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
} catch ( ExecutionException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
}
2016-10-31 15:08:14 +01:00
2016-04-11 12:48:21 +02:00
}
2016-02-01 20:54:01 +01:00
}