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
+3 -3
View File
@@ -39,8 +39,8 @@ add_executable(${PROJECT_NAME}
src/gui/Gui.cpp include/Json.h src/gui/Gui.cpp include/Json.h
src/gui/gifs.cpp src/gui/gifs.h src/gui/gifs.cpp src/gui/gifs.h
src/Network/Http.cpp include/Http.h src/Network/Http.cpp include/Http.h
src/Login.cpp src/Config.cpp src/Network/Login.cpp src/Network/Update.cpp
src/Discord.cpp src/Discord.cpp src/Config.cpp
) )
@@ -55,4 +55,4 @@ endif(WIN32)
add_definitions(-DELPP_NO_DEFAULT_LOG_FILE) add_definitions(-DELPP_NO_DEFAULT_LOG_FILE)
target_include_directories(${PROJECT_NAME} PRIVATE "include") target_include_directories(${PROJECT_NAME} PRIVATE "include")
target_include_directories(${PROJECT_NAME} PRIVATE "include/rapidjson/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")
+13 -7
View File
@@ -4,32 +4,38 @@
/// ///
#pragma once #pragma once
#include <filesystem>
#include <string> #include <string>
#include <thread> #include <thread>
namespace fs = std::filesystem;
class Launcher { class Launcher {
public: //constructors public: //constructors
Launcher(int argc, char* argv[]); Launcher(int argc, char* argv[]);
~Launcher(); ~Launcher();
public: //available functions public: //available functions
std::string Login(const std::string& fields); std::string Login(const std::string& fields);
void runDiscordRPC(); bool Terminate() const;
void loadConfig(); void RunDiscordRPC();
void launchGame(); void LoadConfig();
void checkKey(); void LaunchGame();
void CheckKey();
public: //Getters public: //Getters
const std::string& getFullVersion(); const std::string& getFullVersion();
const std::string& getWorkingDir();
const std::string& getUserRole(); const std::string& getUserRole();
const std::string& getVersion(); const std::string& getVersion();
private: //functions private: //functions
void richPresence(); void AdminRelaunch();
void RichPresence();
void WindowsInit(); void WindowsInit();
void UpdateCheck();
void Relaunch();
private: //variables private: //variables
bool EnableUI = true; bool EnableUI = true;
bool Shutdown = false; bool Shutdown = false;
std::string DirPath{};
bool LoginAuth = false; bool LoginAuth = false;
fs::path CurrentPath{};
std::string UserRole{}; std::string UserRole{};
std::string PublicKey{}; std::string PublicKey{};
std::thread DiscordRPC{}; std::thread DiscordRPC{};
+1 -6
View File
@@ -5,15 +5,10 @@
#include <tomlplusplus/toml.hpp> #include <tomlplusplus/toml.hpp>
#include <filesystem>
#include "Launcher.h" #include "Launcher.h"
#include "Logger.h" #include "Logger.h"
namespace fs = std::filesystem; void Launcher::LoadConfig() {
void Launcher::loadConfig() {
if(fs::exists("Launcher.cfg")) { if(fs::exists("Launcher.cfg")) {
toml::table config = toml::parse_file("Launcher.cfg"); toml::table config = toml::parse_file("Launcher.cfg");
auto ui = config["UI"]; auto ui = config["UI"];
+3 -3
View File
@@ -7,7 +7,7 @@
#include "Launcher.h" #include "Launcher.h"
#include "Logger.h" #include "Logger.h"
void Launcher::richPresence() { void Launcher::RichPresence() {
Discord_Initialize("629743237988352010", nullptr, 1,nullptr); Discord_Initialize("629743237988352010", nullptr, 1,nullptr);
int64_t Start{}; int64_t Start{};
while(!Shutdown) { while(!Shutdown) {
@@ -23,6 +23,6 @@ void Launcher::richPresence() {
Discord_ClearPresence(); Discord_ClearPresence();
} }
void Launcher::runDiscordRPC() { void Launcher::RunDiscordRPC() {
DiscordRPC = std::thread(&Launcher::richPresence, this); DiscordRPC = std::thread(&Launcher::RichPresence, this);
} }
+22 -7
View File
@@ -9,20 +9,27 @@
#include <windows.h> #include <windows.h>
#include <shellapi.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(); Log::Init();
WindowsInit(); WindowsInit();
LOG(INFO) << "Starting Launcher V" << FullVersion;
UpdateCheck();
} }
Launcher::~Launcher() { Launcher::~Launcher() {
Shutdown = true; Shutdown = true;
LOG(INFO) << "Shutting down";
if(DiscordRPC.joinable()) { if(DiscordRPC.joinable()) {
DiscordRPC.join(); DiscordRPC.join();
} }
} }
void Launcher::launchGame() { bool Launcher::Terminate() const {
return Shutdown;
}
void Launcher::LaunchGame() {
ShellExecuteA(nullptr, nullptr, "steam://rungameid/284160", nullptr, nullptr, SW_SHOWNORMAL); ShellExecuteA(nullptr, nullptr, "steam://rungameid/284160", nullptr, nullptr, SW_SHOWNORMAL);
//ShowWindow(GetConsoleWindow(), HIDE_WINDOW); //ShowWindow(GetConsoleWindow(), HIDE_WINDOW);
} }
@@ -36,10 +43,6 @@ const std::string& Launcher::getFullVersion() {
return FullVersion; return FullVersion;
} }
const std::string& Launcher::getWorkingDir() {
return DirPath;
}
const std::string &Launcher::getVersion() { const std::string &Launcher::getVersion() {
return Version; return Version;
} }
@@ -48,5 +51,17 @@ const std::string& Launcher::getUserRole() {
return UserRole; 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;
}
+1 -4
View File
@@ -4,14 +4,11 @@
/// ///
#include "Launcher.h" #include "Launcher.h"
#include <filesystem>
#include "Logger.h" #include "Logger.h"
#include <fstream> #include <fstream>
#include "Http.h" #include "Http.h"
#include "Json.h" #include "Json.h"
namespace fs = std::filesystem;
void UpdateKey(const char* newKey){ void UpdateKey(const char* newKey){
if(newKey){ if(newKey){
std::ofstream Key("key"); std::ofstream Key("key");
@@ -77,7 +74,7 @@ std::string Launcher::Login(const std::string& fields) {
return GetFail("Invalid message parsing!"); return GetFail("Invalid message parsing!");
} }
void Launcher::checkKey() { void Launcher::CheckKey() {
if(fs::exists("key") && fs::file_size("key") < 100){ if(fs::exists("key") && fs::file_size("key") < 100){
std::ifstream Key("key"); std::ifstream Key("key");
if(Key.is_open()) { if(Key.is_open()) {
+54
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";
}
+6 -4
View File
@@ -7,9 +7,11 @@
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
Launcher launcher(argc, argv); Launcher launcher(argc, argv);
launcher.runDiscordRPC(); if(!launcher.Terminate()) {
launcher.loadConfig(); launcher.RunDiscordRPC();
launcher.checkKey(); launcher.LoadConfig();
//UI call launcher.CheckKey();
//UI call
}
return 0; return 0;
} }