add cases for empty paths

edit path usages to use filesystem
This commit is contained in:
Anonymous275 2022-08-20 19:51:00 +03:00
parent 4b09048d10
commit 000af36510
6 changed files with 36 additions and 26 deletions

View File

@ -20,7 +20,7 @@ struct VersionParser {
}; };
struct HKEY__; struct HKEY__;
typedef HKEY__ *HKEY; typedef HKEY__* HKEY;
class Launcher { class Launcher {
public: // constructors public: // constructors
@ -43,8 +43,8 @@ class Launcher {
void setDiscordMessage(const std::string& message); void setDiscordMessage(const std::string& message);
static void setExit(bool exit) noexcept; static void setExit(bool exit) noexcept;
const std::string& getFullVersion(); const std::string& getFullVersion();
const std::string& getMPUserPath(); const fs::path& getMPUserPath();
const std::string& getCachePath(); const fs::path& getCachePath();
static bool Terminated() noexcept; static bool Terminated() noexcept;
const std::string& getPublicKey(); const std::string& getPublicKey();
const std::string& getUserRole(); const std::string& getUserRole();
@ -61,7 +61,7 @@ class Launcher {
void ListenIPC(); void ListenIPC();
void Abort(); void Abort();
public: // variables public: // variables
static inline std::thread EntryThread{}; static inline std::thread EntryThread{};
static inline VersionParser SupportedVersion{"0.25.4.0"}; static inline VersionParser SupportedVersion{"0.25.4.0"};
static inline std::string Version{"2.0"}; static inline std::string Version{"2.0"};
@ -70,6 +70,9 @@ class Launcher {
private: // variables private: // variables
uint32_t GamePID{0}; uint32_t GamePID{0};
bool EnableUI = true; bool EnableUI = true;
fs::path MPUserPath{};
fs::path BeamUserPath{};
fs::path LauncherCache{};
int64_t DiscordTime{}; int64_t DiscordTime{};
bool LoginAuth = false; bool LoginAuth = false;
fs::path CurrentPath{}; fs::path CurrentPath{};
@ -78,13 +81,11 @@ class Launcher {
std::string PublicKey{}; std::string PublicKey{};
std::thread IPCSystem{}; std::thread IPCSystem{};
std::thread DiscordRPC{}; std::thread DiscordRPC{};
std::string MPUserPath{};
std::string BeamVersion{}; std::string BeamVersion{};
std::string BeamUserPath{};
std::string DiscordMessage{}; std::string DiscordMessage{};
Server ServerHandler{this}; Server ServerHandler{this};
std::string TargetBuild{"default"}; std::string TargetBuild{"default"};
std::string LauncherCache{};
static inline std::atomic<bool> Shutdown{false}, Exit{false}; static inline std::atomic<bool> Shutdown{false}, Exit{false};
std::unique_ptr<IPC> IPCToGame{}; std::unique_ptr<IPC> IPCToGame{};
std::unique_ptr<IPC> IPCFromGame{}; std::unique_ptr<IPC> IPCFromGame{};

View File

@ -25,19 +25,28 @@ void Launcher::LoadConfig() {
for (char& c : TargetBuild) c = char(tolower(c)); for (char& c : TargetBuild) c = char(tolower(c));
} else LOG(ERROR) << "Failed to get 'Build' string from config"; } else LOG(ERROR) << "Failed to get 'Build' string from config";
if (GamePath.is_string()) BeamRoot = GamePath.as_string()->get(); if (GamePath.is_string()) {
if(!GamePath.as_string()->get().empty()) {
BeamRoot = GamePath.as_string()->get();
} else throw ShutdownException("GamePath cannot be empty");
}
else LOG(ERROR) << "Failed to get 'GamePath' string from config"; else LOG(ERROR) << "Failed to get 'GamePath' string from config";
if (ProfilePath.is_string()) { if (ProfilePath.is_string()) {
BeamUserPath = ProfilePath.as_string()->get(); auto GameVer = VersionParser(UIData::GameVer).split;
if (!BeamUserPath.empty()) {
MPUserPath = BeamUserPath + "\\mods\\multiplayer"; if (!ProfilePath.as_string()->get().empty()) {
} BeamUserPath = fs::path(ProfilePath.as_string()->get())/(GameVer[0] + '.' + GameVer[1]);
MPUserPath = BeamUserPath/"mods"/"multiplayer";
} else throw ShutdownException("ProfilePath cannot be empty");
} }
else LOG(ERROR) << "Failed to get 'ProfilePath' string from config"; else LOG(ERROR) << "Failed to get 'ProfilePath' string from config";
if (CachePath.is_string()) LauncherCache = CachePath.as_string()->get(); if (CachePath.is_string()) {
else LOG(ERROR) << "Failed to get 'CachePath' string from config"; if(!CachePath.as_string()->get().empty()) {
LauncherCache = CachePath.as_string()->get();
} else throw ShutdownException("CachePath cannot be empty");
} else LOG(ERROR) << "Failed to get 'CachePath' string from config";
BeamVersion = UIData::GameVer; BeamVersion = UIData::GameVer;
} else { } else {

View File

@ -208,7 +208,7 @@ void Launcher::setExit(bool exit) noexcept {
Exit.store(exit); Exit.store(exit);
} }
const std::string& Launcher::getMPUserPath() { const fs::path& Launcher::getMPUserPath() {
return MPUserPath; return MPUserPath;
} }
@ -216,6 +216,6 @@ const std::string& Launcher::getPublicKey() {
return PublicKey; return PublicKey;
} }
const std::string& Launcher::getCachePath() { const fs::path& Launcher::getCachePath() {
return LauncherCache; return LauncherCache;
} }

View File

@ -255,7 +255,7 @@ void Server::SyncResources() {
FN != FNames.end() && !Terminate; ++FN, ++FS) { FN != FNames.end() && !Terminate; ++FN, ++FS) {
auto pos = FN->find_last_of('/'); auto pos = FN->find_last_of('/');
if (pos != std::string::npos) { if (pos != std::string::npos) {
a = LauncherInstance->getCachePath() + FN->substr(pos); a = (LauncherInstance->getCachePath()/FN->substr(pos+1)).string();
} else continue; } else continue;
Pos++; Pos++;
if (fs::exists(a)) { if (fs::exists(a)) {
@ -269,8 +269,8 @@ void Server::SyncResources() {
fs::create_directories(LauncherInstance->getMPUserPath()); fs::create_directories(LauncherInstance->getMPUserPath());
} }
fs::copy_file(a, fs::copy_file(a,
LauncherInstance->getMPUserPath() + LauncherInstance->getMPUserPath() /
a.substr(a.find_last_of('/')), a.substr(a.find_last_of('/')+1),
fs::copy_options::overwrite_existing); fs::copy_options::overwrite_existing);
} catch (std::exception& e) { } catch (std::exception& e) {
LOG(ERROR) << "Failed copy to the mods folder! " << e.what(); LOG(ERROR) << "Failed copy to the mods folder! " << e.what();
@ -282,7 +282,7 @@ void Server::SyncResources() {
} else remove(a.c_str()); } else remove(a.c_str());
} }
CheckForDir(); CheckForDir();
std::string FName = a.substr(a.find_last_of('/')); std::string FName = a.substr(a.find_last_of('/')+1);
do { do {
TCPSend("f" + *FN); TCPSend("f" + *FN);
@ -313,7 +313,7 @@ void Server::SyncResources() {
if (!fs::exists(LauncherInstance->getMPUserPath())) { if (!fs::exists(LauncherInstance->getMPUserPath())) {
fs::create_directories(LauncherInstance->getMPUserPath()); fs::create_directories(LauncherInstance->getMPUserPath());
} }
fs::copy_file(a, LauncherInstance->getMPUserPath() + FName, fs::copy_file(a, LauncherInstance->getMPUserPath()/FName,
fs::copy_options::overwrite_existing); fs::copy_options::overwrite_existing);
} }
WaitForConfirm(); WaitForConfirm();

View File

@ -44,7 +44,7 @@ void Launcher::ResetMods() {
fs::create_directories(MPUserPath); fs::create_directories(MPUserPath);
return; return;
} }
if (DirCount(fs::path(MPUserPath)) > 3) { if (DirCount(MPUserPath) > 3) {
LOG(WARNING) LOG(WARNING)
<< "mods/multiplayer will be cleared in 15 seconds, close to abort"; << "mods/multiplayer will be cleared in 15 seconds, close to abort";
std::this_thread::sleep_for(std::chrono::seconds(15)); std::this_thread::sleep_for(std::chrono::seconds(15));
@ -54,7 +54,7 @@ void Launcher::ResetMods() {
} }
void Launcher::EnableMP() { void Launcher::EnableMP() {
std::string File(BeamUserPath + "mods\\db.json"); fs::path File(BeamUserPath/"mods"/"db.json");
if (!fs::exists(File)) return; if (!fs::exists(File)) return;
auto Size = fs::file_size(File); auto Size = fs::file_size(File);
if (Size < 2) return; if (Size < 2) return;
@ -86,5 +86,5 @@ void Launcher::SetupMOD() {
"https://backend.beammp.com/builds/client?download=true" "https://backend.beammp.com/builds/client?download=true"
"&pk=" + "&pk=" +
PublicKey + "&branch=" + TargetBuild, PublicKey + "&branch=" + TargetBuild,
MPUserPath + "\\BeamMP.zip"); (MPUserPath/"BeamMP.zip").string());
} }

View File

@ -758,8 +758,8 @@ MySettingsFrame::MySettingsFrame() :
ctrlProfileDirectory->SetLabel("ProfilePath"); ctrlProfileDirectory->SetLabel("ProfilePath");
auto btnDetectProfileDirectory = new wxButton(panel, 204, wxT("Detect"), wxPoint(185, 240), wxSize(90, 25)); auto btnDetectProfileDirectory = new wxButton(panel, 204, wxT("Detect"), wxPoint(185, 240), wxSize(90, 25));
auto* txtCacheDirectory = new wxStaticText(panel, wxID_ANY, wxT("Cache Directory: "), wxPoint(30, 300)); auto* txtCacheDirectory = new wxStaticText(panel, wxID_ANY, wxT("Download Cache: "), wxPoint(30, 300));
ctrlCacheDirectory = new wxDirPickerCtrl(panel, 202, wxEmptyString, wxT("Cache Directory"), wxPoint(130, 300), wxSize(300, -1)); ctrlCacheDirectory = new wxDirPickerCtrl(panel, 202, wxEmptyString, wxT("Download Cache"), wxPoint(130, 300), wxSize(300, -1));
ctrlCacheDirectory->SetLabel("CachePath"); ctrlCacheDirectory->SetLabel("CachePath");
auto btnCacheDirectory = new wxButton(panel, 205, wxT("Reset"), wxPoint(185, 340), wxSize(90, 25)); auto btnCacheDirectory = new wxButton(panel, 205, wxT("Reset"), wxPoint(185, 340), wxSize(90, 25));