From c27d6957fa2ed6191d86813a7bffa79178646c27 Mon Sep 17 00:00:00 2001 From: Koda-Dog <koda@macwyver.de> Date: Tue, 11 Mar 2025 12:33:49 +0100 Subject: [PATCH] Improve error handling - Enhanced error catching from the PowerShell script to ensure robust execution. --- .gitignore | 1 + AutoTrackR2/HomePage.xaml.cs | 11 ++++++-- AutoTrackR2/KillTrackR_MainScript.ps1 | 36 +++++++++++++++++---------- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 9491a2f..52d9dd1 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ bld/ # Visual Studio 2015/2017 cache/options directory .vs/ +.vscode/ # Uncomment if you have tasks that create the project's static files in wwwroot #wwwroot/ diff --git a/AutoTrackR2/HomePage.xaml.cs b/AutoTrackR2/HomePage.xaml.cs index 74d99e2..3879c13 100644 --- a/AutoTrackR2/HomePage.xaml.cs +++ b/AutoTrackR2/HomePage.xaml.cs @@ -98,7 +98,14 @@ namespace AutoTrackR2 Dispatcher.Invoke(() => { // Parse and display key-value pairs in the OutputTextBox - if (e.Data.Contains("PlayerName=")) + if (e.Data.Contains("LogERROR=")) + { + string LogERROR = e.Data.Split('=')[1].Trim(); + string currentText = DebugPanel.Text; + DebugPanel.Text = "An error has occurred: " + LogERROR + Environment.NewLine + currentText; + StopButton_Click(null, null); + } + else if (e.Data.Contains("PlayerName=")) { string pilotName = e.Data.Split('=')[1].Trim(); PilotNameTextBox.Text = pilotName; // Update the Button's Content @@ -342,7 +349,7 @@ namespace AutoTrackR2 Grid.SetColumn(killTextBlock, 0); killGrid.Children.Add(killTextBlock); - if (eventType == "Kill") + if (eventType != "Other") { // Create the Image for the profile string urlToUse = string.IsNullOrEmpty(killParts[7]) ? "https://cdn.robertsspaceindustries.com/static/images/account/avatar_default_big.jpg" : killParts[7]; diff --git a/AutoTrackR2/KillTrackR_MainScript.ps1 b/AutoTrackR2/KillTrackR_MainScript.ps1 index 34d2dc3..e8a8d4c 100644 --- a/AutoTrackR2/KillTrackR_MainScript.ps1 +++ b/AutoTrackR2/KillTrackR_MainScript.ps1 @@ -2,7 +2,7 @@ # GitHub: https://github.com/BubbaGumpShrump/AutoTrackR2 # Script version -$script:TrackRver = "2.07-koda-mod" +$script:TrackRver = "2.07-koda-mod_20250311_001" [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 @@ -69,7 +69,7 @@ $prefixes = @( # Define the regex pattern to extract information $script:KillPattern = "<Actor Death> CActor::Kill: '(?<VictimPilot>[^']+)' \[\d+\] in zone '(?<VictimShip>[^']+)' killed by '(?<AgressorPilot>[^']+)' \[[^']+\] using '(?<Weapon>[^']+)' \[Class (?<Class>[^\]]+)\] with damage type '(?<DamageType>[^']+)'" -$script:PuPattern = '<\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z> \[Notice\] <ContextEstablisherTaskFinished> establisher="CReplicationModel" message="CET completed" taskname="StopLoadingScreen" state=[^ ]+ status="Finished" runningTime=\d+\.\d+ numRuns=\d+ map="megamap" gamerules="(?<Gamerules>[^"]+)" sessionId="[a-f0-9\-]+" \[Team_Network\]\[Network\]\[Replication\]\[Loading\]\[Persistence\]' +$script:PuPattern = '<\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z> \[Notice\] <ContextEstablisherTaskFinished> establisher="CReplicationModel" message="CET completed" taskname="StopLoadingScreen" state=[^\s()]+\(\d+\) status="Finished" runningTime=\d+\.\d+ numRuns=\d+ map="megamap" gamerules="(?<Gamerules>[^"]+)" sessionId="[a-f0-9\-]+" \[Team_Network\]\[Network\]\[Replication\]\[Loading\]\[Persistence\]' # $script:AcPattern = "Requesting Mode Change" # "ArenaCommanderFeature" $script:LoadoutPattern = '\[InstancedInterior\] OnEntityLeaveZone - InstancedInterior \[(?<InstancedInterior>[^\]]+)\] \[\d+\] -> Entity \[(?<Entity>[^\]]+)\] \[\d+\] -- m_openDoors\[\d+\], m_managerGEID\[(?<ManagerGEID>\d+)\], m_ownerGEID\[(?<OwnerGEID>[^\[]+)\]' $script:ShipManPattern = "^(" + ($prefixes -join "|") + ")" @@ -105,7 +105,16 @@ function Get-ConfigurationSettings { $requiredSettings = @('Logfile') foreach ($setting in $requiredSettings) { if (-not $config.ContainsKey($setting)) { - Write-Error "Missing required setting: $setting" + Write-OutputData "LogERROR=Missing required setting $setting in $configFile" + Write-Error "Missing required setting $setting in $configFile" + return $null + } + + # Überprüfen, ob die Datei existiert + $filePath = $config[$setting] + if (-not (Test-Path -Path $filePath)) { + Write-OutputData "LogERROR=The $setting specified in $configFile does not exist: $filePath" + Write-Error "The $setting specified in $configFile does not exist: $filePath" return $null } } @@ -563,7 +572,7 @@ function Get-PlayerInfo { $script:PlayerCache[$playerName] = $playerInfo return $playerInfo } catch { - Write-Warning "Error retrieving player information for PlayerName: $_" + Write-Warning "Unable retrieving player information for PlayerName: $_" return $null } } @@ -620,12 +629,7 @@ function Send-ApiData { If ($null -ne $apiUrl){ if ($apiUrl -notlike "*/register-kill") { - if ($apiUrl -like "*/"){ - $apiUrl = $apiUrl + "register-kill" - } - if ($apiUrl -notlike "*/"){ - $apiUrl = $apiUrl + "/register-kill" - } + $apiUrl = $apiUrl.TrimEnd("/") + "/register-kill" } Write-OutputData "LogInfo=ApiURL: $apiURL" } @@ -634,11 +638,12 @@ function Send-ApiData { $null = Invoke-RestMethod -Uri $apiUrl -Method Post -Body ($sendData | ConvertTo-Json -Depth 5) -Headers $headers return "API" } catch { - Write-Warning "LogInfo=API-Error: $_" + Write-Warning "API-Error: $_" return "Err-Local" } } + function Write-CSVData { param ( [PSCustomObject]$csvData, @@ -690,9 +695,12 @@ function Invoke-PostEventActions{ function Write-OutputData { param ( - [string]$data + [string]$data, + [switch]$force ) - if ($data -ne $script:WritheCache) { + if($force){ + Write-Host $data + }elseif ($data -ne $script:WritheCache) { if($script:DebugLVL -eq 1){ Write-Host $data }else{ @@ -709,6 +717,7 @@ function Write-OutputData { # ================================= Main script ================================= # Check if the config file exists if (-not (Test-Path $script:ConfigFile)) { + Write-OutputData "LogERROR=$script:CSVFileName not found." Write-Error "$script:CSVFileName not found." return $null } @@ -717,6 +726,7 @@ $config = Get-ConfigurationSettings $script:ConfigFile # Check if the log file exists if (-not (Test-Path $config.LogFile)) { + Write-OutputData "LogERROR=Log file not found: $($config.LogPath)" Write-Error "Log file not found: $($config.LogPath)" return $null }