Add support for relative URLs in environments list

This commit is contained in:
Guillaume Meunier
2025-10-26 10:33:49 +01:00
parent 41abd36e33
commit b6b3a4da80
3 changed files with 54 additions and 15 deletions
+3
View File
@@ -979,6 +979,8 @@ void scenes::lobby::on_focused()
about_picture = imgui_ctx->load_texture("assets://" + image + ".ktx2");
default_environment_screenshot = imgui_ctx->load_texture("assets://default-environment.ktx2");
try
{
local_environments = load_environment_json(utils::read_whole_file<std::string>(application::get_config_path() / "environments.json"));
@@ -1039,6 +1041,7 @@ void scenes::lobby::on_unfocused()
renderer->wait_idle(); // Must be before the scene data because the renderer uses its descriptor sets
about_picture = 0;
default_environment_screenshot = 0;
local_environments.clear();
imgui_ctx.reset();
+3 -2
View File
@@ -175,12 +175,13 @@ class lobby : public scene_impl<lobby>
}
};
ImTextureID default_environment_screenshot;
std::vector<environment_model> downloadable_environments;
std::vector<environment_model> local_environments;
environment_model * environment_to_be_deleted = nullptr;
libcurl curl; // needs to be before current_transfers
std::map<std::string, std::pair<libcurl::curl_handle, std::function<void(libcurl::curl_handle & handle)>>> current_transfers;
libcurl curl; // needs to be after current_transfers
void update_file_picker();
void update_transfers();
@@ -190,7 +191,7 @@ class lobby : public scene_impl<lobby>
std::string downloadable_environment_list_status;
std::vector<environment_model> load_environment_json(const std::string & json);
std::vector<environment_model> load_environment_json(const std::string & json, std::string_view base_url = "");
void save_environment_json();
void download_environment(const environment_model & model, bool use_after_downloading);
void delete_environment(const environment_model & model);
+48 -13
View File
@@ -30,6 +30,8 @@
#include "utils/i18n.h"
#include "utils/json_string.h"
#include <algorithm>
#include <boost/url/url.hpp>
#include <boost/url/url_view.hpp>
#include "IconsFontAwesome6.h"
@@ -61,7 +63,24 @@ void scenes::lobby::save_environment_json()
utils::write_whole_file(application::get_config_path() / "environments.json", ss.str());
}
std::vector<scenes::lobby::environment_model> scenes::lobby::load_environment_json(const std::string & json)
static std::string join_url(std::string_view base, std::string_view ref)
{
if (base == "")
return (std::string)ref;
boost::urls::url base_url(base);
boost::urls::url ref_url(ref);
if (auto res = base_url.resolve(boost::urls::url_view{ref}); res.has_error())
{
// The only possible error is if base is not a base url
return (std::string)ref;
}
return base_url.buffer();
}
std::vector<scenes::lobby::environment_model> scenes::lobby::load_environment_json(const std::string & json, std::string_view base_url)
{
std::vector<scenes::lobby::environment_model> models;
@@ -83,11 +102,11 @@ std::vector<scenes::lobby::environment_model> scenes::lobby::load_environment_js
if (auto val = i["description"]; val.is_string())
model.description = val.get_string().value();
if (auto val = i["screenshot"]; val.is_string())
model.screenshot_url = val.get_string().value();
if (auto val = i["screenshot"]; val.is_string() and val.get_string().value() != "")
model.screenshot_url = join_url(base_url, val.get_string().value());
if (auto val = i["url"]; val.is_string())
model.gltf_url = val.get_string().value();
model.gltf_url = join_url(base_url, val.get_string().value());
if (auto val = i["size"]; val.is_number())
model.size = val.get_int64();
@@ -206,7 +225,7 @@ void scenes::lobby::download_environment_list()
try
{
// TODO cache in filesystem?
downloadable_environments = load_environment_json(handle.get_response());
downloadable_environments = load_environment_json(handle.get_response(), WIVRN_ENVIRONMENTS_URL);
}
catch (std::exception & e)
{
@@ -256,15 +275,30 @@ scenes::lobby::environment_item_action scenes::lobby::environment_item(environme
download(model.screenshot_url,
[this, url = model.screenshot_url](libcurl::curl_handle & handle) {
auto png = handle.get_response_bytes();
auto screenshot = imgui_ctx->load_texture(png);
for (auto & model: downloadable_environments)
try
{
if (model.screenshot_url == url)
auto png = handle.get_response_bytes();
auto screenshot = imgui_ctx->load_texture(png);
for (auto & model: downloadable_environments)
{
model.screenshot = screenshot;
model.screenshot_png = std::vector<std::byte>{png.begin(), png.end()};
if (model.screenshot_url == url)
{
model.screenshot = screenshot;
model.screenshot_png = std::vector<std::byte>{png.begin(), png.end()};
}
}
}
catch (std::exception & e)
{
spdlog::warn("Cannot load image from {}: {}", handle.get_url(), e.what());
for (auto & model: downloadable_environments)
{
if (model.screenshot_url == url)
{
model.screenshot_url = "";
model.screenshot = default_environment_screenshot;
}
}
}
});
@@ -283,6 +317,7 @@ scenes::lobby::environment_item_action scenes::lobby::environment_item(environme
std::error_code ec;
std::filesystem::remove(model.local_screenshot_path, ec); // Ignore errors
model.local_screenshot_path = ""; // Avoid subsequent loading errors
model.screenshot = default_environment_screenshot;
}
}
}
@@ -312,7 +347,7 @@ scenes::lobby::environment_item_action scenes::lobby::environment_item(environme
// Display the screenshot
ImGui::SetCursorScreenPos(backup_cursor + ImVec2(style.FramePadding.x, 0));
ImGui::Image(screenshot, screenshot_size);
ImGui::Image(screenshot ? screenshot : default_environment_screenshot, screenshot_size);
ImGui::PushClipRect(backup_cursor, backup_cursor + ImVec2(selectable_size.x, text_clip_height), true);
float indent = screenshot_size.x + style.FramePadding.x + 2 * style.ImageBorderSize + style.ItemSpacing.x;