Allow studio to search for models in predetermined search paths.

A future CL will allow users to specify search paths in their settings.

PiperOrigin-RevId: 846240293
Change-Id: I1a1a4027d0fc85004a12fe01a406f50fd929f3e4
This commit is contained in:
Haroon Qureshi
2025-12-18 06:31:34 -08:00
committed by Copybara-Service
parent 7190bd4912
commit bebdd62943
4 changed files with 55 additions and 4 deletions
+42
View File
@@ -18,6 +18,7 @@
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <ios>
#include <iterator>
@@ -47,6 +48,47 @@ std::string LoadText(const std::string& filename) {
return contents;
}
static std::string CheckPathForFile(const std::filesystem::path& path,
const std::string& filename) {
std::filesystem::path resolved = path / filename;
if (std::filesystem::exists(resolved)) {
return resolved.string();
}
resolved += ".xml";
if (std::filesystem::exists(resolved)) {
return resolved.string();
}
return "";
}
std::string ResolveFile(const std::string& filename,
const std::vector<std::string>& search_paths) {
if (std::filesystem::exists(filename)) {
return filename;
}
std::string resolved;
for (const std::string& path : search_paths) {
if (!std::filesystem::exists(path) ||
!std::filesystem::is_directory(path)) {
continue;
}
resolved = CheckPathForFile(std::filesystem::path(path), filename);
if (!resolved.empty()) {
return resolved;
}
for (const auto& it : std::filesystem::recursive_directory_iterator(path)) {
resolved = CheckPathForFile(it.path(), filename);
if (!resolved.empty()) {
return resolved;
}
}
}
return "";
}
void SaveColorToWebp(int width, int height, const unsigned char* data,
const std::string& filename) {
uint8_t* webp = nullptr;
+5
View File
@@ -34,6 +34,11 @@ using LoadAssetFn = std::function<std::vector<std::byte>(std::string_view)>;
void SaveText(const std::string& contents, const std::string& filename);
std::string LoadText(const std::string& filename);
// Attempts to find a file with the given name by recursively searching the
// given search paths.
std::string ResolveFile(const std::string& filename,
const std::vector<std::string>& search_paths);
// Exports the given color buffer to a webp file.
void SaveColorToWebp(int width, int height, const unsigned char* data,
const std::string& filename);
+6 -4
View File
@@ -201,10 +201,12 @@ void App::LoadModel(std::string data, ContentType type) {
// Store the file path as the model name. Note that we use this model name
// to perform reload operations.
model_name_ = std::move(data);
if (model_name_.ends_with(".mjb")) {
model_ = mj_loadModel(model_name_.c_str(), 0);
} else if (model_name_.ends_with(".xml")) {
spec_ = mj_parseXML(model_name_.c_str(), nullptr, err, sizeof(err));
const std::string resolved_file =
platform::ResolveFile(model_name_, search_paths_);
if (resolved_file.ends_with(".mjb")) {
model_ = mj_loadModel(resolved_file.c_str(), 0);
} else if (resolved_file.ends_with(".xml")) {
spec_ = mj_parseXML(resolved_file.c_str(), nullptr, err, sizeof(err));
if (spec_ && err[0] == 0) {
model_ = mj_compile(spec_, nullptr);
}
+2
View File
@@ -185,6 +185,8 @@ class App {
platform::SimProfiler profiler_;
platform::SimHistory history_;
std::vector<std::string> search_paths_;
mjSpec* spec_ = nullptr;
mjModel* model_ = nullptr;
mjData* data_ = nullptr;