Skip to content

Commit

Permalink
Merge pull request #36 from RaidcoreGG/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
DeltaGW2 authored Apr 15, 2024
2 parents 00722b4 + a5a2428 commit ec8935d
Show file tree
Hide file tree
Showing 33 changed files with 1,159 additions and 752 deletions.
2 changes: 1 addition & 1 deletion src/API/APIController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ namespace API
/* Do some rate limiting */
while (QueuedRequests.size() > 0)
{
//DoHttpReq(QueuedRequests.front());
//HttpGet(QueuedRequests.front());
/* Callback ? */
QueuedRequests.erase(QueuedRequests.begin());
}
Expand Down
16 changes: 12 additions & 4 deletions src/API/APIRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@
#include "nlohmann/json.hpp"
using json = nlohmann::json;

enum class ERequestType
{
None,
Get,
Post
};

struct APIRequest
{
bool* IsComplete;
std::condition_variable* CV;
int Attempts;
std::string Query;
ERequestType Type;
bool* IsComplete;
std::condition_variable* CV;
int Attempts;
std::string Query;
};

#endif
72 changes: 68 additions & 4 deletions src/API/CAPIClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ json CAPIClient::Get(std::string aEndpoint, std::string aParameters)

// if not cached, push it into requests queue, so it can be done async
APIRequest req{
ERequestType::Get,
&done,
&cv,
0,
Expand All @@ -108,6 +109,60 @@ json CAPIClient::Get(std::string aEndpoint, std::string aParameters)

return cachedResponse != nullptr ? cachedResponse->Content : json{};
}
json CAPIClient::Post(std::string aEndpoint, std::string aParameters)
{
std::string query = GetQuery(aEndpoint, aParameters);

CachedResponse* cachedResponse = GetCachedResponse(query);

if (cachedResponse != nullptr)
{
long long diff = Timestamp() - cachedResponse->Timestamp;

if (diff < CacheLifetime && cachedResponse->Content != nullptr)
{
//LogDebug(("CAPIClient::" + BaseURL).c_str(), "Cached message %d seconds old. Reading from cache.", diff);
return cachedResponse->Content;
}
else
{
//LogDebug(("CAPIClient::" + BaseURL).c_str(), "Cached message %d seconds old. CacheLifetime %d. Queueing request.", diff, CacheLifetime);
}
}

// Variables for synchronization
std::mutex mtx;
bool done = false;
std::condition_variable cv;

// if not cached, push it into requests queue, so it can be done async
APIRequest req{
ERequestType::Post,
&done,
&cv,
0,
query
};

// Trigger the worker thread
{
const std::lock_guard<std::mutex> lock(Mutex);
QueuedRequests.push_back(req);
IsSuspended = false;
ConVar.notify_all();
}

// Wait for the response
{
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [&] { return done; });
}

cachedResponse = nullptr; // sanity
cachedResponse = GetCachedResponse(query);

