Fix wstring for windows and add crossplatform code for fs strings

This commit is contained in:
Tixx
2025-01-26 23:08:31 +01:00
parent 5e448dc34f
commit 06c741edc5
10 changed files with 81 additions and 126 deletions

View File

@@ -6,10 +6,12 @@
#pragma once
#include "Logger.h"
#include "Utils.h"
#include <string>
class HTTP {
public:
static bool Download(const std::string& IP, const std::wstring& Path);
static bool Download(const std::string& IP, const beammp_fs_string& 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);

View File

@@ -6,9 +6,9 @@
#pragma once
#include <string>
void PreGame(const std::wstring& GamePath);
std::string CheckVer(const std::wstring& path);
void InitGame(const std::wstring& Dir);
std::wstring GetGameDir();
void PreGame(const beammp_fs_string& GamePath);
std::string CheckVer(const beammp_fs_string& path);
void InitGame(const beammp_fs_string& Dir);
beammp_fs_string GetGameDir();
void LegitimacyCheck();
void CheckLocalKey();

View File

@@ -5,14 +5,16 @@
*/
#pragma once
#include "Utils.h"
#include <compare>
#include <string>
#include <vector>
void InitLauncher();
std::wstring GetEP(const wchar_t* P = nullptr);
std::wstring GetGamePath();
beammp_fs_string GetEP(const beammp_fs_char* P = nullptr);
beammp_fs_string GetGamePath();
std::string GetVer();
std::string GetPatch();
std::wstring GetEN();
beammp_fs_string GetEN();
void ConfigInit();

View File

@@ -15,6 +15,16 @@
#include <string>
#include <vector>
#ifdef _WIN32
#define beammp_fs_string std::wstring
#define beammp_fs_char wchar_t
#define beammp_wide(str) L##str
#else
#define beammp_fs_string std::string
#define beammp_fs_char char
#define beammp_wide(str) str
#endif
namespace Utils {
inline std::vector<std::string> Split(const std::string& String, const std::string& delimiter) {
@@ -120,71 +130,16 @@ namespace Utils {
inline std::string ToString(const std::wstring& w) {
return std::wstring_convert<std::codecvt<wchar_t, char, std::mbstate_t>>().to_bytes(w);
}
#ifdef _WIN32
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 "";
}
#else
inline std::string ToWString(const std::string& s) {
return s;
}
inline std::string GetSha256HashReallyFast(const std::wstring& filename) {
#endif
inline std::string GetSha256HashReallyFast(const beammp_fs_string& filename) {
try {
EVP_MD_CTX* mdctx;
const EVP_MD* md;
@@ -241,7 +196,7 @@ namespace Utils {
}
return result;
} catch (const std::exception& e) {
error(L"Sha256 hashing of '" + filename + L"' failed: " + ToWString(e.what()));
error(beammp_wide("Sha256 hashing of '") + filename + beammp_wide("' failed: ") + ToWString(e.what()));
return "";
}
}