| 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-HoMM2DirectoryPath { |
| 25 |
param ( |
| 26 |
[string]$Path |
| 27 |
) |
| 28 |
|
| 29 |
if ((Test-Path -Path "$Path\DATA\HEROES2.AGG" -PathType Leaf) -And |
| 30 |
(Test-Path -Path "$Path\MAPS" -PathType Container)) { |
| 31 |
return $true |
| 32 |
} |
| 33 |
|
| 34 |
return $false |
| 35 |
} |
| 36 |
|
| 37 |
Write-Host -ForegroundColor Green "This script will extract and copy game resources from the original distribution of Heroes of Might and Magic II`r`n" |
| 38 |
|
| 39 |
Write-Host "[1/3] determining the destination directory" |
| 40 |
|
| 41 |
$destPath = $null |
| 42 |
|
| 43 |
if (Test-Path -Path "fheroes2.exe" -PathType Leaf) { |
| 44 |
$destPath = "." |
| 45 |
} elseif (Test-Path -Path "..\..\src" -PathType Container) { |
| 46 |
# Special hack for developers running this script from the source tree |
| 47 |
$destPath = "..\.." |
| 48 |
} |
| 49 |
|
| 50 |
try { |
| 51 |
if ($null -Eq $destPath) { |
| 52 |
throw |
| 53 |
} |
| 54 |
|
| 55 |
while ($true) { |
| 56 |
$randName = [System.IO.Path]::GetRandomFileName() |
| 57 |
|
| 58 |
if (-Not (Test-Path -Path "$destPath\$randName")) { |
| 59 |
[void](New-Item -Path "$destPath\$randName" -ItemType "directory") |
| 60 |
Remove-Item -Path "$destPath\$randName" |
| 61 |
|
| 62 |
break |
| 63 |
} |
| 64 |
} |
| 65 |
} catch { |
| 66 |
if ($null -Eq $Env:APPDATA) { |
| 67 |
Write-Host -ForegroundColor Red "FATAL ERROR: Unable to determine the destination directory" |
| 68 |
|
| 69 |
return |
| 70 |
} |
| 71 |
|
| 72 |
$destPath = "$Env:APPDATA\fheroes2" |
| 73 |
|
| 74 |
if (-Not (Test-Path -Path $destPath -PathType Container)) { |
| 75 |
[void](New-Item -Path $destPath -ItemType "directory") |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
Write-Host -ForegroundColor Green (-Join("Destination directory: ", (Resolve-Path $destPath).Path)) |
| 80 |
|
| 81 |
Write-Host "[2/3] determining the HoMM2 directory" |
| 82 |
|
| 83 |
$homm2Path = $null |
| 84 |
# Some legacy HoMM2 installation use a CD drive |
| 85 |
$homm2CD = $null |
| 86 |
|
| 87 |
foreach ($key in @("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", |
| 88 |
"HKEY_CURRENT_USER\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall", |
| 89 |
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", |
| 90 |
"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall", |
| 91 |
# Legacy HoMM2 installation |
| 92 |
"HKEY_LOCAL_MACHINE\SOFTWARE\New World Computing\Heroes of Might and Magic 2", |
| 93 |
"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\New World Computing\Heroes of Might and Magic 2")) { |
| 94 |
if (-Not (Test-Path -Path "Microsoft.PowerShell.Core\Registry::$key" -PathType Container)) { |
| 95 |
continue |
| 96 |
} |
| 97 |
|
| 98 |
foreach ($subkey in (Get-ChildItem -Path "Microsoft.PowerShell.Core\Registry::$key")) { |
| 99 |
$path = $subkey.GetValue("InstallLocation") |
| 100 |
|
| 101 |
if ($null -Eq $path) { |
| 102 |
# From HKLM\SOFTWARE\New World Computing\Heroes of Might and Magic 2 |
| 103 |
$path = $subkey.GetValue("AppPath") |
| 104 |
} |
| 105 |
|
| 106 |
if ($null -Ne $path) { |
| 107 |
$path = $path.TrimEnd("\") |
| 108 |
|
| 109 |
if (Test-HoMM2DirectoryPath -Path $path) { |
| 110 |
$homm2Path = $path |
| 111 |
|
| 112 |
# From HKLM\SOFTWARE\New World Computing\Heroes of Might and Magic 2 |
| 113 |
$cdDrive = $subkey.GetValue("CDDrive") |
| 114 |
|
| 115 |
if ($null -Ne $cdDrive) { |
| 116 |
# If CDDrive is not a regular disk name (e.g. F:), then it is some path (absolute or relative), we need to try to resolve it |
| 117 |
if ($cdDrive -notmatch '^[A-Za-z]:$') { |
| 118 |
$currentPath = (Get-Location).Path |
| 119 |
|
| 120 |
# If it is a relative path, then it should be resolved against the HoMM2 installation path, therefore, we will temporarily |
| 121 |
# change the current directory to the HoMM2 installation directory |
| 122 |
try { |
| 123 |
Set-Location $homm2Path |
| 124 |
|
| 125 |
$cdDrive = (Resolve-Path $cdDrive).Path |
| 126 |
|
| 127 |
# If CDDrive eventually resolves to the HoMM2 installation directory, then ignore it, because there is no need to copy |
| 128 |
# the same files twice |
| 129 |
if ((Resolve-Path $homm2Path).Path -Eq $cdDrive) { |
| 130 |
throw |
| 131 |
} |
| 132 |
} catch { |
| 133 |
$cdDrive = $null |
| 134 |
} finally { |
| 135 |
Set-Location $currentPath |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if ($null -Ne $cdDrive) { |
| 140 |
$homm2CD = $cdDrive.TrimEnd("\") |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
break |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if ($null -Eq $homm2Path) { |
| 151 |
Write-Host -ForegroundColor Yellow "WARNING: Unable to determine the HoMM2 directory" |
| 152 |
|
| 153 |
$homm2Path = Read-Host -Prompt "Please enter the full path to the HoMM2 directory (e.g. C:\GOG Games\HoMM 2 Gold)" |
| 154 |
|
| 155 |
if (-Not (Test-HoMM2DirectoryPath -Path $homm2Path)) { |
| 156 |
Write-Host -ForegroundColor Red "FATAL ERROR: Unable to find the HoMM2 directory" |
| 157 |
|
| 158 |
return |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
Write-Host -ForegroundColor Green (-Join("HoMM2 directory: ", (Resolve-Path $homm2Path).Path)) |
| 163 |
|
| 164 |
if ($null -Ne $homm2CD) { |
| 165 |
Write-Host -ForegroundColor Green "HoMM2 CD drive: $homm2CD" |
| 166 |
} |
| 167 |
|
| 168 |
Write-Host "[3/3] copying game resources" |
| 169 |
|
| 170 |
$shell = New-Object -ComObject "Shell.Application" |
| 171 |
|
| 172 |
foreach ($homm2Dir in @($homm2Path, $homm2CD)) { |
| 173 |
if ($null -Eq $homm2Dir) { |
| 174 |
continue |
| 175 |
} |
| 176 |
|
| 177 |
if (-Not (Test-Path -Path "$homm2Dir\" -PathType Container)) { |
| 178 |
continue |
| 179 |
} |
| 180 |
|
| 181 |
# fheroes2 can be installed to the HoMM2 directory, there is no need to copy files in this case |
| 182 |
if ((Resolve-Path $homm2Dir).Path -Eq (Resolve-Path $destPath).Path) { |
| 183 |
continue |
| 184 |
} |
| 185 |
|
| 186 |
foreach ($srcDir in @("HEROES2\ANIM", "ANIM", "DATA", "MAPS", "MUSIC")) { |
| 187 |
if (-Not (Test-Path -Path "$homm2Dir\$srcDir" -PathType Container)) { |
| 188 |
continue |
| 189 |
} |
| 190 |
|
| 191 |
$destDir = (Split-Path $srcDir -Leaf).ToLower() |
| 192 |
|
| 193 |
if (-Not (Test-Path -Path "$destPath\$destDir" -PathType Container)) { |
| 194 |
[void](New-Item -Path "$destPath\$destDir" -ItemType "directory") |
| 195 |
} |
| 196 |
|
| 197 |
$content = $shell.NameSpace((Resolve-Path "$homm2Dir\$srcDir").Path) |
| 198 |
|
| 199 |
foreach ($item in $content.Items()) { |
| 200 |
$shell.Namespace((Resolve-Path "$destPath\$destDir").Path).CopyHere($item, 0x14) |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
# Special case - CD image from GOG |
| 206 |
if (Test-Path -Path "$homm2Path\homm2.gog" -PathType Leaf) { |
| 207 |
foreach ($dosboxExe in @("DOSBox.exe", "DOSBOX\DOSBox.exe")) { |
| 208 |
if (-Not (Test-Path -Path "$homm2Path\$dosboxExe" -PathType Leaf)) { |
| 209 |
continue |
| 210 |
} |
| 211 |
|
| 212 |
if (-Not (Test-Path -Path "$destPath\anim" -PathType Container)) { |
| 213 |
[void](New-Item -Path "$destPath\anim" -ItemType "directory") |
| 214 |
} |
| 215 |
|
| 216 |
Write-Host -ForegroundColor Green "Running DOSBOX to extract animation resources, please wait..." |
| 217 |
|
| 218 |
Start-Process -FilePath "$homm2Path\$dosboxExe" -ArgumentList @("-c", "`"imgmount D '$homm2Path\homm2.gog' -t iso -fs iso`"", ` |
| 219 |
"-c", "`"mount E '$destPath'`"", ` |
| 220 |
"-c", "`"copy D:\HEROES2\ANIM\*.* E:\ANIM`"", ` |
| 221 |
"-c", "exit") ` |
| 222 |
-Wait |
| 223 |
|
| 224 |
break |
| 225 |
} |
| 226 |
} |
| 227 |
} catch { |
| 228 |
Write-Host -ForegroundColor Red (-Join("FATAL ERROR: ", ($_ | Out-String))) |
| 229 |
} finally { |
| 230 |
Write-Host "Press any key to exit..." |
| 231 |
|
| 232 |
[void][System.Console]::ReadKey($true) |
| 233 |
} |