Basic foundation done

This commit is contained in:
Anonymous275
2022-01-19 21:36:52 +02:00
parent 62d5936f71
commit 2b73c120a8
9 changed files with 138 additions and 31 deletions

View File

@@ -82,7 +82,7 @@ bool HTTP::ProgressBar(size_t c, size_t t){
static double progress_bar_adv;
progress_bar_adv = round(double(c) / double(t) * 25);
std::cout << "\r";
std::cout << "Progress : [ ";
std::cout << "Progress: [ ";
std::cout << round(double(c) / double(t) * 100);
std::cout << "% ] [";
int i;
@@ -102,15 +102,14 @@ bool HTTP::Download(const std::string &IP, const std::string &Path) {
isDownload = false;
if(Ret.empty())return false;
std::cout << "\n";
std::ofstream File(Path, std::ios::binary);
if(File.is_open()) {
File << Ret;
File.close();
std::cout << "\n";
LOG(INFO) << "Download Complete!";
LOG(INFO) << "Download complete!";
} else {
LOG(INFO) << "Failed to open file directory: " << Path;
LOG(ERROR) << "Failed to open file directory: " << Path;
return false;
}
return true;

View File

@@ -6,6 +6,7 @@
#include "Launcher.h"
#include "Logger.h"
#include "Http.h"
#include "Json.h"
VersionParser::VersionParser(const std::string &from_string) {
std::string token;
@@ -78,3 +79,57 @@ void Launcher::UpdateCheck() {
Relaunch();
}else LOG(INFO) << "Launcher version is up to date";
}
size_t DirCount(const std::filesystem::path& path){
return (size_t)std::distance(std::filesystem::directory_iterator{path}, std::filesystem::directory_iterator{});
}
void Launcher::ResetMods() {
if (!fs::exists(MPUserPath)) {
fs::create_directories(MPUserPath);
return;
}
if (DirCount(fs::path(MPUserPath)) > 3) {
LOG(WARNING) << "mods/multiplayer will be cleared in 15 seconds, close to abort";
std::this_thread::sleep_for(std::chrono::seconds(15));
}
fs::remove_all(MPUserPath);
fs::create_directories(MPUserPath);
}
void Launcher::EnableMP() {
std::string File(BeamUserPath + "mods\\db.json");
if(!fs::exists(File))return;
auto Size = fs::file_size(File);
if(Size < 2)return;
std::ifstream db(File);
if(db.is_open()) {
std::string Data(Size, 0);
db.read(&Data[0], std::streamsize(Size));
db.close();
Json::Document d;
d.Parse(Data.c_str());
if(Data.at(0) != '{' || d.HasParseError())return;
if(!d["mods"].IsNull() && !d["mods"]["multiplayerbeammp"].IsNull()){
d["mods"]["multiplayerbeammp"]["active"] = true;
Json::StringBuffer buffer;
Json::Writer<Json::StringBuffer> writer(buffer);
d.Accept(writer);
std::ofstream ofs(File);
if(ofs.is_open()){
ofs << buffer.GetString();
ofs.close();
} else {
LOG(ERROR) << "Failed to write " << File;
}
}
}
}
void Launcher::SetupMOD() {
ResetMods();
EnableMP();
LOG(INFO) << "Downloading mod please wait";
HTTP::Download("https://backend.beammp.com/builds/client?download=true"
"&pk=" + PublicKey + "&branch=" + TargetBuild, MPUserPath + "\\BeamMP.zip");
}