/ Delving into Windows PowerShell
Created 2019-03-11 Modified 2019-03-11

805 Words

Delving into Windows Powershell

Originally posted on 2019-03-11.

During the duration of my open source releases, I found myself going back to Flashpoint and curating for fun.

Freearcade (as I mentioned in my previous post) is a site that mainly has Java applet games and those games tend to have a lot of assets.

Deciding to curate another Java applet game, I realized that it wasn’t only Wiz3 with a bunch of assets but this game as well.

Flashpoint comes with autocurator scripts (which I didn’t know at the time)but I decided to make my own using Windows PowerShell. I’ve been wanting to learn Powershell scripting after enjoying bash, and decided that as a struggling Windows developer, I should get to know the tools on this platform. So this dilemma was a good excuse.

So this is my first PowerShell script.

Issue:

Doing HTTP GET requests on multiple Flashpoint redirector 404s.

Redirector

You can copy all 404 URLs by hitting Tools -> Copy All 404 Not Found URLs. It’s tedious to go to each URL and download files one by one.

Solution:

Make a PowerShell Script to automate it.

The Script URL

Get-Assets.ps1

<# A useful powershell script to download files off of URLs #>

# $output = whatever folder location you want the script to save the files to. 
$output = "..\..\Flashpoint Core 2.3\Games\Flash\htdocs\ww2.freearcade.com\Jetslalom.jav\"
$client = new-object System.Net.WebClient
  • Accept the location of where the user wants to save the assets to in $output (This is currently manually changed in the script itself - need to do it through the UI)
  • $client sets the Webclient object that provides methods to send data and receive data from URLs
while($select -ne 0){
    Write-Host "Select from the following options:
                        1. Exit script
                        2. Grab asset from one URL
                        3. Grab multiple assets from multiple URLs"
    $select = Read-Host -Prompt "Enter your selection"
  • The while line loops the prompt until the user exits the script so that you can keep grabbing multiple or single assets without restarting.
  • Write-Host writes to the screen, and Read-Host reads user input - as opposed to using Write-Host, you can use the -Prompt instead. But here I wanted to show all the options for the script.
    switch($select){
        1{
            $url = Read-Host -Prompt "Enter URL (with http included)"
            $file = Split-Path $url -Leaf
            $client.DownloadFile($url, $output + $file)
        }
  • The switch statement is explanatory for the script selections
  • If it’s selection 1, read user input and save it
  • PowerShell doesn’t download files off of a URL location without knowing the name of the file prior. But with a script it’s impossible to anticipate what filename something will be. So Split-Path $url -Leaf splits the URL into portions, the -Leaf parameter referring to the last section of a URL (i.e http://www.example.com/src/leaf.png would grab just the leaf of the URL, leaf.png)
  • Then using the WebClient object, we can use the method DownloadFile to download from the URL and save it to the $output path plus the leaf portion of the URL we extracted, in this case an asset file.
        2{
            $txt = Read-Host -Prompt "Enter location of input file with the 404 URLS (if in same directory, it's .\ syntax"
            foreach($line in Get-Content $txt){
                $filename = Split-Path $line -leaf
                $client.DownloadFile($line, $output + $filename)
            }
        }
    }
  • For multiple URLs, we ask the user to place the copied 404s from Redirector into a txt file for easy processing. They are divided by a line break (but that doesn’t matter to PowerShell).
  • The foreach method loops through the file, using Get-Content cmdlet. In the MS docs, text files are “read one line at a time and returns a collection of objects, each of which represents a line of content”. So we can right away Split-Path again to get the names of the files and download each line (which is a URL).

    # Line 29 is optional, it's to make sure the game is reset to run the new assets
    Stop-Process -name "appletviewer" -ErrorAction SilentlyContinue
}
  • Stop-Process line stops the appletviewer (which is running the game), as a way to force the tester to restart the game. Redirector (and the game) does not update the fact that the assets have been downloaded dynamically. A restart is required to continue debugging.
  • -ErrorAction SilentlyContinue Allows the command to fail without complaining if it finds that the process is not running at the moment.
Write-Host "Thanks for using my tool!"

<# TODO: Allow users to specify output location - most likely manually for now
 - Maybe talk to redirector to grab 404 urls
 - Dynamically add missing folders #>

Rest of the code with some TODOs.

This script I passed on to the Discord and was happy to see that some people had cloned it on Github (through the analytics). It’s optimized for Freearcade.com java applets but I hope to expand it to become better.