| 1 |
########################################################################### |
| 2 |
# fheroes2: https://github.com/ihhub/fheroes2 # |
| 3 |
# Copyright (C) 2021 - 2023 # |
| 4 |
# # |
| 5 |
# This program is free software; you can redistribute it and/or modify # |
| 6 |
# it under the terms of the GNU General Public License as published by # |
| 7 |
# the Free Software Foundation; either version 2 of the License, or # |
| 8 |
# (at your option) any later version. # |
| 9 |
# # |
| 10 |
# This program is distributed in the hope that it will be useful, # |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 13 |
# GNU General Public License for more details. # |
| 14 |
# # |
| 15 |
# You should have received a copy of the GNU General Public License # |
| 16 |
# along with this program; if not, write to the # |
| 17 |
# Free Software Foundation, Inc., # |
| 18 |
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # |
| 19 |
########################################################################### |
| 20 |
|
| 21 |
$ErrorActionPreference = "Stop" |
| 22 |
|
| 23 |
try { |
| 24 |
function Test-DragDropFile { |
| 25 |
param ( |
| 26 |
[string[]]$FileDropList, |
| 27 |
[string]$AllowedExtension |
| 28 |
) |
| 29 |
|
| 30 |
if ($FileDropList.Count -Ne 1) { |
| 31 |
return $false |
| 32 |
} |
| 33 |
if (-Not (Test-Path -Path $FileDropList[0] -PathType Leaf)) { |
| 34 |
return $false |
| 35 |
} |
| 36 |
|
| 37 |
$fileExtension = (Get-ChildItem $FileDropList[0]).Extension |
| 38 |
|
| 39 |
if ($null -Eq $fileExtension) { |
| 40 |
return $false |
| 41 |
} |
| 42 |
|
| 43 |
$fileExtension = $fileExtension.ToLower() |
| 44 |
|
| 45 |
if ($fileExtension -Ne $AllowedExtension) { |
| 46 |
return $false |
| 47 |
} |
| 48 |
|
| 49 |
return $true |
| 50 |
} |
| 51 |
|
| 52 |
function Copy-GOGMusic { |
| 53 |
param ( |
| 54 |
[string]$ArchiveName, |
| 55 |
[string]$DestPath |
| 56 |
) |
| 57 |
|
| 58 |
Write-Host -ForegroundColor Green "Extracting GOG OST files, please wait..." |
| 59 |
|
| 60 |
$tempPath = $null |
| 61 |
|
| 62 |
while ($true) { |
| 63 |
$randName = [System.IO.Path]::GetRandomFileName() |
| 64 |
$tempPath = "$DestPath\$randName" |
| 65 |
|
| 66 |
if (-Not (Test-Path -Path $tempPath)) { |
| 67 |
[void](New-Item -Path $tempPath -ItemType "directory") |
| 68 |
|
| 69 |
break |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
$shell = New-Object -ComObject "Shell.Application" |
| 74 |
|
| 75 |
$zip = $shell.NameSpace((Resolve-Path $ArchiveName).Path) |
| 76 |
|
| 77 |
foreach ($item in $zip.Items()) { |
| 78 |
$shell.Namespace((Resolve-Path $tempPath).Path).CopyHere($item, 0x14) |
| 79 |
} |
| 80 |
|
| 81 |
$tmpMusicPath = $null |
| 82 |
|
| 83 |
while ($true) { |
| 84 |
$randName = [System.IO.Path]::GetRandomFileName() |
| 85 |
$tmpMusicPath = "$DestPath\$randName" |
| 86 |
|
| 87 |
if (-Not (Test-Path -Path $tmpMusicPath)) { |
| 88 |
[void](New-Item -Path $tmpMusicPath -ItemType "directory") |
| 89 |
|
| 90 |
break |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (-Not (Test-Path -Path $tmpMusicPath -PathType Container)) { |
| 95 |
[void](New-Item -Path $tmpMusicPath -ItemType "directory") |
| 96 |
} |
| 97 |
if (-Not (Test-Path -Path "$tmpMusicPath\pol" -PathType Container)) { |
| 98 |
[void](New-Item -Path "$tmpMusicPath\pol" -ItemType "directory") |
| 99 |
} |
| 100 |
if (-Not (Test-Path -Path "$tmpMusicPath\sw" -PathType Container)) { |
| 101 |
[void](New-Item -Path "$tmpMusicPath\sw" -ItemType "directory") |
| 102 |
} |
| 103 |
|
| 104 |
# Additional PoL soundtrack id -> SW soundtrack id |
| 105 |
$polSWMap = @{"44" = "05"; "45" = "06"; "46" = "07"; "47" = "08"; "48" = "09"; "49" = "10"} |
| 106 |
|
| 107 |
$musicType = $null |
| 108 |
|
| 109 |
foreach ($item in (Get-ChildItem -Path $tempPath -Recurse)) { |
| 110 |
if ($item.Name -notmatch "[0-9]{2}") { |
| 111 |
continue |
| 112 |
} |
| 113 |
|
| 114 |
$fileExtension = (Get-ChildItem $item.FullName).Extension |
| 115 |
|
| 116 |
if ($null -Eq $fileExtension) { |
| 117 |
continue |
| 118 |
} |
| 119 |
|
| 120 |
$fileExtension = $fileExtension.ToLower() |
| 121 |
|
| 122 |
if ($fileExtension -Eq ".flac") { |
| 123 |
$musicType = $fileExtension |
| 124 |
} elseif ($fileExtension -Eq ".mp3") { |
| 125 |
# An archive with FLAC can contain some MP3 files |
| 126 |
if ($null -Eq $musicType) { |
| 127 |
$musicType = $fileExtension |
| 128 |
} |
| 129 |
} else { |
| 130 |
continue |
| 131 |
} |
| 132 |
|
| 133 |
$trackNumber = ($item.Name | Select-String -Pattern "[0-9]{2}").Matches[0].Value |
| 134 |
|
| 135 |
# Castle soundtracks from the Succession Wars |
| 136 |
if ($polSWMap.Values -contains $trackNumber) { |
| 137 |
Copy-Item -Path $item.FullName -Destination "$tmpMusicPath\sw\Track$trackNumber$fileExtension" |
| 138 |
# Castle soundtracks from the Price of Loyalty expansion |
| 139 |
} elseif ($polSWMap.Keys -contains $trackNumber) { |
| 140 |
$trackNumber = $polSWMap[$trackNumber] |
| 141 |
|
| 142 |
Copy-Item -Path $item.FullName -Destination "$tmpMusicPath\pol\Track$trackNumber$fileExtension" |
| 143 |
} |
| 144 |
|
| 145 |
Copy-Item -Path $item.FullName -Destination "$tmpMusicPath\Track$trackNumber$fileExtension" |
| 146 |
} |
| 147 |
|
| 148 |
Remove-Item -Path $tempPath -Recurse |
| 149 |
|
| 150 |
if ($null -Eq $musicType) { |
| 151 |
Remove-Item -Path $tmpMusicPath -Recurse |
| 152 |
|
| 153 |
Write-Host -ForegroundColor Yellow "WARNING: No soundtracks were found" |
| 154 |
} else { |
| 155 |
$dirName = $null |
| 156 |
$uniqId = 0 |
| 157 |
|
| 158 |
while ($true) { |
| 159 |
if ($uniqId -Eq 0) { |
| 160 |
$dirName = "music$musicType" |
| 161 |
} else { |
| 162 |
$dirName = "music$musicType.$uniqId" |
| 163 |
} |
| 164 |
|
| 165 |
if (-Not (Test-Path -Path "$DestPath\$dirName")) { |
| 166 |
break |
| 167 |
} |
| 168 |
|
| 169 |
$uniqId++ |
| 170 |
} |
| 171 |
|
| 172 |
Rename-Item -Path $tmpMusicPath -NewName $dirName |
| 173 |
|
| 174 |
Write-Host -ForegroundColor Green (-Join("GOG OST directory: ", (Resolve-Path "$DestPath\$dirName").Path)) |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
Write-Host -ForegroundColor Green "This script will run the extraction toolset to extract and copy additional game resources from the original distribution of Heroes of Might and Magic II`r`n" |
| 179 |
|
| 180 |
Write-Host "[1/2] determining the destination directory" |
| 181 |
|
| 182 |
$destPath = $null |
| 183 |
|
| 184 |
if (Test-Path -Path "fheroes2.exe" -PathType Leaf) { |
| 185 |
$destPath = "." |
| 186 |
} elseif (Test-Path -Path "..\..\src" -PathType Container) { |
| 187 |
# Special hack for developers running this script from the source tree |
| 188 |
$destPath = "..\.." |
| 189 |
} |
| 190 |
|
| 191 |
try { |
| 192 |
if ($null -Eq $destPath) { |
| 193 |
throw |
| 194 |
} |
| 195 |
|
| 196 |
while ($true) { |
| 197 |
$randName = [System.IO.Path]::GetRandomFileName() |
| 198 |
|
| 199 |
if (-Not (Test-Path -Path "$destPath\$randName")) { |
| 200 |
[void](New-Item -Path "$destPath\$randName" -ItemType "directory") |
| 201 |
Remove-Item -Path "$destPath\$randName" |
| 202 |
|
| 203 |
break |
| 204 |
} |
| 205 |
} |
| 206 |
} catch { |
| 207 |
if ($null -Eq $Env:APPDATA) { |
| 208 |
Write-Host -ForegroundColor Red "FATAL ERROR: Unable to determine the destination directory" |
| 209 |
|
| 210 |
return |
| 211 |
} |
| 212 |
|
| 213 |
$destPath = "$Env:APPDATA\fheroes2" |
| 214 |
|
| 215 |
if (-Not (Test-Path -Path $destPath -PathType Container)) { |
| 216 |
[void](New-Item -Path $destPath -ItemType "directory") |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
Write-Host -ForegroundColor Green (-Join("Destination directory: ", (Resolve-Path $destPath).Path)) |
| 221 |
|
| 222 |
Write-Host "[2/2] running the resource extraction toolset" |
| 223 |
|
| 224 |
$commandToRun = $null |
| 225 |
|
| 226 |
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") |
| 227 |
|
| 228 |
$mainForm = New-Object -TypeName "System.Windows.Forms.Form" -Property @{Text = "Resource Extraction Toolset"} |
| 229 |
|
| 230 |
$gogOSTExtractionGroupBox = New-Object -TypeName "System.Windows.Forms.GroupBox" -Property @{ |
| 231 |
Text = "Extract a GOG OST archive" |
| 232 |
Dock = "Fill" |
| 233 |
Padding = 32 |
| 234 |
AllowDrop = $true |
| 235 |
} |
| 236 |
$gogOSTExtractionGroupBox.Add_DragEnter({ |
| 237 |
if (Test-DragDropFile -FileDropList $_.Data.GetFileDropList() -AllowedExtension ".zip") { |
| 238 |
$_.Effect = [Windows.Forms.DragDropEffects]::Copy |
| 239 |
} else { |
| 240 |
$_.Effect = [Windows.Forms.DragDropEffects]::None |
| 241 |
} |
| 242 |
}) |
| 243 |
$gogOSTExtractionGroupBox.add_DragDrop({ |
| 244 |
$fileDropList = $_.Data.GetFileDropList() |
| 245 |
|
| 246 |
if (-Not (Test-DragDropFile -FileDropList $fileDropList -AllowedExtension ".zip")) { |
| 247 |
return |
| 248 |
} |
| 249 |
|
| 250 |
$global:commandToRun = {Copy-GOGMusic -ArchiveName $fileDropList[0] -DestPath $destPath}.GetNewClosure() |
| 251 |
|
| 252 |
$mainForm.Close() |
| 253 |
}) |
| 254 |
$mainForm.Controls.Add($gogOSTExtractionGroupBox) |
| 255 |
|
| 256 |
$gogOSTExtractionBrowseButton = New-Object -TypeName "System.Windows.Forms.Button" -Property @{ |
| 257 |
Text = "Choose an OST archive file..." |
| 258 |
Dock = "Top" |
| 259 |
} |
| 260 |
$gogOSTExtractionBrowseButton.Add_Click({ |
| 261 |
$fileDialog = New-Object -TypeName "System.Windows.Forms.OpenFileDialog" -Property @{ |
| 262 |
Filter = "ZIP Archives (*.zip)|*.zip" |
| 263 |
} |
| 264 |
|
| 265 |
[void]$fileDialog.ShowDialog() |
| 266 |
|
| 267 |
if ($fileDialog.FileName -Eq "") { |
| 268 |
return |
| 269 |
} |
| 270 |
|
| 271 |
$global:commandToRun = {Copy-GOGMusic -ArchiveName $fileDialog.FileName -DestPath $destPath}.GetNewClosure() |
| 272 |
|
| 273 |
$mainForm.Close() |
| 274 |
}) |
| 275 |
$gogOSTExtractionGroupBox.Controls.Add($gogOSTExtractionBrowseButton) |
| 276 |
|
| 277 |
$gogOSTExtractionDragLabel = New-Object -TypeName "System.Windows.Forms.Label" -Property @{ |
| 278 |
Text = "Or drag && drop an OST archive file here" |
| 279 |
Dock = "Bottom" |
| 280 |
TextAlign = "MiddleCenter" |
| 281 |
} |
| 282 |
$gogOSTExtractionGroupBox.Controls.Add($gogOSTExtractionDragLabel) |
| 283 |
|
| 284 |
[System.Windows.Forms.Application]::Run($mainForm) |
| 285 |
|
| 286 |
if ($null -Ne $commandToRun) { |
| 287 |
& $commandToRun |
| 288 |
} |
| 289 |
} catch { |
| 290 |
Write-Host -ForegroundColor Red (-Join("FATAL ERROR: ", ($_ | Out-String))) |
| 291 |
} finally { |
| 292 |
Write-Host "Press any key to exit..." |
| 293 |
|
| 294 |
[void][System.Console]::ReadKey($true) |
| 295 |
} |