Browse Source

File Read/Write to byte arrays support

Josh Engebretson 9 years ago
parent
commit
eeb1984cf6
2 changed files with 93 additions and 0 deletions
  1. 79 0
      Script/AtomicNET/AtomicNET/IO/File.cs
  2. 14 0
      Source/AtomicNET/NETNative/NETCInterop.cpp

+ 79 - 0
Script/AtomicNET/AtomicNET/IO/File.cs

@@ -0,0 +1,79 @@
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace AtomicEngine
+{
+    public partial class File : AObject, Deserializer, Serializer
+    {
+
+        [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
+        internal static extern uint csi_Atomic_File_Read(IntPtr self, IntPtr dest, uint size);
+
+
+        /// <summary>
+        /// Read bytes from the file. Return array of bytes of the length actually read (can be 0 length)
+        /// </summary>
+        unsafe public byte[] Read(int size)
+        {
+            byte[] bytes = null;
+            byte[] buffer = new byte[size];
+            int sizeRead = 0;
+
+            fixed (byte* p = buffer)
+            {
+                IntPtr ptr = (IntPtr)p;
+
+                sizeRead = (int)csi_Atomic_File_Read(nativeInstance, ptr, (uint)size);
+
+                bytes = new byte[sizeRead];
+
+                if (sizeRead > 0)
+                {
+                    Marshal.Copy(ptr, bytes, 0, (int)sizeRead);
+                }
+            }
+
+            return bytes;
+
+        }
+
+        [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
+        internal static extern uint csi_Atomic_File_Write(IntPtr self, IntPtr data, uint offset, uint size);
+
+        /// <summary>
+        /// Write bytes to the file, with optional offset into array and count of bytes to write. 
+        /// Return number of bytes actually written.
+        /// </summary>
+        unsafe public int Write(byte[] bytes, int offset = 0, int count = 0)
+        {
+            if (bytes == null || bytes.Length == 0)
+                return 0;
+
+            if (count == 0)
+                count = bytes.Length;
+
+            if (offset >= bytes.Length)
+                return 0;
+
+            // should this truncate?
+            if ((offset + count) > bytes.Length)
+                return 0;
+
+            int bytesWritten = 0;
+
+            fixed (byte* p = bytes)
+            {
+                IntPtr ptr = (IntPtr)p;
+                bytesWritten = (int)csi_Atomic_File_Write(nativeInstance, ptr, (uint) offset, (uint)count);
+            }
+
+            return bytesWritten;
+
+        }
+
+
+    };
+
+}
+

+ 14 - 0
Source/AtomicNET/NETNative/NETCInterop.cpp

@@ -578,6 +578,20 @@ namespace Atomic
             return self->SetKeyFrame(time, variant->GetVariant());
         }
 
+        // File
+
+        ATOMIC_EXPORT_API unsigned csi_Atomic_File_Read(File* self, void* dest, unsigned size)
+        {
+            return self->Read(dest, size);
+        }
+
+        ATOMIC_EXPORT_API unsigned csi_Atomic_File_Write(File* self, void* data, unsigned offset, unsigned size)
+        {
+            unsigned char* bytes = (unsigned char*) data;
+            bytes += offset;
+            return self->Write(bytes, size);
+        }
+
 
 #ifdef ATOMIC_PLATFORM_IOS
         ATOMIC_EXPORT_API void SDL_IOS_Init(const char *resourceDir, const char *documentsDir)