Skip to content

Commit

Permalink
Merge pull request #21 from RaidcoreGG/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
DeltaGW2 authored Feb 13, 2024
2 parents ae0aa56 + 01f3810 commit 80aa3e3
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 80 deletions.
25 changes: 15 additions & 10 deletions src/API/APIClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ APIClient::APIClient(std::string aBaseURL, bool aEnableSSL, std::filesystem::pat
RefillAmount = aRefillAmount;
RefillInterval = aRefillInterval;

LogDebug("APIClient", "APIClient(BaseURL: %s, EnableSSL: %s, CacheDirectory: %s, CacheLifetime: %d, BucketCapacity: %d, RefillAmount: %d, RefillInterval: %d)",
LogDebug(("APIClient::" + BaseURL).c_str(), "APIClient(BaseURL: %s, EnableSSL: %s, CacheDirectory: %s, CacheLifetime: %d, BucketCapacity: %d, RefillAmount: %d, RefillInterval: %d)",
BaseURL.c_str(),
aEnableSSL ? "true" : "false",
CacheDirectory.string().c_str(),
Expand Down Expand Up @@ -52,7 +52,7 @@ APIClient::~APIClient()

delete Client;

LogDebug("APIClient", "~APIClient(%s)", BaseURL.c_str());
LogDebug(("APIClient::" + BaseURL).c_str(), "~APIClient(%s)", BaseURL.c_str());
}

json APIClient::Get(std::string aEndpoint, std::string aParameters)
Expand All @@ -64,12 +64,17 @@ json APIClient::Get(std::string aEndpoint, std::string aParameters)
if (cachedResponse != nullptr)
{
long long diff = Timestamp() - cachedResponse->Timestamp;
Log("APIClient", "diff: %d | curr: %d | cache: %d", diff, Timestamp(), cachedResponse->Timestamp);
Log(("APIClient::" + BaseURL).c_str(), "diff: %d | curr: %d | cache: %d", diff, Timestamp(), cachedResponse->Timestamp);

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

// Variables for synchronization
Expand Down Expand Up @@ -119,7 +124,7 @@ void APIClient::Download(std::filesystem::path aOutPath, std::string aEndpoint,

if (!downloadResult || downloadResult->status != 200 || bytesWritten == 0)
{
LogWarning("APIClient", "Error fetching %s", query.c_str());
LogWarning(("APIClient::" + BaseURL).c_str(), "Error fetching %s", query.c_str());
return;
}
}
Expand Down Expand Up @@ -153,7 +158,7 @@ CachedResponse* APIClient::GetCachedResponse(const std::string& aQuery)
}
catch (json::parse_error& ex)
{
Log("APIClient", "%s could not be parsed. Error: %s", path.string().c_str(), ex.what());
Log(("APIClient::" + BaseURL).c_str(), "%s could not be parsed. Error: %s", path.string().c_str(), ex.what());
}
}

Expand Down Expand Up @@ -300,7 +305,7 @@ void APIClient::ProcessRequests()
std::error_code err;
if (!CreateDirectoryRecursive(normalizedPath.parent_path().string(), err))
{
LogWarning("APIClient", "CreateDirectoryRecursive FAILED, err: % s", err.message());
LogWarning(("APIClient::" + BaseURL).c_str(), "CreateDirectoryRecursive FAILED, err: % s", err.message());
}

std::ofstream file(normalizedPath);
Expand Down Expand Up @@ -343,14 +348,14 @@ APIResponse APIClient::DoHttpReq(APIRequest aRequest)

if (!result)
{
LogWarning("APIClient", "Error fetching %s", aRequest.Query.c_str());
LogWarning(("APIClient::" + BaseURL).c_str(), "Error fetching %s", aRequest.Query.c_str());
response.Status = 1;
return response;
}

if (result->status != 200) // not HTTP_OK
{
LogWarning("APIClient", "Status %d when fetching %s", result->status, aRequest.Query.c_str());
LogWarning(("APIClient::" + BaseURL).c_str(), "Status %d when fetching %s", result->status, aRequest.Query.c_str());
response.Status = result->status;
return response;
}
Expand All @@ -363,13 +368,13 @@ APIResponse APIClient::DoHttpReq(APIRequest aRequest)
}
catch (json::parse_error& ex)
{
LogWarning("APIClient", "Response from %s could not be parsed. Error: %s", aRequest.Query.c_str(), ex.what());
LogWarning(("APIClient::" + BaseURL).c_str(), "Response from %s could not be parsed. Error: %s", aRequest.Query.c_str(), ex.what());
return response;
}

