Skip to content

Commit

Permalink
feat: automatically upgrade configs
Browse files Browse the repository at this point in the history
  • Loading branch information
ABeltramo committed Aug 21, 2024
1 parent 0f8939c commit 759a4c4
Showing 1 changed file with 15 additions and 41 deletions.
56 changes: 15 additions & 41 deletions src/moonlight-server/state/configTOML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,38 +174,6 @@ static bool is_available(const GPU_VENDOR &gpu_vendor, const GstEncoder &setting
return false;
}

toml::value v1_to_v2(const toml::value &v1, const std::string &source) {
create_default(source);
auto v2 = toml::parse(source);
v2["hostname"] = v1.at("hostname").as_string();
v2["uuid"] = v1.at("uuid").as_string();
v2["support_hevc"] = v1.at("support_hevc").as_boolean();
v2["paired_clients"] = v1.at("paired_clients").as_array() | //
ranges::views::transform([](const toml::value &client) { //
return PairedClient{.client_cert = client.at("client_cert").as_string()}; //
}) //
| ranges::to<toml::array>();
write(v2, source);
return v2;
}

toml::value v2_to_v3(const toml::value &v2, const std::string &source) {
auto v3 = toml::parse(source);
v3["uuid"] = v2.at("uuid").as_string();
v3["config_version"] = 3;
v3["gstreamer"]["video"]["default_sink"] =
"rtpmoonlightpay_video name=moonlight_pay\n"
"payload_size={payload_size} fec_percentage={fec_percentage} "
"min_required_fec_packets={min_required_fec_packets} !\n"
"udpsink bind-port={host_port} host={client_ip} port={client_port} sync=true";
v3["gstreamer"]["audio"]["default_sink"] =
"rtpmoonlightpay_audio name=moonlight_pay packet_duration={packet_duration} encrypt={encrypt}\n"
"aes_key=\"{aes_key}\" aes_iv=\"{aes_iv}\" !\n"
"udpsink bind-port={host_port} host={client_ip} port={client_port} sync=true";
write(v3, source);
return v3;
}

std::optional<GstEncoder>
get_encoder(std::string_view tech, const std::vector<GstEncoder> &encoders, const GPU_VENDOR &vendor) {
auto default_is_available = std::bind(is_available, vendor, std::placeholders::_1);
Expand All @@ -220,6 +188,19 @@ get_encoder(std::string_view tech, const std::vector<GstEncoder> &encoders, cons
return std::nullopt;
}

toml::value v3_to_v4(const toml::value &v3, const std::string &source) {
std::filesystem::rename(source, source + ".v3.old");
create_default(source);
auto v4 = toml::parse(source);
// Copy back everything apart from the Gstreamer pipelines
v4["hostname"] = v3.at("hostname").as_string();
v4["uuid"] = v3.at("uuid").as_string();
v4["apps"] = v3.at("apps");
v4["paired_clients"] = v3.at("paired_clients");
write(v4, source);
return v4;
}

Config load_or_default(const std::string &source, const std::shared_ptr<dp::event_bus> &ev_bus) {
if (!file_exist(source)) {
logs::log(logs::warning, "Unable to open config file: {}, creating one using defaults", source);
Expand All @@ -228,16 +209,9 @@ Config load_or_default(const std::string &source, const std::shared_ptr<dp::even

auto cfg = toml::parse(source);
auto version = toml::find_or(cfg, "config_version", 2);
if (version <= 1) {
logs::log(logs::warning, "Found old config file, migrating to newer version");
cfg = v1_to_v2(cfg, source);
version = 2;
}

if (version <= 2) {
if (version <= 3) {
logs::log(logs::warning, "Found old config file, migrating to newer version");
cfg = v2_to_v3(cfg, source);
version = 3;
cfg = v3_to_v4(cfg, source);
}

std::string uuid;
Expand Down

0 comments on commit 759a4c4

Please sign in to comment.