metalx1000

Get and play NES Emulator and random roms

Jun 28th, 2014
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. Gets and Runs NES ROMS for Windows
  3. PowerShell -ExecutionPolicy Bypass .\nes.ps1
  4. #>
  5. function Main(){
  6.     Add-Type -AssemblyName presentationCore
  7.     $mediaPlayer = New-Object system.windows.media.mediaplayer
  8.     $music = "http://www.gamethemesongs.com/download.php?f=Super_Mario_Bros._-_Full"
  9.     $mediaPlayer.open($music)
  10.     $mediaPlayer.Play()
  11.    
  12.    
  13.     function DownloadFile($url, $targetFile)
  14.    
  15.     {
  16.    
  17.        $uri = New-Object "System.Uri" "$url"
  18.    
  19.        $request = [System.Net.HttpWebRequest]::Create($uri)
  20.    
  21.        $request.set_Timeout(15000) #15 second timeout
  22.    
  23.        $response = $request.GetResponse()
  24.    
  25.        $totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
  26.    
  27.        $responseStream = $response.GetResponseStream()
  28.    
  29.        $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
  30.    
  31.        $buffer = new-object byte[] 10KB
  32.    
  33.        $count = $responseStream.Read($buffer,0,$buffer.length)
  34.    
  35.        $downloadedBytes = $count
  36.    
  37.        while ($count -gt 0)
  38.    
  39.        {
  40.    
  41.            $targetStream.Write($buffer, 0, $count)
  42.    
  43.            $count = $responseStream.Read($buffer,0,$buffer.length)
  44.    
  45.            $downloadedBytes = $downloadedBytes + $count
  46.    
  47.            Write-Progress -activity "Downloading file '$($url.split('/') | Select -Last 1)'" -status "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength)  * 100)
  48.    
  49.        }
  50.    
  51.        #Write-Progress -activity "Finished downloading file '$($url.split('/') | Select -Last 1)'"
  52.    
  53.        $targetStream.Flush()
  54.    
  55.        $targetStream.Close()
  56.    
  57.        $targetStream.Dispose()
  58.    
  59.        $responseStream.Dispose()
  60.    
  61.     }
  62.    
  63.     cls
  64.     Write-Output "Welcome!!!"
  65.     Start-Sleep -seconds 2
  66.    
  67.     $folder = "fceux-2.2.2-win32"
  68.     $storageDir = "$pwd"
  69.    
  70.     if(!(Test-Path -Path $folder )){
  71.         Write-Output "Downloading NES Emulator for Windows..."
  72.         Start-Sleep -seconds 2
  73.    
  74.         $url = "https://dl.dropbox.com/s/0b9yok0ugs756r1/nes.zip"
  75.         $file = "$storageDir\nes.zip"
  76.         DownloadFile $url $file
  77.    
  78.         Write-Output "Unzipping Emulator for Windows"
  79.         Start-Sleep -seconds 2
  80.        
  81.         $shell = new-object -com shell.application
  82.         $zip = $shell.NameSpace("$file")
  83.    
  84.         foreach($item in $zip.items())
  85.         {
  86.             $shell.Namespace("$storageDir\").copyhere($item)
  87.         }
  88.         Remove-Item $file -Force -Recurse
  89.     }else{
  90.         Write-Output "Emulator Already Installed";
  91.         Start-Sleep -seconds 2
  92.     }
  93.    
  94.    
  95.  
  96.     $wc = New-Object Net.WebClient
  97.     $list = $wc.DownloadString("https://raw.githubusercontent.com/metalx1000/MyBin/master/games/nes/linux/rom.lst")
  98.     $url = ($list -split '[\r\n]') |? {$_} | Get-Random
  99.    
  100.     write-output "Retrieving ROM"
  101.     write-output "$url"
  102.    
  103.     $folder = "fceux-2.2.2-win32"
  104.     $file = "rom.zip"
  105.     $rom = "$folder\roms\$file"
  106.     $wc.DownloadFile($url,$rom)
  107.    
  108.     Write-Output "Starting Random Game!!!"
  109.    
  110.     $mediaPlayer.Stop()
  111.    
  112.     cmd /c $folder\fceux $rom
  113.    
  114.     $del = Read-Host 'Do you want to remove the emulator and roms? (Y/n)'
  115.    
  116.     if(($del -eq "Y" ) -or ( $del -eq "")){
  117.         Write-Output "Removing Emulator and ROMS"
  118.         Remove-Item $folder -Force -Recurse
  119.     }
  120.    
  121.     write-output "Thank you for playing"
  122. }
  123.  
  124. Main
Add Comment
Please, Sign In to add comment