Update checking and minor tweaks

This commit is contained in:
Anonymous275
2022-01-18 16:20:42 +02:00
parent c9c4159367
commit 674aae6b7c
8 changed files with 103 additions and 34 deletions

View File

@@ -39,8 +39,8 @@ add_executable(${PROJECT_NAME}
src/gui/Gui.cpp include/Json.h
src/gui/gifs.cpp src/gui/gifs.h
src/Network/Http.cpp include/Http.h
src/Login.cpp src/Config.cpp
src/Discord.cpp
src/Network/Login.cpp src/Network/Update.cpp
src/Discord.cpp src/Config.cpp
)
@@ -55,4 +55,4 @@ endif(WIN32)
add_definitions(-DELPP_NO_DEFAULT_LOG_FILE)
target_include_directories(${PROJECT_NAME} PRIVATE "include")
target_include_directories(${PROJECT_NAME} PRIVATE "include/rapidjson/include")
target_include_directories(${PROJECT_NAME} PRIVATE "include/discord-rpc/include")
target_include_directories(${PROJECT_NAME} PRIVATE "include/discord-rpc/include")

View File

@@ -4,32 +4,38 @@
///
#pragma once
#include <filesystem>
#include <string>
#include <thread>
namespace fs = std::filesystem;
class Launcher {
public: //constructors
Launcher(int argc, char* argv[]);
~Launcher();
public: //available functions
std::string Login(const std::string& fields);
void runDiscordRPC();
void loadConfig();
void launchGame();
void checkKey();
bool Terminate() const;
void RunDiscordRPC();
void LoadConfig();
void LaunchGame();
void CheckKey();
public: //Getters
const std::string& getFullVersion();
const std::string& getWorkingDir();
const std::string& getUserRole();
const std::string& getVersion();
private: //functions
void richPresence();
void AdminRelaunch();
void RichPresence();
void WindowsInit();
void UpdateCheck();
void Relaunch();
private: //variables
bool EnableUI = true;
bool Shutdown = false;
std::string DirPath{};
bool LoginAuth = false;
fs::path CurrentPath{};
std::string UserRole{};
std::string PublicKey{};
std::thread DiscordRPC{};

View File

@@ -5,15 +5,10 @@
#include <tomlplusplus/toml.hpp>
#include <filesystem>
#include "Launcher.h"
#include "Logger.h"
namespace fs = std::filesystem;
void Launcher::loadConfig() {
void Launcher::LoadConfig() {
if(fs::exists("Launcher.cfg")) {
toml::table config = toml::parse_file("Launcher.cfg");
auto ui = config["UI"];

View File

@@ -7,7 +7,7 @@
#include "Launcher.h"
#include "Logger.h"
void Launcher::richPresence() {
void Launcher::RichPresence() {
Discord_Initialize("629743237988352010", nullptr, 1,nullptr);
int64_t Start{};
while(!Shutdown) {
@@ -23,6 +23,6 @@ void Launcher::richPresence() {
Discord_ClearPresence();
}
void Launcher::runDiscordRPC() {
DiscordRPC = std::thread(&Launcher::richPresence, this);
void Launcher::RunDiscordRPC() {
DiscordRPC = std::thread(&Launcher::RichPresence, this);
}

View File

@@ -9,20 +9,27 @@
#include <windows.h>
#include <shellapi.h>
Launcher::Launcher(int argc, char* argv[]) : DirPath(argv[0]), DiscordMessage("Just launched") {
DirPath = DirPath.substr(0, DirPath.find_last_of("\\/") + 1);
Launcher::Launcher(int argc, char* argv[]) : CurrentPath(std::filesystem::path(argv[0])), DiscordMessage("Just launched") {
Log::Init();
WindowsInit();
LOG(INFO) << "Starting Launcher V" << FullVersion;
UpdateCheck();
}
Launcher::~Launcher() {
Shutdown = true;
LOG(INFO) << "Shutting down";
if(DiscordRPC.joinable()) {
DiscordRPC.join();
}
}
void Launcher::launchGame() {
bool Launcher::Terminate() const {
return Shutdown;
}
void Launcher::LaunchGame() {
ShellExecuteA(nullptr, nullptr, "steam://rungameid/284160", nullptr, nullptr, SW_SHOWNORMAL);
//ShowWindow(GetConsoleWindow(), HIDE_WINDOW);
}
@@ -36,10 +43,6 @@ const std::string& Launcher::getFullVersion() {
return FullVersion;
}
const std::string& Launcher::getWorkingDir() {
return DirPath;
}
const std::string &Launcher::getVersion() {
return Version;
}
@@ -48,5 +51,17 @@ const std::string& Launcher::getUserRole() {
return UserRole;
}
void Launcher::AdminRelaunch() {
system("cls");
ShellExecuteA(nullptr, "runas", CurrentPath.string().c_str(), nullptr, nullptr, SW_SHOWNORMAL);
ShowWindow(GetConsoleWindow(),0);
Shutdown = true;
}
void Launcher::Relaunch() {
ShellExecuteA(nullptr, "open", CurrentPath.string().c_str(), nullptr, nullptr, SW_SHOWNORMAL);
ShowWindow(GetConsoleWindow(),0);
std::this_thread::sleep_for(std::chrono::seconds(1));
Shutdown = true;
}

View File

@@ -4,14 +4,11 @@
///
#include "Launcher.h"
#include <filesystem>
#include "Logger.h"
#include <fstream>
#include "Http.h"
#include "Json.h"
namespace fs = std::filesystem;
void UpdateKey(const char* newKey){
if(newKey){
std::ofstream Key("key");
@@ -77,7 +74,7 @@ std::string Launcher::Login(const std::string& fields) {
return GetFail("Invalid message parsing!");
}
void Launcher::checkKey() {
void Launcher::CheckKey() {
if(fs::exists("key") && fs::file_size("key") < 100){
std::ifstream Key("key");
if(Key.is_open()) {

54
src/Network/Update.cpp Normal file
View File

@@ -0,0 +1,54 @@
///
/// Created by Anonymous275 on 1/18/22
/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info.
///
#include "Launcher.h"
#include "Logger.h"
#include "Http.h"
void Launcher::UpdateCheck() {
std::string link;
std::string HTTP = HTTP::Get("https://beammp.com/builds/launcher?version=true");
bool fallback = false;
if(HTTP.find_first_of("0123456789") == std::string::npos){
HTTP = HTTP::Get("https://backup1.beammp.com/builds/launcher?version=true");
fallback = true;
if(HTTP.find_first_of("0123456789") == std::string::npos) {
LOG(FATAL) << "Primary Servers Offline! sorry for the inconvenience!";
}
}
if(fallback){
link = "https://backup1.beammp.com/builds/launcher?download=true";
}else link = "https://beammp.com/builds/launcher?download=true";
std::string EP(CurrentPath.string()), Back(CurrentPath.parent_path().string() + "\\BeamMP-Launcher.back");
if(fs::exists(Back))remove(Back.c_str());
std::string RemoteVer;
for(char& c : HTTP) {
if(std::isdigit(c) || c == '.') {
RemoteVer += c;
}
}
if(RemoteVer > FullVersion){
system("cls");
LOG(INFO) << "Update found! Downloading...";
if(std::rename(EP.c_str(), Back.c_str())){
LOG(ERROR) << "Failed to create a backup!";
}
if(!HTTP::Download(link, EP)){
LOG(ERROR) << "Launcher Update failed! trying again...";
std::this_thread::sleep_for(std::chrono::seconds(2));
if(!HTTP::Download(link, EP)){
LOG(ERROR) << "Launcher Update failed!";
std::this_thread::sleep_for(std::chrono::seconds(5));
AdminRelaunch();
}
}
Relaunch();
}else LOG(INFO) << "Launcher version is up to date";
}

View File

@@ -7,9 +7,11 @@
int main(int argc, char* argv[]) {
Launcher launcher(argc, argv);
launcher.runDiscordRPC();
launcher.loadConfig();
launcher.checkKey();
//UI call
if(!launcher.Terminate()) {
launcher.RunDiscordRPC();
launcher.LoadConfig();
launcher.CheckKey();
//UI call
}
return 0;
}