Files
bdot1andClaude Opus 5.5 80c5b0eea5 Ship the sync launcher: no more "Can not find script file" for players
An older WhoDidIt could leave a "WhoDidIt-Sync" scheduled task on a
player's PC. It points at tools\WhoDidIt-Sync-Hidden.vbs, which used to be
generated on the one PC that ran AutoSync and wasn't in the download, so
Windows showed "Can not find script file" at every login, unlock and every
15 minutes.

The launcher now ships with WhoDidIt and works out its own folder at run
time (so umlauts are fine):
- if tools\WhoDidIt-Sync.ps1 is there (the maintainer), it starts the helper
  with no window, as before;
- otherwise (any player: the helper isn't in the download) it deletes the
  task and the old Startup shortcut without a word.

Players only need to download WhoDidIt again. AutoSync no longer writes or
deletes the launcher.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 21:51:16 +01:00

143 lines
7.7 KiB
PowerShell
Vendored

<#
AutoSync - run WhoDidIt's sync helper in the background, starting with Windows.
For the WhoDidIt maintainer only (on a PC with a master character; -Force otherwise).
For anyone happy to have the helper (WhoDidIt-Sync.ps1) running: it starts
with no window every time you log into Windows, so new Chronicle raid times
keep arriving (every 30 minutes, or straight away with Sync now on
WhoDidIt's Rankings tab) without you opening anything. The Rankings bar in
game shows what it's doing.
It adds one scheduled task for your Windows user ("WhoDidIt-Sync", no admin
rights needed) that runs the launcher next to this file
(WhoDidIt-Sync-Hidden.vbs, part of WhoDidIt) to start the helper without a
window. On a PC without the helper that launcher removes the task instead,
so an older version's leftover task goes away by itself. The
task starts the helper when you log into Windows, when you unlock the PC
(also after sleep), and every 15 minutes in case it stopped; the helper
only ever runs once, so a start while it's running just ends. Nothing
else changes. (Older versions used a Startup folder shortcut instead;
it's removed.)
-Minimised keeps the old way: a Startup shortcut with a minimised window.
Usage (or double-click AutoSync-On.cmd / AutoSync-Off.cmd):
AutoSync.ps1 start it with Windows, in the background, and start it now
AutoSync.ps1 -Minimised the same, but with a minimised window on the taskbar
AutoSync.ps1 -Off stop it starting with Windows, and stop it now
#>
param([switch]$Off, [switch]$Minimised, [switch]$Force)
$taskName = "WhoDidIt-Sync"
$startup = [Environment]::GetFolderPath("Startup")
$link = Join-Path $startup "WhoDidIt-Sync.lnk"
$script = Join-Path $PSScriptRoot "WhoDidIt-Sync.ps1"
$vbs = Join-Path $PSScriptRoot "WhoDidIt-Sync-Hidden.vbs"
$ps = Join-Path $env:SystemRoot "System32\WindowsPowerShell\v1.0\powershell.exe"
$wscript = Join-Path $env:SystemRoot "System32\wscript.exe"
function Get-Helper {
@(Get-CimInstance Win32_Process -Filter "Name = 'powershell.exe' OR Name = 'pwsh.exe'" -ErrorAction SilentlyContinue |
Where-Object { $_.CommandLine -and $_.CommandLine -like "*WhoDidIt-Sync.ps1*" })
}
if ($Off) {
$had = $false
if (Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue) {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
$had = $true
}
if (Test-Path -LiteralPath $link) {
Remove-Item -LiteralPath $link -Force
$had = $true
}
if ($had) { Write-Host "Done: the WhoDidIt sync helper no longer starts by itself." }
else { Write-Host "It wasn't set to start by itself." }
$running = @(Get-Helper)
foreach ($p in $running) { Stop-Process -Id $p.ProcessId -Force -ErrorAction SilentlyContinue }
if ($running.Count -gt 0) { Write-Host "Stopped the helper that was running." }
return
}
# for the WhoDidIt maintainer only (the helper feeds everyone's raid times):
# a PC without one of the master characters (B.MASTERS in Board.lua) has no use for it
function Test-IsMaster {
$wtf = Join-Path $PSScriptRoot "..\..\..\..\WTF\Account"
$board = Join-Path $PSScriptRoot "..\Board.lua"
if (-not (Test-Path -LiteralPath $wtf) -or -not (Test-Path -LiteralPath $board)) { return $false }
$chars = @{}
foreach ($acct in Get-ChildItem -LiteralPath $wtf -Directory) {
foreach ($realm in Get-ChildItem -LiteralPath $acct.FullName -Directory) {
foreach ($c in Get-ChildItem -LiteralPath $realm.FullName -Directory) { $chars["$($realm.Name)|$($c.Name.ToLower())"] = $true }
}
}
$m = [regex]::Match([IO.File]::ReadAllText($board), '(?s)B\.MASTERS\s*=\s*\{(.*?)\n\}')
foreach ($r in [regex]::Matches($m.Groups[1].Value, '\["([^"]+)"\]\s*=\s*\{([^}]*)\}')) {
foreach ($n in [regex]::Matches($r.Groups[2].Value, '(\w+)\s*=\s*true')) {
if ($chars.ContainsKey("$($r.Groups[1].Value)|$($n.Groups[1].Value.ToLower())")) { return $true }
}
}
return $false
}
if (-not $Force -and -not (Test-IsMaster)) {
Write-Host "You don't need this: the sync helper is only for WhoDidIt's maintainer."
Write-Host "Your raid times arrive in game by themselves, and everything else ships with WhoDidIt."
return
}
if (-not $Minimised) {
# the hidden launcher (it ships with WhoDidIt), run by a scheduled task (log on, unlock, every 15 minutes)
if (-not (Test-Path -LiteralPath $vbs)) {
Write-Host "The launcher (tools\WhoDidIt-Sync-Hidden.vbs) is missing - get it back from the repository first." -ForegroundColor Yellow
return
}
$me = "$env:USERDOMAIN\$env:USERNAME"
$action = New-ScheduledTaskAction -Execute $wscript -Argument "`"$vbs`"" -WorkingDirectory $PSScriptRoot
$logon = New-ScheduledTaskTrigger -AtLogOn -User $me
$every = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) -RepetitionInterval (New-TimeSpan -Minutes 15) -RepetitionDuration (New-TimeSpan -Days 3650)
$cls = Get-CimClass -Namespace Root/Microsoft/Windows/TaskScheduler -ClassName MSFT_TaskSessionStateChangeTrigger
$unlock = New-CimInstance -CimClass $cls -ClientOnly
$unlock.StateChange = 8 # unlocking the PC (also after sleep)
$unlock.UserId = $me
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable `
-MultipleInstances IgnoreNew -ExecutionTimeLimit (New-TimeSpan -Minutes 5)
$principal = New-ScheduledTaskPrincipal -UserId $me -LogonType Interactive -RunLevel Limited
Register-ScheduledTask -TaskName $taskName -Action $action -Trigger @($logon, $unlock, $every) -Settings $settings `
-Principal $principal -Description "WhoDidIt: keeps the sync helper running (Chronicle raid times for the Rankings tab). Remove with AutoSync-Off.cmd." -Force | Out-Null
# the old way (a Startup shortcut) isn't needed any more
if (Test-Path -LiteralPath $link) { Remove-Item -LiteralPath $link -Force }
Write-Host "Done: the WhoDidIt sync helper now runs in the background (no window), starting when you log into"
Write-Host "Windows, when you unlock the PC, and every 15 minutes if it ever stopped."
Write-Host "Its progress shows on WhoDidIt's Rankings tab. To undo: double-click AutoSync-Off.cmd."
if (@(Get-Helper).Count -gt 0) {
Write-Host "The helper is already running."
} else {
Start-ScheduledTask -TaskName $taskName
Write-Host "Started it now, in the background."
Start-Sleep -Seconds 6
if (@(Get-Helper).Count -eq 0) {
Write-Host "But the helper isn't running. Try double-clicking WhoDidIt-Sync.cmd to see why." -ForegroundColor Yellow
}
}
return
}
# -Minimised: the old way, a Startup shortcut with a minimised window
$shell = New-Object -ComObject WScript.Shell
$sc = $shell.CreateShortcut($link)
$sc.TargetPath = $ps
$sc.Arguments = "-NoProfile -ExecutionPolicy Bypass -WindowStyle Minimized -File `"$script`""
$sc.WindowStyle = 7
$sc.WorkingDirectory = $PSScriptRoot
$sc.Description = "WhoDidIt: fetch Chronicle raid times for the Rankings tab"
$sc.Save()
if (Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue) { Unregister-ScheduledTask -TaskName $taskName -Confirm:$false }
Write-Host "Done: the WhoDidIt sync helper now starts with Windows (minimised)."
Write-Host "Its progress shows on WhoDidIt's Rankings tab. To undo: double-click AutoSync-Off.cmd."
if (@(Get-Helper).Count -gt 0) {
Write-Host "The helper is already running."
} else {
Start-Process -FilePath $ps -ArgumentList $sc.Arguments -WorkingDirectory $PSScriptRoot -WindowStyle Minimized
Write-Host "Started it now (minimised on your taskbar)."
}