if (jsonResult.is_null())
{
LogWarning("APIClient", "Error parsing API response from %s.", aRequest.Query.c_str());
LogWarning(("APIClient::" + BaseURL).c_str(), "Error parsing API response from %s.", aRequest.Query.c_str());
return response;
}

Expand Down
3 changes: 3 additions & 0 deletions src/GUI/Widgets/Addons/AddonsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ namespace GUI
ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(0.5f, 0.5f));
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 0);
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0);
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1, 1, 1, 1));
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0, 0, 0, 0));

ImGui::SetNextWindowSize(ImVec2(windowWidth * Renderer::Scaling, windowHeight * Renderer::Scaling));
if (ImGui::Begin(Name.c_str(), &Visible, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoTitleBar |
Expand Down Expand Up @@ -249,6 +251,7 @@ namespace GUI
}
ImGui::End();

ImGui::PopStyleColor(2);
ImGui::PopStyleVar(6);
ImGui::PopFont();
}
Expand Down
48 changes: 1 addition & 47 deletions src/GUI/Widgets/Menu/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,58 +74,12 @@ namespace GUI
void AddMenuItem(std::string aLabel, std::string aTextureIdentifier, bool* aToggle)
{
Texture* icon = TextureLoader::Get(aTextureIdentifier.c_str());
MenuItem* mItem = new MenuItem{ aLabel, aToggle, icon, false };
MenuItem* mItem = new MenuItem{ aLabel, aTextureIdentifier, aToggle, icon, false };

{
const std::lock_guard<std::mutex> lock(Menu::Mutex);
MenuItems.push_back(mItem);
}

if (icon == nullptr)
{
std::thread([mItem, aLabel, aTextureIdentifier, aToggle]()
{
const std::lock_guard<std::mutex> lock(Menu::Mutex);
{
// This code is copy pasted from quick access, bit of a clownfiesta not gonna lie
int tries = 0;

LogDebug("CH_MENU", "Menu Item \"%s\" was promised 1 textures, but received 0.", aLabel.c_str());
Sleep(100); // first retry after 100ms

while (mItem->Icon == nullptr)
{
if (tries > 10)
{
LogWarning("CH_MENU", "Cancelled getting textures for menu item \"%s\" after 10 failed attempts.", aLabel.c_str());
break;
}

if (mItem->Icon == nullptr) { mItem->Icon = TextureLoader::Get(aTextureIdentifier.c_str()); }

tries++;
Sleep(tries * 500);
}

/* if not all tries were used, then the texture was loaded */
if (tries <= 10)
{
LogDebug("CH_MENU", "Menu Item \"%s\" received promised texture after %d attempt(s).", aLabel.c_str(), tries);
return;
}

/* fallback icons */
mItem->Icon = TextureLoader::Get(ICON_GENERIC);

/* absolute sanity check */
if (mItem->Icon == nullptr)
{
LogWarning("CH_MENU", "Neither promised textures nor fallback textures could be loaded, removing menu item \"%s\".", aLabel.c_str());
return;
}
}
}).detach();
}
}

void ReceiveTextures(const char* aIdentifier, Texture* aTexture)
Expand Down
5 changes: 5 additions & 0 deletions src/GUI/Widgets/Menu/MenuItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Renderer.h"

#include "Menu.h"
#include "Textures/TextureLoader.h"
#include "Textures/Texture.h"

