This commit is contained in:
Asnivor 2018-09-13 10:44:58 +01:00
commit afc5d516b8
9 changed files with 72 additions and 19 deletions

View File

@ -140,7 +140,7 @@ namespace BizHawk.Client.Common
//handling of initial .. was removed (Path.GetFullPath can handle it) //handling of initial .. was removed (Path.GetFullPath can handle it)
//handling of file:// or file:\\ was removed (can Path.GetFullPath handle it? not sure) //handling of file:// or file:\\ was removed (can Path.GetFullPath handle it? not sure)
// all pad paths default to EXE // all bad paths default to EXE
return GetExeDirectoryAbsolute(); return GetExeDirectoryAbsolute();
} }
@ -461,5 +461,19 @@ namespace BizHawk.Client.Common
return entry; return entry;
} }
/// <summary>
/// Puts the currently configured temp path into the environment for use as actual temp directory
/// </summary>
public static void RefreshTempPath()
{
if (Global.Config.PathEntries.TempFilesFragment != "")
{
//TODO - BUG - needs to route through PathManager.MakeAbsolutePath or something similar, but how?
string target = Global.Config.PathEntries.TempFilesFragment;
BizHawk.Common.TempFileManager.HelperSetTempPath(target);
}
}
} }
} }

View File

@ -37,6 +37,7 @@ namespace BizHawk.Client.Common
{ {
PathEntries.ResolveWithDefaults(); PathEntries.ResolveWithDefaults();
HotkeyBindings.ResolveWithDefaults(); HotkeyBindings.ResolveWithDefaults();
PathManager.RefreshTempPath();
} }
// Core preference for generic file extension, key: file extension, value: a systemID or empty if no preference // Core preference for generic file extension, key: file extension, value: a systemID or empty if no preference

View File

@ -165,6 +165,8 @@ namespace BizHawk.Client.Common
public string GlobalRomFragment => Global.Config.PathEntries["Global", "ROM"].Path; public string GlobalRomFragment => Global.Config.PathEntries["Global", "ROM"].Path;
public string TempFilesFragment => Global.Config.PathEntries["Global", "Temp Files"].Path;
// this one is special // this one is special
public string GlobalBaseFragment => Global.Config.PathEntries["Global", "Base"].Path; public string GlobalBaseFragment => Global.Config.PathEntries["Global", "Base"].Path;
@ -184,6 +186,7 @@ namespace BizHawk.Client.Common
new PathEntry { System = "Global_NULL", SystemDisplayName = "Global", Type = "TAStudio states", Path = Path.Combine(".", "Movies", "TAStudio states"), Ordinal = 12 }, new PathEntry { System = "Global_NULL", SystemDisplayName = "Global", Type = "TAStudio states", Path = Path.Combine(".", "Movies", "TAStudio states"), Ordinal = 12 },
new PathEntry { System = "Global_NULL", SystemDisplayName = "Global", Type = "Multi-Disk Bundles", Path = Path.Combine(".", ""), Ordinal = 13 }, new PathEntry { System = "Global_NULL", SystemDisplayName = "Global", Type = "Multi-Disk Bundles", Path = Path.Combine(".", ""), Ordinal = 13 },
new PathEntry { System = "Global_NULL", SystemDisplayName = "Global", Type = "External Tools", Path = Path.Combine(".", "ExternalTools"), Ordinal = 14 }, new PathEntry { System = "Global_NULL", SystemDisplayName = "Global", Type = "External Tools", Path = Path.Combine(".", "ExternalTools"), Ordinal = 14 },
new PathEntry { System = "Global_NULL", SystemDisplayName = "Global", Type = "Temp Files", Path = "", Ordinal = 15 },
new PathEntry { System = "INTV", SystemDisplayName = "Intellivision", Type = "Base", Path = Path.Combine(".", "Intellivision"), Ordinal = 0 }, new PathEntry { System = "INTV", SystemDisplayName = "Intellivision", Type = "Base", Path = Path.Combine(".", "Intellivision"), Ordinal = 0 },
new PathEntry { System = "INTV", SystemDisplayName = "Intellivision", Type = "ROM", Path = ".", Ordinal = 1 }, new PathEntry { System = "INTV", SystemDisplayName = "Intellivision", Type = "ROM", Path = ".", Ordinal = 1 },

View File

@ -75,7 +75,7 @@ namespace BizHawk.Client.Common
_mDisk = disk; _mDisk = disk;
if (disk) if (disk)
{ {
var path = TempFileCleaner.GetTempFilename("movieOnDisk"); var path = TempFileManager.GetTempFilename("movieOnDisk");
stream = new FileStream(path, FileMode.Create, System.Security.AccessControl.FileSystemRights.FullControl, FileShare.None, 4 * 1024, FileOptions.DeleteOnClose); stream = new FileStream(path, FileMode.Create, System.Security.AccessControl.FileSystemRights.FullControl, FileShare.None, 4 * 1024, FileOptions.DeleteOnClose);
} }
else else

View File

