Skip to content

Commit

Permalink
export get_user_agent_clause (#1071)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored Feb 24, 2024
1 parent f2e8239 commit 11a935e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/data/context/EmulatorContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ void EmulatorContext::UpdateUserAgent()
ra::services::ServiceLocator::GetMutable<ra::services::IHttpRequester>().SetUserAgent(sUserAgent);
}

std::string EmulatorContext::GetUserAgentClause() const
{
std::string sClause = "Integration/";
AppendIntegrationVersion(sClause);
return sClause;
}

static unsigned long long ParseVersion(const char* sVersion)
{
Expects(sVersion != nullptr);
Expand Down
4 changes: 4 additions & 0 deletions src/data/context/EmulatorContext.hh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public:
/// </summary>
void SetClientUserAgentDetail(const std::string& sDetail) { m_sClientUserAgentDetail = sDetail; UpdateUserAgent(); }

/// <summary>
/// Gets the Integration/X.X.X.X portion of the User-Agent clause.
/// </summary>
std::string GetUserAgentClause() const;

/// <summary>
/// Prompts the user to confirm disabling hardcore mode to perform some activity.
Expand Down
8 changes: 8 additions & 0 deletions src/services/AchievementRuntimeExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ class AchievementRuntimeExports : private AchievementRuntime
pConfiguration->SetHost(value);
}

static size_t get_user_agent_clause(char buffer[], size_t buffer_size)
{
auto& pEmulatorContext = ra::services::ServiceLocator::GetMutable<ra::data::context::EmulatorContext>();
const auto sUserAgent = pEmulatorContext.GetUserAgentClause();
return snprintf(buffer, buffer_size, "%s", sUserAgent.c_str());
}

static bool IsUpdatingHardcore() noexcept { return s_bUpdatingHardcore; }

static void set_hardcore_enabled(int value)
Expand Down Expand Up @@ -892,6 +899,7 @@ static void GetExternalClientV1(rc_client_external_t* pClientExternal) noexcept
pClientExternal->set_read_memory = ra::services::AchievementRuntimeExports::set_read_memory;
pClientExternal->set_get_time_millisecs = ra::services::AchievementRuntimeExports::set_get_time_millisecs;
pClientExternal->set_host = ra::services::AchievementRuntimeExports::set_host;
pClientExternal->get_user_agent_clause = ra::services::AchievementRuntimeExports::get_user_agent_clause;

pClientExternal->set_hardcore_enabled = ra::services::AchievementRuntimeExports::set_hardcore_enabled;
pClientExternal->get_hardcore_enabled = ra::services::AchievementRuntimeExports::get_hardcore_enabled;
Expand Down
21 changes: 21 additions & 0 deletions tests/services/AchievementRuntimeExports_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <rcheevos\include\rc_client_raintegration.h>

#include "Exports.hh"
#include "RA_BuildVer.h"
#include "RA_Resource.h"

// the exported functions are not defined in a header anywhere. the only thing that should normally be
Expand Down Expand Up @@ -224,6 +225,7 @@ TEST_CLASS(AchievementRuntimeExports_Tests)
Assert::IsNotNull((void*)pClient.set_read_memory, L"set_read_memory not set");
Assert::IsNotNull((void*)pClient.set_get_time_millisecs, L"set_get_time_millisecs not set");
Assert::IsNotNull((void*)pClient.set_host, L"set_host not set");
Assert::IsNotNull((void*)pClient.get_user_agent_clause, L"set_host not set");

Assert::IsNotNull((void*)pClient.set_hardcore_enabled, L"set_hardcore_enabled not set");
Assert::IsNotNull((void*)pClient.get_hardcore_enabled, L"get_hardcore_enabled not set");
Expand Down Expand Up @@ -425,6 +427,25 @@ TEST_CLASS(AchievementRuntimeExports_Tests)

runtime.AssertResetEventSeen();
}

TEST_METHOD(TestUserAgentClause)
{
AchievementRuntimeExportsHarness runtime;

rc_client_external_t pClient;
memset(&pClient, 0, sizeof(pClient));
_Rcheevos_GetExternalClient(&pClient, 1);

char buffer[32];
Assert::IsTrue(pClient.get_user_agent_clause(buffer, 8) > 8);
Assert::AreEqual("Integra", buffer);

pClient.get_user_agent_clause(buffer, 32);
char* ptr = strchr(buffer, '-');
if (ptr != nullptr)
*ptr = '\0';
Assert::AreEqual("Integration/" RA_INTEGRATION_VERSION, buffer);
}
};

} // namespace tests
Expand Down

0 comments on commit 11a935e

Please sign in to comment.