Refactor mjVFS and resource management.
Resource operations (e.g. mju_openResource, mju_readResource, and mju_closeResource, etc.) are now all handled by a VFS instance. It is now up to the VFS to determine which provider to use in order to handle those operations. This allows us to dynamically add/remove (aka "mount") providers to a VFS to handle special requests. mj_addFileVFS and mj_addBufferVFS have been reimplemented as two such use-cases. Moreover, we expose the mounting behaviour with two new functions: mj_mountVFS and mj_unmountVFS. PiperOrigin-RevId: 861550939 Change-Id: I070eb4bcc2982466c8f368f7918005538baa5185
This commit is contained in:
committed by
Copybara-Service
parent
e977b0d660
commit
0ddbb46fa1
@@ -27,6 +27,7 @@
|
||||
struct mjResource_ {
|
||||
char* name; // name of resource (filename, etc)
|
||||
void* data; // opaque data pointer
|
||||
mjVFS* vfs; // pointer to the VFS
|
||||
char timestamp[512]; // timestamp of the resource
|
||||
const struct mjpResourceProvider* provider; // pointer to the provider
|
||||
};
|
||||
@@ -42,6 +43,12 @@ typedef int (*mjfReadResource)(mjResource* resource, const void** buffer);
|
||||
// callback for closing a resource (responsible for freeing any allocated memory)
|
||||
typedef void (*mjfCloseResource)(mjResource* resource);
|
||||
|
||||
// callback for mounting a resource (provider), returns zero on failure
|
||||
typedef int (*mjfMountResource)(mjResource* resource);
|
||||
|
||||
// callback for unmounting a resource (provider), returns zero on failure
|
||||
typedef int (*mjfUnmountResource)(mjResource* resource);
|
||||
|
||||
// callback for checking if the current resource was modified from the time
|
||||
// specified by the timestamp
|
||||
// returns 0 if the resource's timestamp matches the provided timestamp
|
||||
@@ -55,6 +62,8 @@ struct mjpResourceProvider {
|
||||
mjfOpenResource open; // opening callback
|
||||
mjfReadResource read; // reading callback
|
||||
mjfCloseResource close; // closing callback
|
||||
mjfMountResource mount; // mounting callback (optional)
|
||||
mjfUnmountResource unmount; // unmounting callback (optional)
|
||||
mjfResourceModified modified; // resource modified callback (optional)
|
||||
void* data; // opaque data pointer (resource invariant)
|
||||
};
|
||||
|
||||
@@ -78,6 +78,13 @@ MJAPI extern const char* mjRNDSTRING[mjNRNDFLAG][3];
|
||||
// Initialize an empty VFS, mj_deleteVFS must be called to deallocate the VFS.
|
||||
MJAPI void mj_defaultVFS(mjVFS* vfs);
|
||||
|
||||
// Mount a ResourceProvider to handle file operations under the given path; return 0: success,
|
||||
// 2: repeated name, -1: invalid resource provider.
|
||||
MJAPI int mj_mountVFS(mjVFS* vfs, const char* filepath, const mjpResourceProvider* provider);
|
||||
|
||||
// Unmount a previously mounted ResourceProvider; return 0: success, -1: not found in VFS.
|
||||
MJAPI int mj_unmountVFS(mjVFS* vfs, const char* filename);
|
||||
|
||||
// Add file to VFS; return 0: success, 2: repeated name, -1: failed to load.
|
||||
MJAPI int mj_addFileVFS(mjVFS* vfs, const char* directory, const char* filename);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user