From c969f5054d739587f2712978e42f8752e78c71e0 Mon Sep 17 00:00:00 2001 From: /dev/root Date: Mon, 21 Nov 2016 10:50:55 +0100 Subject: [PATCH 1/5] [Cache] Rewrote code [ci skip] --- src/STDInclude.hpp | 1 + src/Utils/Cache.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/Utils/Cache.hpp | 16 ++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 src/Utils/Cache.cpp create mode 100644 src/Utils/Cache.hpp diff --git a/src/STDInclude.hpp b/src/STDInclude.hpp index 56ef8899..64976641 100644 --- a/src/STDInclude.hpp +++ b/src/STDInclude.hpp @@ -101,6 +101,7 @@ template class Sizer { }; #include "Utils\IO.hpp" #include "Utils\CSV.hpp" #include "Utils\Time.hpp" +#include "Utils\Cache.hpp" #include "Utils\Chain.hpp" #include "Utils\Utils.hpp" #include "Utils\WebIO.hpp" diff --git a/src/Utils/Cache.cpp b/src/Utils/Cache.cpp new file mode 100644 index 00000000..e19b4d88 --- /dev/null +++ b/src/Utils/Cache.cpp @@ -0,0 +1,45 @@ +#include "STDInclude.hpp" + +namespace Utils +{ + const char* Cache::urls[] = + { + "https://iw4xcachep26muba.onion.to"/*, + "https://iw4xcachejnetuln.onion.to", + "https://iw4xcachedjodc4y.onion.to", + */ + }; + std::string Cache::validUrl; + + Cache::Cache(std::string path) + { + this->Path = path; + } + + std::string Cache::GetUrl() + { + if (Cache::validUrl.empty()) + return Cache::urls[0] + this->Path; + else + return Cache::validUrl + this->Path; + } + + std::string Cache::GetUrl(std::string url, std::string path) + { + return url + path; + } + + std::string Cache::GetFile(int timeout, std::string useragent) + { + for (int i = 0; i < ARRAY_SIZE(Cache::urls); i++) + { + std::string result = Utils::WebIO(useragent, this->GetUrl(Cache::urls[i], this->Path)).setTimeout(timeout)->get(); + if (!result.empty()) + { + Cache::validUrl = Cache::urls[i]; + return result; + } + } + return ""; + } +} diff --git a/src/Utils/Cache.hpp b/src/Utils/Cache.hpp new file mode 100644 index 00000000..d546a112 --- /dev/null +++ b/src/Utils/Cache.hpp @@ -0,0 +1,16 @@ +namespace Utils +{ + class Cache + { + public: + Cache(std::string path); + std::string GetUrl(); + std::string GetFile(int timeout = 5000, std::string useragent = "IW4x"); + + //private: + static const char* urls[]; + static std::string validUrl; + std::string Path; + std::string GetUrl(std::string url, std::string path); + }; +} From 98d9ad402f8aa16ef9f86c6f4644b3754533d4f5 Mon Sep 17 00:00:00 2001 From: /dev/root Date: Mon, 21 Nov 2016 11:09:42 +0100 Subject: [PATCH 2/5] [Cache] Added mutex [ci skip] --- src/Utils/Cache.cpp | 20 ++++++++++++++------ src/Utils/Cache.hpp | 3 ++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Utils/Cache.cpp b/src/Utils/Cache.cpp index e19b4d88..eadea30a 100644 --- a/src/Utils/Cache.cpp +++ b/src/Utils/Cache.cpp @@ -10,6 +10,7 @@ namespace Utils */ }; std::string Cache::validUrl; + std::mutex Cache::CacheMutex; Cache::Cache(std::string path) { @@ -31,15 +32,22 @@ namespace Utils std::string Cache::GetFile(int timeout, std::string useragent) { - for (int i = 0; i < ARRAY_SIZE(Cache::urls); i++) + if (Cache::validUrl.empty()) { - std::string result = Utils::WebIO(useragent, this->GetUrl(Cache::urls[i], this->Path)).setTimeout(timeout)->get(); - if (!result.empty()) + std::lock_guard _(Cache::CacheMutex); + for (int i = 0; i < ARRAY_SIZE(Cache::urls); i++) { - Cache::validUrl = Cache::urls[i]; - return result; + std::string result = Utils::WebIO(useragent, this->GetUrl(Cache::urls[i], this->Path)).setTimeout(timeout)->get(); + if (!result.empty()) + { + Cache::validUrl = Cache::urls[i]; + return result; + } } } - return ""; + else + { + return Utils::WebIO(useragent, this->GetUrl(Cache::validUrl, this->Path)).setTimeout(timeout)->get(); + } } } diff --git a/src/Utils/Cache.hpp b/src/Utils/Cache.hpp index d546a112..bc2e6090 100644 --- a/src/Utils/Cache.hpp +++ b/src/Utils/Cache.hpp @@ -7,7 +7,8 @@ namespace Utils std::string GetUrl(); std::string GetFile(int timeout = 5000, std::string useragent = "IW4x"); - //private: + private: + static std::mutex CacheMutex; static const char* urls[]; static std::string validUrl; std::string Path; From d17f287bf2378ba24500b96c2aac5dac6736cf25 Mon Sep 17 00:00:00 2001 From: /dev/root Date: Mon, 21 Nov 2016 11:29:55 +0100 Subject: [PATCH 3/5] [Cache] Used Utils::Cache [ci skip] --- src/Components/Modules/Maps.cpp | 16 ++++++++-------- src/Components/Modules/News.cpp | 8 ++++---- src/Components/Modules/Node.cpp | 2 +- src/Utils/Cache.cpp | 1 + 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Components/Modules/Maps.cpp b/src/Components/Modules/Maps.cpp index 992ab5f4..86b46696 100644 --- a/src/Components/Modules/Maps.cpp +++ b/src/Components/Modules/Maps.cpp @@ -492,14 +492,14 @@ namespace Components { Dvar::Register("isDlcInstalled_All", false, Game::DVAR_FLAG_USERCREATED | Game::DVAR_FLAG_WRITEPROTECTED, ""); - Maps::AddDlc({ 1, "https://iw4xcachep26muba.onion.to/dlc/IW4.DLC.1.Stimulus.Package.zip", { "mp_complex", "mp_compact", "mp_storm", "mp_overgrown", "mp_crash" } }); - Maps::AddDlc({ 2, "https://iw4xcachep26muba.onion.to/dlc/IW4.DLC.2.Resurgence.Pack.zip", { "mp_abandon", "mp_vacant", "mp_trailerpark", "mp_strike", "mp_fuel2" } }); - Maps::AddDlc({ 3, "https://iw4xcachep26muba.onion.to/dlc/IW4.DLC.3.Nuketown.zip", { "mp_nuked" } }); - Maps::AddDlc({ 4, "https://iw4xcachep26muba.onion.to/dlc/IW4.DLC.4.Classics.1.zip", { "mp_cross_fire", "mp_cargoship", "mp_bloc" } }); - Maps::AddDlc({ 5, "https://iw4xcachep26muba.onion.to/dlc/IW4.DLC.5.Classics.2.zip", { "mp_killhouse", "mp_bog_sh" } }); - Maps::AddDlc({ 6, "https://iw4xcachep26muba.onion.to/dlc/IW4.DLC.6.Cargoship.Winter.zip", { "mp_cargoship_sh" } }); - Maps::AddDlc({ 7, "https://iw4xcachep26muba.onion.to/dlc/IW4.DLC.7.Resurrection.Pack.zip", { "mp_shipment_long", "mp_rust_long", "mp_firingrange" } }); - Maps::AddDlc({ 8, "",{ /*"mp_bloc_sh", "mp_crash_tropical", "mp_estate_tropical", "mp_fav_tropical", "mp_storm_spring"*/ } }); + Maps::AddDlc({ 1, Utils::Cache("/dlc/IW4.DLC.1.Stimulus.Package.zip").GetUrl(), { "mp_complex", "mp_compact", "mp_storm", "mp_overgrown", "mp_crash" } }); + Maps::AddDlc({ 2, Utils::Cache("/dlc/IW4.DLC.2.Resurgence.Pack.zip").GetUrl(), { "mp_abandon", "mp_vacant", "mp_trailerpark", "mp_strike", "mp_fuel2" } }); + Maps::AddDlc({ 3, Utils::Cache("/dlc/IW4.DLC.3.Nuketown.zip").GetUrl(), { "mp_nuked" } }); + Maps::AddDlc({ 4, Utils::Cache("/dlc/IW4.DLC.4.Classics.1.zip").GetUrl(), { "mp_cross_fire", "mp_cargoship", "mp_bloc" } }); + Maps::AddDlc({ 5, Utils::Cache("/dlc/IW4.DLC.5.Classics.2.zip").GetUrl(), { "mp_killhouse", "mp_bog_sh" } }); + Maps::AddDlc({ 6, Utils::Cache("/dlc/IW4.DLC.6.Cargoship.Winter.zip").GetUrl(), { "mp_cargoship_sh" } }); + Maps::AddDlc({ 7, Utils::Cache("/dlc/IW4.DLC.7.Resurrection.Pack.zip").GetUrl(), { "mp_shipment_long", "mp_rust_long", "mp_firingrange" } }); + Maps::AddDlc({ 8, ""/*Utils::Cache("/dlc/").GetUrl()*/,{ /*"mp_bloc_sh", "mp_crash_tropical", "mp_estate_tropical", "mp_fav_tropical", "mp_storm_spring"*/ } }); Maps::UpdateDlcStatus(); diff --git a/src/Components/Modules/News.cpp b/src/Components/Modules/News.cpp index bd85044b..be18f568 100644 --- a/src/Components/Modules/News.cpp +++ b/src/Components/Modules/News.cpp @@ -73,7 +73,7 @@ namespace Components void News::CheckForUpdate() { - std::string caches = Utils::WebIO("IW4x", "https://iw4xcachep26muba.onion.to/iw4/caches.xml").setTimeout(5000)->get(); + std::string caches = Utils::Cache("/iw4/caches.xml").GetFile(); if (!caches.empty()) { @@ -134,7 +134,7 @@ namespace Components std::thread([] () { - std::string data = Utils::WebIO("IW4x", "https://iw4xcachep26muba.onion.to/iw4/updater.exe").setTimeout(5000)->get(); + std::string data = Utils::Cache("/iw4/updater.exe").GetFile(); if (data.empty()) { @@ -156,9 +156,9 @@ namespace Components News::Terminate = false; News::Thread = std::thread([] () { - Localization::Set("MPUI_CHANGELOG_TEXT", Utils::WebIO("IW4x", "https://iw4xcachep26muba.onion.to/iw4/changelog.txt").setTimeout(5000)->get()); + Localization::Set("MPUI_CHANGELOG_TEXT", Utils::Cache("/iw4/changelog.txt").GetFile()); - std::string data = Utils::WebIO("IW4x", "https://iw4xcachep26muba.onion.to/iw4/motd.txt").setTimeout(5000)->get(); + std::string data = Utils::Cache("/iw4/motd.txt").GetFile(); if (!data.empty()) { diff --git a/src/Components/Modules/Node.cpp b/src/Components/Modules/Node.cpp index dacc1015..9d209e62 100644 --- a/src/Components/Modules/Node.cpp +++ b/src/Components/Modules/Node.cpp @@ -10,7 +10,7 @@ namespace Components void Node::LoadNodeRemotePreset() { - std::string nodes = Utils::WebIO("IW4x", "https://iw4xcachep26muba.onion.to/iw4/nodes.txt").setTimeout(5000)->get(); + std::string nodes = Utils::Cache("/iw4/nodes.txt").GetFile(); if (nodes.empty()) return; auto nodeList = Utils::String::Explode(nodes, '\n'); diff --git a/src/Utils/Cache.cpp b/src/Utils/Cache.cpp index eadea30a..0fc1c391 100644 --- a/src/Utils/Cache.cpp +++ b/src/Utils/Cache.cpp @@ -49,5 +49,6 @@ namespace Utils { return Utils::WebIO(useragent, this->GetUrl(Cache::validUrl, this->Path)).setTimeout(timeout)->get(); } + return ""; } } From ddce60ee00cc159d96d64346d87dc693cc6a3787 Mon Sep 17 00:00:00 2001 From: /dev/root Date: Mon, 21 Nov 2016 20:49:33 +0100 Subject: [PATCH 4/5] [Cache] Removed Constructor - removed Constructor - changed all functions to static [ci skip] --- src/Components/Modules/Maps.cpp | 16 +++++++-------- src/Components/Modules/News.cpp | 8 ++++---- src/Components/Modules/Node.cpp | 2 +- src/Utils/Cache.cpp | 36 ++++++++++++++++----------------- src/Utils/Cache.hpp | 12 +++++------ 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/Components/Modules/Maps.cpp b/src/Components/Modules/Maps.cpp index 86b46696..d7a3e0e8 100644 --- a/src/Components/Modules/Maps.cpp +++ b/src/Components/Modules/Maps.cpp @@ -492,14 +492,14 @@ namespace Components { Dvar::Register("isDlcInstalled_All", false, Game::DVAR_FLAG_USERCREATED | Game::DVAR_FLAG_WRITEPROTECTED, ""); - Maps::AddDlc({ 1, Utils::Cache("/dlc/IW4.DLC.1.Stimulus.Package.zip").GetUrl(), { "mp_complex", "mp_compact", "mp_storm", "mp_overgrown", "mp_crash" } }); - Maps::AddDlc({ 2, Utils::Cache("/dlc/IW4.DLC.2.Resurgence.Pack.zip").GetUrl(), { "mp_abandon", "mp_vacant", "mp_trailerpark", "mp_strike", "mp_fuel2" } }); - Maps::AddDlc({ 3, Utils::Cache("/dlc/IW4.DLC.3.Nuketown.zip").GetUrl(), { "mp_nuked" } }); - Maps::AddDlc({ 4, Utils::Cache("/dlc/IW4.DLC.4.Classics.1.zip").GetUrl(), { "mp_cross_fire", "mp_cargoship", "mp_bloc" } }); - Maps::AddDlc({ 5, Utils::Cache("/dlc/IW4.DLC.5.Classics.2.zip").GetUrl(), { "mp_killhouse", "mp_bog_sh" } }); - Maps::AddDlc({ 6, Utils::Cache("/dlc/IW4.DLC.6.Cargoship.Winter.zip").GetUrl(), { "mp_cargoship_sh" } }); - Maps::AddDlc({ 7, Utils::Cache("/dlc/IW4.DLC.7.Resurrection.Pack.zip").GetUrl(), { "mp_shipment_long", "mp_rust_long", "mp_firingrange" } }); - Maps::AddDlc({ 8, ""/*Utils::Cache("/dlc/").GetUrl()*/,{ /*"mp_bloc_sh", "mp_crash_tropical", "mp_estate_tropical", "mp_fav_tropical", "mp_storm_spring"*/ } }); + Maps::AddDlc({ 1, Utils::Cache::GetUrl("/dlc/IW4.DLC.1.Stimulus.Package.zip"), { "mp_complex", "mp_compact", "mp_storm", "mp_overgrown", "mp_crash" } }); + Maps::AddDlc({ 2, Utils::Cache::GetUrl("/dlc/IW4.DLC.2.Resurgence.Pack.zip"), { "mp_abandon", "mp_vacant", "mp_trailerpark", "mp_strike", "mp_fuel2" } }); + Maps::AddDlc({ 3, Utils::Cache::GetUrl("/dlc/IW4.DLC.3.Nuketown.zip"), { "mp_nuked" } }); + Maps::AddDlc({ 4, Utils::Cache::GetUrl("/dlc/IW4.DLC.4.Classics.1.zip"), { "mp_cross_fire", "mp_cargoship", "mp_bloc" } }); + Maps::AddDlc({ 5, Utils::Cache::GetUrl("/dlc/IW4.DLC.5.Classics.2.zip"), { "mp_killhouse", "mp_bog_sh" } }); + Maps::AddDlc({ 6, Utils::Cache::GetUrl("/dlc/IW4.DLC.6.Cargoship.Winter.zip"), { "mp_cargoship_sh" } }); + Maps::AddDlc({ 7, Utils::Cache::GetUrl("/dlc/IW4.DLC.7.Resurrection.Pack.zip"), { "mp_shipment_long", "mp_rust_long", "mp_firingrange" } }); + Maps::AddDlc({ 8, ""/*Utils::Cache::GetUrl("/dlc/")*/,{ /*"mp_bloc_sh", "mp_crash_tropical", "mp_estate_tropical", "mp_fav_tropical", "mp_storm_spring"*/ } }); Maps::UpdateDlcStatus(); diff --git a/src/Components/Modules/News.cpp b/src/Components/Modules/News.cpp index be18f568..c5f1a86f 100644 --- a/src/Components/Modules/News.cpp +++ b/src/Components/Modules/News.cpp @@ -73,7 +73,7 @@ namespace Components void News::CheckForUpdate() { - std::string caches = Utils::Cache("/iw4/caches.xml").GetFile(); + std::string caches = Utils::Cache::GetFile("/iw4/caches.xml"); if (!caches.empty()) { @@ -134,7 +134,7 @@ namespace Components std::thread([] () { - std::string data = Utils::Cache("/iw4/updater.exe").GetFile(); + std::string data = Utils::Cache::GetFile("/iw4/updater.exe"); if (data.empty()) { @@ -156,9 +156,9 @@ namespace Components News::Terminate = false; News::Thread = std::thread([] () { - Localization::Set("MPUI_CHANGELOG_TEXT", Utils::Cache("/iw4/changelog.txt").GetFile()); + Localization::Set("MPUI_CHANGELOG_TEXT", Utils::Cache::GetFile("/iw4/changelog.txt")); - std::string data = Utils::Cache("/iw4/motd.txt").GetFile(); + std::string data = Utils::Cache::GetFile("/iw4/motd.txt"); if (!data.empty()) { diff --git a/src/Components/Modules/Node.cpp b/src/Components/Modules/Node.cpp index 9d209e62..bd77ef0c 100644 --- a/src/Components/Modules/Node.cpp +++ b/src/Components/Modules/Node.cpp @@ -10,7 +10,7 @@ namespace Components void Node::LoadNodeRemotePreset() { - std::string nodes = Utils::Cache("/iw4/nodes.txt").GetFile(); + std::string nodes = Utils::Cache::GetFile("/iw4/nodes.txt"); if (nodes.empty()) return; auto nodeList = Utils::String::Explode(nodes, '\n'); diff --git a/src/Utils/Cache.cpp b/src/Utils/Cache.cpp index 0fc1c391..f3d6c4aa 100644 --- a/src/Utils/Cache.cpp +++ b/src/Utils/Cache.cpp @@ -2,27 +2,24 @@ namespace Utils { - const char* Cache::urls[] = + const char* Cache::Urls[] = { "https://iw4xcachep26muba.onion.to"/*, "https://iw4xcachejnetuln.onion.to", "https://iw4xcachedjodc4y.onion.to", */ }; - std::string Cache::validUrl; + std::string Cache::ValidUrl; std::mutex Cache::CacheMutex; - Cache::Cache(std::string path) + std::string Cache::GetUrl(std::string path) { - this->Path = path; - } + std::lock_guard _(Cache::CacheMutex); - std::string Cache::GetUrl() - { - if (Cache::validUrl.empty()) - return Cache::urls[0] + this->Path; + if (Cache::ValidUrl.empty()) + return Cache::Urls[0] + path; else - return Cache::validUrl + this->Path; + return Cache::ValidUrl + path; } std::string Cache::GetUrl(std::string url, std::string path) @@ -30,25 +27,28 @@ namespace Utils return url + path; } - std::string Cache::GetFile(int timeout, std::string useragent) + std::string Cache::GetFile(std::string path, int timeout, std::string useragent) { - if (Cache::validUrl.empty()) + std::lock_guard _(Cache::CacheMutex); + + if (Cache::ValidUrl.empty()) { - std::lock_guard _(Cache::CacheMutex); - for (int i = 0; i < ARRAY_SIZE(Cache::urls); i++) + for (int i = 0; i < ARRAY_SIZE(Cache::Urls); i++) { - std::string result = Utils::WebIO(useragent, this->GetUrl(Cache::urls[i], this->Path)).setTimeout(timeout)->get(); + std::string result = Utils::WebIO(useragent, Cache::GetUrl(Cache::Urls[i], path)).setTimeout(timeout)->get(); + if (!result.empty()) { - Cache::validUrl = Cache::urls[i]; + Cache::ValidUrl = Cache::Urls[i]; return result; } } + + return ""; } else { - return Utils::WebIO(useragent, this->GetUrl(Cache::validUrl, this->Path)).setTimeout(timeout)->get(); + return Utils::WebIO(useragent, Cache::GetUrl(Cache::ValidUrl, path)).setTimeout(timeout)->get(); } - return ""; } } diff --git a/src/Utils/Cache.hpp b/src/Utils/Cache.hpp index bc2e6090..0d97fe3e 100644 --- a/src/Utils/Cache.hpp +++ b/src/Utils/Cache.hpp @@ -3,15 +3,13 @@ namespace Utils class Cache { public: - Cache(std::string path); - std::string GetUrl(); - std::string GetFile(int timeout = 5000, std::string useragent = "IW4x"); + static std::string GetUrl(std::string path); + static std::string GetFile(std::string path, int timeout = 5000, std::string useragent = "IW4x"); private: static std::mutex CacheMutex; - static const char* urls[]; - static std::string validUrl; - std::string Path; - std::string GetUrl(std::string url, std::string path); + static const char* Urls[]; + static std::string ValidUrl; + static std::string GetUrl(std::string url, std::string path); }; } From f169fd5274db31c48e48a917b70907dd25060a6e Mon Sep 17 00:00:00 2001 From: momo5502 Date: Tue, 22 Nov 2016 07:59:46 +0100 Subject: [PATCH 5/5] [Cache] Use curly brackets --- src/Utils/Cache.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Utils/Cache.cpp b/src/Utils/Cache.cpp index f3d6c4aa..78b1038a 100644 --- a/src/Utils/Cache.cpp +++ b/src/Utils/Cache.cpp @@ -17,9 +17,13 @@ namespace Utils std::lock_guard _(Cache::CacheMutex); if (Cache::ValidUrl.empty()) + { return Cache::Urls[0] + path; + } else + { return Cache::ValidUrl + path; + } } std::string Cache::GetUrl(std::string url, std::string path)