Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Platform.ReadFileBytes(filename) and Assets dir #902

Open
paulmelis opened this issue Mar 14, 2024 · 4 comments
Open

Platform.ReadFileBytes(filename) and Assets dir #902

paulmelis opened this issue Mar 14, 2024 · 4 comments
Milestone

Comments

@paulmelis
Copy link
Contributor

Description

The docs for the filename argument of Platform.ReadFileBytes(string filename) say "Path to the file. Not affected by Assets folder path".

I'm not sure what the latter part of that sentence implies, but apparently I need to use different paths to the same file, depending on the platform (and so needs a conditional in the code):

  • Magic Leap 2: ReadFileBytes("myfile.bin")
  • Simulator on PC (Windows and Linux): ReadFileBytes("Assets/myfile.bin")

Is this the expected behaviour, or should it be possible to have the same path in all cases?

Platform / Environment

0.3.9

@maluoi
Copy link
Collaborator

maluoi commented Mar 14, 2024

This is actually the expected behavior! The "Assets folder path" is a trick used to help prevent needing different file names on different platforms, which is why file names behave fine for Assets. But for a function as generic as Platform.ReadFileBytes, this trick could be problematic, so I didn't add it here.

The main thing here is that on Android, myfile.bin is not exactly a file, it's an asset that's packed in your APK. I don't believe it's accessible by regular file IO functions! Instead, Android uses the AssetManager to read these, and they "aren't in a folder", which is why you just use the filename here instead of the "Assets/" path. Platform.ReadFileBytes does include calls to the AssetManager to enable you to read these "assets, not files", but also calls to regular file IO functions to read those too.

It's definitely confusing, and I do believe Platform.ReadFileBytes is due for some revision. I just haven't decided exactly how to revise this in a way that isn't also confusing in other contexts! Maybe I'm overthinking it and the solution is not all that complicated 😅 I dunno! I'll try and take a look at this soon.

@paulmelis
Copy link
Contributor Author

The main thing here is that on Android, myfile.bin is not exactly a file, it's an asset that's packed in your APK. I don't believe it's accessible by regular file IO functions!

Right, I figured something like this 😄

It's definitely confusing, and I do believe Platform.ReadFileBytes is due for some revision. I just haven't decided exactly how to revise this in a way that isn't also confusing in other contexts!

Perhaps having a renamed/dedicated ReadAssetFileBytes(filename) would be an option, to at least take away the confusion on what exactly can be read and how. This call would always only read the contents of a "file" directly under the Assets dir, so can't navigate to a higher relative path.

Apart from that could be a call to read a regular file, where filename is some form of platform-native path. That at least still provides an abstraction for file I/O over the underlying platform, which can be a nice thing.

@maluoi
Copy link
Collaborator

maluoi commented Mar 15, 2024

It looks like I have actually put some thought into this one previously, #771 😅

@maluoi maluoi added this to the v0.3.10 milestone Apr 25, 2024
@maluoi
Copy link
Collaborator

maluoi commented Apr 25, 2024

This is done in #960, will try and add this to a 0.3.10 soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Review
Development

No branches or pull requests

2 participants