Skip to content

Commit

Permalink
Switch to wstring for paths on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
WiserTixx committed Jan 26, 2025
1 parent 9e93fa3 commit 83a08cb
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 160 deletions.
2 changes: 1 addition & 1 deletion include/Http.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <string>
class HTTP {
public:
static bool Download(const std::string& IP, const std::string& Path);
static bool Download(const std::string& IP, const std::wstring& Path);
static std::string Post(const std::string& IP, const std::string& Fields);
static std::string Get(const std::string& IP);
static bool ProgressBar(size_t c, size_t t);
Expand Down
7 changes: 7 additions & 0 deletions include/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ void debug(const std::string& toPrint);
void error(const std::string& toPrint);
void info(const std::string& toPrint);
void warn(const std::string& toPrint);

void except(const std::wstring& toPrint);
void fatal(const std::wstring& toPrint);
void debug(const std::wstring& toPrint);
void error(const std::wstring& toPrint);
void info(const std::wstring& toPrint);
void warn(const std::wstring& toPrint);
std::string getDate();
8 changes: 4 additions & 4 deletions include/Security/Init.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#pragma once
#include <string>
void PreGame(const std::string& GamePath);
std::string CheckVer(const std::string& path);
void InitGame(const std::string& Dir);
std::string GetGameDir();
void PreGame(const std::wstring& GamePath);
std::string CheckVer(const std::wstring& path);
void InitGame(const std::wstring& Dir);
std::wstring GetGameDir();
void LegitimacyCheck();
void CheckLocalKey();
6 changes: 3 additions & 3 deletions include/Startup.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include <vector>

void InitLauncher();
std::string GetEP(const char* P = nullptr);
std::string GetGamePath();
std::wstring GetEP(const wchar_t* P = nullptr);
std::wstring GetGamePath();
std::string GetVer();
std::string GetPatch();
std::string GetEN();
std::wstring GetEN();
void ConfigInit();
133 changes: 133 additions & 0 deletions include/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
*/

#pragma once
#include <filesystem>
#include <fstream>
#include <locale>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <string>
#include <vector>

Expand All @@ -23,4 +28,132 @@ namespace Utils {
Val.push_back(s);
return Val;
};
inline std::string ToString(const std::wstring& w) {
return std::wstring_convert<std::codecvt<wchar_t, char, std::mbstate_t>>().to_bytes(w);
}
inline std::wstring ToWString(const std::string& s) {
return std::wstring_convert<std::codecvt<wchar_t, char, std::mbstate_t>>().from_bytes(s);
}
inline std::string GetSha256HashReallyFast(const std::string& filename) {
try {
EVP_MD_CTX* mdctx;
const EVP_MD* md;
uint8_t sha256_value[EVP_MAX_MD_SIZE];
md = EVP_sha256();
if (md == nullptr) {
throw std::runtime_error("EVP_sha256() failed");
}

mdctx = EVP_MD_CTX_new();
if (mdctx == nullptr) {
throw std::runtime_error("EVP_MD_CTX_new() failed");
}
if (!EVP_DigestInit_ex2(mdctx, md, NULL)) {
EVP_MD_CTX_free(mdctx);
throw std::runtime_error("EVP_DigestInit_ex2() failed");
}

std::ifstream stream(filename, std::ios::binary);

const size_t FileSize = std::filesystem::file_size(filename);
size_t Read = 0;
std::vector<char> Data;
while (Read < FileSize) {
Data.resize(size_t(std::min<size_t>(FileSize - Read, 4096)));
size_t RealDataSize = Data.size();
stream.read(Data.data(), std::streamsize(Data.size()));
if (stream.eof() || stream.fail()) {
RealDataSize = size_t(stream.gcount());
}
Data.resize(RealDataSize);
if (RealDataSize == 0) {
break;
}
if (RealDataSize > 0 && !EVP_DigestUpdate(mdctx, Data.data(), Data.size())) {
EVP_MD_CTX_free(mdctx);
throw std::runtime_error("EVP_DigestUpdate() failed");
}
Read += RealDataSize;
}
unsigned int sha256_len = 0;
if (!EVP_DigestFinal_ex(mdctx, sha256_value, &sha256_len)) {
EVP_MD_CTX_free(mdctx);
throw std::runtime_error("EVP_DigestFinal_ex() failed");
}
EVP_MD_CTX_free(mdctx);

std::string result;
for (size_t i = 0; i < sha256_len; i++) {
char buf[3];
sprintf(buf, "%02x", sha256_value[i]);
buf[2] = 0;
result += buf;
}
return result;
} catch (const std::exception& e) {
error("Sha256 hashing of '" + filename + "' failed: " + e.what());
return "";
}
}
inline std::string GetSha256HashReallyFast(const std::wstring& filename) {
try {
EVP_MD_CTX* mdctx;
const EVP_MD* md;
uint8_t sha256_value[EVP_MAX_MD_SIZE];
md = EVP_sha256();
if (md == nullptr) {
throw std::runtime_error("EVP_sha256() failed");
}

mdctx = EVP_MD_CTX_new();
if (mdctx == nullptr) {
throw std::runtime_error("EVP_MD_CTX_new() failed");
}
if (!EVP_DigestInit_ex2(mdctx, md, NULL)) {
EVP_MD_CTX_free(mdctx);
throw std::runtime_error("EVP_DigestInit_ex2() failed");
}

std::wifstream stream(filename, std::ios::binary);

const size_t FileSize = std::filesystem::file_size(filename);
size_t Read = 0;
std::vector<wchar_t> Data;
while (Read < FileSize) {
Data.resize(size_t(std::min<size_t>(FileSize - Read, 4096)));
size_t RealDataSize = Data.size();
stream.read(Data.data(), std::streamsize(Data.size()));
if (stream.eof() || stream.fail()) {
RealDataSize = size_t(stream.gcount());
}
Data.resize(RealDataSize);
if (RealDataSize == 0) {
break;
}
if (RealDataSize > 0 && !EVP_DigestUpdate(mdctx, Data.data(), Data.size())) {
EVP_MD_CTX_free(mdctx);
throw std::runtime_error("EVP_DigestUpdate() failed");
}
Read += RealDataSize;
}
unsigned int sha256_len = 0;
if (!EVP_DigestFinal_ex(mdctx, sha256_value, &sha256_len)) {
EVP_MD_CTX_free(mdctx);
throw std::runtime_error("EVP_DigestFinal_ex() failed");
}
EVP_MD_CTX_free(mdctx);

std::string result;
for (size_t i = 0; i < sha256_len; i++) {
char buf[3];
sprintf(buf, "%02x", sha256_value[i]);
buf[2] = 0;
result += buf;
}
return result;
} catch (const std::exception& e) {
error(L"Sha256 hashing of '" + filename + L"' failed: " + ToWString(e.what()));
return "";
}
}
};
37 changes: 20 additions & 17 deletions src/GameStart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@

