Skip to content

Instantly share code, notes, and snippets.

@mkropat
Created August 21, 2019 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkropat/8dd5043fee40e34f6fa61500c2b62561 to your computer and use it in GitHub Desktop.
Save mkropat/8dd5043fee40e34f6fa61500c2b62561 to your computer and use it in GitHub Desktop.
$ErrorActionPreference = 'Stop'
function Get-RomFromArchive {
[CmdletBinding()]
param(
[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[Alias("PsPath")]
[string] $Path,
[Parameter(Mandatory=$True)]
[string] $DestinationPath
)
begin {
$DestinationPath = Resolve-Path $DestinationPath
}
process {
$archive = Get-Item -LiteralPath $Path
$workdir = New-TemporaryDirectory
Push-Location $workdir
try {
& 7z x $archive.FullName | Out-Null
$choice = Get-ChildItem -File -Recurse |
Where-Object Name -notmatch '\[[^!ft][^]]*\]' |
Sort-Object { Get-LanguageTier $_.Name },{ $_.Name -like '*[!]*' } -Descending |
Select-Object -First 1
if ($choice) {
Write-Verbose "Extracting... ${choice}"
Move-Item -LiteralPath $choice -Destination $DestinationPath -Force
}
}
finally {
Pop-Location
$workdir | Remove-Item -Force -Recurse
}
}
}
function Get-LanguageTier {
param (
[string] $Name
)
if ($Name -notmatch '\[t[^]]*\]') {
if ($Name -like '*(U)*') {
return 5
}
if ($Name -like '*(JUE)*' -or $Name -like '*(JU)*' -or $Name -like '*(UE)*') {
return 4
}
if ($Name -like '*(E)*') {
return 3
}
}
if ($Name -match '\[t\+eng[^]]*\]') {
return 2
}
if ($Name -match '\[t-rng[^]]*\]') {
return 1
}
return 0
}
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment