link UI data with launcher data

fix auto update
fix crash caused by stdout
This commit is contained in:
Anonymous275 2022-08-19 14:01:35 +03:00
parent 4e8421a515
commit eeac661bd0
9 changed files with 81 additions and 154 deletions

View File

@ -18,6 +18,7 @@ struct VersionParser {
std::vector<std::string> split;
std::vector<size_t> data;
};
struct HKEY__;
typedef HKEY__ *HKEY;
@ -31,14 +32,11 @@ class Launcher {
std::string Login(const std::string& fields);
void SendIPC(const std::string& Data, bool core = true);
void RunDiscordRPC();
void QueryRegistry();
void WaitForGame();
void LoadConfig();
void LaunchGame();
void CheckKey();
void SetupMOD();
static void AdminRelaunch();
static void Relaunch();
static std::string QueryValue(HKEY& hKey, const char* Name);
public: // Getters and Setters
@ -54,11 +52,9 @@ class Launcher {
private: // functions
void HandleIPC(const std::string& Data);
std::string GetLocalAppdata();
void UpdatePresence();
void RichPresence();
void WindowsInit();
void UpdateCheck();
void ResetMods();
void EnableMP();
void ListenIPC();
@ -68,7 +64,7 @@ class Launcher {
static inline std::thread EntryThread{};
static inline VersionParser SupportedVersion{"0.25.4.0"};
static inline std::string Version{"2.0"};
static inline std::string FullVersion{Version + ".1"};
static inline std::string FullVersion{Version + ".99"};
private: // variables
uint32_t GamePID{0};
@ -87,9 +83,7 @@ class Launcher {
std::string DiscordMessage{};
Server ServerHandler{this};
std::string TargetBuild{"default"};
std::string GameConfigPath{};
std::string ProfileConfigPath{};
std::string CacheConfigPath{};
std::string LauncherCache{};
static inline std::atomic<bool> Shutdown{false}, Exit{false};
std::unique_ptr<IPC> IPCToGame{};
std::unique_ptr<IPC> IPCFromGame{};
@ -102,10 +96,8 @@ class ShutdownException : public std::runtime_error {
};
struct UIData {
static inline std::string GamePath, ProfilePath, CachePath, Build, PublicKey, UserRole, Username;
static inline std::string GamePath, ProfilePath, CachePath, Build, PublicKey, UserRole, Username, GameVer;
static inline bool LoginAuth{false}, Console{false};
};
void UpdateKey(const std::string& newKey);

View File

@ -10,4 +10,7 @@
class Log {
public:
static void Init();
static void ConsoleOutput(bool enable);
private:
static inline el::Configurations Conf{};
};

View File

@ -25,15 +25,21 @@ void Launcher::LoadConfig() {
for (char& c : TargetBuild) c = char(tolower(c));
} else LOG(ERROR) << "Failed to get 'Build' string from config";
if (GamePath.is_string()) GameConfigPath = GamePath.as_string()->get();
if (GamePath.is_string()) BeamRoot = GamePath.as_string()->get();
else LOG(ERROR) << "Failed to get 'GamePath' string from config";
if (ProfilePath.is_string()) ProfileConfigPath = ProfilePath.as_string()->get();
if (ProfilePath.is_string()) {
BeamUserPath = ProfilePath.as_string()->get();
if (!BeamUserPath.empty()) {
MPUserPath = BeamUserPath + "mods\\multiplayer";
}
}
else LOG(ERROR) << "Failed to get 'ProfilePath' string from config";
if (CachePath.is_string()) CacheConfigPath = CachePath.as_string()->get();
if (CachePath.is_string()) LauncherCache = CachePath.as_string()->get();
else LOG(ERROR) << "Failed to get 'CachePath' string from config";
BeamVersion = UIData::GameVer;
} else {
LOG(FATAL) << "Failed to find config on disk!";
throw ShutdownException("Fatal Error");

View File

@ -31,7 +31,6 @@ Launcher::Launcher() :
WindowsInit();
SetUnhandledExceptionFilter(CrashHandler);
LOG(INFO) << "Starting Launcher V" << FullVersion;
UpdateCheck();
}
void Launcher::Abort() {
@ -184,72 +183,6 @@ std::string Launcher::QueryValue(HKEY& hKey, const char* Name) {
}
return {};
}
std::string Launcher::GetLocalAppdata() {
PWSTR folderPath = nullptr;
HRESULT hr =
SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &folderPath);
if (!SUCCEEDED(hr)) {
LOG(FATAL) << "Failed to get path of localAppData";
throw ShutdownException("Fatal Error");
}
_bstr_t bstrPath(folderPath);
std::string Path((char*)bstrPath);
CoTaskMemFree(folderPath);
if (!Path.empty()) {
Path += "\\BeamNG.drive\\";
VersionParser GameVer(BeamVersion);
Path += GameVer.split[0] + '.' + GameVer.split[1] + '\\';
return Path;
}
return {};
}
void Launcher::QueryRegistry() {
HKEY BeamNG;
LONG RegRes =
RegOpenKeyExA(HKEY_CURRENT_USER, R"(Software\BeamNG\BeamNG.drive)", 0,
KEY_READ, &BeamNG);
if (RegRes == ERROR_SUCCESS) {
BeamRoot = QueryValue(BeamNG, "rootpath");
BeamVersion = QueryValue(BeamNG, "version");
BeamUserPath = QueryValue(BeamNG, "userpath_override");
RegCloseKey(BeamNG);
if (BeamUserPath.empty() && !BeamVersion.empty()) {
BeamUserPath = GetLocalAppdata();
} else if (!BeamUserPath.empty() && !BeamVersion.empty()) {
VersionParser GameVer(BeamVersion);
BeamUserPath += GameVer.split[0] + '.' + GameVer.split[1] + '\\';
}
if (!BeamUserPath.empty()) {
MPUserPath = BeamUserPath + "mods\\multiplayer";
}
if (!BeamRoot.empty() && !BeamVersion.empty() && !BeamUserPath.empty())
return;
}
LOG(FATAL)
<< "Please launch the game at least once, failed to read registry "
"key Software\\BeamNG\\BeamNG.drive";
throw ShutdownException("Fatal Error");
}
void Launcher::AdminRelaunch() {
system("cls");
ShellExecuteA(nullptr, "runas", fs::current_path().string().c_str(), nullptr,
nullptr, SW_SHOWNORMAL);
ShowWindow(GetConsoleWindow(), 0);
throw ShutdownException("Relaunching");
}
void Launcher::Relaunch() {
ShellExecuteA(nullptr, "open", fs::current_path().string().c_str(), nullptr,
nullptr, SW_SHOWNORMAL);
ShowWindow(GetConsoleWindow(), 0);
std::this_thread::sleep_for(std::chrono::seconds(1));
throw ShutdownException("Relaunching");
}
const std::string& Launcher::getFullVersion() {
return FullVersion;

View File

@ -7,13 +7,13 @@
INITIALIZE_EASYLOGGINGPP
using namespace el;
void Log::Init() {
Configurations Conf;
Conf.setToDefault();
std::string DFormat(
"%datetime{[%d/%M/%y %H:%m:%s]} %fbase:%line [%level] %msg");
Conf.setGlobally(ConfigurationType::Format,
"%datetime{[%d/%M/%y %H:%m:%s]} [%level] %msg");
Conf.setGlobally(ConfigurationType::LogFlushThreshold, "2");
Conf.setGlobally(ConfigurationType::ToStandardOutput, "false");
Conf.set(Level::Verbose, ConfigurationType::Format, DFormat);
Conf.set(Level::Debug, ConfigurationType::Format, DFormat);
Conf.set(Level::Trace, ConfigurationType::Format, DFormat);
@ -25,3 +25,8 @@ void Log::Init() {
Loggers::addFlag(LoggingFlag::HierarchicalLogging);
Loggers::setLoggingLevel(Level::Global);
}
void Log::ConsoleOutput(bool enable) {
Conf.setGlobally(ConfigurationType::ToStandardOutput, enable ? "true":"false");
Loggers::reconfigureAllLoggers(Conf);
}

View File

@ -87,14 +87,16 @@ bool HTTP::ProgressBar(size_t c, size_t t) {
if (isDownload) {
static double progress_bar_adv;
progress_bar_adv = round(double(c) / double(t) * 25);
std::cout << "\r";
std::cout << "Progress: [ ";
std::cout << round(double(c) / double(t) * 100);
std::cout << "% ] [";
int i;
for (i = 0; i <= progress_bar_adv; i++) std::cout << "#";
for (i = 0; i < 25 - progress_bar_adv; i++) std::cout << ".";
std::cout << "]";
if (UIData::Console) {
std::cout << "\r";
std::cout << "Progress: [ ";
std::cout << round(double(c) / double(t) * 100);
std::cout << "% ] [";
int i;
for (i = 0; i <= progress_bar_adv; i++) std::cout << "#";
for (i = 0; i < 25 - progress_bar_adv; i++) std::cout << ".";
std::cout << "]";
}
}
if (Launcher::Terminated()) {
CliRef.load()->stop();
@ -114,7 +116,11 @@ bool HTTP::Download(const std::string& IP, const std::string& Path, DownloadProg
isDownload = false;
if (Ret.empty()) return false;
std::cout << "\n";
if (UIData::Console) {
std::cout << "\n";
}
std::ofstream File(Path, std::ios::binary);
if (File.is_open()) {
File << Ret;

View File

@ -34,56 +34,6 @@ bool VersionParser::operator==(const VersionParser& rhs) const noexcept {
return std::is_eq(*this <=> rhs);
}
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!";
throw ShutdownException("Fatal Error");
}
}
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 (VersionParser(RemoteVer) > VersionParser(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";
}
size_t DirCount(const std::filesystem::path& path) {
return (size_t)std::distance(std::filesystem::directory_iterator{path},
std::filesystem::directory_iterator{});

View File

@ -401,6 +401,7 @@ bool MyApp::OnInit() {
CheckKey();
WindowsConsole(UIData::Console);
Log::ConsoleOutput(UIData::Console);
auto* MainFrame = new MyMainFrame();
MyMainFrame::MainFrameInstance = MainFrame;
@ -504,6 +505,8 @@ std::string GetPictureName() {
return "";
}
/////////// Progress Bar Function ///////////
bool ProgressBar (size_t c, size_t t) {
int Percent = int(round(double(c) / double(t) * 100));
MyMainFrame::MainFrameInstance->UpdateBar->SetValue(Percent);
@ -511,6 +514,24 @@ bool ProgressBar (size_t c, size_t t) {
return true;
}
/////////// Relaunch Functions ///////////
void AdminRelaunch(const std::string& executable) {
system("cls");
ShellExecuteA(nullptr, "runas", executable.c_str(), nullptr,
nullptr, SW_SHOWNORMAL);
ShowWindow(GetConsoleWindow(), 0);
throw ShutdownException("Launcher Relaunching");
}
void Relaunch(const std::string& executable) {
ShellExecuteA(nullptr, "open", executable.c_str(), nullptr,
nullptr, SW_SHOWNORMAL);
ShowWindow(GetConsoleWindow(), 0);
std::this_thread::sleep_for(std::chrono::seconds(1));
throw ShutdownException("Launcher Relaunching");
}
/////////// Update Check Function ///////////
void UpdateCheck() {
std::string link;
@ -525,11 +546,12 @@ void UpdateCheck() {
if (fallback) {
link = "https://backup1.beammp.com/builds/launcher?download=true";
} else link = "https://beammp.com/builds/launcher?download=true";
auto CurrentPath = fs::current_path();
std::string EP(CurrentPath.append("BeamMP-Launcher.exe").string()), Back(CurrentPath.append("BeamMP-Launcher.back").string());
LOG(INFO) <<EP + " AND " + Back;
const auto CurrentPath = fs::current_path();
std::string EP((CurrentPath/"BeamMP-Launcher.exe").string()), Tmp(EP + ".tmp");
std::string Back((CurrentPath/"BeamMP-Launcher.back").string());
if (fs::exists(Back)) remove(Back.c_str());
std::string RemoteVer;
for (char& c : HTTP) {
if (std::isdigit(c) || c == '.') {
@ -540,24 +562,28 @@ void UpdateCheck() {
if (VersionParser(RemoteVer) > VersionParser(Launcher::FullVersion)) {
system("cls");
MyMainFrame::MainFrameInstance->txtUpdate->SetLabel("Downloading...");
if (std::rename(EP.c_str(), Back.c_str())) {
wxMessageBox("Failed to create a backup!", "Error");
}
if (!HTTP::Download(link, EP, ProgressBar)) {
if (!HTTP::Download(link, Tmp, ProgressBar)) {
wxMessageBox("Launcher Update failed! trying again...", "Error");
std::this_thread::sleep_for(std::chrono::seconds(2));
if (!HTTP::Download(link, EP, ProgressBar)) {
if (!HTTP::Download(link, Tmp, ProgressBar)) {
wxMessageBox("Launcher Update failed!", "Error");
std::this_thread::sleep_for(std::chrono::seconds(5));
Launcher::AdminRelaunch();
AdminRelaunch(EP);
}
}
Launcher::Relaunch();
} else MyMainFrame::MainFrameInstance->txtUpdate->SetLabel("BeamMP V" + Launcher::FullVersion);
MyMainFrame::MainFrameInstance->Destroy();
if (std::rename(EP.c_str(), Back.c_str()) || std::rename(Tmp.c_str(), EP.c_str())) {
LOG(ERROR) << "Failed to create a backup!";
}
Relaunch(EP);
} else MyMainFrame::MainFrameInstance->txtUpdate->SetLabel("BeamMP V" + Launcher::FullVersion);
}
/////////// TestFrame Function ///////////
@ -781,6 +807,7 @@ void MyMainFrame::GameVersionLabel() {
std::string read = jsonRead();
if (!read.empty()) {
txtGameVersion->SetLabel(read);
UIData::GameVer = read;
VersionParser CurrentVersion(read);
if (CurrentVersion > Launcher::SupportedVersion)
txtGameVersion->SetForegroundColour(wxColour(255, 173, 0));
@ -790,6 +817,7 @@ void MyMainFrame::GameVersionLabel() {
} else {
txtGameVersion->SetLabel(wxT("NA"));
txtGameVersion->SetForegroundColour("red");
UIData::GameVer = "";
}
}
@ -831,6 +859,10 @@ void MyMainFrame::OnClickSettings(wxCommandEvent& event WXUNUSED(event)) {
/////////// OnClick Launch Event ///////////
void MyMainFrame::OnClickLaunch(wxCommandEvent& event WXUNUSED(event)) {
static bool FirstTime = true;
if(UIData::GameVer.empty()) {
wxMessageBox("Game path is invalid please check settings", "Error");
return;
}
if (Launcher::EntryThread.joinable()) Launcher::EntryThread.join();
Launcher::EntryThread = std::thread([&]() {
entry();
@ -889,6 +921,7 @@ void MyAccountFrame::OnClickLogout(wxCommandEvent& event WXUNUSED(event)) {
/////////// OnClick Console Event ///////////
void MySettingsFrame::OnClickConsole(wxCommandEvent& event) {
WindowsConsole(checkConsole->IsChecked());
Log::ConsoleOutput(checkConsole->IsChecked());
bool status = event.IsChecked();
UpdateConfig("Console", status);
UIData::Console = status;

View File

@ -13,7 +13,6 @@ void entry() {
launcher.RunDiscordRPC();
launcher.LoadConfig(); // check if json (issue)
launcher.CheckKey();
launcher.QueryRegistry();
// UI call
// launcher.SetupMOD();
launcher.LaunchGame();