From 729795ff9e0f79fe582ae2a40c682c771f4d2b7e Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Wed, 5 Jun 2024 00:29:15 +0900 Subject: [PATCH] Move temp file gen for File.Replace to separate file --- .../Textures/Internal/TextureManager.Wic.cs | 2 +- Dalamud/Utility/Util.cs | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Dalamud/Interface/Textures/Internal/TextureManager.Wic.cs b/Dalamud/Interface/Textures/Internal/TextureManager.Wic.cs index 40b9d3ee0..3c93ba875 100644 --- a/Dalamud/Interface/Textures/Internal/TextureManager.Wic.cs +++ b/Dalamud/Interface/Textures/Internal/TextureManager.Wic.cs @@ -90,7 +90,7 @@ internal sealed partial class TextureManager throw new NullReferenceException($"{nameof(path)} cannot be null."); using var wrapAux = new WrapAux(wrap, true); - var pathTemp = $"{path}.{GetCurrentThreadId():X08}{Environment.TickCount64:X16}.tmp"; + var pathTemp = Util.GetTempFileNameForFileReplacement(path); var trashfire = new List(); try { diff --git a/Dalamud/Utility/Util.cs b/Dalamud/Utility/Util.cs index 2f6b4fb4a..112427cf0 100644 --- a/Dalamud/Utility/Util.cs +++ b/Dalamud/Utility/Util.cs @@ -640,6 +640,30 @@ public static class Util throw new Win32Exception(); } + /// Gets a temporary file name, for use as the sourceFileName in + /// . + /// The target file. + /// A temporary file name that should be usable with . + /// + /// No write operation is done on the filesystem. + public static string GetTempFileNameForFileReplacement(string targetFile) + { + Span buf = stackalloc byte[9]; + Random.Shared.NextBytes(buf); + for (var i = 0; ; i++) + { + var tempName = + Path.GetFileName(targetFile) + + Convert.ToBase64String(buf) + .TrimEnd('=') + .Replace('+', '-') + .Replace('/', '_'); + var tempPath = Path.Join(Path.GetDirectoryName(targetFile), tempName); + if (i >= 64 || !Path.Exists(tempPath)) + return tempPath; + } + } + /// /// Gets a random, inoffensive, human-friendly string. ///