@ -27,7 +27,7 @@ namespace BizHawk.Client.Common
_mCapacity = capacity; _mCapacity = capacity;
if (onDisk) if (onDisk)
{ {
var path = TempFileCleaner.GetTempFilename("rewindbuf"); var path = TempFileManager.GetTempFilename("rewindbuf");
// I checked the DeleteOnClose operation to make sure it cleans up when the process is aborted, and it seems to. // 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. // Otherwise we would have a more complex tempfile management problem here.

View File

@ -115,7 +115,7 @@ namespace BizHawk.Client.EmuHawk
} }
} }
BizHawk.Common.TempFileCleaner.Start(); BizHawk.Common.TempFileManager.Start();
HawkFile.ArchiveHandlerFactory = new SevenZipSharpArchiveHandler(); HawkFile.ArchiveHandlerFactory = new SevenZipSharpArchiveHandler();

View File

@ -308,6 +308,9 @@ namespace BizHawk.Client.EmuHawk
private void Ok_Click(object sender, EventArgs e) private void Ok_Click(object sender, EventArgs e)
{ {
SaveSettings(); SaveSettings();
PathManager.RefreshTempPath();
GlobalWin.OSD.AddMessage("Path settings saved"); GlobalWin.OSD.AddMessage("Path settings saved");
Close(); Close();
} }

View File

@ -12,7 +12,7 @@ namespace BizHawk.Common
public InstanceDll(string dllPath) public InstanceDll(string dllPath)
{ {
// copy the dll to a temp directory // copy the dll to a temp directory
var path = TempFileCleaner.GetTempFilename(string.Format("{0}", Path.GetFileNameWithoutExtension(dllPath)),".dll",false); var path = TempFileManager.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 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)) using (var sdll = File.OpenRead(dllPath))
sdll.CopyTo(stream); sdll.CopyTo(stream);
@ -32,7 +32,7 @@ namespace BizHawk.Common
var lastError = GetLastError(); var lastError = GetLastError();
throw new InvalidOperationException($"Failed to load plugin {path}, error code: 0x{lastError:X}"); throw new InvalidOperationException($"Failed to load plugin {path}, error code: 0x{lastError:X}");
} }
var newfname = TempFileCleaner.RenameTempFilenameForDelete(path); var newfname = TempFileManager.RenameTempFilenameForDelete(path);
File.Move(path, newfname); File.Move(path, newfname);
} }
finally finally

View File

@ -1,4 +1,6 @@
using System; using System;
using System.Linq;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
@ -10,7 +12,7 @@ namespace BizHawk.Common
/// Files shouldn't be named that unless they're safe to delete, but notably, they may stil be in use. That won't hurt this component. /// Files shouldn't be named that unless they're safe to delete, but notably, they may stil be in use. That won't hurt this component.
/// When they're no longer in use, this component will then be able to delete them. /// When they're no longer in use, this component will then be able to delete them.
/// </summary> /// </summary>
public static class TempFileCleaner public static class TempFileManager
{ {
// TODO - manage paths other than %temp%, make not static, or allow adding multiple paths to static instance // TODO - manage paths other than %temp%, make not static, or allow adding multiple paths to static instance
@ -41,7 +43,7 @@ namespace BizHawk.Common
public static void Start() public static void Start()
{ {
lock (typeof(TempFileCleaner)) lock (typeof(TempFileManager))
{ {
if (thread != null) if (thread != null)
{ {
@ -64,27 +66,49 @@ namespace BizHawk.Common
static void ThreadProc() static void ThreadProc()
{ {
var di = new DirectoryInfo(Path.GetTempPath()); //squirrely logic, trying not to create garbage
HashSet<string> knownTempDirs = new HashSet<string>();
List<DirectoryInfo> dis = new List<DirectoryInfo>();
for (;;) for (;;)
{ {
var fis = di.GetFiles("bizdelete-*"); lock (typeof(TempFileManager))
foreach (var fi in fis)
{ {
knownTempDirs.Add(Path.GetTempPath());
if (dis.Count != knownTempDirs.Count)
dis = knownTempDirs.Select(x => new DirectoryInfo(x)).ToList();
}
foreach(var di in dis)
{
FileInfo[] fis = null;
try try
{ {
// SHUT. UP. THE. EXCEPTIONS. fis = di.GetFiles("bizdelete-*");
#if WINDOWS
DeleteFileW(fi.FullName);
#else
fi.Delete();
#endif
} }
catch catch
{ {
} }
if(fis != null)
{
foreach (var fi in fis)
{
try
{
// SHUT. UP. THE. EXCEPTIONS.
#if WINDOWS
DeleteFileW(fi.FullName);
#else
fi.Delete();
#endif
}
catch
{
}
// try not to do more than one thing per frame // try not to do more than one thing per frame
Thread.Sleep(100); Thread.Sleep(100);
}
}
} }
// try not to slam the filesystem too hard, we dont want this to cause any hiccups // try not to slam the filesystem too hard, we dont want this to cause any hiccups
@ -97,5 +121,13 @@ namespace BizHawk.Common
} }
static Thread thread; static Thread thread;
public static void HelperSetTempPath(string path)
{
//yes... this is how we're doing it, for now, until it's proven to be troublesome
Directory.CreateDirectory(path);
Environment.SetEnvironmentVariable("TMP", path);
Environment.SetEnvironmentVariable("TEMP", path);
}
} }
} }