Skip to content

Commit

Permalink
Merge branch 'early-unstable' into early
Browse files Browse the repository at this point in the history
  • Loading branch information
dragoonDorise committed Aug 23, 2024
2 parents 835f103 + f1e163d commit 210a6d2
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 4 deletions.
10 changes: 6 additions & 4 deletions functions/ToolScripts/emuDeckSaveSync.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,12 @@ function cloud_sync_init($emulator){
"$emulator" | Set-Content $savesPath/.emulator -Encoding UTF8
}

& "$env:USERPROFILE/AppData/Roaming/EmuDeck/backend/wintools/nssm.exe" stop "CloudWatch"
cls
Start-Process "$env:USERPROFILE/AppData/Roaming/EmuDeck/backend/wintools/nssm.exe" -Args "start CloudWatch" -WindowStyle Hidden
cls
Start-Process powershell.exe -ArgumentList "-ExecutionPolicy Bypass -File `"$env:USERPROFILE/AppData/Roaming/EmuDeck/backend/tools/cloudSync/cloud_sync_watcher_user.ps1`" $env:USERNAME" -WindowStyle Hidden

# & "$env:USERPROFILE/AppData/Roaming/EmuDeck/backend/wintools/nssm.exe" stop "CloudWatch"
# cls
# Start-Process "$env:USERPROFILE/AppData/Roaming/EmuDeck/backend/wintools/nssm.exe" -Args "start CloudWatch" -WindowStyle Hidden
# cls
$toast.Close()
}
}
Expand Down
226 changes: 226 additions & 0 deletions tools/cloudSync/cloud_sync_watcher_user.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
$user=$args[0]
$userPath = ( Get-CimInstance Win32_UserProfile -Filter "SID = '$((Get-LocalUser $user).Sid)'" ).LocalPath
$logPath = Join-Path -Path "$userPath" -ChildPath 'EmuDeck\logs\cloudWatcher.log'
$f1 = Join-Path -Path "$userPath" -ChildPath 'EmuDeck\settings.ps1'
$f2 = Join-Path -Path "$userPath" -ChildPath 'AppData\Roaming\EmuDeck\backend\functions\createLink.ps1'
$f3 = Join-Path -Path "$userPath" -ChildPath 'AppData\Roaming\EmuDeck\backend\functions\createLauncher.ps1'
$f4 = Join-Path -Path "$userPath" -ChildPath 'AppData\Roaming\EmuDeck\backend\functions\helperFunctions.ps1'
$f5 = Join-Path -Path "$userPath" -ChildPath 'AppData\Roaming\EmuDeck\backend\functions\ToolScripts\emuDeckSaveSync.ps1'

$string = "Starting Log."
$string | Out-File -FilePath $logPath


. $f1
. $f2
. $f3
. $f4
. $f5

$nssm = Join-Path -Path $userPath -ChildPath '\AppData\Roaming\EmuDeck\backend\wintools\nssm.exe'
$emuName = Get-Content "$savesPath/.emulator"
$lockFile = Join-Path -Path $userPath -ChildPath 'EmuDeck\cloud.lock'
rm -fo -r $lockFile -ErrorAction SilentlyContinue

# specify the path to the folder you want to watch:

if ($emuName -eq 'all'){
$emuPath = "$savesPath"
}elseif($emuName -eq $null -or $emuName -eq ''){
$emuPath = "$savesPath"
$emuName = 'all'
}else{
$emuPath = "$savesPath\$emuName"
}

$Path = "$emuPath"

# specify which files you want to monitor
$FileFilter = '*'

# 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, [IO.NotifyFilters]::LastWrite

# Create a dictionary to track the last modified time of each file
$lastModifiedTimes = @{}

try
{
$watcher = New-Object -TypeName System.IO.FileSystemWatcher -Property @{
Path = $Path
Filter = $FileFilter
IncludeSubdirectories = $IncludeSubfolders
NotifyFilter = $AttributeFilter
}

# define the code that should execute when a change occurs:
$action = {
# the code is receiving this to work with:

# change type information:
$details = $event.SourceEventArgs
$Name = $details.Name
$FullPath = $details.FullPath
$OldFullPath = $details.OldFullPath
$OldName = $details.OldName

$blackList = @(".hash", ".last_upload", ".pending_upload", ".watching", "$emuPath")

# Check if the file is in the blacklist to skip to the next loop
$skip = $blackList | ForEach-Object { $FullPath -like "*$_" }

if($skip -contains $true){
echo "Ignoring blacklisted file"
return
}

# type of change:
$ChangeType = $details.ChangeType

# when the change occurred:
$Timestamp = $event.TimeGenerated

# Check if the file was modified in the last 2 seconds
$lastModifiedTime = $lastModifiedTimes[$FullPath]
if ($lastModifiedTime -and ($Timestamp).Subtract($lastModifiedTime).TotalMilliseconds -lt 500) {
echo "Ignoring $FullPath because it was modified again too quickly."
return
}

# Update the last modified time for this file
$lastModifiedTimes[$FullPath] = (Get-Date)

# save information to a global variable for testing purposes
# so you can examine it later
# MAKE SURE YOU REMOVE THIS IN PRODUCTION!
#$global:all = $details

# now you can define some action to take based on the
# details about the change event:

# let's compose a message:
$text = "{0} was {1} at {2}" -f $FullPath, $ChangeType, $Timestamp
echo ""
echo $text -ForegroundColor DarkYellow

# you can also execute code based on change type here:
switch ($ChangeType)
{
'Changed' {

if ($skip -contains $true -or $FullPath -eq $savesPath -or $FullPath -eq $emuPath) {
echo "No upload"
} else {
$toast = steamToast -MessageText "CloudSync - Uploading Modified file"
Get-Date -Format "yyyy-MM-dd HH:mm:ss" | Out-File -FilePath $savesPath/$emuName/.pending_upload
cloud_sync_uploadEmu -emuName $emuName -mode "$userPath"
rm -fo -r "$savesPath/$emuName/.pending_upload" -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1.5
$toast.Close()
}
}
'Created' {
if ($skip -contains $true -or $FullPath -eq $savesPath -or $FullPath -eq $emuPath) {
echo "No upload"
} else {
$toast = steamToast -MessageText "CloudSync - Uploading new file"
Get-Date -Format "yyyy-MM-dd HH:mm:ss" | Out-File -FilePath $savesPath/$emuName/.pending_upload
cloud_sync_uploadEmu -emuName $emuName -mode "$userPath"
rm -fo -r "$savesPath/$emuName/.pending_upload" -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1.5
$toast.Close()
}

}
'Deleted' { "DELETED"
# to illustrate that ALL changes are picked up even if
# handling an event takes a lot of time, we artificially
# extend the time the handler needs whenever a file is deleted
echo "Deletion Handler Start"
Start-Sleep -Seconds 4
echo "Deletion Handler End"
}
'Renamed' {
# this executes only when a file was renamed
$text = "File {0} was renamed to {1}" -f $OldName, $Name
echo $text
}

# any unhandled change types surface here:
default { echo $_ }
}
}

# subscribe your event handler to all event types that are
# important to you. Do this as a scriptblock so all returned
# event handlers can be easily stored in $handlers:
$handlers = . {
Register-ObjectEvent -InputObject $watcher -EventName Changed -Action $action
Register-ObjectEvent -InputObject $watcher -EventName Created -Action $action
#Register-ObjectEvent -InputObject $watcher -EventName Deleted -Action $action
#Register-ObjectEvent -InputObject $watcher -EventName Renamed -Action $action
}

# monitoring starts now:
$watcher.EnableRaisingEvents = $true

echo "Watching for changes to $Path"

# since the FileSystemWatcher is no longer blocking PowerShell
# we need a way to pause PowerShell while being responsive to
# incoming events. Use an endless loop to keep PowerShell busy:
do
{
# Wait-Event waits for a second and stays responsive to events
# Start-Sleep in contrast would NOT work and ignore incoming events
Wait-Event -Timeout 1

# write a dot to indicate we are still monitoring:
echo "." -NoNewline

# Process name to find
$processName = "EmuDeck Launcher"
$cmdFile = Join-Path -Path $savesPath -ChildPath '.watching'
$lockFile = Join-Path -Path $userPath -ChildPath 'EmuDeck\cloud.lock'

# Check if the process exists
$process = Get-Process | Where-Object { $_.MainWindowTitle -eq $processName }


# We exit if it doesn't
if (-not (Test-Path $cmdFile)) {
echo "There's no .watching file"
$dialog = steamToast -MessageText "Uploading... don't turn off your device"
Add-Type -AssemblyName System.speech
# Check for lock file
if (-not (Test-Path $lockFile)) {
$dialog.Close()
echo "There's no lock file, bye!"
$dialog = steamToast -MessageText "Sync Completed! You can safely turn off your device"
& $nssm stop CloudWatch
$dialog.Close()

exit
}
}

} while ($true)
}
finally
{
# this gets executed when the user presses CTRL+C:

# stop monitoring
$watcher.EnableRaisingEvents = $false

# remove the event handlers
$handlers | ForEach-Object {
Unregister-Event -SourceIdentifier $_.Name
}

# event handlers are technically implemented as a special kind
# of background job, so remove the jobs now
}

0 comments on commit 210a6d2

Please sign in to comment.