Add VFS handling logic to mj_openResource.

PiperOrigin-RevId: 653124909
Change-Id: I4d031eb0bfb0070f8493753d2aeaac60ac283b85
This commit is contained in:
Kyle Bayes
2024-07-17 00:40:20 -07:00
committed by Copybara-Service
parent 7f46e936f6
commit 2cf352c2e6
13 changed files with 244 additions and 247 deletions
+2 -2
View File
@@ -47,7 +47,7 @@ void CacheText(mjCCache& cache, const std::string& model,
mjVFS vfs;
mj_defaultVFS(&vfs);
mj_addBufferVFS(&vfs, name.c_str(), text.data(), text.size());
mjResource* resource = mju_openVfsResource(name.c_str(), &vfs);
mjResource* resource = mju_openResource(name.c_str(), &vfs, nullptr, 0);
std::shared_ptr<const void> data(&text, +[](const void* data) {});
cache.Insert(model, resource, data, text.size());
mju_closeResource(resource);
@@ -61,7 +61,7 @@ GetCachedText(mjCCache& cache, const std::string& model,
mjVFS vfs;
mj_defaultVFS(&vfs);
mj_addBufferVFS(&vfs, name.c_str(), text.data(), std::strlen(text.c_str()));
mjResource* resource = mju_openVfsResource(name.c_str(), &vfs);
mjResource* resource = mju_openResource(name.c_str(), &vfs, nullptr, 0);
bool inserted = cache.PopulateData(resource,
[&cached_text](const void* data) {
cached_text = *(static_cast<const std::string*>(data));
+7 -6
View File
@@ -233,7 +233,7 @@ TEST_F(ResourceTest, GeneralTest) {
EXPECT_GT(i, 0);
// open resource
mjResource* resource = mju_openResource("str:file", nullptr, 0);
mjResource* resource = mju_openResource("str:file", nullptr, nullptr, 0);
ASSERT_THAT(resource, NotNull());
const char* buffer = NULL;
@@ -256,7 +256,7 @@ TEST_F(ResourceTest, GeneralFailureTest) {
static std::array<char, 1024> error;
// open resource
mjResource* resource = mju_openResource("str:notfound",
mjResource* resource = mju_openResource("str:notfound", nullptr,
error.data(), error.size());
ASSERT_THAT(resource, IsNull());
@@ -281,7 +281,7 @@ TEST_F(ResourceTest, NameWithValidPrefix) {
};
// open resource
mjResource* resource = mju_openResource("nop:found", nullptr, 0);
mjResource* resource = mju_openResource("nop:found", nullptr, nullptr, 0);
ASSERT_THAT(resource, NotNull());
mju_closeResource(resource);
}
@@ -304,7 +304,7 @@ TEST_F(ResourceTest, NameWithUpperCasePrefix) {
};
// open resource
mjResource* resource = mju_openResource("NOP:found", nullptr, 0);
mjResource* resource = mju_openResource("NOP:found", nullptr, nullptr, 0);
ASSERT_THAT(resource, NotNull());
mju_closeResource(resource);
}
@@ -327,7 +327,7 @@ TEST_F(ResourceTest, NameWithInvalidPrefix) {
};
// open resource
mjResource* resource = mju_openResource("nopfound", nullptr, 0);
mjResource* resource = mju_openResource("nopfound", nullptr, nullptr, 0);
ASSERT_THAT(resource, IsNull());
}
@@ -338,7 +338,8 @@ TEST_F(ResourceTest, OSFilesystemTimestamps) {
const char* const file = "engine/testdata/collision_box/boxbox_deep.xml";
const std::string xml_path = GetTestDataFilePath(file);
mjResource* resource = mju_openResource(xml_path.c_str(), nullptr, 0);
mjResource* resource = mju_openResource(xml_path.c_str(), nullptr,
nullptr, 0);
mju_decodeBase64((uint8_t*) &t, resource->timestamp);
// equal timestamps
+2 -2
View File
@@ -30,7 +30,7 @@ using ::testing::NotNull;
using UserVfsTest = MujocoTest;
static bool HasFile(const mjVFS* vfs, const std::string& filename) {
mjResource* resource = mju_openVfsResource(filename.c_str(), vfs);
mjResource* resource = mju_openResource(filename.c_str(), vfs, nullptr, 0);
bool result = resource != nullptr;
mju_closeResource(resource);
return result;
@@ -222,7 +222,7 @@ TEST_F(UserVfsTest, Timestamps) {
mj_defaultVFS(&vfs);
mj_addBufferVFS(&vfs, "cube.obj", cube, sizeof(cube));
mjResource* resource = mju_openVfsResource("cube.obj", &vfs);
mjResource* resource = mju_openResource("cube.obj", &vfs, nullptr, 0);
// same timestamps
EXPECT_EQ(mju_isModifiedResource(resource, resource->timestamp), 0);