From 1c50d4d7790ffb217878f0feb35a035b2bcb482c Mon Sep 17 00:00:00 2001 From: Luna Date: Wed, 23 Oct 2024 02:01:34 +0300 Subject: [PATCH 1/3] added multithreading --- src/Regex.cpp | 26 ++++++++++++++++++-------- src/includes/Regex.hpp | 10 ++++------ src/maincommand.cpp | 21 ++++++++++----------- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/Regex.cpp b/src/Regex.cpp index d9bc249..3145216 100644 --- a/src/Regex.cpp +++ b/src/Regex.cpp @@ -52,9 +52,9 @@ void Regex(cmd *inputCmd) { } } -void filerestort(cmd *inputCmd) { - std::filesystem::path steamdir = std::string(inputCmd->userHome) + "/.cache/steamapps/workshop/content/" + inputCmd->gameid + "/" + inputCmd->idnumber; - std::filesystem::path modname = inputCmd->dir + "/" + inputCmd->idname; +void filerestort(cmd *inputCmd, std::string idnumber, std::string idname) { + std::filesystem::path steamdir = std::string(inputCmd->userHome) + "/.cache/steamapps/workshop/content/" + inputCmd->gameid + "/" + idnumber; + std::filesystem::path modname = inputCmd->dir + "/" + idname; // renames and moves the files. try { @@ -63,29 +63,38 @@ void filerestort(cmd *inputCmd) { std::filesystem::remove_all(modname); } std::filesystem::rename(steamdir, modname); + inputCmd->threadsCompleted++; } else { std::cerr << "Error: The directory " << steamdir << " does not exist." << std::endl; + inputCmd->threadsCompleted++; + } } catch (const std::filesystem::filesystem_error& e) { std::cerr << "Filesystem error: " << e.what() << std::endl; + inputCmd->threadsCompleted++; + } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; + inputCmd->threadsCompleted++; + } } -void Modname(cmd *inputCmd, std::string input) { +void Modname(cmd *inputCmd, size_t index) { + std::string idnumber; + std::string idname; if (!inputCmd->sucids.empty()) { std::regex downloadItemRegex(R"(Downloaded item (\d+))"); std::smatch match; - if (std::regex_search(input, match, downloadItemRegex)) { + if (std::regex_search(inputCmd->sucids[index], match, downloadItemRegex)) { std::string downloadItemNumber = match[1].str(); - inputCmd->idnumber = downloadItemNumber; + idnumber = downloadItemNumber; } std::istringstream inputStream(inputCmd->source); - std::regex grepRegex("\"id\":\"" + inputCmd->idnumber + "\",\"title\":\""); + std::regex grepRegex("\"id\":\"" + idnumber + "\",\"title\":\""); std::string line; while (std::getline(inputStream, line)) { @@ -103,8 +112,9 @@ void Modname(cmd *inputCmd, std::string input) { line = line.substr(idPos + idPrefix.length()); // Keep everything after "id=" } - inputCmd->idname = line; + idname = line; } } + filerestort(inputCmd, idnumber, idname); } } diff --git a/src/includes/Regex.hpp b/src/includes/Regex.hpp index ce31c53..fab1499 100644 --- a/src/includes/Regex.hpp +++ b/src/includes/Regex.hpp @@ -1,7 +1,7 @@ #ifndef REGEX_H #define REGEX_H #include - +#include // all the var's struct cmd { const char* userHome = getenv("HOME"); @@ -24,17 +24,15 @@ int slashtp = 0; std::string slash; // counter -std::string sucids; -std::string idnumber; -std::string idname; +std::vector sucids; int successes = 0; int timedout = 0; int errors = 0; int totalmeow = 0; +size_t threadsCompleted = 0; }; void Regex(cmd *inputCmd); -void filerestort(cmd *inputCmd); -void Modname(cmd *inputCmd, std::string input); +void Modname(cmd *inputCmd, size_t index); #endif diff --git a/src/maincommand.cpp b/src/maincommand.cpp index 2a970dc..5514600 100644 --- a/src/maincommand.cpp +++ b/src/maincommand.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -24,7 +23,8 @@ void execAndDisplay(cmd *inputCmd, const std::string& cmd, std::atomic& ru // checks for success. if (line.find("Success") != std::string::npos) { inputCmd->successes++; - inputCmd->sucids += line + "\n"; + inputCmd->sucids.push_back( line + "\n"); + std::cout << "pushed success to array\n"; } // checks for timed out ones. @@ -93,15 +93,14 @@ void maincommand(cmd *inputCmd) { + "Timed out: " + std::to_string(inputCmd->timedout) + "\n" + "Errored: " + std::to_string(inputCmd->errors) + "\n" + "\n"; - // mod names - std::istringstream inputStream(inputCmd->sucids); - std::string line; - - while (std::getline(inputStream, line)) { - inputCmd->slashtp++; - Modname(inputCmd, line); - filerestort(inputCmd); + //start the threads + for(size_t nya{0}; nya < inputCmd->sucids.size(); ++nya){ + inputCmd->slashtp++; + std::thread meowT {Modname, inputCmd, nya}; + meowT.detach(); } - + std::cout << "waiting for threads to finish >.<\n"; + while(inputCmd->threadsCompleted != inputCmd->sucids.size()){} + std::cout << "threads finished!\n"; std::cout << "\n\n" + mods + colm + " has been downloaded too: " + inputCmd->dir + "\n"; } From adad232b4bdf42d1b6c5b94bd011fedcf682ba62 Mon Sep 17 00:00:00 2001 From: Luna Date: Wed, 23 Oct 2024 02:03:58 +0300 Subject: [PATCH 2/3] removed std::cout --- src/maincommand.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/maincommand.cpp b/src/maincommand.cpp index 5514600..c7c3d0b 100644 --- a/src/maincommand.cpp +++ b/src/maincommand.cpp @@ -24,7 +24,6 @@ void execAndDisplay(cmd *inputCmd, const std::string& cmd, std::atomic& ru if (line.find("Success") != std::string::npos) { inputCmd->successes++; inputCmd->sucids.push_back( line + "\n"); - std::cout << "pushed success to array\n"; } // checks for timed out ones. From 689127ffe90580fa2dcb48b475a55e5078a5b2c7 Mon Sep 17 00:00:00 2001 From: DRAGONTOS Date: Wed, 23 Oct 2024 02:58:21 +0200 Subject: [PATCH 3/3] struct: changed some names --- src/Regex.cpp | 2 +- src/includes/Regex.hpp | 5 ++--- src/maincommand.cpp | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Regex.cpp b/src/Regex.cpp index d9bc249..84b8de2 100644 --- a/src/Regex.cpp +++ b/src/Regex.cpp @@ -48,7 +48,7 @@ void Regex(cmd *inputCmd) { // Count the number of lines while (std::getline(inputStream, line)) { - inputCmd->slashtp++; + inputCmd->totalmods++; } } diff --git a/src/includes/Regex.hpp b/src/includes/Regex.hpp index ce31c53..d9e905d 100644 --- a/src/includes/Regex.hpp +++ b/src/includes/Regex.hpp @@ -19,9 +19,8 @@ std::string dir; bool ab = false; -// slashing -int slashtp = 0; -std::string slash; +// total mods +int totalmods = 0; // counter std::string sucids; diff --git a/src/maincommand.cpp b/src/maincommand.cpp index 2a970dc..40ec7d5 100644 --- a/src/maincommand.cpp +++ b/src/maincommand.cpp @@ -83,7 +83,7 @@ void maincommand(cmd *inputCmd) { } // mod or collection? - std::string total = (inputCmd->ab == true) ? "Total: 1" : "Total: " + std::to_string(inputCmd->slashtp -1) + "\n"; + std::string total = (inputCmd->ab == true) ? "Total: 1" : "Total: " + std::to_string(inputCmd->totalmods -1) + "\n"; std::string colm = (inputCmd->ab == true) ? R"(Mod)" : R"(Collection)"; // shows how much and what has downloaded @@ -98,7 +98,7 @@ void maincommand(cmd *inputCmd) { std::string line; while (std::getline(inputStream, line)) { - inputCmd->slashtp++; + inputCmd->totalmods++; Modname(inputCmd, line); filerestort(inputCmd); }