| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include "user/user_vfs.h" |
| |
|
| | #include <cstddef> |
| | #include <cstring> |
| | #include <cstdint> |
| | #include <cstdio> |
| | #include <cstdlib> |
| | #include <string> |
| | #include <unordered_map> |
| | #include <utility> |
| | #include <vector> |
| |
|
| | #include "engine/engine_util_misc.h" |
| | #include "user/user_util.h" |
| |
|
| | namespace { |
| |
|
| | using mujoco::user::FilePath; |
| | using mujoco::user::FileToMemory; |
| |
|
| | |
| | struct VFSFile { |
| | FilePath filename; |
| | std::vector<uint8_t> filedata; |
| | std::size_t filesize; |
| | uint64_t filestamp; |
| | }; |
| |
|
| | |
| | class VFS { |
| | public: |
| | |
| | bool HasFile(const FilePath& filename) const; |
| |
|
| | |
| | |
| | VFSFile* AddFile(const FilePath& filename, std::vector<uint8_t>&& buffer, |
| | uint64_t filestamp); |
| |
|
| | |
| | const VFSFile* GetFile(const FilePath& filename) const; |
| |
|
| | |
| | int DeleteFile(const FilePath& filename); |
| |
|
| | private: |
| | std::unordered_map<std::string, VFSFile> files_; |
| | }; |
| |
|
| | |
| | inline VFS* GetVFSImpl(const mjVFS* vfs) { |
| | return vfs->impl_ ? static_cast<VFS*>(vfs->impl_) : nullptr; |
| | } |
| |
|
| | |
| | FilePath StripPath(const char* filename) { |
| | return FilePath(filename).StripPath().Lower(); |
| | } |
| |
|
| | |
| | uint64_t vfs_memcpy(std::vector<uint8_t>& dest, const void* src, size_t n) { |
| | uint64_t hash = 0xcbf29ce484222325; |
| | uint64_t prime = 0x100000001b3; |
| | const uint8_t* bytes = (uint8_t*) src; |
| | for (size_t i = 0; i < n; i++) { |
| | dest.push_back(bytes[i]); |
| |
|
| | |
| | hash |= bytes[i]; |
| | hash *= prime; |
| | } |
| | return hash; |
| | } |
| |
|
| | |
| | uint64_t vfs_hash(const std::vector<uint8_t>& buffer) { |
| | uint64_t hash = 0xcbf29ce484222325; |
| | uint64_t prime = 0x100000001b3; |
| | const uint8_t* bytes = (uint8_t*) buffer.data(); |
| | std::size_t n = buffer.size(); |
| | for (std::size_t i = 0; i < n; i++) { |
| | hash |= bytes[i]; |
| | hash *= prime; |
| | } |
| | return hash; |
| | } |
| |
|
| | bool VFS::HasFile(const FilePath& filename) const { |
| | return files_.find(filename.Str()) != files_.end(); |
| | } |
| |
|
| | VFSFile* VFS::AddFile(const FilePath& filename, std::vector<uint8_t>&& buffer, |
| | uint64_t filestamp) { |
| | auto [it, inserted] = files_.insert({filename.Str(), VFSFile()}); |
| | if (!inserted) { |
| | return nullptr; |
| | } |
| | it->second.filename = filename; |
| | it->second.filedata = buffer; |
| | it->second.filestamp = filestamp; |
| | return &(it->second); |
| | } |
| |
|
| | const VFSFile* VFS::GetFile(const FilePath& filename) const { |
| | auto it = files_.find(filename.Str()); |
| | if (it == files_.end()) { |
| | return nullptr; |
| | } |
| | return &it->second; |
| | } |
| |
|
| | int VFS::DeleteFile(const FilePath& filename) { |
| | auto it = files_.find(filename.Str()); |
| | if (it == files_.end()) { |
| | return -1; |
| | } |
| | files_.erase(it); |
| | return 0; |
| | } |
| |
|
| | |
| | int Open(mjResource* resource) { |
| | if (!resource || !resource->name || !resource->data) { |
| | return 0; |
| | } |
| |
|
| | const mjVFS* vfs = (const mjVFS*) resource->data; |
| | const VFS* cvfs = GetVFSImpl(vfs); |
| | const VFSFile* file = cvfs->GetFile(StripPath(resource->name)); |
| | if (file == nullptr) { |
| | file = cvfs->GetFile(FilePath(resource->name)); |
| | if (file == nullptr) { |
| | return 0; |
| | } |
| | } |
| |
|
| | resource->data = (void*) file; |
| | resource->timestamp[0] = '\0'; |
| | if (file->filestamp) { |
| | mju_encodeBase64(resource->timestamp, (uint8_t*) &file->filestamp, |
| | sizeof(uint64_t)); |
| | } |
| | return 1; |
| | } |
| |
|
| | |
| | int Read(mjResource* resource, const void** buffer) { |
| | if (!resource || !resource->name || !resource->data) { |
| | *buffer = nullptr; |
| | return -1; |
| | } |
| |
|
| | const VFSFile* file = static_cast<const VFSFile*>(resource->data); |
| | if (file == nullptr) { |
| | *buffer = nullptr; |
| | return -1; |
| | } |
| |
|
| | *buffer = file->filedata.data(); |
| | return file->filedata.size(); |
| | } |
| |
|
| | |
| | void Close(mjResource* resource) { |
| | } |
| |
|
| | |
| | void GetDir(mjResource* resource, const char** dir, int* ndir) { |
| | *dir = (resource) ? resource->name : nullptr; |
| | *ndir = (resource) ? mjuu_dirnamelen(resource->name) : 0; |
| | } |
| |
|
| | |
| | |
| | int Modified(const mjResource* resource, const char* timestamp) { |
| | uint64_t filestamp; |
| | if (mju_isValidBase64(timestamp) > sizeof(uint64_t)) { |
| | return 2; |
| | } |
| |
|
| | mju_decodeBase64((uint8_t*) &filestamp, timestamp); |
| | if (!filestamp) return 3; |
| |
|
| | if (resource) { |
| | const VFSFile* file = static_cast<const VFSFile*>(resource->data); |
| | if (file == nullptr) return 4; |
| | if (!file->filestamp) return 5; |
| |
|
| | if (file->filestamp == filestamp) { |
| | return 0; |
| | } |
| | } |
| | return 1; |
| | } |
| |
|
| | } |
| |
|
| | |
| | void mj_defaultVFS(mjVFS* vfs) { |
| | vfs->impl_ = new VFS(); |
| | } |
| |
|
| | |
| | int mj_addFileVFS(mjVFS* vfs, const char* directory, const char* filename) { |
| | VFS* cvfs = GetVFSImpl(vfs); |
| |
|
| | |
| | FilePath fullname = FilePath(directory, filename); |
| |
|
| | |
| | FilePath newname = StripPath(filename); |
| |
|
| | |
| | if (cvfs->HasFile(newname)) { |
| | return 2; |
| | } |
| |
|
| | |
| | std::vector<uint8_t> buffer = FileToMemory(fullname.c_str()); |
| | if (buffer.empty()) { |
| | return -1; |
| | } |
| |
|
| | if (!cvfs->AddFile(newname, std::move(buffer), vfs_hash(buffer))) { |
| | return 2; |
| | } |
| | return 0; |
| | } |
| |
|
| | |
| | int mj_addBufferVFS(mjVFS* vfs, const char* name, const void* buffer, |
| | int nbuffer) { |
| | std::vector<uint8_t> inbuffer; |
| | VFS* cvfs = GetVFSImpl(vfs); |
| | VFSFile* file; |
| | if (!(file = cvfs->AddFile(FilePath(name), std::move(inbuffer), 0))) { |
| | return 2; |
| | } |
| | file->filedata.reserve(nbuffer); |
| | file->filestamp = vfs_memcpy(file->filedata, buffer, nbuffer); |
| | return 0; |
| | } |
| |
|
| | |
| | int mj_deleteFileVFS(mjVFS* vfs, const char* filename) { |
| | VFS* cvfs = GetVFSImpl(vfs); |
| | if (cvfs->DeleteFile(StripPath(filename))) { |
| | return cvfs->DeleteFile(FilePath(filename)); |
| | } |
| | return 0; |
| | } |
| |
|
| | |
| | void mj_deleteVFS(mjVFS* vfs) { |
| | if (vfs) { |
| | delete GetVFSImpl(vfs); |
| | } |
| | } |
| |
|
| | const mjpResourceProvider* GetVfsResourceProvider() { |
| | static mjpResourceProvider provider |
| | = { nullptr, &Open, &Read, &Close, &GetDir, &Modified, nullptr }; |
| | return &provider; |
| | } |
| |
|