From 78f42d9c9ae7d43bde0ee30aa7acf1f2c10a2ba7 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sun, 11 Apr 2021 14:28:48 +0200 Subject: [PATCH] Return result of directory creation, so that the game may handle creation errors --- Assets/Scripts/Modules/SystemModule.Interop.cs | 6 +++--- Assets/Scripts/Modules/SystemModule.cs | 12 ++++++++---- engine/UniQuake/sys_uniquake.c | 9 +++------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Assets/Scripts/Modules/SystemModule.Interop.cs b/Assets/Scripts/Modules/SystemModule.Interop.cs index 07bcdd1..8100ce4 100644 --- a/Assets/Scripts/Modules/SystemModule.Interop.cs +++ b/Assets/Scripts/Modules/SystemModule.Interop.cs @@ -151,11 +151,11 @@ public partial class SystemModule: CallbackHandler } [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - private delegate void SysMkDirCallback(IntPtr target, [MarshalAs(UnmanagedType.LPStr)] string path); + private delegate bool SysMkDirCallback(IntPtr target, [MarshalAs(UnmanagedType.LPStr)] string path); [MonoPInvokeCallback(typeof(SysMkDirCallback))] - private static void Callback_SysMkDir(IntPtr target, string path) + private static bool Callback_SysMkDir(IntPtr target, string path) { - GetSelf(target).MkDir(path); + return GetSelf(target).MkDir(path); } } diff --git a/Assets/Scripts/Modules/SystemModule.cs b/Assets/Scripts/Modules/SystemModule.cs index ef33350..4b24010 100644 --- a/Assets/Scripts/Modules/SystemModule.cs +++ b/Assets/Scripts/Modules/SystemModule.cs @@ -70,6 +70,8 @@ public partial class SystemModule private int FileOpenWrite(string path) { + // TODO: we could use this method to redirect user config & save data to a platform-specific user storage + int i = FindFileHandle(); try { @@ -155,15 +157,17 @@ public partial class SystemModule return File.Exists(path) ? 1 : -1; } - private void MkDir(string path) + private bool MkDir(string path) { try { - Directory.CreateDirectory(path); + var info = Directory.CreateDirectory(path); + return info.Exists; } - catch + catch (Exception ex) { - Debug.LogWarning($"Could not create directory: {path}"); + Debug.LogWarning($"Could not create directory {path}, reason: {ex.Message}"); + return false; } } } diff --git a/engine/UniQuake/sys_uniquake.c b/engine/UniQuake/sys_uniquake.c index 7b4b72f..deb4b9b 100644 --- a/engine/UniQuake/sys_uniquake.c +++ b/engine/UniQuake/sys_uniquake.c @@ -18,7 +18,7 @@ typedef struct unity_syscalls_s int(*SysFileRead)(void *target, int handle, void *dest, int count); int(*SysFileWrite)(void *target, int handle, const void *data, int count); int(*SysFileTime)(void *target, const char *path); - void(*SysMkDir)(void *target, const char *path); + qboolean(*SysMkDir)(void *target, const char *path); } unity_syscalls_t; const unity_syscalls_t *unity_syscalls; @@ -94,11 +94,8 @@ int Sys_FileTime(const char *path) void Sys_mkdir(const char *path) { - unity_syscalls->SysMkDir(unity_syscalls->target, path); - - // TODO: handle creation errors - /*if (GetLastError() != ERROR_ALREADY_EXISTS) - Sys_Error("Unable to create directory %s", path);*/ + if (!unity_syscalls->SysMkDir(unity_syscalls->target, path)) + Sys_Error("Unable to create directory %s", path); } void Sys_Sleep_New(unsigned long msecs)