Make mju_getXMLDependencies read XMLs through the resource provider.
The function called tinyxml2's LoadFile directly, which only works on the OS file system. Reading through mju_openResource lets it work against any registered backend (VFS, HTTP, github:, ...).
This commit is contained in:
+36
-24
@@ -222,34 +222,46 @@ void mjCopyError(char* dst, const char* src, int maxlen) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void mju_getXMLDependencies(const char* filename, mjStringVec* dependencies) {
|
void mju_getXMLDependencies(const char* filename, mjStringVec* dependencies) {
|
||||||
// load XML file or parse string
|
// Open through the resource provider so the function works against any
|
||||||
tinyxml2::XMLDocument doc;
|
// registered backend (OS file system, VFS, HTTP, "github:", ...) rather
|
||||||
doc.LoadFile(filename);
|
// than only the OS file system.
|
||||||
|
mjResource* resource = mju_openResource("", filename, nullptr, nullptr, 0);
|
||||||
// error checking
|
if (resource == nullptr) {
|
||||||
if (doc.Error()) {
|
mju_error("Could not open '%s'", filename);
|
||||||
mju_error("Problem reading XML file '%s': %s", filename, doc.ErrorStr());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get top-level element
|
// Read the XML bytes from the resource.
|
||||||
tinyxml2::XMLElement *root = doc.RootElement();
|
const void* buffer = nullptr;
|
||||||
if (!root) {
|
int size = mju_readResource(resource, &buffer);
|
||||||
mju_error("XML root element not found");
|
if (size < 0 || !size) {
|
||||||
}
|
|
||||||
std::unordered_set<std::string> files = {filename};
|
|
||||||
|
|
||||||
std::optional<FilePath> model_dir = std::nullopt;
|
|
||||||
mjResource *resource = mju_openResource("", filename, nullptr,
|
|
||||||
nullptr, 0);
|
|
||||||
if (resource != nullptr) {
|
|
||||||
const char* dir;
|
|
||||||
int ndir;
|
|
||||||
mju_getResourceDir(resource, &dir, &ndir);
|
|
||||||
model_dir = FilePath(std::string(dir, ndir));
|
|
||||||
mju_closeResource(resource);
|
mju_closeResource(resource);
|
||||||
|
mju_error("Could not read '%s'", filename);
|
||||||
}
|
}
|
||||||
// Get file references from include and model tags.
|
|
||||||
AccumulateFiles(files, root, model_dir.value());
|
// Capture the model directory while the resource is still open.
|
||||||
|
const char* dir = nullptr;
|
||||||
|
int ndir = 0;
|
||||||
|
mju_getResourceDir(resource, &dir, &ndir);
|
||||||
|
FilePath model_dir(std::string(dir, ndir));
|
||||||
|
|
||||||
|
// Parse from buffer and close (the parsed DOM is independent of the
|
||||||
|
// resource buffer once Parse returns).
|
||||||
|
tinyxml2::XMLDocument doc;
|
||||||
|
tinyxml2::XMLError err =
|
||||||
|
doc.Parse(static_cast<const char*>(buffer), static_cast<size_t>(size));
|
||||||
|
mju_closeResource(resource);
|
||||||
|
|
||||||
|
if (err != tinyxml2::XML_SUCCESS) {
|
||||||
|
mju_error("Problem reading XML file '%s': %s", filename,
|
||||||
|
doc.ErrorStr() ? doc.ErrorStr() : "");
|
||||||
|
}
|
||||||
|
tinyxml2::XMLElement* root = doc.RootElement();
|
||||||
|
if (!root) {
|
||||||
|
mju_error("XML root element not found in '%s'", filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unordered_set<std::string> files = {filename};
|
||||||
|
AccumulateFiles(files, root, model_dir);
|
||||||
|
|
||||||
*dependencies = {files.begin(), files.end()};
|
*dependencies = {files.begin(), files.end()};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,11 +13,14 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
|
||||||
|
#include <map>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <gmock/gmock.h>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
#include <mujoco/mjplugin.h>
|
||||||
#include <mujoco/mujoco.h>
|
#include <mujoco/mujoco.h>
|
||||||
#include "test/fixture.h"
|
#include "test/fixture.h"
|
||||||
|
|
||||||
@@ -42,5 +45,64 @@ TEST_F(MujocoTest, GetXMLDependenciesTest) {
|
|||||||
kModelPaths.end()};
|
kModelPaths.end()};
|
||||||
EXPECT_EQ(dependency_set, expected_dependency_set);
|
EXPECT_EQ(dependency_set, expected_dependency_set);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Custom resource provider that serves XML strings from an in-memory map,
|
||||||
|
// used to exercise the non-filesystem code path.
|
||||||
|
namespace memxml {
|
||||||
|
static const std::map<std::string, std::string>* g_files = nullptr;
|
||||||
|
|
||||||
|
int Open(mjResource* resource) {
|
||||||
|
return g_files && g_files->count(resource->name) ? 1 : 0;
|
||||||
|
}
|
||||||
|
int Read(mjResource* resource, const void** buffer) {
|
||||||
|
auto it = g_files->find(resource->name);
|
||||||
|
if (it == g_files->end()) return -1;
|
||||||
|
*buffer = it->second.data();
|
||||||
|
return static_cast<int>(it->second.size());
|
||||||
|
}
|
||||||
|
void Close(mjResource* resource) {}
|
||||||
|
} // namespace memxml
|
||||||
|
|
||||||
|
// Verifies mju_getXMLDependencies works against a non-filesystem resource
|
||||||
|
// provider (the case the WASM/HTTP build relies on).
|
||||||
|
TEST_F(MujocoTest, GetXMLDependenciesViaResourceProvider) {
|
||||||
|
const std::map<std::string, std::string> files = {
|
||||||
|
{"memxml:/scene.xml",
|
||||||
|
"<mujoco>\n"
|
||||||
|
" <include file=\"child.xml\"/>\n"
|
||||||
|
" <compiler meshdir=\"meshes\"/>\n"
|
||||||
|
" <asset>\n"
|
||||||
|
" <mesh name=\"m\" file=\"m.obj\"/>\n"
|
||||||
|
" </asset>\n"
|
||||||
|
"</mujoco>"},
|
||||||
|
{"memxml:/child.xml",
|
||||||
|
"<mujoco>\n"
|
||||||
|
" <asset>\n"
|
||||||
|
" <texture name=\"t\" type=\"2d\" file=\"t.png\"/>\n"
|
||||||
|
" </asset>\n"
|
||||||
|
"</mujoco>"},
|
||||||
|
};
|
||||||
|
memxml::g_files = &files;
|
||||||
|
|
||||||
|
mjpResourceProvider provider = {
|
||||||
|
.prefix = "memxml",
|
||||||
|
.open = memxml::Open,
|
||||||
|
.read = memxml::Read,
|
||||||
|
.close = memxml::Close,
|
||||||
|
};
|
||||||
|
ASSERT_GT(mjp_registerResourceProvider(&provider), 0);
|
||||||
|
|
||||||
|
mjStringVec dependencies;
|
||||||
|
mju_getXMLDependencies("memxml:/scene.xml", &dependencies);
|
||||||
|
std::set<std::string> dep_set(dependencies.begin(), dependencies.end());
|
||||||
|
|
||||||
|
EXPECT_THAT(dep_set, testing::UnorderedElementsAre(
|
||||||
|
"memxml:/scene.xml",
|
||||||
|
"memxml:/child.xml",
|
||||||
|
"memxml:/meshes/m.obj",
|
||||||
|
"memxml:/t.png"));
|
||||||
|
|
||||||
|
memxml::g_files = nullptr;
|
||||||
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace mujoco
|
} // namespace mujoco
|
||||||
|
|||||||
Reference in New Issue
Block a user