From c58f95b79e6866b55478d2a6bb0c7d80c3b2e0ff Mon Sep 17 00:00:00 2001 From: Jacek Jagosz Date: Sat, 24 Jul 2021 15:26:08 +0200 Subject: [PATCH 1/4] Try making sunshine on Linux stateless --- CMakeLists.txt | 10 ++++++++++ sunshine/config.cpp | 12 ++++++++++-- sunshine/confighttp.cpp | 8 +++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2763191e..d718c697 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -230,6 +230,14 @@ if(NOT SUNSHINE_ASSETS_DIR) set(SUNSHINE_ASSETS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/assets") endif() +if(NOT SUNSHINE_CONFIG_DIR) + set(SUNSHINE_CONFIG_DIR "${CMAKE_CURRENT_SOURCE_DIR}/assets") +endif() + +if(NOT SUNSHINE_DEFAULT_DIR) + set(SUNSHINE_CONFIG_DIR "${CMAKE_CURRENT_SOURCE_DIR}/assets") +endif() + list(APPEND CBS_EXTERNAL_LIBRARIES cbs) @@ -246,6 +254,8 @@ list(APPEND SUNSHINE_EXTERNAL_LIBRARIES ${PLATFORM_LIBRARIES}) list(APPEND SUNSHINE_DEFINITIONS SUNSHINE_ASSETS_DIR="${SUNSHINE_ASSETS_DIR}") +list(APPEND SUNSHINE_DEFINITIONS SUNSHINE_CONFIG_DIR="${SUNSHINE_CONFIG_DIR}") +list(APPEND SUNSHINE_DEFINITIONS SUNSHINE_DEFAULT_DIR="${SUNSHINE_DEFAULT_DIR}") add_executable(sunshine ${SUNSHINE_TARGET_FILES}) target_link_libraries(sunshine ${SUNSHINE_EXTERNAL_LIBRARIES}) target_compile_definitions(sunshine PUBLIC ${SUNSHINE_DEFINITIONS}) diff --git a/sunshine/config.cpp b/sunshine/config.cpp index f8cc735d..b5082f59 100644 --- a/sunshine/config.cpp +++ b/sunshine/config.cpp @@ -20,7 +20,7 @@ using namespace std::literals; #define PRIVATE_KEY_FILE CA_DIR "/cakey.pem" #define CERTIFICATE_FILE CA_DIR "/cacert.pem" -#define APPS_JSON_PATH SUNSHINE_ASSETS_DIR "/" APPS_JSON +#define APPS_JSON_PATH SUNSHINE_CONFIG_DIR "/" APPS_JSON namespace config { namespace nv { @@ -223,7 +223,7 @@ sunshine_t sunshine { {}, // Username {}, // Password {}, // Password Salt - SUNSHINE_ASSETS_DIR "/sunshine.conf", // config file + SUNSHINE_CONFIG_DIR "/sunshine.conf", // config file {}, // cmd args 47989, }; @@ -573,6 +573,10 @@ int apply_flags(const char *line) { } void apply_config(std::unordered_map &&vars) { + if(!fs::exists(stream.file_apps.c_str())) { + stream.file_apps = SUNSHINE_DEFAULT_DIR "/" APPS_JSON; + } + for(auto &[name, val] : vars) { std::cout << "["sv << name << "] -- ["sv << val << ']' << std::endl; } @@ -754,6 +758,10 @@ int parse(int argc, char *argv[]) { } } + if(!fs::exists(sunshine.config_file.c_str())) { + sunshine.config_file = SUNSHINE_DEFAULT_DIR "/sunshine.conf"; + } + auto vars = parse_config(read_file(sunshine.config_file.c_str())); for(auto &[name, value] : cmd_vars) { diff --git a/sunshine/confighttp.cpp b/sunshine/confighttp.cpp index b21c7571..a1ef32f5 100644 --- a/sunshine/confighttp.cpp +++ b/sunshine/confighttp.cpp @@ -250,7 +250,8 @@ void saveApp(resp_https_t response, req_https_t request) { fileTree.erase("apps"); fileTree.push_back(std::make_pair("apps", newApps)); } - pt::write_json(config::stream.file_apps, fileTree); + std::string sunshine_apps_path = SUNSHINE_CONFIG_DIR "/" APPS_JSON; + pt::write_json(sunshine_apps_path, fileTree); } catch(std::exception &e) { BOOST_LOG(warning) << "SaveApp: "sv << e.what(); @@ -360,7 +361,8 @@ void saveConfig(resp_https_t response, req_https_t request) { configStream << kv.first << " = " << value << std::endl; } - write_file(config::sunshine.config_file.c_str(), configStream.str()); + std::string sunshine_config_path = SUNSHINE_CONFIG_DIR "/sunshine.conf"; + write_file(sunshine_config_path.c_str(), configStream.str()); } catch(std::exception &e) { BOOST_LOG(warning) << "SaveConfig: "sv << e.what(); @@ -512,4 +514,4 @@ void start() { tcp.join(); } -} // namespace confighttp \ No newline at end of file +} // namespace confighttp From a07ad3e4791a189511a01df5aef33555f1ec41b8 Mon Sep 17 00:00:00 2001 From: Jacek Jagosz Date: Sun, 25 Jul 2021 14:09:47 +0200 Subject: [PATCH 2/4] If CONFIG and DEFAULT directories haven't been configured, make them point to ASSETS --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d718c697..2c625841 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -231,11 +231,11 @@ if(NOT SUNSHINE_ASSETS_DIR) endif() if(NOT SUNSHINE_CONFIG_DIR) - set(SUNSHINE_CONFIG_DIR "${CMAKE_CURRENT_SOURCE_DIR}/assets") + set(SUNSHINE_CONFIG_DIR SUNSHINE_ASSETS_DIR) endif() if(NOT SUNSHINE_DEFAULT_DIR) - set(SUNSHINE_CONFIG_DIR "${CMAKE_CURRENT_SOURCE_DIR}/assets") + set(SUNSHINE_DEFAULT_DIR SUNSHINE_ASSETS_DIR) endif() list(APPEND CBS_EXTERNAL_LIBRARIES From c39f2b0c1fa22b2f398159e7ae9d6cddaa7f1cfd Mon Sep 17 00:00:00 2001 From: Jacek Jagosz Date: Sun, 25 Jul 2021 20:39:45 +0200 Subject: [PATCH 3/4] If config files don't exist in user specified directory copy files from default, not override user choice --- sunshine/config.cpp | 6 ++++-- sunshine/confighttp.cpp | 6 ++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sunshine/config.cpp b/sunshine/config.cpp index b5082f59..706da2b0 100644 --- a/sunshine/config.cpp +++ b/sunshine/config.cpp @@ -574,7 +574,8 @@ int apply_flags(const char *line) { void apply_config(std::unordered_map &&vars) { if(!fs::exists(stream.file_apps.c_str())) { - stream.file_apps = SUNSHINE_DEFAULT_DIR "/" APPS_JSON; + std::string sunshine_apps_default = SUNSHINE_DEFAULT_DIR "/" APPS_JSON; + fs::copy_file(sunshine_apps_default.c_str(), stream.file_apps.c_str()); } for(auto &[name, val] : vars) { @@ -759,7 +760,8 @@ int parse(int argc, char *argv[]) { } if(!fs::exists(sunshine.config_file.c_str())) { - sunshine.config_file = SUNSHINE_DEFAULT_DIR "/sunshine.conf"; + std::string sunshine_config_default = SUNSHINE_DEFAULT_DIR "/sunshine.conf"; + fs::copy_file(sunshine_config_default.c_str(), sunshine.config_file.c_str()); } auto vars = parse_config(read_file(sunshine.config_file.c_str())); diff --git a/sunshine/confighttp.cpp b/sunshine/confighttp.cpp index a1ef32f5..c1a643bb 100644 --- a/sunshine/confighttp.cpp +++ b/sunshine/confighttp.cpp @@ -250,8 +250,7 @@ void saveApp(resp_https_t response, req_https_t request) { fileTree.erase("apps"); fileTree.push_back(std::make_pair("apps", newApps)); } - std::string sunshine_apps_path = SUNSHINE_CONFIG_DIR "/" APPS_JSON; - pt::write_json(sunshine_apps_path, fileTree); + pt::write_json(config::stream.file_apps, fileTree); } catch(std::exception &e) { BOOST_LOG(warning) << "SaveApp: "sv << e.what(); @@ -361,8 +360,7 @@ void saveConfig(resp_https_t response, req_https_t request) { configStream << kv.first << " = " << value << std::endl; } - std::string sunshine_config_path = SUNSHINE_CONFIG_DIR "/sunshine.conf"; - write_file(sunshine_config_path.c_str(), configStream.str()); + write_file(config::sunshine.config_file.c_str(), configStream.str()); } catch(std::exception &e) { BOOST_LOG(warning) << "SaveConfig: "sv << e.what(); From 2e8b462fe5f22900014570b0dbc23477061ff031 Mon Sep 17 00:00:00 2001 From: Jacek Jagosz Date: Tue, 27 Jul 2021 15:58:01 +0200 Subject: [PATCH 4/4] Not create unnecessary variables and make fs use strings directly instead of converting them --- sunshine/config.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sunshine/config.cpp b/sunshine/config.cpp index 706da2b0..26e9639b 100644 --- a/sunshine/config.cpp +++ b/sunshine/config.cpp @@ -574,8 +574,7 @@ int apply_flags(const char *line) { void apply_config(std::unordered_map &&vars) { if(!fs::exists(stream.file_apps.c_str())) { - std::string sunshine_apps_default = SUNSHINE_DEFAULT_DIR "/" APPS_JSON; - fs::copy_file(sunshine_apps_default.c_str(), stream.file_apps.c_str()); + fs::copy_file(SUNSHINE_DEFAULT_DIR "/" APPS_JSON, stream.file_apps); } for(auto &[name, val] : vars) { @@ -760,8 +759,7 @@ int parse(int argc, char *argv[]) { } if(!fs::exists(sunshine.config_file.c_str())) { - std::string sunshine_config_default = SUNSHINE_DEFAULT_DIR "/sunshine.conf"; - fs::copy_file(sunshine_config_default.c_str(), sunshine.config_file.c_str()); + fs::copy_file(SUNSHINE_DEFAULT_DIR "/sunshine.conf", sunshine.config_file); } auto vars = parse_config(read_file(sunshine.config_file.c_str()));