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:
committed by
Copybara-Service
parent
7190bd4912
commit
bebdd62943
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user