#include "imgui.h"
Expand Down Expand Up @@ -60,6 +61,10 @@ namespace GUI
ImGui::Image(Icon->Resource, ImVec2(iconSize, iconSize));
ImGui::PopItemFlag();
}
else
{
Icon = TextureLoader::Get(TextureIdentifier.c_str());
}

ImGui::SetCursorPos(txtPos);
ImGui::Text(Label.c_str());
Expand Down
1 change: 1 addition & 0 deletions src/GUI/Widgets/Menu/MenuItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace GUI
struct MenuItem
{
std::string Label;
std::string TextureIdentifier;
bool* Toggle;
Texture* Icon;
bool IsHovering;
Expand Down
39 changes: 21 additions & 18 deletions src/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,35 @@ namespace Hooks

LRESULT __stdcall WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// don't pass to game if loader
if (Loader::WndProc(hWnd, uMsg, wParam, lParam) == 0) { return 0; }
if (State::Nexus != ENexusState::SHUTDOWN)
{
// don't pass to game if loader
if (Loader::WndProc(hWnd, uMsg, wParam, lParam) == 0) { return 0; }

// don't pass to game if custom wndproc
if (WndProc::WndProc(hWnd, uMsg, wParam, lParam) == 0) { return 0; }
// don't pass to game if custom wndproc
if (WndProc::WndProc(hWnd, uMsg, wParam, lParam) == 0) { return 0; }

// don't pass to game if keybind
if (Keybinds::WndProc(hWnd, uMsg, wParam, lParam) == 0) { return 0; }
// don't pass to game if keybind
if (Keybinds::WndProc(hWnd, uMsg, wParam, lParam) == 0) { return 0; }

// don't pass to game if gui
if (GUI::WndProc(hWnd, uMsg, wParam, lParam) == 0) { return 0; }
// don't pass to game if gui
if (GUI::WndProc(hWnd, uMsg, wParam, lParam) == 0) { return 0; }

// don't pass keys to game if currently editing keybinds
if (Keybinds::IsSettingKeybind)
{
if (uMsg == WM_SYSKEYDOWN || uMsg == WM_KEYDOWN || uMsg == WM_SYSKEYUP || uMsg == WM_KEYUP)
// don't pass keys to game if currently editing keybinds
if (Keybinds::IsSettingKeybind)
{
return 0;
if (uMsg == WM_SYSKEYDOWN || uMsg == WM_KEYDOWN || uMsg == WM_SYSKEYUP || uMsg == WM_KEYUP)
{
return 0;
}
}
}

if (uMsg == WM_DESTROY || uMsg == WM_QUIT || uMsg == WM_CLOSE)
{
Main::Shutdown(uMsg);
if (uMsg == WM_DESTROY || uMsg == WM_QUIT || uMsg == WM_CLOSE)
{
Main::Shutdown(uMsg);
}
}

return CallWindowProcA(Hooks::GW2::WndProc, hWnd, uMsg, wParam, lParam);
}
HRESULT __stdcall DXGIPresent(IDXGISwapChain* pChain, UINT SyncInterval, UINT Flags)
Expand Down
5 changes: 5 additions & 0 deletions src/Keybinds/KeybindHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ namespace Keybinds
kb.Key = 0;
}

if (!kb.Alt && !kb.Ctrl && !kb.Shift && kb.Key == 0)
{
return uMsg;
}

