Skip to content

IL2Cpp free space checking #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> IsThereEnoughDiskSpaceFor(long bytes)
{
try
Expand All @@ -265,8 +271,15 @@ public async Task<bool> 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)
Expand Down