Ejemplo n.º 1
0
        /// <summary>
        /// Reads the file at the specified file path.
        /// </summary>
        /// <param name="path">The specified path to read from.</param>
        /// <param name="callback">Callback for Filekit to call when file is read.</param>
        public static void ReadFile(string path, FileReadComplete callback)
        {
#if BROWSER
            GCHandle filepathHandle = new GCHandle();
            try
            {
                Int32 filepathLength;
                filepathHandle = Common.NewUTF8String(path, out filepathLength);

                UInt32 size;
                var    fileSizeResult = trail_flk_get_file_size(
                    SDK.Raw,
                    filepathHandle.AddrOfPinnedObject(),
                    filepathLength,
                    out size
                    );

                if (size > int.MaxValue)
                {
                    callback.Invoke(Result.FLKFileTooBig, null, 0);
                    return;
                }

                if (fileSizeResult.IsError())
                {
                    callback.Invoke(fileSizeResult, null, 0);
                }

                var data = new byte[(int)size];

                var wrapper = new FileReadWrapper(data, (int)size, callback);
                trail_flk_read_file(
                    SDK.Raw,
                    filepathHandle.AddrOfPinnedObject(),
                    filepathLength,
                    wrapper.arrayHandle.AddrOfPinnedObject(),
                    size,
                    Marshal.GetFunctionPointerForDelegate(new ReadFileCB(FileKit.OnReadFileCB)),
                    GCHandle.ToIntPtr(GCHandle.Alloc(wrapper))
                    );
            }
            finally { filepathHandle.Free(); }
#else
            callback.Invoke(Result.InternalError, null, 0);
#endif
        }
Ejemplo n.º 2
0
        private static void OnReadFileCB(Result result, IntPtr data)
        {
            var             handle  = GCHandle.FromIntPtr(data);
            FileReadWrapper wrapper = null;

            try
            {
                wrapper = (FileReadWrapper)handle.Target;
                wrapper.Done(result);
            }
            finally
            {
                handle.Free();
                if (wrapper != null)
                {
                    wrapper.arrayHandle.Free();
                }
            }
        }