HttpLib/SteamUtils: Endpoint to get Steam User config as JSON

pull/239/head
Peter Repukat 1 year ago
parent e6db328273
commit 00127dcd2b

@ -21,6 +21,7 @@ limitations under the License.
#include "AppLauncher.h" #include "AppLauncher.h"
#include "../common/Settings.h" #include "../common/Settings.h"
#include "../common/steam_util.h"
HttpServer::HttpServer(AppLauncher& app_launcher, std::function<void()> close) : app_launcher_(app_launcher), close_(std::move(close)) HttpServer::HttpServer(AppLauncher& app_launcher, std::function<void()> close) : app_launcher_(app_launcher), close_(std::move(close))
{ {
@ -28,12 +29,17 @@ HttpServer::HttpServer(AppLauncher& app_launcher, std::function<void()> close) :
void HttpServer::run() void HttpServer::run()
{ {
server_.Get("/launched-pids", [this](const httplib::Request& req, httplib::Response& res) { auto setCorsHeader = [](httplib::Response& res) {
res.set_header("Access-Control-Allow-Origin", "*");
};
server_.Get("/launched-pids", [this, &setCorsHeader](const httplib::Request& req, httplib::Response& res) {
const nlohmann::json j = app_launcher_.launchedPids(); const nlohmann::json j = app_launcher_.launchedPids();
res.set_content(j.dump(), "text/json"); res.set_content(j.dump(), "text/json");
setCorsHeader(res);
}); });
server_.Post("/launched-pids", [this](const httplib::Request& req, httplib::Response& res) { server_.Post("/launched-pids", [this, &setCorsHeader](const httplib::Request& req, httplib::Response& res) {
setCorsHeader(res);
try { try {
const nlohmann::json postbody = nlohmann::json::parse(req.body); const nlohmann::json postbody = nlohmann::json::parse(req.body);
app_launcher_.addPids(postbody.get<std::vector<DWORD>>()); app_launcher_.addPids(postbody.get<std::vector<DWORD>>());
@ -63,14 +69,21 @@ void HttpServer::run()
res.set_content(j.dump(), "text/json"); res.set_content(j.dump(), "text/json");
}); });
server_.Post("/quit", [this](const httplib::Request& req, httplib::Response& res) { server_.Post("/quit", [this, &setCorsHeader](const httplib::Request& req, httplib::Response& res) {
setCorsHeader(res);
close_(); close_();
}); });
server_.Get("/settings", [this](const httplib::Request& req, httplib::Response& res) { server_.Get("/settings", [this, &setCorsHeader](const httplib::Request& req, httplib::Response& res) {
setCorsHeader(res);
res.set_content(Settings::toJson().dump(), "text/json"); res.set_content(Settings::toJson().dump(), "text/json");
}); });
server_.Get("/steam_settings", [this, &setCorsHeader](const httplib::Request& req, httplib::Response& res) {
setCorsHeader(res);
res.set_content(util::steam::getSteamConfig().dump(4), "text/json");
});
server_thread_ = std::thread([this]() { server_thread_ = std::thread([this]() {
if (!server_.listen("0.0.0.0", port_)) { if (!server_.listen("0.0.0.0", port_)) {
spdlog::error("Couldn't start http-server"); spdlog::error("Couldn't start http-server");

@ -177,5 +177,45 @@ namespace util
} }
return xbsup == "1"; return xbsup == "1";
} }
inline nlohmann::json getSteamConfig(const std::wstring& steam_path = getSteamPath(), const std::wstring& steam_user_id = getSteamUserId())
{
const auto config_path = std::wstring(steam_path) + std::wstring(user_data_path) + steam_user_id + std::wstring(config_file_name);
if (!std::filesystem::exists(config_path)) {
spdlog::warn(L"Couldn't read Steam config file: \"{}\"", config_path);
return nlohmann::json();
}
std::ifstream config_file(config_path);
auto root = tyti::vdf::read(config_file);
if (root.attribs.empty())
{
return {};
}
auto res = nlohmann::json::object();
res[root.name] = nlohmann::json::object();
for (auto& [key, value] : root.attribs)
{
res[root.name][key] = value;
}
auto parse_child = [](nlohmann::json& j, std::shared_ptr<tyti::vdf::basic_object<char>> child, auto&& recurse) -> void
{
for (auto& [key, value] : child->attribs)
{
j[key] = value;
for (auto& [childkey, childval] : child->childs)
{
j[childkey] = {};
recurse(j[childkey], childval, recurse);
}
}
};
for (auto& [key, value] : root.childs)
{
res[root.name][key] = {};
parse_child(res[root.name][key], value, parse_child);
}
return res;
}
} }
} }
Loading…
Cancel
Save