if (keylp.PreviousKeyState)
{
return uMsg;
Expand Down
2 changes: 1 addition & 1 deletion src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ namespace Main

LogInfo(CH_CORE, "Shutdown performed.");

SetWindowLongPtr(Renderer::WindowHandle, GWLP_WNDPROC, (LONG_PTR)Hooks::GW2::WndProc);
//SetWindowLongPtr(Renderer::WindowHandle, GWLP_WNDPROC, (LONG_PTR)Hooks::GW2::WndProc);

State::Nexus = ENexusState::SHUTDOWN;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nexus.rc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ BEGIN
VALUE "FileDescription", "Unofficial Guild Wars 2 Addon Host"
VALUE "FileVersion", TOSTRING(V_MAJOR) "." TOSTRING(V_MINOR) "." TOSTRING(V_BUILD) "." TOSTRING(V_REVISION)
VALUE "InternalName", "nexus.dll"
VALUE "LegalCopyright", "� 2023 Raidcore"
VALUE "LegalCopyright", "� 2023-2024 Raidcore"
VALUE "ProductName", "Nexus"
VALUE "ProductVersion", TOSTRING(V_MAJOR) "." TOSTRING(V_MINOR) "." TOSTRING(V_BUILD) "." TOSTRING(V_REVISION)
END
Expand Down
14 changes: 11 additions & 3 deletions src/Textures/TextureLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace TextureLoader

void LoadFromFile(const char* aIdentifier, const char* aFilename, TEXTURES_RECEIVECALLBACK aCallback)
{
LogInfo(CH_TEXTURES, "TextureLoader::LoadFromFile(aIdentifier: %s, aFilename: %s, aCallback: %p)", aIdentifier, aFilename, aCallback);

std::string str = aIdentifier;

Texture* tex = Get(str.c_str());
Expand Down Expand Up @@ -70,6 +72,8 @@ namespace TextureLoader
}
void LoadFromResource(const char* aIdentifier, unsigned aResourceID, HMODULE aModule, TEXTURES_RECEIVECALLBACK aCallback)
{
LogInfo(CH_TEXTURES, "TextureLoader::LoadFromResource(aIdentifier: %s, aResourceID: %u, aModule: %p, aCallback: %p)", aIdentifier, aResourceID, aModule, aCallback);

std::string str = aIdentifier;

Texture* tex = Get(str.c_str());
Expand Down Expand Up @@ -119,6 +123,8 @@ namespace TextureLoader
}
void LoadFromURL(const char* aIdentifier, const char* aRemote, const char* aEndpoint, TEXTURES_RECEIVECALLBACK aCallback)
{
LogInfo(CH_TEXTURES, "TextureLoader::LoadFromURL(aIdentifier: %s, aRemote: %s, aEndpoint: %s, aCallback: %p)", aIdentifier, aRemote, aEndpoint, aCallback);

std::string str = aIdentifier;

Texture* tex = Get(str.c_str());
Expand Down Expand Up @@ -164,6 +170,8 @@ namespace TextureLoader
}
void LoadFromMemory(const char* aIdentifier, void* aData, size_t aSize, TEXTURES_RECEIVECALLBACK aCallback)
{
LogInfo(CH_TEXTURES, "TextureLoader::LoadFromMemory(aIdentifier: %s, aData: %p, aSize: %u, aCallback: %p)", aIdentifier, aData, aSize, aCallback);

std::string str = aIdentifier;

Texture* tex = Get(str.c_str());
Expand Down Expand Up @@ -199,7 +207,7 @@ namespace TextureLoader
{
std::string str = aIdentifier;

//LogDebug(CH_TEXTURES, "Queued %s", str.c_str());
LogDebug(CH_TEXTURES, "Queued %s", str.c_str());

QueuedTexture raw{};
raw.Identifier = str;
Expand All @@ -216,7 +224,7 @@ namespace TextureLoader
}
void CreateTexture(QueuedTexture aQueuedTexture)
{
//LogDebug(CH_TEXTURES, "Create %s", aQueuedTexture.Identifier.c_str());
LogDebug(CH_TEXTURES, "Create %s", aQueuedTexture.Identifier.c_str());

Texture* tex = new Texture{};
tex->Width = aQueuedTexture.Width;
Expand Down Expand Up @@ -244,7 +252,7 @@ namespace TextureLoader

if (!pTexture)
{
//LogDebug(CH_TEXTURES, "pTexture was null");
LogDebug(CH_TEXTURES, "pTexture was null");
stbi_image_free(aQueuedTexture.Data);
return;
}
Expand Down

0 comments on commit 80aa3e3

Please sign in to comment.