| 1 |
########################################################################### |
| 2 |
# fheroes2: https://github.com/ihhub/fheroes2 # |
| 3 |
# Copyright (C) 2021 - 2024 # |
| 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 |
$h2DemoURL = "https://archive.org/download/HeroesofMightandMagicIITheSuccessionWars_1020/h2demo.zip" |
| 24 |
$h2DemoSHA256 = "12048C8B03875C81E69534A3813AAF6340975E77B762DC1B79A4FF5514240E3C" |
| 25 |
|
| 26 |
try { |
| 27 |
function Get-FileViaHTTP { |
| 28 |
param ( |
| 29 |
[string]$URL, |
| 30 |
[string]$FilePath |
| 31 |
) |
| 32 |
|
| 33 |
try { |
| 34 |
Invoke-WebRequest -Uri $URL -OutFile $FilePath |
| 35 |
} catch [System.Management.Automation.CommandNotFoundException] { |
| 36 |
if ($_.Exception.CommandName -Eq "Invoke-WebRequest") { |
| 37 |
$webClient = New-Object System.Net.WebClient |
| 38 |
|
| 39 |
$webClient.DownloadFile($URL, $FilePath) |
| 40 |
} else { |
| 41 |
throw |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
function Get-SHA256HashForFile { |
| 47 |
param ( |
| 48 |
[string]$Path |
| 49 |
) |
| 50 |
|
| 51 |
try { |
| 52 |
return (Get-FileHash -Path $Path -Algorithm SHA256).Hash |
| 53 |
} catch [System.Management.Automation.CommandNotFoundException] { |
| 54 |
if ($_.Exception.CommandName -Eq "Get-FileHash") { |
| 55 |
try { |
| 56 |
$output = certutil.exe -hashfile $Path sha256 2>&1 |
| 57 |
|
| 58 |
return ($output | Select-String -Pattern "^[0-9A-Fa-f]{64}$").Line.ToUpper() |
| 59 |
} catch { |
| 60 |
Write-Host -ForegroundColor Yellow "WARNING: Neither the Get-FileHash cmdlet nor certutil.exe is supported on this system, the hash of the downloaded file cannot be verified" |
| 61 |
|
| 62 |
if ($null -Ne $output) { |
| 63 |
Write-Host -ForegroundColor Yellow (-Join("certutil.exe output: ", ($output | Out-String))) |
| 64 |
} |
| 65 |
|
| 66 |
return $false |
| 67 |
} |
| 68 |
} else { |
| 69 |
throw |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
# Reserve space for the Invoke-WebRequest progress indicator |
| 75 |
for ($i = 0; $i -Le 7; $i++) { |
| 76 |
Write-Host "" |
| 77 |
} |
| 78 |
|
| 79 |
Write-Host -ForegroundColor Green (-Join("This script will download the demo version of the original Heroes of Might and Magic II`r`n", ` |
| 80 |
"It may take a few minutes, please wait...`r`n")) |
| 81 |
|
| 82 |
Write-Host "[1/4] determining the destination directory" |
| 83 |
|
| 84 |
$destPath = $null |
| 85 |
|
| 86 |
if (Test-Path -Path "fheroes2.exe" -PathType Leaf) { |
| 87 |
$destPath = "." |
| 88 |
} elseif (Test-Path -Path "..\..\src" -PathType Container) { |
| 89 |
# Special hack for developers running this script from the source tree |
| 90 |
$destPath = "..\.." |
| 91 |
} |
| 92 |
|
| 93 |
try { |
| 94 |
if ($null -Eq $destPath) { |
| 95 |
throw |
| 96 |
} |
| 97 |
|
| 98 |
while ($true) { |
| 99 |
$randName = [System.IO.Path]::GetRandomFileName() |
| 100 |
|
| 101 |
if (-Not (Test-Path -Path "$destPath\$randName")) { |
| 102 |
[void](New-Item -Path "$destPath\$randName" -ItemType "directory") |
| 103 |
Remove-Item -Path "$destPath\$randName" |
| 104 |
|
| 105 |
break |
| 106 |
} |
| 107 |
} |
| 108 |
} catch { |
| 109 |
if ($null -Eq $Env:APPDATA) { |
| 110 |
Write-Host -ForegroundColor Red "FATAL ERROR: Unable to determine the destination directory" |
| 111 |
|
| 112 |
return |
| 113 |
} |
| 114 |
|
| 115 |
$destPath = "$Env:APPDATA\fheroes2" |
| 116 |
|
| 117 |
if (-Not (Test-Path -Path $destPath -PathType Container)) { |
| 118 |
[void](New-Item -Path $destPath -ItemType "directory") |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
Write-Host -ForegroundColor Green (-Join("Destination directory: ", (Resolve-Path $destPath).Path)) |
| 123 |
|
| 124 |
Write-Host "[2/4] downloading the demo version" |
| 125 |
|
| 126 |
$demoPath = "$destPath\demo" |
| 127 |
|
| 128 |
if (-Not (Test-Path -Path $demoPath -PathType Container)) { |
| 129 |
[void](New-Item -Path $demoPath -ItemType "directory") |
| 130 |
} |
| 131 |
|
| 132 |
Get-FileViaHTTP -URL $h2DemoURL -FilePath "$demoPath\h2demo.zip" |
| 133 |
|
| 134 |
$result = Get-SHA256HashForFile -Path "$demoPath\h2demo.zip" |
| 135 |
|
| 136 |
if (-Not ($result -Is [Boolean]) -And ($result -Ne $h2DemoSHA256)) { |
| 137 |
Write-Host -ForegroundColor Red (-Join("FATAL ERROR: Invalid hash for HoMM2 demo archive`r`n", ` |
| 138 |
"Expected:`t$h2DemoSHA256`r`n", ` |
| 139 |
"Got:`t`t$result`r`n", ` |
| 140 |
"Installation aborted")) |
| 141 |
|
| 142 |
return |
| 143 |
} |
| 144 |
|
| 145 |
Write-Host "[3/4] unpacking archives" |
| 146 |
|
| 147 |
$shell = New-Object -ComObject "Shell.Application" |
| 148 |
|
| 149 |
$zip = $shell.NameSpace((Resolve-Path "$demoPath\h2demo.zip").Path) |
| 150 |
|
| 151 |
foreach ($item in $zip.Items()) { |
| 152 |
$shell.Namespace((Resolve-Path $demoPath).Path).CopyHere($item, 0x14) |
| 153 |
} |
| 154 |
|
| 155 |
Write-Host "[4/4] copying files" |
| 156 |
|
| 157 |
$dataPath = "$destPath\data" |
| 158 |
$mapsPath = "$destPath\maps" |
| 159 |
|
| 160 |
if (-Not (Test-Path -Path $dataPath -PathType Container)) { |
| 161 |
[void](New-Item -Path $dataPath -ItemType "directory") |
| 162 |
} |
| 163 |
if (-Not (Test-Path -Path $mapsPath -PathType Container)) { |
| 164 |
[void](New-Item -Path $mapsPath -ItemType "directory") |
| 165 |
} |
| 166 |
|
| 167 |
$data = $shell.NameSpace((Resolve-Path "$demoPath\DATA").Path) |
| 168 |
$maps = $shell.NameSpace((Resolve-Path "$demoPath\MAPS").Path) |
| 169 |
|
| 170 |
foreach ($item in $data.Items()) { |
| 171 |
$shell.Namespace((Resolve-Path $dataPath).Path).CopyHere($item, 0x14) |
| 172 |
} |
| 173 |
foreach ($item in $maps.Items()) { |
| 174 |
$shell.Namespace((Resolve-Path $mapsPath).Path).CopyHere($item, 0x14) |
| 175 |
} |
| 176 |
} catch { |
| 177 |
Write-Host -ForegroundColor Red (-Join("FATAL ERROR: ", ($_ | Out-String))) |
| 178 |
} finally { |
| 179 |
Write-Host "Press any key to exit..." |
| 180 |
|
| 181 |
[void][System.Console]::ReadKey($true) |
| 182 |
} |