return cachedResponse != nullptr ? cachedResponse->Content : json{};
}
void CAPIClient::Download(std::filesystem::path aOutPath, std::string aEndpoint, std::string aParameters)
{
std::string query = GetQuery(aEndpoint, aParameters);
Expand Down Expand Up @@ -251,8 +306,8 @@ void CAPIClient::ProcessRequests()

APIRequest request = QueuedRequests.front();

// DoHttpReq should set last request timestamp
APIResponse response = DoHttpReq(request);
// HttpGet should set last request timestamp
APIResponse response = HttpGet(request);

// does the bucket get reduced on unsuccessful requests? we assume it does
Bucket--;
Expand Down Expand Up @@ -344,14 +399,23 @@ void CAPIClient::ProcessRequests()
IsSuspended = true;
}
}
APIResponse CAPIClient::DoHttpReq(APIRequest aRequest)
APIResponse CAPIClient::HttpGet(APIRequest aRequest)
{
APIResponse response{
0,
nullptr
};

auto result = Client->Get(aRequest.Query);
httplib::Result result{};
switch (aRequest.Type)
{
case ERequestType::Get:
result = Client->Get(aRequest.Query);
break;
case ERequestType::Post:
result = Client->Post(aRequest.Query);
break;
}

if (!result)
{
Expand Down
4 changes: 3 additions & 1 deletion src/API/CAPIClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class CAPIClient
Returns the response string.
*/
json Get(std::string aEndpoint, std::string aParameters = "");

json Post(std::string aEndpoint, std::string aParameters = "");
/*
Download:
Downloads the remote resource to disk.
Expand Down Expand Up @@ -87,7 +89,7 @@ class CAPIClient
long long FileTimeOffset;

void ProcessRequests();
APIResponse DoHttpReq(APIRequest aRequest);
APIResponse HttpGet(APIRequest aRequest);
};

#endif
1 change: 1 addition & 0 deletions src/Consts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const char* ICON_NEXUS_HOVER = "ICON_NEXUS_HOVER";
const char* ICON_GENERIC = "ICON_GENERIC";
const char* ICON_GENERIC_HOVER = "ICON_GENERIC_HOVER";
const char* ICON_NOTIFICATION = "ICON_NOTIFICATION";
const char* ICON_WARNING = "ICON_WARNING";
const char* ICON_ADDONS = "ICON_ADDONS";
const char* ICON_OPTIONS = "ICON_OPTIONS";
const char* ICON_OPTIONS_HOVER = "ICON_OPTIONS_HOVER";
Expand Down
1 change: 1 addition & 0 deletions src/Consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern const char* ICON_NEXUS_HOVER;
extern const char* ICON_GENERIC;
extern const char* ICON_GENERIC_HOVER;
extern const char* ICON_NOTIFICATION;
extern const char* ICON_WARNING;
extern const char* ICON_ADDONS;
extern const char* ICON_OPTIONS;
extern const char* ICON_OPTIONS_HOVER;
Expand Down
2 changes: 1 addition & 1 deletion src/DataLink/DataLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace DataLink
{
void* ADDONAPI_ShareResource(const char* aIdentifier, size_t aResourceSize)
{
ShareResource(aIdentifier, aResourceSize);
return ShareResource(aIdentifier, aResourceSize);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Events/EventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ namespace Events
EventSubscriber sub{};
sub.Callback = aConsumeEventCallback;

for (auto& [path, addon] : Loader::Addons)
for (auto addon : Loader::Addons)
{
if (addon->Module == nullptr ||
addon->ModuleSize == 0 ||
Expand Down
44 changes: 22 additions & 22 deletions src/GUI/Widgets/Addons/AddonItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Loader/AddonDefinition.h"
#include "Loader/EAddonFlags.h"
#include "Loader/Loader.h"
#include "Loader/Library.h"
#include "Textures/TextureLoader.h"
#include "Textures/Texture.h"

Expand Down Expand Up @@ -40,7 +41,7 @@ namespace GUI
aAddon->Definitions == nullptr ||
aAddon->State == EAddonState::NotLoadedDuplicate ||
aAddon->State == EAddonState::NotLoadedIncompatible ||
aAddon->WillBeUninstalled)
aAddon->IsFlaggedForUninstall)
{
return;
}
Expand Down Expand Up @@ -120,22 +121,18 @@ namespace GUI
{
if (!aAddon->IsCheckingForUpdates)
{
for (auto& it : Loader::Addons)
for (auto addon : Loader::Addons)
{
if (it.second->Definitions == aAddon->Definitions)
if (addon->Definitions == aAddon->Definitions)
{
aAddon->IsCheckingForUpdates = true;

std::filesystem::path tmpPath = it.first.string();
signed int tmpSig = aAddon->Definitions->Signature;
std::string tmpName = aAddon->Definitions->Name;
AddonVersion tmpVers = aAddon->Definitions->Version;
EUpdateProvider tmpProv = aAddon->Definitions->Provider;
std::string tmpLink = aAddon->Definitions->UpdateLink != nullptr ? aAddon->Definitions->UpdateLink : "";

std::thread([aAddon, tmpPath, tmpSig, tmpName, tmpVers, tmpProv, tmpLink]()
std::filesystem::path tmpPath = addon->Path.string();
std::thread([aAddon, tmpPath]()
{
if (Loader::UpdateAddon(tmpPath, tmpSig, tmpName, tmpVers, tmpProv, tmpLink))
if (Loader::UpdateAddon(tmpPath, aAddon->Definitions->Signature, aAddon->Definitions->Name,
aAddon->Definitions->Version, aAddon->Definitions->Provider,
aAddon->Definitions->UpdateLink != nullptr ? aAddon->Definitions->UpdateLink : ""))
{
Loader::QueueAddon(ELoaderAction::Reload, tmpPath);
}
Expand Down Expand Up @@ -203,17 +200,20 @@ namespace GUI
// just check if loaded, if it was not hot-reloadable it would be EAddonState::LoadedLOCKED
if (aAddon->State == EAddonState::Loaded)
{
if (ImGui::GW2::Button((Language.Translate("((000020))") + sig).c_str(), ImVec2(btnWidth * ImGui::GetFontSize(), btnHeight)))
if (ImGui::GW2::Button((aAddon->IsWaitingForUnload ? Language.Translate("((000078))") : Language.Translate("((000020))") + sig).c_str(), ImVec2(btnWidth * ImGui::GetFontSize(), btnHeight)))
{
//LogDebug(CH_GUI, "Unload called: %s", it.second->Definitions->Name);
Loader::QueueAddon(ELoaderAction::Unload, aPath);
if (!aAddon->IsWaitingForUnload)
{
//LogDebug(CH_GUI, "Unload called: %s", it.second->Definitions->Name);
Loader::QueueAddon(ELoaderAction::Unload, aPath);
}
}
if (RequestedAddons.size() > 0)
{
ImGui::GW2::TooltipGeneric(Language.Translate("((000021))"));
}
}
else if (aAddon->State == EAddonState::LoadedLOCKED && aAddon->ShouldDisableNextLaunch == false)
else if (aAddon->State == EAddonState::LoadedLOCKED && aAddon->IsFlaggedForDisable == false)
{
std::string additionalInfo;

Expand All @@ -225,11 +225,11 @@ namespace GUI

if (ImGui::GW2::Button((Language.Translate("((000022))") + sig).c_str(), ImVec2(btnWidth * ImGui::GetFontSize(), btnHeight)))
{
aAddon->ShouldDisableNextLaunch = true;
aAddon->IsFlaggedForDisable = true;
}
ImGui::GW2::TooltipGeneric(Language.Translate("((000023))"), additionalInfo.c_str());
}
else if (aAddon->State == EAddonState::LoadedLOCKED && aAddon->ShouldDisableNextLaunch == true)
else if (aAddon->State == EAddonState::LoadedLOCKED && aAddon->IsFlaggedForDisable == true)
{
std::string additionalInfo;

Expand All @@ -241,7 +241,7 @@ namespace GUI

if (ImGui::GW2::Button((Language.Translate("((000024))") + sig).c_str(), ImVec2(btnWidth * ImGui::GetFontSize(), btnHeight)))
{
aAddon->ShouldDisableNextLaunch = false;
aAddon->IsFlaggedForDisable = false;
}
ImGui::GW2::TooltipGeneric(Language.Translate("((000025))"), additionalInfo.c_str());
}
Expand Down Expand Up @@ -346,7 +346,7 @@ namespace GUI
}
else
{
ToSComplianceWarning = TextureLoader::GetOrCreate(ICON_NOTIFICATION, RES_ICON_NOTIFICATION, NexusHandle);
ToSComplianceWarning = TextureLoader::GetOrCreate(ICON_WARNING, RES_ICON_WARNING, NexusHandle);
}
}

Expand All @@ -359,12 +359,12 @@ namespace GUI
{
std::thread([aAddon]()
{
Loader::InstallAddon(aAddon);
Loader::Library::InstallAddon(aAddon);
aAddon->IsInstalling = false;
})
.detach();

Loader::AddonConfig[aAddon->Signature].IsLoaded = true;
//Loader::AddonConfig[aAddon->Signature].IsLoaded = true;
}
}
}
Expand Down
32 changes: 15 additions & 17 deletions src/GUI/Widgets/Addons/CAddonsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Renderer.h"

#include "Loader/Loader.h"
#include "Loader/Library.h"
#include "Loader/ArcDPS.h"
#include "AddonItem.h"
#include "Textures/TextureLoader.h"
Expand Down Expand Up @@ -206,10 +207,10 @@ namespace GUI
{
const std::lock_guard<std::mutex> lock(Loader::Mutex);
{
for (auto& [path, addon] : Loader::Addons)
for (auto addon : Loader::Addons)
{
if (path.filename() == "arcdps_integration64.dll") { continue; }
AddonItem(path, addon);
if (addon->Path.filename() == "arcdps_integration64.dll") { continue; }
AddonItem(addon->Path, addon);
}
}
}
Expand All @@ -236,26 +237,23 @@ namespace GUI
checkedForUpdates = 0;
queuedForCheck = 0;
/* pre-iterate to get the count of how many need to be checked, else one call might finish before the count can be incremented */
for (auto& [path, addon] : Loader::Addons)
for (auto addon : Loader::Addons)
{
if (nullptr == addon->Definitions) { continue; }
queuedForCheck++;
}

for (auto& [path, addon] : Loader::Addons)
for (auto addon : Loader::Addons)
{
if (nullptr == addon->Definitions) { continue; }

std::filesystem::path tmpPath = path.string();
signed int tmpSig = addon->Definitions->Signature;
std::string tmpName = addon->Definitions->Name;
AddonVersion tmpVers = addon->Definitions->Version;
EUpdateProvider tmpProv = addon->Definitions->Provider;
std::string tmpLink = addon->Definitions->UpdateLink != nullptr ? addon->Definitions->UpdateLink : "";
std::filesystem::path tmpPath = addon->Path.string();

std::thread([tmpPath, tmpSig, tmpName, tmpVers, tmpProv, tmpLink]()
std::thread([tmpPath, addon]()
{
if (Loader::UpdateAddon(tmpPath, tmpSig, tmpName, tmpVers, tmpProv, tmpLink))
if (Loader::UpdateAddon(tmpPath, addon->Definitions->Signature, addon->Definitions->Name,
addon->Definitions->Version, addon->Definitions->Provider,
addon->Definitions->UpdateLink != nullptr ? addon->Definitions->UpdateLink : ""))
{
Loader::QueueAddon(ELoaderAction::Reload, tmpPath);
}
Expand All @@ -281,13 +279,13 @@ namespace GUI

int downloadable = 0;
const std::lock_guard<std::mutex> lockLoader(Loader::Mutex);
if (Loader::AddonLibrary.size() != 0)
if (Loader::Library::Addons.size() != 0)
{
for (auto& libAddon : Loader::AddonLibrary)
for (auto& libAddon : Loader::Library::Addons)
{
bool exists = false;
{
for (auto& [path, addon] : Loader::Addons)
for (auto addon : Loader::Addons)
{
// if libAddon already exist in installed addons
// or if arcdps is loaded another way and the libAddon is arc
Expand All @@ -307,7 +305,7 @@ namespace GUI
}
}

if (Loader::AddonLibrary.size() == 0 || downloadable == 0)
if (Loader::Library::Addons.size() == 0 || downloadable == 0)
{
ImVec2 windowSize = ImGui::GetWindowSize();
ImVec2 textSize = ImGui::CalcTextSize(Language.Translate("((000037))"));
Expand Down
Loading

0 comments on commit ec8935d

Please sign in to comment.