From e9cbcd3a377833c30dce342b043483c188a066f9 Mon Sep 17 00:00:00 2001 From: Cian Ormond Date: Wed, 6 Apr 2022 11:37:13 -0700 Subject: [PATCH 1/2] Ask which xPilot executable to download --- download.cpp | 34 +++++++++++++++++++++++++++++----- download.h | 2 +- main.cpp | 14 ++++---------- utils.cpp | 14 +++++++++++++- utils.h | 1 + 5 files changed, 48 insertions(+), 17 deletions(-) diff --git a/download.cpp b/download.cpp index c8139cd..0b7cb38 100644 --- a/download.cpp +++ b/download.cpp @@ -1,4 +1,3 @@ -#include "download.h" #include #include #include @@ -6,6 +5,11 @@ #include #include +#include "download.h" +#include "utils.h" + +utils ut_dl; + size_t download::write_data(char *ptr, size_t size, size_t nmemb, void *userdata) { std::ostringstream *stream = (std::ostringstream*)userdata; size_t count = size * nmemb; @@ -52,7 +56,7 @@ std::vector download::downloadPilotVersions() { return versions; } -int download::downloadPilotClient(int program) { +int download::downloadPilotClient(int program, int variant, bool force) { std::string programVersion = download::downloadPilotVersions().at(program); std::string programName; @@ -62,7 +66,15 @@ int download::downloadPilotClient(int program) { case 0: programName = "xPilot"; url = "https://github.com/xpilot-project/xpilot/releases/download/v" + programVersion + "/xPilot-" + - programVersion + "-linux-x64-installer.run"; + programVersion + "-linux-x64"; + switch (variant) { + case 0: + url += "-ubuntu-latest-installer.run"; + break; + case 1: + url += "-ubuntu-18.04-installer.run"; + break; + } break; case 1: programName = "Swift"; @@ -71,12 +83,24 @@ int download::downloadPilotClient(int program) { break; } - std::cout << "Downloading " << programName << " " << programVersion << std::endl; + std::cout << "Downloading " << programName << " " << programVersion << " variant " << variant + 1 << std::endl; CURL* curl = curl_easy_init(); CURLcode res; - std::string outputName = "/tmp/vatsim-manager/" + programName + ".run"; + std::string outputName = "/tmp/vatsim-manager/" + programName + programVersion + "-" + std::to_string(variant) + ".run"; + + system(("find " + outputName + " > /tmp/vatsim-manager/findinstaller 2>&1") + .c_str()); + //TODO better solution for no reaction to if + if (!ut_dl.iequals(ut_dl.readFile("/tmp/vatsim-manager/findinstaller"), outputName) || force) {} + else { + //TODO detect old versions + std::cout << "Found installer in /tmp/vatsim-manager/" << std::endl << "If you encounter errors or this is an old " << + "installer, use --force-download" << std::endl << std::endl; + return 1; + } + FILE* output = fopen(outputName.c_str(), "wb"); if (!curl) { diff --git a/download.h b/download.h index 6213da1..c516618 100644 --- a/download.h +++ b/download.h @@ -10,7 +10,7 @@ class download { public: static std::vector downloadPilotVersions(); - int downloadPilotClient(int program); + int downloadPilotClient(int program, int variant, bool force); private: static size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata); static int progress_func(void* ptr, double TotalToDownload, double NowDownloaded, double TotalToUpload, diff --git a/main.cpp b/main.cpp index 5098135..243dc30 100644 --- a/main.cpp +++ b/main.cpp @@ -20,10 +20,13 @@ int install(const char* program, bool forceDownload) { string programName; int programIndex; + int programVar = 0; if (ut.iequals(program, "xpilot")) { programName = "xPilot"; programIndex = 0; + programVar = ut.askForChoice("What build of xPilot would you like to download?", + {"Ubuntu Latest", "Ubuntu 18.04"}); } else if (ut.iequals(program, "swift")) { programName = "Swift"; programIndex = 1; @@ -34,16 +37,7 @@ int install(const char* program, bool forceDownload) { string cmdOut; if (ut.askForConfirmation(programName.c_str())) { - system(("find /tmp/vatsim-manager/" + programName + ".run > /tmp/vatsim-manager/findinstaller 2>&1") - .c_str()); - if (!ut.iequals(ut.readFile("/tmp/vatsim-manager/findinstaller"), "/tmp/vatsim-manager/" + - programName + ".run") || forceDownload) { - dl.downloadPilotClient(programIndex); - } else { - //TODO detect old versions - cout << "Found installer in /tmp/vatsim-manager/" << endl << "If you encounter errors or this is an old " << - "installer, use --force-download" << endl << endl; - } + dl.downloadPilotClient(programIndex, programVar, forceDownload); system(("chmod +x /tmp/vatsim-manager/" + programName + ".run").c_str()); system(("/tmp/vatsim-manager/" + programName + ".run").c_str()); } diff --git a/utils.cpp b/utils.cpp index c9706be..2dffccb 100644 --- a/utils.cpp +++ b/utils.cpp @@ -30,6 +30,19 @@ bool utils::askForConfirmation(const char* program) { } +int utils::askForChoice(const char* question, std::vector choices) { + char in[1]; + + std::cout << question << std::endl; + for (auto i = 0; i < choices.size(); ++i) { + std::cout << "[" << i + 1 << "] : " << choices[i] << std::endl; + } + std::cout << std::endl << "Enter an option: "; + std::cin.getline(in, 2, '\n'); + + return std::stoi(in) - 1; +} + std::string utils::readFile(const char* filename) { std::filebuf fb; fb.open(filename, std::ios::in); @@ -48,7 +61,6 @@ std::string utils::readFile(const char* filename) { } int utils::findInVector(std::vector arr, std::string item) { - for (auto i = 0; i < arr.size(); ++i) { if (iequals(arr[i], item)) return i; diff --git a/utils.h b/utils.h index 792586d..047a539 100644 --- a/utils.h +++ b/utils.h @@ -10,6 +10,7 @@ class utils { public: bool iequals(const std::string& a, const std::string& b); //strcmp but case insensitive bool askForConfirmation(const char* program); + int askForChoice(const char* question, std::vector choices); int findInVector(std::vector v, std::string x); std::string readFile(const char* filename); }; \ No newline at end of file From 2b0d5c91d71ebc7af9b0ed99f46356a783144a67 Mon Sep 17 00:00:00 2001 From: Cian Ormond Date: Wed, 6 Apr 2022 12:48:32 -0700 Subject: [PATCH 2/2] Update version number --- global.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.h b/global.h index 1c15965..9d18da5 100644 --- a/global.h +++ b/global.h @@ -5,5 +5,5 @@ class global { public: - const char* version = "0.2.1"; + const char* version = "0.2.2"; }; \ No newline at end of file