From c706ba4ca2db565b183090df0695437fdf721906 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 2 May 2023 01:06:38 -0500 Subject: [PATCH] Add Windows-specific warnings if Sunshine is unable to access config or credentials http::init() failure must be fatal because attempting to start the web servers will crash due to null creds. --- src/config.cpp | 50 ++++++++++++++++++++++++++++++++++++-------------- src/main.cpp | 9 ++++++++- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index 820c32fc..33cb9115 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1150,24 +1150,46 @@ namespace config { } } - // create appdata folder if it does not exist - if (!boost::filesystem::exists(platf::appdata().string())) { - boost::filesystem::create_directories(platf::appdata().string()); + bool config_loaded = false; + try { + // Create appdata folder if it does not exist + if (!boost::filesystem::exists(platf::appdata().string())) { + boost::filesystem::create_directories(platf::appdata().string()); + } + + // Create empty config file if it does not exist + if (!fs::exists(sunshine.config_file)) { + std::ofstream { sunshine.config_file }; + } + + // Read config file + auto vars = parse_config(read_file(sunshine.config_file.c_str())); + + for (auto &[name, value] : cmd_vars) { + vars.insert_or_assign(std::move(name), std::move(value)); + } + + // Apply the config. Note: This will try to create any paths + // referenced in the config, so we may receive exceptions if + // the path is incorrect or inaccessible. + apply_config(std::move(vars)); + config_loaded = true; + } + catch (const std::filesystem::filesystem_error &err) { + BOOST_LOG(fatal) << "Failed to apply config: "sv << err.what(); + } + catch (const boost::filesystem::filesystem_error &err) { + BOOST_LOG(fatal) << "Failed to apply config: "sv << err.what(); } - // create config file if it does not exist - if (!fs::exists(sunshine.config_file)) { - std::ofstream { sunshine.config_file }; // create empty config file + if (!config_loaded) { +#ifdef _WIN32 + BOOST_LOG(fatal) << "To relaunch Sunshine successfully, use the shortcut in the Start Menu. Do not run Sunshine.exe manually."sv; + std::this_thread::sleep_for(10s); +#endif + return -1; } - auto vars = parse_config(read_file(sunshine.config_file.c_str())); - - for (auto &[name, value] : cmd_vars) { - vars.insert_or_assign(std::move(name), std::move(value)); - } - - apply_config(std::move(vars)); - #ifdef _WIN32 // We have to wait until the config is loaded to handle these launches, // because we need to have the correct base port loaded in our config. diff --git a/src/main.cpp b/src/main.cpp index 5a7348fb..bc8eb9fa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -600,7 +600,14 @@ main(int argc, char *argv[]) { } if (http::init()) { - BOOST_LOG(error) << "http failed to initialize"sv; + BOOST_LOG(fatal) << "HTTP interface failed to initialize"sv; + +#ifdef _WIN32 + BOOST_LOG(fatal) << "To relaunch Sunshine successfully, use the shortcut in the Start Menu. Do not run Sunshine.exe manually."sv; + std::this_thread::sleep_for(10s); +#endif + + return -1; } std::unique_ptr mDNS;