Files

157 lines
5.5 KiB
PowerShell

param()
$ErrorActionPreference = "Stop"
$selfUrl = "https://gitea.qortal.link/crowetic/qortal-DevNet-scripts/raw/branch/main/start-windows.ps1"
$tmpDir = Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) "tmp"
$targetName = "start.ps1"
$selfPath = $MyInvocation.MyCommand.Path
if ((Split-Path -Leaf $selfPath) -ne $targetName) {
$selfPath = Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) $targetName
}
New-Item -ItemType Directory -Force -Path $tmpDir | Out-Null
function Cleanup-Tmp {
if (Test-Path $tmpDir) {
Remove-Item -Recurse -Force $tmpDir
}
}
$tmpSelf = Join-Path $tmpDir ("start-windows.{0}.ps1" -f $PID)
try {
Invoke-WebRequest -Uri $selfUrl -OutFile $tmpSelf
} catch {
$tmpSelf = $null
}
if ($tmpSelf -and (Test-Path $tmpSelf)) {
$needsUpdate = $true
if (Test-Path $selfPath) {
$localHash = (Get-FileHash -Algorithm SHA256 -Path $selfPath).Hash
$remoteHash = (Get-FileHash -Algorithm SHA256 -Path $tmpSelf).Hash
if ($localHash -eq $remoteHash) {
$needsUpdate = $false
}
}
if ($needsUpdate) {
Write-Host "Updating start-windows.ps1..."
Copy-Item -Path $tmpSelf -Destination $selfPath -Force
Unblock-File -Path $selfPath -ErrorAction SilentlyContinue
Cleanup-Tmp
& powershell -ExecutionPolicy Bypass -File $selfPath @args
exit
}
}
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if ($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Please run as a non-administrator user before running"
Cleanup-Tmp
exit 1
}
$minJavaVer = 11
if (-not (Get-Command java -ErrorAction SilentlyContinue)) {
Write-Host "Java is not available, please install Java $minJavaVer or greater"
Cleanup-Tmp
exit 1
}
$versionOutput = & cmd /c "java -version 2>&1"
$versionMatch = ($versionOutput | Select-String -Pattern 'version "([^"]+)"' | Select-Object -First 1)
if (-not $versionMatch) {
Write-Host "Unable to determine Java version."
Cleanup-Tmp
exit 1
}
$versionStr = $versionMatch.Matches[0].Groups[1].Value
$parts = $versionStr.Split('.')
if ($parts[0] -eq "1" -and $parts.Length -ge 2) {
$major = [int]$parts[1]
} else {
$major = [int]$parts[0]
}
if ($major -lt $minJavaVer) {
Write-Host "Please upgrade your Java to version $minJavaVer or greater"
Cleanup-Tmp
exit 1
}
Write-Host "Passed Java version check"
if (-not (Test-Path "qortal.jar")) {
Write-Host "qortal.jar not found. Downloading latest version..."
Invoke-WebRequest -Uri "https://cloud.qortal.org/s/QortalDevNetJarLatest/download/qortal.jar" -OutFile "qortal.jar"
}
$hashFile = Join-Path $tmpDir "hash.txt"
try {
Write-Host "Downloading hash.txt for validation..."
Invoke-WebRequest -Uri "https://cloud.qortal.org/s/QortalDevNetJarHashFile/download/hash.txt" -OutFile $hashFile
$expectedHash = (Get-Content -Raw -Path $hashFile).Trim().ToLower()
Write-Host "Generating MD5 checksum of local qortal.jar..."
$localHash = (Get-FileHash -Algorithm MD5 -Path "qortal.jar").Hash.ToLower()
Write-Host "Validating qortal.jar file..."
if ($expectedHash -eq $localHash) {
Write-Host "qortal.jar is up to date!"
} else {
Write-Host "qortal.jar is outdated. Downloading latest version..."
Invoke-WebRequest -Uri "https://cloud.qortal.org/s/QortalDevNetJarLatest/download/qortal.jar" -OutFile "qortal.jar"
Write-Host "qortal.jar has been updated to the latest version."
}
} catch {
Write-Host "Warning: failed to validate qortal.jar; skipping validation."
}
$localSettings = "settings.json"
if (Test-Path $localSettings) {
try {
$localData = Get-Content -Raw -Path $localSettings | ConvertFrom-Json
} catch {
Write-Host "Warning: settings.json is not valid JSON; skipping fixedNetwork removal."
$localData = $null
}
if ($localData -and ($localData.PSObject.Properties.Name -contains "fixedNetwork")) {
$null = $localData.PSObject.Properties.Remove("fixedNetwork")
$localData | ConvertTo-Json -Depth 20 | Set-Content -Encoding UTF8 -Path $localSettings
Write-Host "Removed fixedNetwork from settings.json."
}
}
if (-not (Test-Path "qortal.jar")) {
$candidate = Get-ChildItem -Path "target" -Filter "qortal*.jar" -ErrorAction SilentlyContinue | Select-Object -First 1
if ($candidate) {
Write-Host "Copying Maven-built Qortal JAR to correct pathname"
Copy-Item -Path $candidate.FullName -Destination "qortal.jar" -Force
}
}
$runPid = $null
if (Test-Path "run.pid") {
$pidText = (Get-Content -Raw -Path "run.pid").Trim()
if ($pidText -match '^\d+$') {
$runPid = [int]$pidText
}
}
if ($runPid) {
$proc = Get-CimInstance Win32_Process -Filter "ProcessId=$runPid" -ErrorAction SilentlyContinue
if ($proc -and $proc.CommandLine -match "qortal\.jar") {
Write-Host "qortal appears to be running already (run.pid); skipping start."
Cleanup-Tmp
exit 0
}
}
$jvmMemoryArgs = @("-XX:MaxRAMPercentage=80", "-Xss5m", "-XX:+UseSerialGC")
$javaArgs = @("-Djava.net.preferIPv4Stack=false") + $jvmMemoryArgs + @("-jar", "qortal.jar")
$proc = Start-Process -FilePath "java" -ArgumentList $javaArgs -RedirectStandardOutput "run.log" -RedirectStandardError "run.err" -PassThru -NoNewWindow
$proc.Id | Set-Content -Path "run.pid" -Encoding ASCII
Write-Host "qortal running as pid $($proc.Id)"
Cleanup-Tmp