Open bushrat011899 opened 2 weeks ago
Converting to draft as I have a rough implementation of this now working on WASM as well as desktop (mobile is not something I am equipped to test so that is still outside of my scope). WASM support is built on top of the Origin Private File System, which provides a file-level API in the browser. I have implemented an AssetReader
and an AssetWriter
which are suitable for use with the proposed temp://
asset source.
I'm marking this as draft because while I have these implementations working in the browser (tested on Firefox using temp_asset
example), I believe there is room for improvement in the exact implementations, and the potential for an AssetWatcher
as well for completeness.
I would encourage anyone with wasm-bindgen
experience (especially if you've worked with the OPFS API!) to leave any remarks as I will gladly accept and greatly appreciate any feedback!
Objective
Solution
Created a new
temp://
asset source which can be configured through theAssetPlugin
via a new optional fieldtemporary_file_path
. If a path is provided, it will be considered as a persistent temporary directory, otherwise a directory is created in a platform appropriate location which will be automatically deleted on application exit.Example Usage
A typical use for a temporary directory is the storage of data which you may need at some point, but don't right now (e.g., pre-fetching a map from a game server). In the below example, we create an asset
my_text_asset
, save it to thetemp://
source, and later retrieve it.Since the
temp://
source is on-disk, the unloaded asset will free memory for the rest of the application to use. In the above example, the memory used is trivial, but in a more complex game, a pre-fetched level could be significantly larger.Testing
Changelog
temp://
Asset Sourcepub temporary_file_path: Option<String>
toAssetPlugin
(defaults toNone
)temp_asset
, which demonstrates how to save and load arbitrary assets in the new temporary asset source.OriginPrivateFileSystem
for the WASM platform as anAssetReader
/AssetWriter
for the Origin Private File System APIMigration Guide
temp://
asset source ID, please use an alternate ID (e.g.,my_temp://
)AssetPlugin
manually, use..default()
to populate the new field with an appropriate default, or configure it yourself.