diff --git a/CompactGUI.Core/CompactGUI.Core.csproj b/CompactGUI.Core/CompactGUI.Core.csproj index f7dd71b..25ce4f9 100644 --- a/CompactGUI.Core/CompactGUI.Core.csproj +++ b/CompactGUI.Core/CompactGUI.Core.csproj @@ -14,7 +14,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CompactGUI.Core/Estimator.cs b/CompactGUI.Core/Estimator.cs index b482a98..d0374fe 100644 --- a/CompactGUI.Core/Estimator.cs +++ b/CompactGUI.Core/Estimator.cs @@ -159,7 +159,7 @@ private float EstimateCompressabilityHDD(FileStream input, long fileSize, Compre unsafe long GetFirstLcn(string fileName) { - SafeFileHandle handle = File.OpenHandle(fileName); + using SafeFileHandle handle = File.OpenHandle(fileName); if (handle.IsInvalid) throw new IOException("Failed to open file handle for " + fileName); @@ -172,7 +172,7 @@ unsafe long GetFirstLcn(string fileName) uint bytesReturned = 0; var result = PInvoke.DeviceIoControl( - handle, + (Windows.Win32.Foundation.HANDLE)handle.DangerousGetHandle(), NTFSInterop.FSCTL_GET_RETRIEVAL_POINTERS, &inBuffer, inBufferSize, @@ -181,6 +181,8 @@ unsafe long GetFirstLcn(string fileName) &bytesReturned, null); + GC.KeepAlive(handle); + if (!result) return long.MaxValue; int extentOffset = Marshal.OffsetOf("Extents").ToInt32(); @@ -193,4 +195,4 @@ unsafe long GetFirstLcn(string fileName) -} +} \ No newline at end of file diff --git a/CompactGUI.Core/SharedMethods.cs b/CompactGUI.Core/SharedMethods.cs index 74bca18..ae0f435 100644 --- a/CompactGUI.Core/SharedMethods.cs +++ b/CompactGUI.Core/SharedMethods.cs @@ -150,17 +150,14 @@ private static string GetShortPath(string filePath) - public static unsafe uint GetClusterSize(string folderPath) + public static uint GetClusterSize(string folderPath) { - UInt32 lpSectorsPerCluster; - UInt32 lpBytesPerSector; - PInvoke.GetDiskFreeSpace( new DirectoryInfo(folderPath).Root.ToString(), - &lpSectorsPerCluster, - &lpBytesPerSector, - null, - null + out UInt32 lpSectorsPerCluster, + out UInt32 lpBytesPerSector, + out _, + out _ ); return lpSectorsPerCluster * lpBytesPerSector; @@ -168,10 +165,9 @@ public static unsafe uint GetClusterSize(string folderPath) } - public static unsafe long GetFileSizeOnDisk(string file) + public static long GetFileSizeOnDisk(string file) { - uint highOrder; - uint lowOrder = PInvoke.GetCompressedFileSize(file, &highOrder); + uint lowOrder = PInvoke.GetCompressedFileSize(file, out uint highOrder); if (lowOrder == 0xFFFFFFFF && (Marshal.GetLastWin32Error() != 0)) return -1; return ((long)highOrder << 32) | lowOrder; } @@ -300,4 +296,4 @@ public static bool IsDirectStorageGameFolder(string folderPath) } -} +} \ No newline at end of file diff --git a/CompactGUI.Core/Uncompactor.cs b/CompactGUI.Core/Uncompactor.cs index e28b68d..db33830 100644 --- a/CompactGUI.Core/Uncompactor.cs +++ b/CompactGUI.Core/Uncompactor.cs @@ -147,7 +147,7 @@ private unsafe FileOperationResult WOFDecompressFile(string file) using (SafeFileHandle fs = File.OpenHandle(file)) { bool succeeded = PInvoke.DeviceIoControl( - fs, + (Windows.Win32.Foundation.HANDLE)fs.DangerousGetHandle(), WOFHelper.FSCTL_DELETE_EXTERNAL_BACKING, null, 0, @@ -156,6 +156,8 @@ private unsafe FileOperationResult WOFDecompressFile(string file) null, null); + GC.KeepAlive(fs); + if (succeeded) return new FileOperationResult(true); int errorCode = Marshal.GetLastWin32Error(); @@ -205,4 +207,4 @@ public void Dispose() -} +} \ No newline at end of file diff --git a/CompactGUI.Core/WOFHelper.cs b/CompactGUI.Core/WOFHelper.cs index 0581f63..32fb525 100644 --- a/CompactGUI.Core/WOFHelper.cs +++ b/CompactGUI.Core/WOFHelper.cs @@ -102,7 +102,7 @@ public static unsafe WOFCompressionAlgorithm DetectCompression(FileInfo fileInfo WOFHelper.WOF_FILE_COMPRESSION_INFO_V1 info; uint buffer = 8; - var ret = PInvoke.WofIsExternalFile(fileInfo.FullName, &isExternalFile, &provider, &info, &buffer); + var ret = PInvoke.WofIsExternalFile(fileInfo.FullName, out isExternalFile, out provider, &info, ref buffer); WOFCompressionAlgorithm algorithm = (WOFCompressionAlgorithm)info.Algorithm; @@ -112,4 +112,4 @@ public static unsafe WOFCompressionAlgorithm DetectCompression(FileInfo fileInfo return algorithm; } -} +} \ No newline at end of file