File I/O access from Chrome Native Client

With the latest release of Native Client, it now supports disk I/O. This I/O is different than System.File.IO in that it’s asynchronous, more analogous to WWW in Unity terminology. From File I/O - Chrome for Developers

With either of the techniques
described above, you can imagine that
file IO does not lend itself to a
blocking model. As a result, functions
like fread/fwrite don’t work as
intended. To handle file IO in Native
Client you must adjust your file
manager paradigm to an asynchronous
model.

My question is: is there any way to access this functionality from a Unity project compiled for NaCl? Google does provide an example C++ read/write class at http://code.google.com/p/nativeclient-sdk/source/browse/trunk/src/examples/pong/pong.cc, but I have zero experience with C++. We can’t just compile this code to a plugin, because as Unity says: “As with the standard webplayer plugin, native C/C++ plugins are not currently supported by NaCl.”

This is not a question about if it’s possible or not. It just doesn’t allow you to access local files! All Unity players (standalone / webplayer / Android) are written in native code. In this case there’s no problem for the player itself to access everything on the clients PC. The point is that unity created games / apps that are played in the Webplayer aren’t allowed to access files.

The Unity3d Webplayer is a trusted piece of software which has to be installed on the clients PC. A game embedded in a website isn’t trusted software.

There is no way for content that runs in the Unity webplayer to access local files. There is, will and should be no way to do something like that.

The best i could think of if some kind of drag and drop support, so the user fully controls what files can be read. Here’s a feature request for this functionality

It would be helpful if Unity would post a technical page that explains exactly which NaCl features can be leveraged in Unity and some sample code of how to leverage them.

Being able to read/write from disk is a very powerful NaCl feature and would be extremely useful to Unity developers.

I posted this to the forums as well, and a Unity Developer was kind enough to provide an answer:

There is currently no native way to do
that in Unity with NaCl. I don’t
expect that we will directly expose
NaCl FileIO APIs any time soon, as
they don’t map well to existing
File.IO functionality (as you
described), and exposing a specific
NaCl File IO class seems to be too
special cased. I think the preferred
way for us to expose this would be to
add the ability to load plugin DLLs in
NaCl, but that is unlikely to happen
in the short term either (I think that
there are more interesting features to
be added to NaCl at this point).

So, right now your only option would
indeed be using HTML5 file storage
using js. From what I can tell with my
limited knowledge of HTML5 file
storage, this should generally be
possible, though you would probably
have to encode the data as text to
submit it to Unity.

You can read the whole thing here:

http://forum.unity3d.com/threads/124820-File-I-O-in-Native-Client