From 28eae0dcb14da89d12b02eb53f5f55d897dc7da9 Mon Sep 17 00:00:00 2001 From: zeromus Date: Tue, 17 Nov 2015 17:26:03 -0600 Subject: [PATCH] Revert "Revert "common logic for tempfiles"" This reverts commit 5196fc6b70fb026a56e484ed255afac9196fe769. --- .../BizHawk.Client.Common.csproj | 1 - BizHawk.Client.Common/movie/bk2/StringLogs.cs | 4 +++- .../rewind/StreamBlobDatabase.cs | 4 +++- BizHawk.Client.EmuHawk/MainForm.Events.cs | 6 ++++-- BizHawk.Client.EmuHawk/OpenAdvancedChooser.cs | 4 ++-- BizHawk.Client.EmuHawk/Program.cs | 2 +- BizHawk.Common/BizHawk.Common.csproj | 1 + BizHawk.Common/InstanceDll.cs | 10 ++++----- .../TempFileManager.cs | 21 +++++++++++++++++-- 9 files changed, 37 insertions(+), 16 deletions(-) rename BizHawk.Client.Common/TempFileCleaner.cs => BizHawk.Common/TempFileManager.cs (60%) diff --git a/BizHawk.Client.Common/BizHawk.Client.Common.csproj b/BizHawk.Client.Common/BizHawk.Client.Common.csproj index 821672cd53..c35c7ad9d1 100644 --- a/BizHawk.Client.Common/BizHawk.Client.Common.csproj +++ b/BizHawk.Client.Common/BizHawk.Client.Common.csproj @@ -230,7 +230,6 @@ - diff --git a/BizHawk.Client.Common/movie/bk2/StringLogs.cs b/BizHawk.Client.Common/movie/bk2/StringLogs.cs index b11db3a210..355efdc9ef 100644 --- a/BizHawk.Client.Common/movie/bk2/StringLogs.cs +++ b/BizHawk.Client.Common/movie/bk2/StringLogs.cs @@ -5,6 +5,8 @@ using System.IO; using System.Linq; using System.Text; +using BizHawk.Common; + namespace BizHawk.Client.Common { public static class StringLogUtil @@ -62,7 +64,7 @@ namespace BizHawk.Client.Common FileStream stream; public DiskStringLog() { - var path = Path.Combine(Path.GetTempPath(), "bizhawk.disklist-pid" + System.Diagnostics.Process.GetCurrentProcess().Id + "-" + Guid.NewGuid()); + var path = TempFileCleaner.GetTempFilename("movieOnDisk"); stream = new FileStream(path, FileMode.Create, System.Security.AccessControl.FileSystemRights.FullControl, FileShare.None, 4 * 1024, FileOptions.DeleteOnClose); bw = new BinaryWriter(stream); br = new BinaryReader(stream); diff --git a/BizHawk.Client.Common/rewind/StreamBlobDatabase.cs b/BizHawk.Client.Common/rewind/StreamBlobDatabase.cs index 1ec2f9e157..50dbc74333 100644 --- a/BizHawk.Client.Common/rewind/StreamBlobDatabase.cs +++ b/BizHawk.Client.Common/rewind/StreamBlobDatabase.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.IO; +using BizHawk.Common; + namespace BizHawk.Client.Common { /// @@ -23,7 +25,7 @@ namespace BizHawk.Client.Common _mCapacity = capacity; if (onDisk) { - var path = Path.Combine(Path.GetTempPath(), "bizhawk.rewindbuf-pid" + System.Diagnostics.Process.GetCurrentProcess().Id + "-" + Guid.NewGuid()); + var path = TempFileCleaner.GetTempFilename("rewindbuf"); // I checked the DeleteOnClose operation to make sure it cleans up when the process is aborted, and it seems to. // Otherwise we would have a more complex tempfile management problem here. diff --git a/BizHawk.Client.EmuHawk/MainForm.Events.cs b/BizHawk.Client.EmuHawk/MainForm.Events.cs index 42ef3a00a7..72c9d8adb1 100644 --- a/BizHawk.Client.EmuHawk/MainForm.Events.cs +++ b/BizHawk.Client.EmuHawk/MainForm.Events.cs @@ -1185,7 +1185,7 @@ namespace BizHawk.Client.EmuHawk ThrottleMessage(); } - public void RunLibretroCoreChooser() + public bool RunLibretroCoreChooser() { var ofd = new OpenFileDialog(); @@ -1203,9 +1203,11 @@ namespace BizHawk.Client.EmuHawk ofd.Filter = "Libretro Cores (*.dll)|*.dll"; if (ofd.ShowDialog() == DialogResult.Cancel) - return; + return false; Global.Config.LibretroCore = ofd.FileName; + + return true; } private void setLibretroCoreToolStripMenuItem_Click(object sender, EventArgs e) diff --git a/BizHawk.Client.EmuHawk/OpenAdvancedChooser.cs b/BizHawk.Client.EmuHawk/OpenAdvancedChooser.cs index a1e6ee192e..c75d82be1a 100644 --- a/BizHawk.Client.EmuHawk/OpenAdvancedChooser.cs +++ b/BizHawk.Client.EmuHawk/OpenAdvancedChooser.cs @@ -51,8 +51,8 @@ namespace BizHawk.Client.EmuHawk private void btnSetLibretroCore_Click(object sender, EventArgs e) { - mainForm.RunLibretroCoreChooser(); - RefreshLibretroCore(false); + if(mainForm.RunLibretroCoreChooser()) + RefreshLibretroCore(false); } LibRetroEmulator.RetroDescription CurrentDescription; diff --git a/BizHawk.Client.EmuHawk/Program.cs b/BizHawk.Client.EmuHawk/Program.cs index 09a44a0c86..96c37c97c6 100644 --- a/BizHawk.Client.EmuHawk/Program.cs +++ b/BizHawk.Client.EmuHawk/Program.cs @@ -59,7 +59,7 @@ namespace BizHawk.Client.EmuHawk } } - BizHawk.Client.Common.TempFileCleaner.Start(); + BizHawk.Common.TempFileCleaner.Start(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); diff --git a/BizHawk.Common/BizHawk.Common.csproj b/BizHawk.Common/BizHawk.Common.csproj index 825675481d..eaeabf9830 100644 --- a/BizHawk.Common/BizHawk.Common.csproj +++ b/BizHawk.Common/BizHawk.Common.csproj @@ -76,6 +76,7 @@ + diff --git a/BizHawk.Common/InstanceDll.cs b/BizHawk.Common/InstanceDll.cs index 5f4ae2b744..aa02cd3230 100644 --- a/BizHawk.Common/InstanceDll.cs +++ b/BizHawk.Common/InstanceDll.cs @@ -9,7 +9,7 @@ namespace BizHawk.Common public InstanceDll(string dllPath) { //copy the dll to a temp directory - var path = Path.Combine(Path.GetTempPath(), "instancedll-pid" + System.Diagnostics.Process.GetCurrentProcess().Id + "-" + Guid.NewGuid()) + "-" + Path.GetFileName(dllPath); + var path = TempFileCleaner.GetTempFilename(string.Format("{0}", Path.GetFileNameWithoutExtension(dllPath)),".dll",false); using (var stream = new FileStream(path, FileMode.Create, System.Security.AccessControl.FileSystemRights.FullControl, FileShare.ReadWrite | FileShare.Delete, 4 * 1024, FileOptions.None)) using (var sdll = File.OpenRead(dllPath)) sdll.CopyTo(stream); @@ -21,13 +21,11 @@ namespace BizHawk.Common var envpath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process); try { - string envpath_new = Path.GetDirectoryName(path) + ";" + envpath; + string envpath_new = Path.GetDirectoryName(dllPath) + ";" + envpath; Environment.SetEnvironmentVariable("PATH", envpath_new, EnvironmentVariableTarget.Process); _hModule = LoadLibrary(path); //consider using LoadLibraryEx instead of shenanigans? - var newfname = Path.GetFileName(path); - newfname = "bizhawk.bizdelete-" + newfname; - var newpath = Path.Combine(Path.GetDirectoryName(path), newfname); - File.Move(path, newpath); + var newfname = TempFileCleaner.RenameTempFilenameForDelete(path); + File.Move(path, newfname); } finally { diff --git a/BizHawk.Client.Common/TempFileCleaner.cs b/BizHawk.Common/TempFileManager.cs similarity index 60% rename from BizHawk.Client.Common/TempFileCleaner.cs rename to BizHawk.Common/TempFileManager.cs index b6a7f0eda6..2c927f6ab5 100644 --- a/BizHawk.Client.Common/TempFileCleaner.cs +++ b/BizHawk.Common/TempFileManager.cs @@ -1,7 +1,7 @@ using System; using System.IO; -namespace BizHawk.Client.Common +namespace BizHawk.Common { /// /// Starts a thread which cleans any filenames in %temp% beginning with bizhawk.bizdelete. @@ -12,6 +12,23 @@ namespace BizHawk.Client.Common { //todo - manage paths other than %temp%, make not static, or allow adding multiple paths to static instance + public static string GetTempFilename(string friendlyname, string extension = null, bool delete = true) + { + string guidPart = Guid.NewGuid().ToString(); + var fname = string.Format("biz-{0}-{1}-{2}{3}", System.Diagnostics.Process.GetCurrentProcess().Id, friendlyname, guidPart, extension ?? ""); + if (delete) fname = RenameTempFilenameForDelete(fname); + return Path.Combine(Path.GetTempPath(), fname); + } + + public static string RenameTempFilenameForDelete(string path) + { + string filename = Path.GetFileName(path); + string dir = Path.GetDirectoryName(path); + if (!filename.StartsWith("biz-")) throw new InvalidOperationException(); + filename = "bizdelete-" + filename.Remove(0, 4); + return Path.Combine(dir, filename); + } + public static void Start() { lock (typeof(TempFileCleaner)) @@ -31,7 +48,7 @@ namespace BizHawk.Client.Common var di = new DirectoryInfo(Path.GetTempPath()); for (; ; ) { - var fis = di.GetFiles("bizhawk.bizdelete*"); + var fis = di.GetFiles("bizdelete-*"); foreach (var fi in fis) { try