2020-05-24 20:59:09 +00:00
using System ;
using System.Collections.Generic ;
using System.IO ;
2020-05-25 10:58:27 +00:00
using System.IO.Compression ;
2020-05-24 20:59:09 +00:00
using System.Linq ;
using System.Management ;
2020-05-25 10:58:27 +00:00
using System.Net ;
2020-05-24 20:59:09 +00:00
using System.Threading.Tasks ;
using System.Windows ;
using System.Windows.Controls ;
using System.Windows.Input ;
using System.Windows.Media ;
using System.Windows.Threading ;
namespace UWUVCI_AIO_WPF.UI.Windows
{
/// <summary>
/// Interaktionslogik für DownloadWait.xaml
/// </summary>
2020-10-02 00:39:56 +00:00
2020-05-24 20:59:09 +00:00
partial class SDSetup : Window , IDisposable
{
bool gc = false ;
string path = "" ;
ManagementEventWatcher watcher = new ManagementEventWatcher ( ) ;
ManagementEventWatcher watcher2 = new ManagementEventWatcher ( ) ;
string driveletter = "" ;
public SDSetup ( )
{
2020-05-27 20:10:48 +00:00
2020-05-24 20:59:09 +00:00
InitializeComponent ( ) ;
Task . Run ( ( ) = > checkfornewinput ( ) ) ;
Task . Run ( ( ) = > checkfornewoutput ( ) ) ;
GetDrives ( ) ;
}
public void SpecifyDrive ( )
{
if ( sd . SelectedValue ! = null )
{
driveletter = sd . SelectedValue . ToString ( ) . Substring ( 0 , 3 ) ;
}
else
{
driveletter = "" ;
}
}
public SDSetup ( bool gamecube , string injectlocation )
{
InitializeComponent ( ) ;
gc = gamecube ;
2020-10-02 00:39:56 +00:00
path = injectlocation ;
2020-06-13 19:52:39 +00:00
if ( ! gamecube )
{
setup . Content = "Copy to SD" ;
2020-06-13 20:16:15 +00:00
tbTitleBar . Text = "Copy to SD" ;
2020-06-13 19:52:39 +00:00
}
2020-06-22 20:42:25 +00:00
2020-05-24 20:59:09 +00:00
Task . Run ( ( ) = > checkfornewinput ( ) ) ;
Task . Run ( ( ) = > checkfornewoutput ( ) ) ;
GetDrives ( ) ;
}
private void checkfornewinput ( )
{
WqlEventQuery query = new WqlEventQuery ( "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2" ) ;
watcher . EventArrived + = new EventArrivedEventHandler ( watcher_EventArrived ) ;
watcher . Query = query ;
2020-06-14 09:30:44 +00:00
watcher . Options . Timeout = new TimeSpan ( 0 , 0 , 5 ) ;
2020-05-24 20:59:09 +00:00
watcher . Start ( ) ;
2020-06-14 09:30:44 +00:00
try
{
watcher . WaitForNextEvent ( ) ;
}
catch ( Exception )
{
2020-10-02 00:39:56 +00:00
//left empty on purpose
2020-06-14 09:30:44 +00:00
}
2020-05-24 20:59:09 +00:00
}
private void checkfornewoutput ( )
{
WqlEventQuery query = new WqlEventQuery ( "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 3" ) ;
watcher2 . EventArrived + = new EventArrivedEventHandler ( watcher_EventArrived ) ;
watcher2 . Query = query ;
2020-06-14 09:30:44 +00:00
watcher2 . Options . Timeout = new TimeSpan ( 0 , 0 , 5 ) ;
2020-05-24 20:59:09 +00:00
watcher2 . Start ( ) ;
2020-06-14 09:30:44 +00:00
try
{
watcher2 . WaitForNextEvent ( ) ;
}
catch ( Exception )
{
2020-10-02 00:39:56 +00:00
//left empty on purpose
2020-06-14 09:30:44 +00:00
}
2020-05-24 20:59:09 +00:00
}
private void watcher_EventArrived ( object sender , EventArrivedEventArgs e )
{
2020-10-02 00:39:56 +00:00
Dispatcher . Invoke ( ( ) = > { GetDrives ( ) ; } ) ;
2020-05-24 20:59:09 +00:00
}
private void GetDrives ( )
{
sd . ItemsSource = DriveInfo . GetDrives ( ) . Where ( d = > d . IsReady & & d . DriveType = = DriveType . Removable ) . Select ( d = > d . Name + " " + d . VolumeLabel + "" ) . ToList ( ) ;
}
2020-06-22 20:42:25 +00:00
public static long GetDirectorySize ( string p )
{
string [ ] a = Directory . GetFiles ( p , "*.*" ) ;
long b = 0 ;
foreach ( string name in a )
{
FileInfo info = new FileInfo ( name ) ;
b + = info . Length ;
}
return b ;
}
public static long GetDirectorySize ( string b , bool t )
{
long result = 0 ;
Stack < string > stack = new Stack < string > ( ) ;
stack . Push ( b ) ;
while ( stack . Count > 0 )
{
string dir = stack . Pop ( ) ;
try
{
result + = GetDirectorySize ( dir ) ;
foreach ( string dn in Directory . GetDirectories ( dir ) )
{
stack . Push ( dn ) ;
}
}
catch { }
}
return result ;
2020-05-24 20:59:09 +00:00
}
private void Window_Minimize ( object sender , RoutedEventArgs e )
{
2020-10-02 00:39:56 +00:00
WindowState = WindowState . Minimized ;
2020-05-24 20:59:09 +00:00
}
public void changeOwner ( MainWindow ow )
{
2020-10-02 00:39:56 +00:00
Owner = ow ;
2020-05-24 20:59:09 +00:00
try
{
2020-10-02 00:39:56 +00:00
if ( Owner ? . GetType ( ) = = typeof ( MainWindow ) )
2020-05-24 20:59:09 +00:00
{
2020-10-02 00:39:56 +00:00
WindowStartupLocation = WindowStartupLocation . CenterOwner ;
ShowInTaskbar = false ;
2020-05-24 20:59:09 +00:00
}
}
catch ( Exception )
{
2020-10-02 00:39:56 +00:00
WindowStartupLocation = WindowStartupLocation . CenterScreen ;
2020-05-24 20:59:09 +00:00
}
}
private void MoveWindow ( object sender , MouseButtonEventArgs e )
{
try
{
if ( e . ChangedButton = = MouseButton . Left )
2020-10-02 00:39:56 +00:00
DragMove ( ) ;
2020-05-24 20:59:09 +00:00
}
catch ( Exception )
{
2020-10-02 00:39:56 +00:00
//left empty on purpose
2020-05-24 20:59:09 +00:00
}
}
2020-06-11 08:12:03 +00:00
private void Window_Close ( object sender , RoutedEventArgs e )
2020-05-24 20:59:09 +00:00
{
/ * Task t = new Task ( ( ) = > watcher . Stop ( ) ) ;
t . Start ( ) ;
t . Wait ( ) ;
t = new Task ( ( ) = > watcher2 . Stop ( ) ) ;
t . Start ( ) ;
t . Wait ( ) ; * /
watcher . Stop ( ) ;
watcher2 . Stop ( ) ;
2020-06-14 09:30:44 +00:00
watcher . Dispose ( ) ;
watcher2 . Dispose ( ) ;
2020-10-02 00:39:56 +00:00
Close ( ) ;
2020-05-24 20:59:09 +00:00
}
private void close_MouseLeave ( object sender , MouseEventArgs e )
{
close . Background = new SolidColorBrush ( Color . FromArgb ( 0 , 250 , 250 , 250 ) ) ;
}
private void close_MouseEnter ( object sender , MouseEventArgs e )
{
close . Background = new SolidColorBrush ( Color . FromArgb ( 150 , 255 , 100 , 100 ) ) ;
}
private void wind_Closed ( object sender , EventArgs e )
{
try
{
if ( ( FindResource ( "mvm" ) as MainViewModel ) . mw ! = null )
{
( FindResource ( "mvm" ) as MainViewModel ) . mw . Topmost = false ;
}
}
2020-06-11 08:12:03 +00:00
catch ( Exception )
2020-05-24 20:59:09 +00:00
{
2020-10-02 00:39:56 +00:00
//left empty on purpose
2020-05-24 20:59:09 +00:00
}
}
2020-06-05 17:43:22 +00:00
DispatcherTimer dp = new DispatcherTimer ( ) ;
2020-06-22 20:42:25 +00:00
private static string FormatBytes ( long bytes )
{
string [ ] Suffix = { "B" , "KB" , "MB" , "GB" , "TB" } ;
int i ;
double dblSByte = bytes ;
for ( i = 0 ; i < Suffix . Length & & bytes > = 1024 ; i + + , bytes / = 1024 )
{
dblSByte = bytes / 1024.0 ;
}
2020-10-02 00:39:56 +00:00
return string . Format ( "{0:0.##} {1}" , dblSByte , Suffix [ i ] ) ;
2020-06-22 20:42:25 +00:00
}
2020-05-24 20:59:09 +00:00
private void setup_Click ( object sender , RoutedEventArgs e )
2020-06-24 15:54:51 +00:00
{ if ( ! ( FindResource ( "mvm" ) as MainViewModel ) . saveworkaround )
2020-06-22 20:42:25 +00:00
{
2020-10-02 00:39:56 +00:00
long injctSize = GetDirectorySize ( Path . Combine ( path , ( FindResource ( "mvm" ) as MainViewModel ) . foldername ) , true ) ;
2020-06-24 15:54:51 +00:00
if ( injctSize > = new DriveInfo ( driveletter ) . AvailableFreeSpace )
2020-06-22 20:42:25 +00:00
{
2020-06-24 15:54:51 +00:00
long div = injctSize - new DriveInfo ( driveletter ) . AvailableFreeSpace + 1048576 ;
Custom_Message cm = new Custom_Message ( "Insufficient Space" , $" You do not have enough space on the selected drive. \n Please make sure you have at least {FormatBytes(div)} free! " ) ;
try
{
cm . Owner = ( FindResource ( "mvm" ) as MainViewModel ) . mw ;
}
catch ( Exception )
{
2020-10-02 00:39:56 +00:00
//left empty on purpose
2020-06-24 15:54:51 +00:00
}
cm . ShowDialog ( ) ;
2020-06-22 20:42:25 +00:00
}
2020-06-24 15:54:51 +00:00
else
2020-06-22 20:42:25 +00:00
{
2020-06-24 15:54:51 +00:00
dp . Tick + = Dp_Tick ;
dp . Interval = TimeSpan . FromSeconds ( 1 ) ;
dp . Start ( ) ;
Task . Run ( ( ) = >
{
2020-06-05 17:43:22 +00:00
2020-06-24 15:54:51 +00:00
if ( gc )
{
SetupNintendont ( ) ;
}
CopyInject ( ) ;
} ) ;
setup . IsEnabled = false ;
2020-06-22 20:42:25 +00:00
}
}
else
{
2020-06-24 15:54:51 +00:00
dp . Tick + = Dp_Tick ;
dp . Interval = TimeSpan . FromSeconds ( 1 ) ;
dp . Start ( ) ;
Task . Run ( ( ) = >
2020-06-22 20:42:25 +00:00
{
2020-06-24 15:54:51 +00:00
if ( gc )
{
SetupNintendont ( ) ;
}
CopyInject ( ) ;
} ) ;
setup . IsEnabled = false ;
2020-10-02 00:39:56 +00:00
}
2020-06-05 17:43:22 +00:00
}
private void Dp_Tick ( object sender , EventArgs e )
{
MainViewModel mvm = FindResource ( "mvm" ) as MainViewModel ;
status . Content = mvm . msg ;
if ( mvm . msg = = "Done with Setup!" )
2020-05-24 20:59:09 +00:00
{
2020-06-05 17:43:22 +00:00
mvm . msg = "" ;
dp . Stop ( ) ;
setup . IsEnabled = true ;
setup . Click - = setup_Click ;
setup . Click + = Window_Close ;
setup . Content = "Close" ;
2020-05-24 20:59:09 +00:00
}
}
private void SetupNintendont ( )
{
2020-06-05 17:43:22 +00:00
MainViewModel mvm = FindResource ( "mvm" ) as MainViewModel ;
mvm . msg = "" ;
mvm . msg = "Downloading Nintendont..." ;
2020-05-25 10:58:27 +00:00
if ( Directory . Exists ( @"bin\tempsd" ) )
{
Directory . Delete ( @"bin\tempsd" , true ) ;
}
Directory . CreateDirectory ( @"bin\tempsd" ) ;
var client = new WebClient ( ) ;
2020-06-05 17:43:22 +00:00
client . DownloadFile ( "https://dl.dropbox.com/s/3swnsatmautzlk4/Nintendont.zip?dl=1" , @"bin\tempsd\nintendont.zip" ) ;
2020-05-25 10:58:27 +00:00
using ( FileStream s = new FileStream ( @"bin\tempsd\nintendont.zip" , FileMode . Open , FileAccess . ReadWrite ) )
{
ZipArchive z = new ZipArchive ( s ) ;
z . ExtractToDirectory ( @"bin\tempsd\nintendont" ) ;
s . Close ( ) ;
}
2020-06-05 17:43:22 +00:00
mvm . msg = "Setting up Nintendon't..." ;
2020-05-25 10:58:27 +00:00
if ( ! File . Exists ( driveletter + "\\nincfg.bin" ) )
{
File . Copy ( @"bin\tempsd\nintendont\nincfg.bin" , driveletter + @"\nincfg.bin" ) ;
}
2020-09-12 11:21:40 +00:00
if ( ! Directory . Exists ( driveletter + "\\apps\\nintendont" ) )
{
Directory . CreateDirectory ( driveletter + "\\apps\\nintendont" ) ;
}
else
{
Directory . Delete ( driveletter + "\\apps\\nintendont" , true ) ;
Directory . CreateDirectory ( driveletter + "\\apps\\nintendont" ) ;
}
client . DownloadFile ( "https://raw.githubusercontent.com/GaryOderNichts/Nintendont/master/loader/loader.dol" , driveletter + "\\apps\\nintendont\\boot.dol" ) ;
client . DownloadFile ( "https://raw.githubusercontent.com/GaryOderNichts/Nintendont/master/nintendont/meta.xml" , driveletter + "\\apps\\nintendont\\meta.xml" ) ;
client . DownloadFile ( "https://raw.githubusercontent.com/GaryOderNichts/Nintendont/master/nintendont/icon.png" , driveletter + "\\apps\\nintendont\\icon.png" ) ;
2020-05-25 10:58:27 +00:00
DirectoryCopy ( @"bin\tempsd\nintendont\codes" , driveletter + "\\codes" , true ) ;
Directory . Delete ( @"bin\tempsd" , true ) ;
2020-05-24 20:59:09 +00:00
}
private void CopyInject ( )
{
MainViewModel mvm = FindResource ( "mvm" ) as MainViewModel ;
2020-06-05 17:43:22 +00:00
mvm . msg = "Copying Injected Game..." ;
2020-10-02 00:39:56 +00:00
if ( ! Path . Combine ( path , mvm . foldername ) . Contains ( "[WUP]" ) )
2020-05-24 20:59:09 +00:00
{
2020-10-02 00:39:56 +00:00
DirectoryCopy ( Path . Combine ( path , mvm . foldername ) , driveletter + "\\wiiu\\games\\" + mvm . foldername , true ) ;
2020-05-24 20:59:09 +00:00
}
else
{
2020-10-02 00:39:56 +00:00
DirectoryCopy ( Path . Combine ( path , mvm . foldername ) , driveletter + "\\install\\" + mvm . foldername , true ) ;
2020-05-24 20:59:09 +00:00
}
mvm . foldername = "" ;
2020-06-05 17:43:22 +00:00
mvm . msg = "Done with Setup!" ;
2020-05-24 20:59:09 +00:00
}
public static void DirectoryCopy ( string sourceDirName , string destDirName , bool copySubDirs )
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo ( sourceDirName ) ;
if ( ! dir . Exists )
{
throw new DirectoryNotFoundException ( $"Source directory does not exist or could not be found: {sourceDirName}" ) ;
}
// If the destination directory doesn't exist, create it.
if ( ! Directory . Exists ( destDirName ) )
{
Directory . CreateDirectory ( destDirName ) ;
}
// Get the files in the directory and copy them to the new location.
foreach ( FileInfo file in dir . EnumerateFiles ( ) )
{
2020-10-02 00:39:56 +00:00
file . CopyTo ( Path . Combine ( destDirName , file . Name ) , true ) ;
2020-05-24 20:59:09 +00:00
}
// If copying subdirectories, copy them and their contents to new location.
if ( copySubDirs )
{
foreach ( DirectoryInfo subdir in dir . EnumerateDirectories ( ) )
{
2020-10-02 00:39:56 +00:00
DirectoryCopy ( subdir . FullName , Path . Combine ( destDirName , subdir . Name ) , copySubDirs ) ;
2020-05-24 20:59:09 +00:00
}
}
}
private void sd_SelectionChanged ( object sender , SelectionChangedEventArgs e )
{
if ( sd . SelectedIndex > - 1 & & sd . SelectedItem ! = null )
{
setup . IsEnabled = true ;
}
else
{
setup . IsEnabled = false ;
}
SpecifyDrive ( ) ;
}
public void Dispose ( )
{
}
}
}