| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include "user/user_resource.h" |
| |
|
| | #include <errno.h> |
| | #include <sys/types.h> |
| | #include <sys/stat.h> |
| |
|
| | #include <cstddef> |
| | #include <cstdint> |
| | #include <cstdio> |
| | #include <cstring> |
| | #include <ctime> |
| | #include <string> |
| | #include <vector> |
| |
|
| | #if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) |
| | #include <unistd.h> |
| | #endif |
| |
|
| | #ifdef _WIN32 |
| | #define stat _stat |
| | #endif |
| |
|
| | #include <mujoco/mjplugin.h> |
| | #include "engine/engine_plugin.h" |
| | #include "engine/engine_util_misc.h" |
| | #include "user/user_util.h" |
| | #include "user/user_vfs.h" |
| |
|
| | namespace { |
| |
|
| | using mujoco::user::FileToMemory; |
| |
|
| | |
| | struct FileSpec { |
| | bool is_read; |
| | std::vector<uint8_t> buffer; |
| | time_t mtime; |
| | }; |
| |
|
| | |
| | int FileOpen(mjResource* resource, char* error, size_t nerror) { |
| | resource->provider = nullptr; |
| | resource->data = new FileSpec; |
| | const char* name = resource->name; |
| | FileSpec* spec = (FileSpec*) resource->data; |
| | spec->is_read = false; |
| | struct stat file_stat; |
| | if (stat(name, &file_stat) == 0) { |
| | memcpy(&spec->mtime, &file_stat.st_mtime, sizeof(time_t)); |
| | } else { |
| | if (error) { |
| | snprintf(error, nerror, "Error opening file '%s': %s", name, |
| | strerror(errno)); |
| | } |
| | return 0; |
| | } |
| | mju_encodeBase64(resource->timestamp, (uint8_t*) &spec->mtime, |
| | sizeof(time_t)); |
| | return 1; |
| | } |
| |
|
| | |
| | int FileRead(mjResource* resource, const void** buffer) { |
| | FileSpec* spec = (FileSpec*) resource->data; |
| |
|
| | |
| | if (!spec->is_read) { |
| | spec->buffer = FileToMemory(resource->name); |
| | spec->is_read = true; |
| | } |
| | *buffer = spec->buffer.data(); |
| | return spec->buffer.size(); |
| | } |
| |
|
| | |
| | void FileClose(mjResource* resource) { |
| | FileSpec* spec = (FileSpec*) resource->data; |
| | if (spec) delete spec; |
| | } |
| |
|
| | |
| | void FileGetDir(mjResource* resource, const char** dir, int* ndir) { |
| | *dir = resource->name; |
| | *ndir = mjuu_dirnamelen(resource->name); |
| | } |
| |
|
| | |
| | int FileModified(const mjResource* resource, const char*timestamp) { |
| | if (mju_isValidBase64(timestamp) != sizeof(time_t)) { |
| | return 1; |
| | } |
| |
|
| | time_t time1, time2; |
| | mju_decodeBase64((uint8_t*) &time1, timestamp); |
| | time2 = ((FileSpec*) resource->data)->mtime; |
| | double diff = difftime(time2, time1); |
| | if (diff < 0) return -1; |
| | if (diff > 0) return 1; |
| | return 0; |
| | } |
| |
|
| | } |
| |
|
| | |
| | |
| | mjResource* mju_openResource(const char* dir, const char* name, |
| | const mjVFS* vfs, char* error, size_t nerror) { |
| | |
| | if (error) { |
| | error[0] = '\0'; |
| | } |
| |
|
| | mjResource* resource = (mjResource*) mju_malloc(sizeof(mjResource)); |
| | if (resource == nullptr) { |
| | if (error) { |
| | strncpy(error, "could not allocate memory", nerror); |
| | error[nerror - 1] = '\0'; |
| | } |
| | return nullptr; |
| | } |
| |
|
| | |
| | memset(resource, 0, sizeof(mjResource)); |
| |
|
| | |
| | std::string fullname = mjuu_combinePaths(dir, name); |
| | std::size_t n = fullname.size(); |
| | resource->name = (char*) mju_malloc(sizeof(char) * (n + 1)); |
| | if (resource->name == nullptr) { |
| | if (error) { |
| | strncpy(error, "could not allocate memory", nerror); |
| | error[nerror - 1] = '\0'; |
| | } |
| | mju_closeResource(resource); |
| | return nullptr; |
| | } |
| |
|
| | |
| | if (vfs != nullptr) { |
| | memcpy(resource->name, name, |
| | sizeof(char) * (std::strlen(name) + 1)); |
| | const mjpResourceProvider* provider = GetVfsResourceProvider(); |
| | resource->data = (void*) vfs; |
| | resource->provider = provider; |
| | if (provider->open(resource)) { |
| | return resource; |
| | } |
| | } |
| |
|
| | |
| | memcpy(resource->name, fullname.c_str(), sizeof(char) * (n + 1)); |
| |
|
| | |
| | const mjpResourceProvider* provider = mjp_getResourceProvider(resource->name); |
| | if (provider != nullptr) { |
| | resource->provider = provider; |
| | resource->data = nullptr; |
| | if (provider->open(resource)) { |
| | return resource; |
| | } |
| |
|
| | if (error) { |
| | snprintf(error, nerror, "could not open '%s'" |
| | "using a resource provider matching prefix '%s'", |
| | resource->name, provider->prefix); |
| | } |
| |
|
| | mju_closeResource(resource); |
| | return nullptr; |
| | } |
| |
|
| | |
| | if (FileOpen(resource, error, nerror)) { |
| | return resource; |
| | } |
| | mju_closeResource(resource); |
| | return nullptr; |
| | } |
| |
|
| |
|
| |
|
| | |
| | void mju_closeResource(mjResource* resource) { |
| | if (resource == nullptr) { |
| | return; |
| | } |
| |
|
| | |
| | const mjpResourceProvider* provider = resource->provider; |
| | if (provider) { |
| | if (provider->close) provider->close(resource); |
| | } else { |
| | FileClose(resource); |
| | } |
| |
|
| | |
| | if (resource->name) mju_free(resource->name); |
| | mju_free(resource); |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | int mju_readResource(mjResource* resource, const void** buffer) { |
| | if (resource->provider) { |
| | return resource->provider->read(resource, buffer); |
| | } |
| |
|
| | |
| | return FileRead(resource, buffer); |
| | } |
| |
|
| |
|
| |
|
| | |
| | void mju_getResourceDir(mjResource* resource, const char** dir, int* ndir) { |
| | *dir = nullptr; |
| | *ndir = 0; |
| |
|
| | if (resource == nullptr) { |
| | return; |
| | } |
| |
|
| | const mjpResourceProvider* provider = resource->provider; |
| | if (provider) { |
| | if (provider->getdir) provider->getdir(resource, dir, ndir); |
| | } else { |
| | |
| | FileGetDir(resource, dir, ndir); |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | int mju_isModifiedResource(const mjResource* resource, const char* timestamp) { |
| | |
| | if (resource->provider) { |
| | if (resource->provider->modified) { |
| | return resource->provider->modified(resource, timestamp); |
| | } |
| | return 1; |
| | } |
| |
|
| | |
| | return FileModified(resource, timestamp); |
| | } |
| |
|