[CLI] Fix deleting of temp files

This commit is contained in:
VaDiM
2025-09-01 22:19:48 +03:00
parent 963cd6546b
commit 355c99c034
4 changed files with 48 additions and 1 deletions

View File

@ -196,7 +196,7 @@ namespace AssetStudio
var hash = path.GetHashCode();
path = Path.Combine(tempDir, $"{filename}_{hash:X}");
}
return new FileStream(path + ".temp", FileMode.Create, FileAccess.ReadWrite, FileShare.Read, 4096, FileOptions.DeleteOnClose);
return new TempFileStream(path + ".temp", FileMode.Create);
}
private Stream ReadBlocksAndDirectory(FileReader reader)

View File

@ -0,0 +1,40 @@
using System.IO;
using System.Runtime.InteropServices;
namespace AssetStudio
{
public class TempFileStream : FileStream
{
private readonly string _tempFilePath;
private bool _disposed;
public TempFileStream(string path, FileMode fileMode, FileAccess fileAccess = FileAccess.ReadWrite, FileShare fileShare = FileShare.Read, int bufferSize = 4096)
: base(path, fileMode, fileAccess, fileShare, bufferSize, FileOptions.DeleteOnClose)
{
_tempFilePath = path;
}
public override void Close()
{
Dispose(true);
}
protected override void Dispose(bool disposing)
{
if (_disposed)
return;
base.Dispose(disposing);
if (disposing)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && File.Exists(_tempFilePath))
{
File.Delete(_tempFilePath);
}
_disposed = true;
}
}
}
}

View File

@ -73,6 +73,7 @@ namespace AssetStudioCLI
}
finally
{
Studio.Clear();
cliLogger.LogToFile(LoggerEvent.Verbose, "---Program ended---");
}
}

View File

@ -1303,5 +1303,11 @@ namespace AssetStudioCLI
"Nothing exported.";
Logger.Default.Log(LoggerEvent.Info, status, ignoreLevel: true);
}
public static void Clear()
{
assetsManager.Clear();
assemblyLoader.Clear();
}
}
}