diff --git a/Dotnet/AppApi/Common/ImageSaving.cs b/Dotnet/AppApi/Common/ImageSaving.cs index e2d054d8..4c4043a7 100644 --- a/Dotnet/AppApi/Common/ImageSaving.cs +++ b/Dotnet/AppApi/Common/ImageSaving.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Text.Json; +using System.Threading; using System.Threading.Tasks; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Drawing.Processing; @@ -213,7 +214,31 @@ namespace VRCX oldPngFile.Dispose(); newPngFile.Dispose(); - File.Move(tempPath, path, true); + // check if file is in use and we have permission to write + for (var i = 0; i < 10; i++) + { + try + { + await using (File.Open(path, FileMode.Append, FileAccess.Write, FileShare.None)) + { + break; + } + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + await Task.Delay(1000); + } + } + try + { + File.Move(tempPath, path, true); + } + catch (Exception ex) + { + logger.Error(ex, "Failed to replace cropped print image"); + return false; + } + return true; } diff --git a/Dotnet/AppApi/Common/OVRToolkit.cs b/Dotnet/AppApi/Common/OVRToolkit.cs index 9b0b9f5e..4de9dce2 100644 --- a/Dotnet/AppApi/Common/OVRToolkit.cs +++ b/Dotnet/AppApi/Common/OVRToolkit.cs @@ -94,15 +94,19 @@ namespace VRCX { List messages = []; - byte[] imageBytes; - if(!string.IsNullOrWhiteSpace(image) && File.Exists(image)) + byte[] imageBytes = null; + try { - imageBytes = File.ReadAllBytes(image); + if (!string.IsNullOrWhiteSpace(image) && File.Exists(image)) + { + imageBytes = File.ReadAllBytes(image); + } } - else + catch (Exception ex) { - imageBytes = _vrcxIcon; + logger.Error(ex, "Failed to read OVRT notification image"); } + imageBytes ??= _vrcxIcon; if (wristNotification) {