From 245551988c92523f81ec4d3e3cbf66c30902e3f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bia=C5=82as?= Date: Fri, 16 Feb 2024 17:14:38 +0100 Subject: [PATCH] IL2Cpp free space checking https://stackoverflow.com/questions/14465187/get-available-disk-free-space-for-a-given-path-on-windows?answertab=votes#tab-top --- .../SystemIODataService.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Platform/SystemIO/ModIO.Implementation.Platform/SystemIODataService.cs b/Platform/SystemIO/ModIO.Implementation.Platform/SystemIODataService.cs index 4b09ca0..6688cc3 100644 --- a/Platform/SystemIO/ModIO.Implementation.Platform/SystemIODataService.cs +++ b/Platform/SystemIO/ModIO.Implementation.Platform/SystemIODataService.cs @@ -252,6 +252,12 @@ public bool TryCreateParentDirectory(string path) return SystemIOWrapper.TryCreateParentDirectory(path, out Result _); } +#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN + [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] + [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] + static extern bool GetDiskFreeSpaceEx(string lpDirectoryName, out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes, out ulong lpTotalNumberOfFreeBytes); +#endif + public async Task IsThereEnoughDiskSpaceFor(long bytes) { try @@ -265,8 +271,15 @@ public async Task IsThereEnoughDiskSpaceFor(long bytes) #else FileInfo f = new FileInfo(PersistentDataRootDirectory); string drive = Path.GetPathRoot(f.FullName); +#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN + var success = GetDiskFreeSpaceEx(drive, out var freeBytesAvailable, out _, out _); + if (!success) + throw new IOException("Failed to check disk space"); + return (ulong)bytes < freeBytesAvailable; +#else var d = new DriveInfo(drive); return bytes < d.AvailableFreeSpace; +#endif #endif } catch(Exception e)