<# Questarr integration for Playnite. Connects a Playnite install to a self-hosted Questarr server so the couch PC can push its library up and request games back down without touching the browser UI. Talks only to Questarr's /api/integration surface, authenticating with an integration API key minted in Settings -> Integrations. #> $ErrorActionPreference = "Stop" # Playnite runs on Windows PowerShell 5.1, which still negotiates TLS 1.0 by # default. Questarr is commonly reverse-proxied behind HTTPS, so opt in to # TLS 1.2 or every request against such a setup fails at the handshake. try { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 } catch { # Older frameworks without Tls12 in the enum: nothing to do but continue. } $script:ConfigFileName = "config.json" # Playnite caps a single sync at a few thousand games; batch well under the # server's own limit so large libraries stay within one request each time. $script:SyncBatchSize = 500 # Play sessions that could not reach the server wait in a small file and are # retried later. The cap keeps a long outage from growing it without bound. $script:PlayQueueFileName = "play-sessions-queue.json" $script:PlayQueueMax = 200 # Shorter than a sync: this runs when a game closes and must not stall Playnite. $script:PlaySessionTimeoutSec = 10 # A flush on game stop or startup gives up after this long; the rest stays queued. $script:PlayFlushBudgetSec = 30 # Quickly closed launches are noise, and the server caps a session at 24 hours. $script:PlayMinSeconds = 60 $script:PlayMaxSeconds = 24 * 60 * 60 # Playnite library plugin ids, used to tell Questarr which store a game comes from. $script:PluginIdSteam = [Guid]"cb91dfc9-b977-43bf-8e70-55f46e410fab" $script:PluginIdGog = [Guid]"aebe8b7c-6dc3-4a66-af31-e7375c6b5e9e" $script:PluginIdEpic = [Guid]"00000002-dbd1-46c6-b5d0-b1ba559d10e4" # Playnite game id -> UTC start time, filled by OnGameStarted. $script:PlayStarts = @{} function Get-QuestarrConfigPath { return (Join-Path $CurrentExtensionDataPath $script:ConfigFileName) } <# The API key is stored with DPAPI (ConvertFrom-SecureString), which encrypts it against the current Windows user account. That keeps the key unreadable to other users on the machine and to anything that merely copies the file off it. It is not protection against code running as this same user. #> function Save-QuestarrConfig { param( [Parameter(Mandatory)] [string] $ServerUrl, [Parameter(Mandatory)] [string] $ApiKey, [bool] $SyncOnLibraryUpdate = $false, [bool] $MarkInstalledAsOwned = $false, [bool] $TrackPlayTime = $false, [bool] $SyncPlayStatus = $false ) if (-not (Test-Path $CurrentExtensionDataPath)) { New-Item -ItemType Directory -Path $CurrentExtensionDataPath -Force | Out-Null } $secured = ConvertTo-SecureString -String $ApiKey -AsPlainText -Force | ConvertFrom-SecureString $config = [PSCustomObject]@{ ServerUrl = $ServerUrl.TrimEnd("/") ApiKeyEncrypted = $secured SyncOnLibraryUpdate = $SyncOnLibraryUpdate MarkInstalledAsOwned = $MarkInstalledAsOwned TrackPlayTime = $TrackPlayTime SyncPlayStatus = $SyncPlayStatus } $config | ConvertTo-Json -Depth 4 | Set-Content -Path (Get-QuestarrConfigPath) -Encoding UTF8 } function Get-QuestarrConfig { $path = Get-QuestarrConfigPath if (-not (Test-Path $path)) { return $null } try { $raw = Get-Content -Path $path -Raw -Encoding UTF8 | ConvertFrom-Json } catch { $__logger.Error("Questarr: unreadable config file: $($_.Exception.Message)") return $null } if (-not $raw.ServerUrl -or -not $raw.ApiKeyEncrypted) { return $null } try { $secure = ConvertTo-SecureString -String $raw.ApiKeyEncrypted $bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure) try { $apiKey = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr) } finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) } } catch { # DPAPI blobs are bound to the Windows user that wrote them, so this is # what a copied config or a different account looks like. $__logger.Error("Questarr: stored API key could not be decrypted: $($_.Exception.Message)") return $null } return [PSCustomObject]@{ ServerUrl = $raw.ServerUrl ApiKey = $apiKey SyncOnLibraryUpdate = [bool]$raw.SyncOnLibraryUpdate MarkInstalledAsOwned = [bool]$raw.MarkInstalledAsOwned TrackPlayTime = [bool]$raw.TrackPlayTime SyncPlayStatus = [bool]$raw.SyncPlayStatus } } <# Single entry point for every Questarr call. #> function Invoke-QuestarrApi { param( [Parameter(Mandatory)] $Config, [Parameter(Mandatory)] [string] $Path, [string] $Method = "Get", $Body = $null, [int] $TimeoutSec = 60 ) $uri = "$($Config.ServerUrl)/api/integration$Path" return Invoke-QuestarrHttpRequest -Uri $uri -ApiKey $Config.ApiKey -Method $Method -Body $Body -TimeoutSec $TimeoutSec } <# Raw HttpWebRequest, not Invoke-RestMethod: PowerShell forwards X-Api-Key across an automatic redirect (unlike Authorization, which HttpWebRequest strips on its own), and there is no -PreserveAuthorizationOnRedirect equivalent for a custom header -- so a redirect to an arbitrary third-party host must never be followed blindly with the key still attached. Invoke-RestMethod's own -MaximumRedirection 0 can't help decide this: on Windows PowerShell 5.1 it surfaces a blocked redirect as an InvalidOperationException with no Response attached at all, so there is no way to read the redirect's Location header and tell a same-host scheme upgrade (a reverse proxy enforcing https, or a trailing slash) from a redirect to somewhere else entirely. HttpWebRequest with AllowAutoRedirect = $false instead gives a real WebException carrying the 3xx response, Location header included, so only a same-host redirect is followed (with the API key reattached by hand) and anything else still fails exactly the way a refused redirect always has -- including a same-host https -> http redirect, which would otherwise downgrade an https:// connection to sending the key in cleartext with no renewed warning at all. The body is encoded to UTF-8 bytes by hand because Invoke-RestMethod on PowerShell 5.1 otherwise sends JSON as ISO-8859-1, which mangles any non-ASCII game title (Pokémon, Ōkami, Nier Replicant ...) on the way up -- writing raw bytes to the request stream here keeps that same fix. #> function Invoke-QuestarrHttpRequest { param( [Parameter(Mandatory)] [string] $Uri, [Parameter(Mandatory)] [string] $ApiKey, [string] $Method = "Get", $Body = $null, [int] $TimeoutSec = 60, [int] $RedirectsFollowed = 0 ) $request = [System.Net.WebRequest]::Create($Uri) $request.Method = $Method $request.Accept = "application/json" $request.Headers.Add("X-Api-Key", $ApiKey) $request.Timeout = $TimeoutSec * 1000 # Timeout only covers getting the response headers; bound reading the body too. if ($request -is [System.Net.HttpWebRequest]) { $request.ReadWriteTimeout = $TimeoutSec * 1000 } $request.AllowAutoRedirect = $false if ($null -ne $Body) { $json = $Body | ConvertTo-Json -Depth 6 -Compress $bytes = [System.Text.Encoding]::UTF8.GetBytes($json) $request.ContentType = "application/json; charset=utf-8" $request.ContentLength = $bytes.Length $requestStream = $request.GetRequestStream() try { $requestStream.Write($bytes, 0, $bytes.Length) } finally { $requestStream.Close() } } try { $response = $request.GetResponse() } catch [System.Net.WebException] { $webResponse = $_.Exception.Response if ($null -ne $webResponse) { $status = [int]$webResponse.StatusCode if ($status -ge 300 -and $status -lt 400 -and $RedirectsFollowed -lt 1) { $location = $webResponse.Headers["Location"] $webResponse.Close() if ($location) { $originalUri = [Uri]$Uri $redirectUri = [Uri]::new($originalUri, $location) # Same host only, and never downgrade https -> http: an # https:// connection carries no consent to ever send the # key in cleartext, so a same-host redirect to plain http # is exactly as untrusted here as a redirect to a # different host entirely, and falls through to the same # refusal below. $isDowngrade = $originalUri.Scheme -ieq "https" -and $redirectUri.Scheme -ieq "http" if ($redirectUri.Host -ieq $originalUri.Host -and -not $isDowngrade) { return Invoke-QuestarrHttpRequest -Uri $redirectUri.AbsoluteUri -ApiKey $ApiKey ` -Method $Method -Body $Body -TimeoutSec $TimeoutSec ` -RedirectsFollowed ($RedirectsFollowed + 1) } } } } throw } try { $reader = New-Object System.IO.StreamReader($response.GetResponseStream()) $text = $reader.ReadToEnd() } finally { $reader.Close() $response.Close() } if ([string]::IsNullOrWhiteSpace($text)) { return $null } return $text | ConvertFrom-Json } <# Turns an exception from Invoke-QuestarrHttpRequest into something a user can act on. The status code matters here: 401 means the key is wrong, which is a very different fix from "the server is unreachable". A 3xx means Invoke-QuestarrHttpRequest found a redirect it refused to follow (anywhere but the exact same host) -- the API key never got sent there. #> function Get-QuestarrErrorMessage { param([Parameter(Mandatory)] $ErrorRecord) $response = $ErrorRecord.Exception.Response if ($null -ne $response -and $null -ne $response.StatusCode) { $status = [int]$response.StatusCode switch ($status) { 401 { return "Questarr rejected the API key (401). Re-check the key in Settings -> Integrations." } 403 { return "Questarr refused the request (403)." } 404 { return "Questarr has no integration API at this address (404). Check the address, and that the server is new enough to expose /api/integration." } 429 { return "Questarr is rate limiting this client (429). Try again shortly." } { $_ -ge 300 -and $_ -lt 400 } { return "Questarr tried to redirect this request (HTTP $status), which this extension " + "refuses to follow -- the API key would otherwise be forwarded to whatever address the " + "redirect points at. Check the configured server address in Settings -> Integrations." } default { return "Questarr returned HTTP $status. $($ErrorRecord.Exception.Message)" } } } return "Could not reach Questarr at the configured address. $($ErrorRecord.Exception.Message)" } function Get-QuestarrConfigOrWarn { $config = Get-QuestarrConfig if ($null -eq $config) { $PlayniteApi.Dialogs.ShowErrorMessage( "Questarr is not configured yet. Use Extensions -> Questarr -> Connect to Questarr first.", "Questarr") return $null } return $config } # ── Menu wiring ────────────────────────────────────────────────────────────── function GetMainMenuItems { param($getMainMenuItemsArgs) $items = @() $connect = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem $connect.Description = "Connect to Questarr..." $connect.FunctionName = "Invoke-QuestarrConnect" $connect.MenuSection = "@Questarr" $items += $connect $sync = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem $sync.Description = "Sync library to Questarr" $sync.FunctionName = "Invoke-QuestarrLibrarySync" $sync.MenuSection = "@Questarr" $items += $sync $toggle = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem $toggle.Description = "Toggle automatic sync on library update" $toggle.FunctionName = "Invoke-QuestarrToggleAutoSync" $toggle.MenuSection = "@Questarr" $items += $toggle $owned = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem $owned.Description = "Toggle promoting installed games to owned" $owned.FunctionName = "Invoke-QuestarrToggleMarkInstalledAsOwned" $owned.MenuSection = "@Questarr" $items += $owned $track = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem $track.Description = "Toggle sending play time to Questarr" $track.FunctionName = "Invoke-QuestarrToggleTrackPlayTime" $track.MenuSection = "@Questarr" $items += $track $status = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem $status.Description = "Sync play status to Questarr" $status.FunctionName = "Invoke-QuestarrSyncPlayStatus" $status.MenuSection = "@Questarr" $items += $status $toggleStatus = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem $toggleStatus.Description = "Toggle sending play status after each library update" $toggleStatus.FunctionName = "Invoke-QuestarrToggleSyncPlayStatus" $toggleStatus.MenuSection = "@Questarr" $items += $toggleStatus $importGog = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem $importGog.Description = "Import GOG library into Questarr" $importGog.FunctionName = "Invoke-QuestarrImportGogLibrary" $importGog.MenuSection = "@Questarr" $items += $importGog $gogTime = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem $gogTime.Description = "Import GOG play time into Questarr" $gogTime.FunctionName = "Invoke-QuestarrImportGogPlayTime" $gogTime.MenuSection = "@Questarr" $items += $gogTime $importEpic = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem $importEpic.Description = "Import Epic library into Questarr" $importEpic.FunctionName = "Invoke-QuestarrImportEpicLibrary" $importEpic.MenuSection = "@Questarr" $items += $importEpic $epicTime = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem $epicTime.Description = "Import Epic play time into Questarr" $epicTime.FunctionName = "Invoke-QuestarrImportEpicPlayTime" $epicTime.MenuSection = "@Questarr" $items += $epicTime return $items } function GetGameMenuItems { param($getGameMenuItemsArgs) $request = New-Object Playnite.SDK.Plugins.ScriptGameMenuItem $request.Description = "Request on Questarr" $request.FunctionName = "Invoke-QuestarrRequestGame" $request.MenuSection = "Questarr" return @($request) } # ── Actions ────────────────────────────────────────────────────────────────── <# Whether $HostName names a machine on the caller's own local network rather than somewhere out on the internet: loopback, an RFC1918/link-local IP literal, or a hostname with no dot / an mDNS ".local" suffix (the two conventions actual home-network devices use -- "questarr", "nas", "questarr.local"). A plain-HTTP address always gets a warn-and-continue choice rather than being refused outright -- this only decides how alarming that warning's wording is ("your own network" vs. "outside your network, on the open internet"). It only has to be right about "this clearly isn't a local address", not perfectly classify every address, so a literal IP is checked precisely and a bare hostname is judged by convention rather than resolved over the network. #> function Test-QuestarrIsPrivateHost { param([Parameter(Mandatory)] [string] $HostName) if ($HostName -ieq "localhost") { return $true } $ip = $null if ([System.Net.IPAddress]::TryParse($HostName, [ref]$ip)) { if ([System.Net.IPAddress]::IsLoopback($ip)) { return $true } $bytes = $ip.GetAddressBytes() if ($ip.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork) { # 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 169.254.0.0/16 (link-local) if ($bytes[0] -eq 10) { return $true } if ($bytes[0] -eq 172 -and $bytes[1] -ge 16 -and $bytes[1] -le 31) { return $true } if ($bytes[0] -eq 192 -and $bytes[1] -eq 168) { return $true } if ($bytes[0] -eq 169 -and $bytes[1] -eq 254) { return $true } return $false } # IPv6: unique local (fc00::/7) and link-local (fe80::/10) if (($bytes[0] -band 0xfe) -eq 0xfc) { return $true } if ($bytes[0] -eq 0xfe -and ($bytes[1] -band 0xc0) -eq 0x80) { return $true } return $false } # Not an IP literal: judge by the two conventions local-network devices # actually use, rather than spending a DNS round-trip on every Connect. return ($HostName -notlike "*.*") -or ($HostName -ilike "*.local") } function Invoke-QuestarrConnect { param($scriptMainMenuItemActionArgs) $existing = Get-QuestarrConfig $defaultUrl = if ($existing) { $existing.ServerUrl } else { "http://localhost:5000" } $urlResult = $PlayniteApi.Dialogs.SelectString( "Questarr server address (including http:// or https://):", "Questarr", $defaultUrl) if (-not $urlResult.Result) { return } $serverUrl = $urlResult.SelectedString.Trim() if ([string]::IsNullOrWhiteSpace($serverUrl)) { $PlayniteApi.Dialogs.ShowErrorMessage("A server address is required.", "Questarr") return } $parsedUri = $null if (-not [Uri]::TryCreate($serverUrl, [UriKind]::Absolute, [ref]$parsedUri) -or $parsedUri.Scheme -notin @("http", "https")) { $PlayniteApi.Dialogs.ShowErrorMessage( "'$serverUrl' is not a valid http:// or https:// address.", "Questarr") return } # Never a hard stop: it's the user's server and their call to make. But the # API key is a long-lived credential sent on every sync or request, unlike # a password typed once, so plain http:// always gets an explicit, loud # warning before it can proceed -- worded more strongly the further the # address is from "this is obviously just my own LAN". # # DnsSafeHost, not Host: Host keeps the [brackets] around an IPv6 literal, # which [System.Net.IPAddress]::TryParse inside Test-QuestarrIsPrivateHost # rejects outright -- that falls through to the hostname heuristic, which # would otherwise misclassify a public IPv6 address as local. if ($parsedUri.Scheme -eq "http") { $isLocal = $parsedUri.IsLoopback -or (Test-QuestarrIsPrivateHost $parsedUri.DnsSafeHost) $scopeWarning = if ($isLocal) { "'$serverUrl' is a plain http:// address on your local network." } else { "'$serverUrl' is a plain http:// address OUTSIDE your local network. " + "This is considerably riskier than a same-network address: the traffic can " + "cross the open internet in cleartext." } $proceed = $PlayniteApi.Dialogs.ShowMessage( "$scopeWarning`n`n" + "WARNING: your Questarr API key will be sent UNENCRYPTED on every sync or " + "request. Anyone able to observe this traffic -- on this network, or anywhere " + "between here and the server if it's remote -- can capture the key and use it " + "to read and modify your library.`n`n" + "Use an https:// address instead whenever you can (e.g. behind a reverse " + "proxy). Continue with plain http:// anyway?", "Questarr - Security Warning", [System.Windows.MessageBoxButton]::YesNo) if ($proceed -ne [System.Windows.MessageBoxResult]::Yes) { return } } $keyResult = $PlayniteApi.Dialogs.SelectString( "Questarr API key (Settings -> Integrations -> Playnite):", "Questarr", "") if (-not $keyResult.Result) { return } $apiKey = $keyResult.SelectedString.Trim() if ([string]::IsNullOrWhiteSpace($apiKey)) { $PlayniteApi.Dialogs.ShowErrorMessage("An API key is required.", "Questarr") return } $probe = [PSCustomObject]@{ ServerUrl = $serverUrl.TrimEnd("/"); ApiKey = $apiKey } try { $ping = Invoke-QuestarrApi -Config $probe -Path "/ping" -TimeoutSec 20 } catch { $PlayniteApi.Dialogs.ShowErrorMessage((Get-QuestarrErrorMessage $_), "Questarr") return } # /ping also accepts read-only and other-purpose keys, but sync and requests # would then fail with 403 on every run. Older servers send no scope field. if ($ping.scope -and $ping.scope -notin @("library:write", "all")) { $PlayniteApi.Dialogs.ShowErrorMessage( "This API key has the access '$($ping.scope)', which cannot sync or request games. " + "Create a key with the 'Library sync (Playnite)' access in Questarr (Settings -> Integrations -> API Keys).", "Questarr") return } # Only persist a credential that has actually been proven to work. Save-QuestarrConfig ` -ServerUrl $serverUrl ` -ApiKey $apiKey ` -SyncOnLibraryUpdate $(if ($existing) { $existing.SyncOnLibraryUpdate } else { $false }) ` -MarkInstalledAsOwned $(if ($existing) { $existing.MarkInstalledAsOwned } else { $false }) ` -TrackPlayTime $(if ($existing) { $existing.TrackPlayTime } else { $false }) ` -SyncPlayStatus $(if ($existing) { $existing.SyncPlayStatus } else { $false }) $PlayniteApi.Dialogs.ShowMessage( "Connected to Questarr $($ping.version) as $($ping.authenticatedAs.username).", "Questarr") } function Invoke-QuestarrToggleAutoSync { param($scriptMainMenuItemActionArgs) $config = Get-QuestarrConfigOrWarn if ($null -eq $config) { return } $enabled = -not $config.SyncOnLibraryUpdate Save-QuestarrConfig ` -ServerUrl $config.ServerUrl ` -ApiKey $config.ApiKey ` -SyncOnLibraryUpdate $enabled ` -MarkInstalledAsOwned $config.MarkInstalledAsOwned ` -TrackPlayTime $config.TrackPlayTime ` -SyncPlayStatus $config.SyncPlayStatus $state = if ($enabled) { "enabled" } else { "disabled" } $PlayniteApi.Dialogs.ShowMessage("Automatic sync on library update is now $state.", "Questarr") } function Invoke-QuestarrToggleMarkInstalledAsOwned { param($scriptMainMenuItemActionArgs) $config = Get-QuestarrConfigOrWarn if ($null -eq $config) { return } $enabled = -not $config.MarkInstalledAsOwned Save-QuestarrConfig ` -ServerUrl $config.ServerUrl ` -ApiKey $config.ApiKey ` -SyncOnLibraryUpdate $config.SyncOnLibraryUpdate ` -MarkInstalledAsOwned $enabled ` -TrackPlayTime $config.TrackPlayTime ` -SyncPlayStatus $config.SyncPlayStatus $state = if ($enabled) { "enabled" } else { "disabled" } $PlayniteApi.Dialogs.ShowMessage( "Promoting an installed 'wanted' game to 'owned' during sync is now $state.", "Questarr") } function Invoke-QuestarrToggleTrackPlayTime { param($scriptMainMenuItemActionArgs) $config = Get-QuestarrConfigOrWarn if ($null -eq $config) { return } $enabled = -not $config.TrackPlayTime Save-QuestarrConfig ` -ServerUrl $config.ServerUrl ` -ApiKey $config.ApiKey ` -SyncOnLibraryUpdate $config.SyncOnLibraryUpdate ` -MarkInstalledAsOwned $config.MarkInstalledAsOwned ` -TrackPlayTime $enabled ` -SyncPlayStatus $config.SyncPlayStatus $state = if ($enabled) { "enabled" } else { "disabled" } $PlayniteApi.Dialogs.ShowMessage( "Sending the time you play to Questarr is now $state.", "Questarr") } function Invoke-QuestarrToggleSyncPlayStatus { param($scriptMainMenuItemActionArgs) $config = Get-QuestarrConfigOrWarn if ($null -eq $config) { return } $enabled = -not $config.SyncPlayStatus Save-QuestarrConfig ` -ServerUrl $config.ServerUrl ` -ApiKey $config.ApiKey ` -SyncOnLibraryUpdate $config.SyncOnLibraryUpdate ` -MarkInstalledAsOwned $config.MarkInstalledAsOwned ` -TrackPlayTime $config.TrackPlayTime ` -SyncPlayStatus $enabled $state = if ($enabled) { "enabled" } else { "disabled" } $PlayniteApi.Dialogs.ShowMessage( "Sending the play status after each library update is now $state.", "Questarr") } <# Steam is the one Playnite library whose GameId maps cleanly onto an identifier Questarr also stores, so it gets an exact match instead of a title comparison. Returns 0 for any other game. #> function Get-QuestarrSteamAppId { param($Game) if ($Game.PluginId -eq [Guid]"cb91dfc9-b977-43bf-8e70-55f46e410fab") { $steamAppId = 0 if ([int]::TryParse($Game.GameId, [ref]$steamAppId) -and $steamAppId -gt 0) { return $steamAppId } } return 0 } <# Maps the Playnite database onto the payload Questarr's sync endpoint expects. Only games with a real name are sent; Playnite allows blank names and the server rejects them. #> function Get-QuestarrSyncPayload { $entries = New-Object System.Collections.ArrayList foreach ($game in $PlayniteApi.Database.Games) { if ([string]::IsNullOrWhiteSpace($game.Name)) { continue } $entry = @{ title = $game.Name externalId = $game.Id.ToString() installed = [bool]$game.IsInstalled } $steamAppId = Get-QuestarrSteamAppId $game if ($steamAppId -gt 0) { $entry.steamAppId = $steamAppId } [void]$entries.Add($entry) } # Comma operator: returning the ArrayList bare would let PowerShell unroll it # into the pipeline and hand the caller a plain Object[] without GetRange(). return , $entries } function Invoke-QuestarrLibrarySync { param($scriptMainMenuItemActionArgs) $config = Get-QuestarrConfigOrWarn if ($null -eq $config) { return } $entries = Get-QuestarrSyncPayload if ($entries.Count -eq 0) { $PlayniteApi.Dialogs.ShowMessage("There are no games in this Playnite library to sync.", "Questarr") return } $result = Invoke-QuestarrSyncWithProgress -Config $config -Entries $entries if ($null -eq $result) { return } $PlayniteApi.Dialogs.ShowMessage( ("Synced {0} games to Questarr.`n`nMatched: {1}`nNot in Questarr: {2}`nPromoted to owned: {3}" -f ` $entries.Count, $result.Matched, $result.Unmatched, $result.Promoted), "Questarr") } <# Sends the library in batches and folds the per-batch responses into one tally. Throws on the first batch that fails, so the caller reports a single clear reason rather than a partial success that looks complete. #> function Invoke-QuestarrSyncBatches { param( [Parameter(Mandatory)] $Config, [Parameter(Mandatory)] $Entries, $Progress = $null ) $matched = 0 $unmatched = 0 $promoted = 0 for ($offset = 0; $offset -lt $Entries.Count; $offset += $script:SyncBatchSize) { if ($null -ne $Progress -and $Progress.CancelToken.IsCancellationRequested) { # $null here (not a partial tally) is what tells the caller this # was cancelled, not completed -- Invoke-QuestarrSyncWithProgress # and Invoke-QuestarrLibrarySync already treat $null as "say # nothing, don't show a completed-sync dialog for a run the user # cut short". return $null } $take = [Math]::Min($script:SyncBatchSize, $Entries.Count - $offset) $batch = @($Entries.GetRange($offset, $take)) $response = Invoke-QuestarrApi -Config $Config -Path "/library/sync" -Method "Post" -Body @{ games = $batch markInstalledAsOwned = $Config.MarkInstalledAsOwned } $matched += @($response.matched).Count $unmatched += @($response.unmatched).Count $promoted += [int]$response.promotedToOwned if ($null -ne $Progress) { $Progress.CurrentProgressValue = $offset + $take } } return [PSCustomObject]@{ Matched = $matched Unmatched = $unmatched Promoted = $promoted } } <# Runs a sync behind Playnite's progress dialog. The dialog invokes its scriptblock on another thread, so results travel back through script-scoped variables rather than by mutating locals the closure happens to see. #> function Invoke-QuestarrSyncWithProgress { param( [Parameter(Mandatory)] $Config, [Parameter(Mandatory)] $Entries ) $script:SyncConfig = $Config $script:SyncEntries = $Entries $script:SyncResult = $null $script:SyncError = $null $options = New-Object Playnite.SDK.GlobalProgressOptions("Syncing library to Questarr...", $true) $options.IsIndeterminate = $false $PlayniteApi.Dialogs.ActivateGlobalProgress({ param($progress) $progress.ProgressMaxValue = $script:SyncEntries.Count try { $script:SyncResult = Invoke-QuestarrSyncBatches ` -Config $script:SyncConfig -Entries $script:SyncEntries -Progress $progress } catch { $script:SyncError = Get-QuestarrErrorMessage $_ } }, $options) | Out-Null if ($null -ne $script:SyncError) { $__logger.Error("Questarr: library sync failed: $($script:SyncError)") $PlayniteApi.Dialogs.ShowErrorMessage($script:SyncError, "Questarr") return $null } return $script:SyncResult } function Invoke-QuestarrRequestGame { param($scriptGameMenuItemActionArgs) $config = Get-QuestarrConfigOrWarn if ($null -eq $config) { return } $added = New-Object System.Collections.ArrayList $already = New-Object System.Collections.ArrayList $failed = New-Object System.Collections.ArrayList foreach ($game in $scriptGameMenuItemActionArgs.Games) { if ([string]::IsNullOrWhiteSpace($game.Name)) { continue } try { $response = Invoke-QuestarrApi -Config $config -Path "/games/request" -Method "Post" ` -Body @{ title = $game.Name } [void]$added.Add($response.game.title) } catch { $response = $_.Exception.Response if ($null -ne $response -and [int]$response.StatusCode -eq 409) { [void]$already.Add($game.Name) } else { [void]$failed.Add("$($game.Name): $(Get-QuestarrErrorMessage $_)") } } } $lines = New-Object System.Collections.ArrayList if ($added.Count -gt 0) { [void]$lines.Add("Requested on Questarr:`n " + ($added -join "`n ")) } if ($already.Count -gt 0) { [void]$lines.Add("Already in your Questarr library:`n " + ($already -join "`n ")) } if ($failed.Count -gt 0) { [void]$lines.Add("Failed:`n " + ($failed -join "`n ")) } if ($lines.Count -eq 0) { $PlayniteApi.Dialogs.ShowMessage("No games with a usable title were selected.", "Questarr") return } $PlayniteApi.Dialogs.ShowMessage(($lines -join "`n`n"), "Questarr") } # ── Play status ────────────────────────────────────────────────────────────── # Most requests Questarr accepts per call; stay below its own limit like the library sync. $script:PlayStatusBatchSize = 500 <# The store a game comes from, as Questarr names it, or "" for anything else (the server then records the status as coming from Playnite itself). #> function Get-QuestarrPlatformName { param($Game) if ($Game.PluginId -eq $script:PluginIdSteam) { return "steam" } if ($Game.PluginId -eq $script:PluginIdGog) { return "gog" } if ($Game.PluginId -eq $script:PluginIdEpic) { return "epic" } return "" } <# One entry per game that has a completion status in Playnite. Games with no status are left out: there is nothing to tell Questarr about them. #> function Get-QuestarrPlayStatusPayload { $entries = New-Object System.Collections.ArrayList foreach ($game in $PlayniteApi.Database.Games) { if ([string]::IsNullOrWhiteSpace($game.Name)) { continue } if ($null -eq $game.CompletionStatus) { continue } $statusName = [string]$game.CompletionStatus.Name if ([string]::IsNullOrWhiteSpace($statusName)) { continue } # Questarr rejects a whole batch if one entry is over these limits, so leave such games out. if ($statusName.Length -gt 100 -or $game.Name.Length -gt 500) { $__logger.Warn("Questarr: '$($game.Name)' skipped from the play status sync (title or status name too long)") continue } $entry = @{ title = $game.Name externalId = $game.Id.ToString() status = $statusName } $platform = Get-QuestarrPlatformName $game if ($platform) { $entry.platform = $platform } $steamAppId = Get-QuestarrSteamAppId $game if ($steamAppId -gt 0) { $entry.steamAppId = $steamAppId } [void]$entries.Add($entry) } # Comma operator, as in Get-QuestarrSyncPayload: keep the ArrayList intact. return , $entries } <# Sends the statuses in batches and sums the per-entry outcomes. Throws on the first failing batch so the caller reports one clear reason. #> function Invoke-QuestarrPlayStatusBatches { param( [Parameter(Mandatory)] $Config, [Parameter(Mandatory)] $Entries, $Progress = $null ) $updated = 0 $unchanged = 0 $skipped = 0 $unmatched = 0 for ($offset = 0; $offset -lt $Entries.Count; $offset += $script:PlayStatusBatchSize) { if ($null -ne $Progress -and $Progress.CancelToken.IsCancellationRequested) { # $null (not a partial tally) tells the caller the user cut it short. return $null } $take = [Math]::Min($script:PlayStatusBatchSize, $Entries.Count - $offset) $batch = @($Entries.GetRange($offset, $take)) $response = Invoke-QuestarrApi -Config $Config -Path "/play-status" -Method "Post" -Body @{ games = $batch promoteWanted = [bool]$Config.MarkInstalledAsOwned } $updated += [int]$response.updated $unchanged += [int]$response.unchanged $skipped += [int]$response.skipped $unmatched += [int]$response.unmatched if ($null -ne $Progress) { $Progress.CurrentProgressValue = $offset + $take } } return [PSCustomObject]@{ Updated = $updated Unchanged = $unchanged Skipped = $skipped Unmatched = $unmatched } } <# Runs the status upload behind Playnite's progress dialog, like the library sync, so a slow server never freezes the Playnite window. Results travel back through script-scoped variables because the dialog runs on another thread. Returns $null when it failed (already reported) or was cancelled. #> function Invoke-QuestarrPlayStatusWithProgress { param( [Parameter(Mandatory)] $Config, [Parameter(Mandatory)] $Entries ) $script:StatusConfig = $Config $script:StatusEntries = $Entries $script:StatusResult = $null $script:StatusError = $null $options = New-Object Playnite.SDK.GlobalProgressOptions("Sending play status to Questarr...", $true) $options.IsIndeterminate = $false $PlayniteApi.Dialogs.ActivateGlobalProgress({ param($progress) $progress.ProgressMaxValue = $script:StatusEntries.Count try { $script:StatusResult = Invoke-QuestarrPlayStatusBatches ` -Config $script:StatusConfig -Entries $script:StatusEntries -Progress $progress } catch { $script:StatusError = Get-QuestarrErrorMessage $_ } }, $options) | Out-Null if ($null -ne $script:StatusError) { $__logger.Error("Questarr: play status sync failed: $($script:StatusError)") $PlayniteApi.Dialogs.ShowErrorMessage($script:StatusError, "Questarr") return $null } return $script:StatusResult } function Invoke-QuestarrSyncPlayStatus { param($scriptMainMenuItemActionArgs) $config = Get-QuestarrConfigOrWarn if ($null -eq $config) { return } $entries = Get-QuestarrPlayStatusPayload if ($entries.Count -eq 0) { $PlayniteApi.Dialogs.ShowMessage( "No game in this Playnite library has a completion status to send.", "Questarr") return } $result = Invoke-QuestarrPlayStatusWithProgress -Config $config -Entries $entries if ($null -eq $result) { return } $PlayniteApi.Dialogs.ShowMessage( (("Sent the status of {0} games to Questarr.`n`nUpdated: {1}`nAlready the same: {2}`n" + "Left alone (set by hand in Questarr, wanted or downloading): {3}`nNot in Questarr: {4}") -f ` $entries.Count, $result.Updated, $result.Unchanged, $result.Skipped, $result.Unmatched), "Questarr") } # ── GOG and Epic library and play time ─────────────────────────────────────── # One-off imports, run by hand: each library is read by Playnite's own store plugin, so # nothing here talks to GOG or Epic. # Stores with an import command: Questarr's name for the store, how it is shown, and the # Playnite library plugin that reads it. $script:ImportStores = @{ gog = @{ Label = "GOG"; PluginId = $script:PluginIdGog } epic = @{ Label = "Epic"; PluginId = $script:PluginIdEpic } } # The server looks each new title up on IGDB, so it takes few games per request. $script:ImportBatchSize = 25 function Get-QuestarrStoreGames { param([Parameter(Mandatory)] [string] $Store) $pluginId = $script:ImportStores[$Store].PluginId @($PlayniteApi.Database.Games | Where-Object { $_.PluginId -eq $pluginId -and -not [string]::IsNullOrWhiteSpace($_.Name) -and $_.Name.Length -le 500 }) } <# Sends the work in batches behind Playnite's progress dialog. $Kind is "library" (create the missing games) or "time" (send the play time totals). Returns the summed counters, or $null when it failed (already reported) or was cancelled. #> function Invoke-QuestarrStoreBatchesWithProgress { param( [Parameter(Mandatory)] $Config, [Parameter(Mandatory)] $Entries, [Parameter(Mandatory)] [ValidateSet("library", "time")] [string] $Kind, [Parameter(Mandatory)] [string] $Store, [switch] $FlushQueueFirst ) $script:StoreConfig = $Config $script:StoreFlushFirst = [bool]$FlushQueueFirst $script:StoreQueuePending = $false $script:StoreEntries = $Entries $script:StoreKind = $Kind $script:StoreLabel = $script:ImportStores[$Store].Label $script:StoreResult = $null $script:StoreError = $null $title = if ($Kind -eq "library") { "Importing the $($script:StoreLabel) library into Questarr..." } else { "Sending $($script:StoreLabel) play time to Questarr..." } $options = New-Object Playnite.SDK.GlobalProgressOptions($title, $true) $options.IsIndeterminate = $false $PlayniteApi.Dialogs.ActivateGlobalProgress({ param($progress) $progress.ProgressMaxValue = $script:StoreEntries.Count try { if ($script:StoreFlushFirst) { # The flush is a network call: run here, off Playnite's main thread. Invoke-QuestarrFlushPlayQueue -Config $script:StoreConfig if (@(Read-QuestarrPlayQueue).Count -gt 0) { $script:StoreQueuePending = $true return } } $isLibrary = $script:StoreKind -eq "library" $size = if ($isLibrary) { $script:ImportBatchSize } else { $script:PlayStatusBatchSize } $path = if ($isLibrary) { "/library/import" } else { "/play-time-totals" } $totals = @{ Added = 0; Existing = 0; NotFound = 0; Imported = 0; Unchanged = 0; Skipped = 0; Unmatched = 0; Minutes = 0 } for ($offset = 0; $offset -lt $script:StoreEntries.Count; $offset += $size) { if ($progress.CancelToken.IsCancellationRequested) { return } $take = [Math]::Min($size, $script:StoreEntries.Count - $offset) $batch = @($script:StoreEntries.GetRange($offset, $take)) $response = Invoke-QuestarrApi -Config $script:StoreConfig -Path $path -Method "Post" ` -Body @{ games = $batch } -TimeoutSec 300 $totals.Added += [int]$response.added $totals.Existing += [int]$response.existing $totals.NotFound += [int]$response.notFound $totals.Imported += [int]$response.imported $totals.Unchanged += [int]$response.unchanged $totals.Skipped += [int]$response.skipped $totals.Unmatched += [int]$response.unmatched $totals.Minutes += [int]$response.minutesAdded $progress.CurrentProgressValue = $offset + $take } $script:StoreResult = [PSCustomObject]$totals } catch { $script:StoreError = Get-QuestarrErrorMessage $_ } }, $options) | Out-Null if ($script:StoreQueuePending) { $PlayniteApi.Dialogs.ShowMessage( "Some play sessions could not be sent to Questarr yet. Play time is not imported while they wait: " + "check that Questarr is reachable and try again.", "Questarr") return $null } if ($null -ne $script:StoreError) { $__logger.Error("Questarr: $($script:StoreLabel) import failed: $($script:StoreError)") $PlayniteApi.Dialogs.ShowErrorMessage($script:StoreError, "Questarr") return $null } return $script:StoreResult } function Invoke-QuestarrImportStoreLibrary { param([Parameter(Mandatory)] [string] $Store) $config = Get-QuestarrConfigOrWarn if ($null -eq $config) { return } $label = $script:ImportStores[$Store].Label $games = Get-QuestarrStoreGames -Store $Store if ($games.Count -eq 0) { $PlayniteApi.Dialogs.ShowMessage("No $label game in this Playnite library.", "Questarr") return } $answer = $PlayniteApi.Dialogs.ShowMessage( "Add the $($games.Count) $label games of this library to Questarr as owned games?`n`n" + "Games already in Questarr are left alone. Each new title is looked up on IGDB, so a large " + "library takes a few minutes; a title IGDB cannot match to the same game is skipped.", "Questarr", [System.Windows.MessageBoxButton]::YesNo) if ($answer -ne [System.Windows.MessageBoxResult]::Yes) { return } $entries = New-Object System.Collections.ArrayList foreach ($game in $games) { [void]$entries.Add(@{ title = $game.Name; externalId = $game.Id.ToString(); platform = $Store }) } $result = Invoke-QuestarrStoreBatchesWithProgress -Config $config -Entries $entries -Kind "library" -Store $Store if ($null -eq $result) { return } $PlayniteApi.Dialogs.ShowMessage( (("Looked at {0} $label games.`n`nAdded to Questarr: {1}`nAlready in Questarr: {2}`n" + "No matching game on IGDB (skipped): {3}") -f ` $entries.Count, $result.Added, $result.Existing, $result.NotFound), "Questarr") } function Invoke-QuestarrImportGogLibrary { param($scriptMainMenuItemActionArgs) Invoke-QuestarrImportStoreLibrary -Store "gog" } function Invoke-QuestarrImportEpicLibrary { param($scriptMainMenuItemActionArgs) Invoke-QuestarrImportStoreLibrary -Store "epic" } function Invoke-QuestarrImportStorePlayTime { param([Parameter(Mandatory)] [string] $Store) $config = Get-QuestarrConfigOrWarn if ($null -eq $config) { return } $label = $script:ImportStores[$Store].Label # Playnite's total already holds the time of any session still waiting in the queue. Sent # first, the queued session would be counted a second time once it is flushed, so the # progress worker flushes the queue before sending totals and stops if sessions remain. $entries = New-Object System.Collections.ArrayList foreach ($game in (Get-QuestarrStoreGames -Store $Store)) { # Playtime is in seconds. Less than ten minutes is below what the server imports. if ([uint64]$game.Playtime -lt 600) { continue } $entry = @{ title = $game.Name externalId = $game.Id.ToString() playtimeSeconds = [int64][Math]::Min([uint64]$game.Playtime, 100000000) } if ($null -ne $game.LastActivity) { $entry.lastPlayedAt = Format-QuestarrUtc $game.LastActivity } [void]$entries.Add($entry) } if ($entries.Count -eq 0) { $PlayniteApi.Dialogs.ShowMessage("No $label game with recorded play time in this library.", "Questarr") return } $result = Invoke-QuestarrStoreBatchesWithProgress -Config $config -Entries $entries -Kind "time" -Store $Store -FlushQueueFirst if ($null -eq $result) { return } $hours = [Math]::Round($result.Minutes / 60, 1) $PlayniteApi.Dialogs.ShowMessage( (("Sent the play time of {0} $label games.`n`nImported: {1} ({2} h)`nAlready in Questarr: {3}`n" + "Left alone (timer running): {4}`nNot in Questarr: {5}") -f ` $entries.Count, $result.Imported, $hours, $result.Unchanged, $result.Skipped, $result.Unmatched), "Questarr") } function Invoke-QuestarrImportGogPlayTime { param($scriptMainMenuItemActionArgs) Invoke-QuestarrImportStorePlayTime -Store "gog" } function Invoke-QuestarrImportEpicPlayTime { param($scriptMainMenuItemActionArgs) Invoke-QuestarrImportStorePlayTime -Store "epic" } # ── Play time ──────────────────────────────────────────────────────────────── function Get-QuestarrPlayQueuePath { return (Join-Path $CurrentExtensionDataPath $script:PlayQueueFileName) } function Read-QuestarrPlayQueue { $path = Get-QuestarrPlayQueuePath if (-not (Test-Path $path)) { return @() } try { $raw = Get-Content -Path $path -Raw -Encoding UTF8 | ConvertFrom-Json } catch { # Keep the damaged file for recovery instead of letting the next write erase it. $__logger.Error("Questarr: unreadable play session queue, moved aside: $($_.Exception.Message)") try { Move-Item -Path $path -Destination "$path.bad" -Force -ErrorAction Stop } catch { # Without a recovery copy, stop here: returning an empty queue would let the # next write replace the only file left. $__logger.Error("Questarr: could not set the unreadable queue aside, it stays in place: $($_.Exception.Message)") throw } return @() } # ConvertFrom-Json yields $null for "[]"; wrapping $null in @() would count 1. if ($null -eq $raw) { return @() } return @($raw) } function Write-QuestarrPlayQueue { param($Items) if (-not (Test-Path $CurrentExtensionDataPath)) { New-Item -ItemType Directory -Path $CurrentExtensionDataPath -Force | Out-Null } $list = @($Items) if ($list.Count -gt $script:PlayQueueMax) { $list = @($list | Select-Object -Last $script:PlayQueueMax) } # Write a temporary file first so a crash mid-write cannot truncate the only copy. # -InputObject keeps a single entry a JSON array (the pipeline would unwrap it). $path = Get-QuestarrPlayQueuePath $temp = "$path.tmp" ConvertTo-Json -InputObject $list -Depth 4 | Set-Content -Path $temp -Encoding UTF8 if (Test-Path $path) { # Replace keeps the old file until the new one is in place (Move-Item -Force deletes first). [System.IO.File]::Replace($temp, $path, $null) } else { Move-Item -Path $temp -Destination $path } } function Format-QuestarrUtc { param([DateTime] $Value) return $Value.ToUniversalTime().ToString( "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", [Globalization.CultureInfo]::InvariantCulture) } <# Sends one finished session. Returns "sent", "skipped" (the server will never accept it: unknown game, invalid data) or "retry" (unreachable, rate limited, server error or a key problem worth fixing and trying again). #> function Send-QuestarrPlaySession { param( [Parameter(Mandatory)] $Config, [Parameter(Mandatory)] $Entry ) $body = @{ title = $Entry.title startedAt = $Entry.startedAt endedAt = $Entry.endedAt } if ($Entry.steamAppId -and [int]$Entry.steamAppId -gt 0) { $body.steamAppId = [int]$Entry.steamAppId } try { Invoke-QuestarrApi -Config $Config -Path "/play-sessions" -Method "Post" -Body $body ` -TimeoutSec $script:PlaySessionTimeoutSec | Out-Null return "sent" } catch { $response = $_.Exception.Response $status = if ($null -ne $response) { [int]$response.StatusCode } else { 0 } if ($status -eq 404) { # A 404 is also what a server without this route answers, which must # keep the session queued; only the server's own "unknown game" code skips it. $text = "" try { $reader = New-Object System.IO.StreamReader($response.GetResponseStream()) try { $text = $reader.ReadToEnd() } finally { $reader.Close() } } catch { } if ($text -match '"code"\s*:\s*"GAME_NOT_FOUND"') { $__logger.Info("Questarr: '$($Entry.title)' is not in the Questarr library, play session skipped") return "skipped" } $__logger.Warn("Questarr: play session route not available on this server (needs Questarr 1.7), will retry") return "retry" } if ($status -ge 400 -and $status -lt 500 -and $status -notin 401, 403, 408, 429) { $__logger.Warn("Questarr: play session for '$($Entry.title)' rejected: $(Get-QuestarrErrorMessage $_)") return "skipped" } $__logger.Warn("Questarr: play session for '$($Entry.title)' will be retried: $(Get-QuestarrErrorMessage $_)") return "retry" } } <# Sends what is waiting in the queue, oldest first. Stops at the first entry that must be retried (the server is probably unreachable) or once the flush budget is spent, and keeps that entry and everything after it. #> function Invoke-QuestarrFlushPlayQueue { param([Parameter(Mandatory)] $Config) $queue = @(Read-QuestarrPlayQueue) if ($queue.Count -eq 0) { return } $remaining = New-Object System.Collections.ArrayList $stopped = $false $deadline = [DateTime]::UtcNow.AddSeconds($script:PlayFlushBudgetSec) foreach ($entry in $queue) { if (-not $stopped -and [DateTime]::UtcNow -ge $deadline) { $stopped = $true } if ($stopped) { [void]$remaining.Add($entry) continue } if ((Send-QuestarrPlaySession -Config $Config -Entry $entry) -eq "retry") { $stopped = $true [void]$remaining.Add($entry) } } Write-QuestarrPlayQueue -Items $remaining.ToArray() } # ── Playnite lifecycle hooks ───────────────────────────────────────────────── <# Fired after Playnite finishes updating its library from the store plugins. Syncing here is what makes the integration feel automatic: a game installed on the couch PC shows up in Questarr without anyone opening a browser. #> function OnLibraryUpdated { param($eventArgs) $config = Get-QuestarrConfig if ($null -eq $config) { return } if ($config.SyncOnLibraryUpdate) { $entries = Get-QuestarrSyncPayload if ($entries.Count -gt 0) { try { $result = Invoke-QuestarrSyncBatches -Config $config -Entries $entries $__logger.Info( "Questarr: auto-sync matched $($result.Matched), unmatched $($result.Unmatched), promoted $($result.Promoted)") } catch { # A background sync must never interrupt Playnite with a dialog. $__logger.Error("Questarr: auto-sync failed: $(Get-QuestarrErrorMessage $_)") } } } if ($config.SyncPlayStatus) { $statuses = Get-QuestarrPlayStatusPayload if ($statuses.Count -gt 0) { try { $result = Invoke-QuestarrPlayStatusBatches -Config $config -Entries $statuses $__logger.Info( "Questarr: play status updated $($result.Updated), unchanged $($result.Unchanged), skipped $($result.Skipped), unmatched $($result.Unmatched)") } catch { $__logger.Error("Questarr: play status sync failed: $(Get-QuestarrErrorMessage $_)") } } } } <# Playnite raises these around every launch. Only the start time needs remembering: Playnite itself reports how long the game ran when it stops. #> function OnGameStarted { param($eventArgs) $config = Get-QuestarrConfig if ($null -eq $config -or -not $config.TrackPlayTime) { return } $script:PlayStarts[$eventArgs.Game.Id.ToString()] = [DateTime]::UtcNow } function OnGameStopped { param($eventArgs) $config = Get-QuestarrConfig if ($null -eq $config -or -not $config.TrackPlayTime) { return } $game = $eventArgs.Game $key = $game.Id.ToString() $endedAt = [DateTime]::UtcNow $startedAt = $null if ($eventArgs.ElapsedSeconds -gt 0) { $startedAt = $endedAt.AddSeconds(-[double]$eventArgs.ElapsedSeconds) } elseif ($script:PlayStarts.ContainsKey($key)) { $startedAt = $script:PlayStarts[$key] } $script:PlayStarts.Remove($key) if ($null -ne $startedAt -and -not [string]::IsNullOrWhiteSpace($game.Name)) { $seconds = ($endedAt - $startedAt).TotalSeconds if ($seconds -ge $script:PlayMinSeconds -and $seconds -le $script:PlayMaxSeconds) { $entry = [PSCustomObject]@{ title = $game.Name steamAppId = Get-QuestarrSteamAppId $game startedAt = Format-QuestarrUtc $startedAt endedAt = Format-QuestarrUtc $endedAt } # Queue first, then flush: a crash or a dead server never loses the session. try { Write-QuestarrPlayQueue -Items (@(Read-QuestarrPlayQueue) + $entry) } catch { $__logger.Error("Questarr: could not queue the play session: $($_.Exception.Message)") return } } } try { Invoke-QuestarrFlushPlayQueue -Config $config } catch { $__logger.Error("Questarr: play session upload failed: $($_.Exception.Message)") } } <# Playnite is the one place sessions are recorded while Questarr may be unreachable (a laptop away from home), so retry what is still waiting at startup. #> function OnApplicationStarted { param($eventArgs) $config = Get-QuestarrConfig if ($null -eq $config -or -not $config.TrackPlayTime) { return } try { Invoke-QuestarrFlushPlayQueue -Config $config } catch { $__logger.Error("Questarr: play session upload failed: $($_.Exception.Message)") } }