Add CachingDirectory config setting to cache mods elsewhere (#121)

also moved cls/clear to the beginning, idk wtf it was doing in there.
This commit is contained in:
Lion 2024-09-23 22:45:21 +02:00 committed by GitHub
commit 0c3ae43910
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 31 additions and 18 deletions

View File

@ -30,6 +30,7 @@ extern int DEFAULT_PORT;
extern uint64_t UDPSock; extern uint64_t UDPSock;
extern uint64_t TCPSock; extern uint64_t TCPSock;
extern std::string Branch; extern std::string Branch;
extern std::string CachingDirectory;
extern bool TCPTerminate; extern bool TCPTerminate;
extern std::string LastIP; extern std::string LastIP;
extern std::string MStatus; extern std::string MStatus;

View File

@ -11,6 +11,8 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
std::string Branch; std::string Branch;
std::string CachingDirectory = "./Resources";
void ParseConfig(const nlohmann::json& d) { void ParseConfig(const nlohmann::json& d) {
if (d["Port"].is_number()) { if (d["Port"].is_number()) {
DEFAULT_PORT = d["Port"].get<int>(); DEFAULT_PORT = d["Port"].get<int>();
@ -20,12 +22,15 @@ void ParseConfig(const nlohmann::json& d) {
// EA 2 // EA 2
// Dev 3 // Dev 3
// Custom 3 // Custom 3
if (d["Build"].is_string()) { if (d["Build"].is_string()) {
Branch = d["Build"].get<std::string>(); Branch = d["Build"].get<std::string>();
for (char& c : Branch) for (char& c : Branch)
c = char(tolower(c)); c = char(tolower(c));
} }
if (d.contains("CachingDirectory") && d["CachingDirectory"].is_string()) {
CachingDirectory = d["CachingDirectory"].get<std::string>();
info("Mod caching directory: " + CachingDirectory);
}
} }
void ConfigInit() { void ConfigInit() {
@ -49,7 +54,8 @@ void ConfigInit() {
cfg << cfg <<
R"({ R"({
"Port": 4444, "Port": 4444,
"Build": "Default" "Build": "Default",
"CachingDirectory": "./Resources",
})"; })";
cfg.close(); cfg.close();
} else { } else {

View File

@ -36,13 +36,14 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
void CheckForDir() { void CheckForDir() {
if (!fs::exists("Resources")) { if (!fs::exists(CachingDirectory)) {
// Could we just use fs::create_directory instead? try {
#if defined(_WIN32) fs::create_directories(CachingDirectory);
_wmkdir(L"Resources"); } catch (const std::exception& e) {
#elif defined(__linux__) error(std::string("Failed to create caching directory: ") + e.what() + ". This is a fatal error. Please make sure to configure a directory which you have permission to create, read and write from/to.");
fs::create_directory(L"Resources"); std::this_thread::sleep_for(std::chrono::seconds(3));
#endif std::exit(1);
}
} }
} }
void WaitForConfirm() { void WaitForConfirm() {
@ -297,7 +298,7 @@ void SyncResources(SOCKET Sock) {
for (auto FN = FNames.begin(), FS = FSizes.begin(); FN != FNames.end() && !Terminate; ++FN, ++FS) { for (auto FN = FNames.begin(), FS = FSizes.begin(); 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) {
PathToSaveTo = "Resources" + FN->substr(pos); PathToSaveTo = CachingDirectory + FN->substr(pos);
} else { } else {
continue; continue;
} }

View File

@ -81,10 +81,10 @@ std::string GetEN() {
} }
std::string GetVer() { std::string GetVer() {
return "2.0"; return "2.1";
} }
std::string GetPatch() { std::string GetPatch() {
return ".99"; return ".2";
} }
std::string GetEP(char* P) { std::string GetEP(char* P) {
@ -101,6 +101,7 @@ void ReLaunch(int argc, char* args[]) {
Arg += " "; Arg += " ";
Arg += args[c - 1]; Arg += args[c - 1];
} }
info("Relaunch!");
system("cls"); system("cls");
ShellExecute(nullptr, "runas", (GetEP() + GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL); ShellExecute(nullptr, "runas", (GetEP() + GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL);
ShowWindow(GetConsoleWindow(), 0); ShowWindow(GetConsoleWindow(), 0);
@ -125,6 +126,7 @@ void ReLaunch(int argc, char* args[]) {
Arg += " "; Arg += " ";
Arg += args[c - 1]; Arg += args[c - 1];
} }
info("Relaunch!");
system("clear"); system("clear");
execl((GetEP() + GetEN()).c_str(), Arg.c_str(), NULL); execl((GetEP() + GetEN()).c_str(), Arg.c_str(), NULL);
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
@ -167,10 +169,6 @@ void CheckForUpdates(int argc, char* args[], const std::string& CV) {
std::string EP(GetEP() + GetEN()), Back(GetEP() + "BeamMP-Launcher.back"); std::string EP(GetEP() + GetEN()), Back(GetEP() + "BeamMP-Launcher.back");
std::string FileHash = hashpp::get::getFileHash(hashpp::ALGORITHMS::SHA2_256, EP); std::string FileHash = hashpp::get::getFileHash(hashpp::ALGORITHMS::SHA2_256, EP);
#if defined(_WIN32)
#elif defined(__linux__)
system("clear");
#endif
if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion))) && !Dev) { if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion))) && !Dev) {
info("Launcher update found!"); info("Launcher update found!");
@ -237,7 +235,6 @@ void LinuxPatch() {
#if defined(_WIN32) #if defined(_WIN32)
void InitLauncher(int argc, char* argv[]) { void InitLauncher(int argc, char* argv[]) {
system("cls");
SetConsoleTitleA(("BeamMP Launcher v" + std::string(GetVer()) + GetPatch()).c_str()); SetConsoleTitleA(("BeamMP Launcher v" + std::string(GetVer()) + GetPatch()).c_str());
InitLog(); InitLog();
CheckName(argc, argv); CheckName(argc, argv);
@ -249,8 +246,8 @@ void InitLauncher(int argc, char* argv[]) {
} }
#elif defined(__linux__) #elif defined(__linux__)
void InitLauncher(int argc, char* argv[]) { void InitLauncher(int argc, char* argv[]) {
system("clear");
InitLog(); InitLog();
info("BeamMP Launcher v" + GetVer() + GetPatch());
CheckName(argc, argv); CheckName(argc, argv);
CheckLocalKey(); CheckLocalKey();
ConfigInit(); ConfigInit();

View File

@ -26,6 +26,14 @@ int main(int argc, char** argv) try {
th.detach(); th.detach();
#endif #endif
#if defined(_WIN32)
system("cls");
#elif defined(__linux__)
system("clear");
#endif
GetEP(argv[0]); GetEP(argv[0]);
for (int i = 0; i < argc; ++i) { for (int i = 0; i < argc; ++i) {