Skip to content

Instantly share code, notes, and snippets.

@SnoUweR
Created September 8, 2023 19:57
Show Gist options
  • Save SnoUweR/a58334efe971b9907da9913caf419dc0 to your computer and use it in GitHub Desktop.
Save SnoUweR/a58334efe971b9907da9913caf419dc0 to your computer and use it in GitHub Desktop.
baldur's gate 3 corrupted saves monitoring
# simple script that monitors new saves in the Baldur's Gate 3 folder and makes a sound if the save is less than 25 megabytes and most likely corrupted (can be configured in the file). Relevant for the third act
$SavesPath = "$Env:USERPROFILE\AppData\Local\Larian Studios\Baldur's Gate 3\PlayerProfiles\Public\Savegames\Story\"
# minimum size for the save file. If the save size is smaller than this value, the script will play a sound
$MinimumSaveSizeInMegabytes = 25 # the save size depends on the game's act. In the act 1 saves are less than 20 megabytes
$MinimumSaveSizeInBytes = $MinimumSaveSizeInMegabytes * 1000 * 1000
# specify which files you want to monitor
$FileFilter = '*.lsv'
# specify whether you want to monitor subfolders as well:
$IncludeSubfolders = $true
# specify the file or folder properties you want to monitor:
$AttributeFilter = [IO.NotifyFilters]::FileName
# specify the type of changes you want to monitor:
$ChangeTypes = [System.IO.WatcherChangeTypes]::Created
# specify the maximum time (in milliseconds) you want to wait for changes:
$Timeout = 1000
# define a function that gets called for every change:
function Invoke-SomeAction
{
param
(
[Parameter(Mandatory)]
[System.IO.WaitForChangedResult]
$ChangeInformation
)
$changedFilePath = $SavesPath + $ChangeInformation.Name
$size = (Get-Item $changedFilePath).length
if ($size -lt $MinimumSaveSizeInBytes)
{
Write-Warning ("Save file with name " + $ChangeInformation.Name + " has size smaller than " + $MinimumSaveSizeInMegabytes + "MB. Maybe it's broken!")
for (($i = 0); $i -lt 3; $i++)
{
[System.Media.SystemSounds]::Exclamation.Play()
Start-Sleep -m 500
}
}
}
try
{
Write-Output "Monitoring $SavesPath started. Use Ctrl+C to stop"
# create a filesystemwatcher object
$watcher = New-Object -TypeName IO.FileSystemWatcher -ArgumentList $SavesPath, $FileFilter -Property @{
IncludeSubdirectories = $IncludeSubfolders
NotifyFilter = $AttributeFilter
}
# start monitoring manually in a loop:
do
{
$result = $watcher.WaitForChanged($ChangeTypes, $Timeout)
if ($result.TimedOut) { continue }
Invoke-SomeAction -Change $result
# the loop runs forever until you hit CTRL+C
} while ($true)
}
finally
{
# release the watcher and free its memory:
$watcher.Dispose()
Write-Output 'Script stopped'
}
@SnoUweR
Copy link
Author

SnoUweR commented Sep 8, 2023

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment