From 18397e61ecbc23f817229e909094d536f5705f7f Mon Sep 17 00:00:00 2001 From: Kyle Bayes Date: Tue, 4 Jun 2024 08:49:18 -0700 Subject: [PATCH] Replace calls to mj_makeEmptyFileVFS with mj_addBufferVFS in MuJoCo Unity bindings. PiperOrigin-RevId: 640168011 Change-Id: Ic592b5cbedb89981ac24d4b8d2c864ceaf415e46 --- unity/Runtime/Tools/MjVfs.cs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/unity/Runtime/Tools/MjVfs.cs b/unity/Runtime/Tools/MjVfs.cs index f46a9a88..a52da4b2 100644 --- a/unity/Runtime/Tools/MjVfs.cs +++ b/unity/Runtime/Tools/MjVfs.cs @@ -47,17 +47,16 @@ public sealed class MjVfs : IDisposable { // Adds a new file to the virtual filesystem. public unsafe void AddFile(string filename, string contents) { - var result = mj_makeEmptyFileVFS(_unmanagedVfs.ToPointer(), filename, contents.Length); - if (result != 0) { - throw new Exception( - "VFS error (" + result + ") encountered while creating an empty file"); - } - var fileIndex = mj_findFileVFS(_unmanagedVfs.ToPointer(), filename); - if (fileIndex < 0) { - throw new IndexOutOfRangeException("VFS didn't properly create the empty file."); - } var contents_bytes = Encoding.UTF8.GetBytes(contents); - Marshal.Copy(contents_bytes, 0, Data.filedata[fileIndex], contents_bytes.Length); + fixed (byte* bytes = contents_bytes) + { + IntPtr ptr = (IntPtr) bytes; + var result = mj_addBufferVFS(_unmanagedVfs.ToPointer(), filename, ptr.ToPointer(), + contents_bytes.Length); + if (result != 0) { + throw new Exception("VFS error (" + result + ") encountered while creating an empty file"); + } + } } // Searches the VFS for the specified file and returns its index.