Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from wiggleforlife/fixes/xpilot-executables
Browse files Browse the repository at this point in the history
  • Loading branch information
wiggleforlife authored Apr 6, 2022
2 parents 6abc9d1 + 2b0d5c9 commit b7a9633
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
34 changes: 29 additions & 5 deletions download.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include "download.h"
#include <vector>
#include <iostream>
#include <curl/curl.h>
#include <string>
#include <sstream>
#include <math.h>

#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;
Expand Down Expand Up @@ -52,7 +56,7 @@ std::vector<std::string> 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;
Expand All @@ -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";
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion download.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class download {
public:
static std::vector<std::string> 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,
Expand Down
2 changes: 1 addition & 1 deletion global.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

class global {
public:
const char* version = "0.2.1";
const char* version = "0.2.2";
};
14 changes: 4 additions & 10 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
Expand Down
14 changes: 13 additions & 1 deletion utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ bool utils::askForConfirmation(const char* program) {

}

int utils::askForChoice(const char* question, std::vector<std::string> 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);
Expand All @@ -48,7 +61,6 @@ std::string utils::readFile(const char* filename) {
}

int utils::findInVector(std::vector<std::string> arr, std::string item) {

for (auto i = 0; i < arr.size(); ++i) {
if (iequals(arr[i], item))
return i;
Expand Down
1 change: 1 addition & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> choices);
int findInVector(std::vector<std::string> v, std::string x);
std::string readFile(const char* filename);
};

0 comments on commit b7a9633

Please sign in to comment.