#include "Logger.h"
#include "Startup.h"
#include "Utils.h"
#include <Security/Init.h>
#include <filesystem>
#include <thread>
#include "Options.h"

unsigned long GamePID = 0;
#if defined(_WIN32)
std::string QueryKey(HKEY hKey, int ID);
std::string GetGamePath() {
static std::string Path;
std::wstring QueryKey(HKEY hKey, int ID);
std::wstring GetGamePath() {
static std::wstring Path;
if (!Path.empty())
return Path;

Expand All @@ -41,23 +42,25 @@ std::string GetGamePath() {
Path = QueryKey(hKey, 4);

if (Path.empty()) {
Path = "";
char appDataPath[MAX_PATH];
HRESULT result = SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, appDataPath);
Path = L"";
wchar_t* appDataPath = new wchar_t[MAX_PATH];
HRESULT result = SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, appDataPath);
if (SUCCEEDED(result)) {
Path = appDataPath;
}

delete[] appDataPath;

if (Path.empty()) {
fatal("Cannot get Local Appdata directory");
}

Path += "\\BeamNG.drive\\";
Path += L"\\BeamNG.drive\\";
}

std::string Ver = CheckVer(GetGameDir());
Ver = Ver.substr(0, Ver.find('.', Ver.find('.') + 1));
Path += Ver + "\\";
Path += Utils::ToWString(Ver) + L"\\";
return Path;
}
#elif defined(__linux__)
Expand All @@ -75,22 +78,22 @@ std::string GetGamePath() {
#endif

#if defined(_WIN32)
void StartGame(std::string Dir) {
void StartGame(std::wstring Dir) {
BOOL bSuccess = FALSE;
PROCESS_INFORMATION pi;
STARTUPINFO si = { 0 };
STARTUPINFOW si = { 0 };
si.cb = sizeof(si);
std::string BaseDir = Dir; //+"\\Bin64";
std::wstring BaseDir = Dir; //+"\\Bin64";
// Dir += R"(\Bin64\BeamNG.drive.x64.exe)";
Dir += "\\BeamNG.drive.exe";
std::string gameArgs = "";
Dir += L"\\BeamNG.drive.exe";
std::wstring gameArgs = L"";

for (int i = 0; i < options.game_arguments_length; i++) {
gameArgs += " ";
gameArgs += options.game_arguments[i];
gameArgs += L" ";
gameArgs += Utils::ToWString(options.game_arguments[i]);
}

bSuccess = CreateProcessA(nullptr, (LPSTR)(Dir + gameArgs).c_str(), nullptr, nullptr, TRUE, 0, nullptr, BaseDir.c_str(), &si, &pi);
bSuccess = CreateProcessW(nullptr, (wchar_t*)(Dir + gameArgs).c_str(), nullptr, nullptr, TRUE, 0, nullptr, BaseDir.c_str(), &si, &pi);
if (bSuccess) {
info("Game Launched!");
GamePID = pi.dwProcessId;
Expand Down Expand Up @@ -133,7 +136,7 @@ void StartGame(std::string Dir) {
}
#endif

void InitGame(const std::string& Dir) {
void InitGame(const std::wstring& Dir) {
if (!options.no_launch) {
std::thread Game(StartGame, Dir);
Game.detach();
Expand Down
47 changes: 45 additions & 2 deletions src/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "Logger.h"
#include "Startup.h"
#include "Utils.h"
#include <chrono>
#include <fstream>
#include <sstream>
Expand Down Expand Up @@ -36,15 +37,21 @@ std::string getDate() {
}
void InitLog() {
std::ofstream LFS;
LFS.open(GetEP() + "Launcher.log");
LFS.open(GetEP() + L"Launcher.log");
if (!LFS.is_open()) {
error("logger file init failed!");
} else
LFS.close();
}
void addToLog(const std::string& Line) {
std::ofstream LFS;
LFS.open(GetEP() + "Launcher.log", std::ios_base::app);
LFS.open(GetEP() + L"Launcher.log", std::ios_base::app);
LFS << Line.c_str();
LFS.close();
}
void addToLog(const std::wstring& Line) {
std::wofstream LFS;
LFS.open(GetEP() + L"Launcher.log", std::ios_base::app);
LFS << Line.c_str();
LFS.close();
}
Expand Down Expand Up @@ -82,3 +89,39 @@ void except(const std::string& toPrint) {
std::cout << Print;
addToLog(Print);
}


void info(const std::wstring& toPrint) {
std::wstring Print = Utils::ToWString(getDate()) + L"[INFO] " + toPrint + L"\n";
std::wcout << Print;
addToLog(Print);
}
void debug(const std::wstring& toPrint) {
std::wstring Print = Utils::ToWString(getDate()) + L"[DEBUG] " + toPrint + L"\n";
if (options.verbose) {
std::wcout << Print;
}
addToLog(Print);
}
void warn(const std::wstring& toPrint) {
std::wstring Print = Utils::ToWString(getDate()) + L"[WARN] " + toPrint + L"\n";
std::wcout << Print;
addToLog(Print);
}
void error(const std::wstring& toPrint) {
std::wstring Print = Utils::ToWString(getDate()) + L"[ERROR] " + toPrint + L"\n";
std::wcout << Print;
addToLog(Print);
}
void fatal(const std::wstring& toPrint) {
std::wstring Print = Utils::ToWString(getDate()) + L"[FATAL] " + toPrint + L"\n";
std::wcout << Print;
addToLog(Print);
std::this_thread::sleep_for(std::chrono::seconds(5));
std::exit(1);
}
void except(const std::wstring& toPrint) {
std::wstring Print = Utils::ToWString(getDate()) + L"[EXCEP] " + toPrint + L"\n";
std::wcout << Print;
addToLog(Print);
}
4 changes: 2 additions & 2 deletions src/Network/Http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
return Ret;
}

bool HTTP::Download(const std::string& IP, const std::string& Path) {
bool HTTP::Download(const std::string& IP, const std::wstring& Path) {
static std::mutex Lock;
std::scoped_lock Guard(Lock);

Expand All @@ -145,7 +145,7 @@ bool HTTP::Download(const std::string& IP, const std::string& Path) {
File.close();
info("Download Complete!");
} else {
error("Failed to open file directory: " + Path);
error(L"Failed to open file directory: " + Path);
return false;
}

Expand Down
Loading

0 comments on commit 83a08cb

Please sign in to comment.