|
| 1 | +#include <StdInc.h> |
| 2 | + |
| 3 | +#include <MakeClientFunction.h> |
| 4 | + |
| 5 | +#include <ServerInstanceBase.h> |
| 6 | +#include <ServerInstanceBaseRef.h> |
| 7 | + |
| 8 | +#include <ResourceManager.h> |
| 9 | +#include <ScriptEngine.h> |
| 10 | + |
| 11 | +#include <HttpClient.h> |
| 12 | + |
| 13 | +#include <ServerLicensingComponent.h> |
| 14 | + |
| 15 | +#include <json.hpp> |
| 16 | + |
| 17 | +#include <optional> |
| 18 | + |
| 19 | +inline std::string GetLicenseKey() |
| 20 | +{ |
| 21 | + auto resourceManager = fx::ResourceManager::GetCurrent(); |
| 22 | + auto instance = resourceManager->GetComponent<fx::ServerInstanceBaseRef>()->Get(); |
| 23 | + auto licensing = instance->GetComponent<ServerLicensingComponent>(); |
| 24 | + |
| 25 | + return licensing->GetLicenseKey(); |
| 26 | +} |
| 27 | + |
| 28 | +class CommerceComponent : public fwRefCountable |
| 29 | +{ |
| 30 | +public: |
| 31 | + CommerceComponent(fx::Client* client) |
| 32 | + : m_commerceDataLoaded(false), m_client(client) |
| 33 | + { |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + inline bool HasCommerceDataLoaded() |
| 38 | + { |
| 39 | + return m_commerceDataLoaded; |
| 40 | + } |
| 41 | + |
| 42 | + void LoadCommerceData(); |
| 43 | + |
| 44 | + std::optional<int> GetUserId(); |
| 45 | + |
| 46 | + void SetSkus(std::set<int>&& list); |
| 47 | + |
| 48 | + bool OwnsSku(int sku); |
| 49 | + |
| 50 | + void RequestSkuPurchase(int sku); |
| 51 | + |
| 52 | +private: |
| 53 | + fx::Client* m_client; |
| 54 | + |
| 55 | + bool m_commerceDataLoaded; |
| 56 | + |
| 57 | + std::set<int> m_ownedSkus; |
| 58 | +}; |
| 59 | + |
| 60 | +static HttpClient* httpClient = new HttpClient(L"FXServer/Licensing"); |
| 61 | + |
| 62 | +void CommerceComponent::LoadCommerceData() |
| 63 | +{ |
| 64 | + auto userId = GetUserId(); |
| 65 | + |
| 66 | + if (m_commerceDataLoaded || !userId) |
| 67 | + { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + fwRefContainer<CommerceComponent> thisRef(this); |
| 72 | + |
| 73 | + httpClient->DoGetRequest(fmt::sprintf(LICENSING_EP "api/entitlements/%d/%s", *userId, GetLicenseKey()), [thisRef](bool success, const char* data, size_t length) |
| 74 | + { |
| 75 | + if (success) |
| 76 | + { |
| 77 | + try |
| 78 | + { |
| 79 | + auto json = nlohmann::json::parse(std::string(data, length)); |
| 80 | + std::set<int> skuIds; |
| 81 | + |
| 82 | + for (auto& entry : json["entitlements"]) |
| 83 | + { |
| 84 | + skuIds.insert(entry.value<int>("sku_id", 0)); |
| 85 | + } |
| 86 | + |
| 87 | + thisRef->SetSkus(std::move(skuIds)); |
| 88 | + } |
| 89 | + catch (const std::exception& e) |
| 90 | + { |
| 91 | + |
| 92 | + } |
| 93 | + } |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +void CommerceComponent::SetSkus(std::set<int>&& list) |
| 98 | +{ |
| 99 | + m_ownedSkus = std::move(list); |
| 100 | + m_commerceDataLoaded = true; |
| 101 | +} |
| 102 | + |
| 103 | +bool CommerceComponent::OwnsSku(int sku) |
| 104 | +{ |
| 105 | + return m_ownedSkus.find(sku) != m_ownedSkus.end(); |
| 106 | +} |
| 107 | + |
| 108 | +void CommerceComponent::RequestSkuPurchase(int sku) |
| 109 | +{ |
| 110 | + auto userId = GetUserId(); |
| 111 | + |
| 112 | + if (!userId) |
| 113 | + { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + fwRefContainer<CommerceComponent> thisRef(this); |
| 118 | + auto clientRef = m_client->shared_from_this(); |
| 119 | + |
| 120 | + httpClient->DoGetRequest(fmt::sprintf(LICENSING_EP "api/paymentRequest/%d/%d/%s", *userId, sku, GetLicenseKey()), [thisRef, clientRef](bool success, const char* data, size_t length) |
| 121 | + { |
| 122 | + if (success) |
| 123 | + { |
| 124 | + // build the target event |
| 125 | + net::Buffer outBuffer; |
| 126 | + outBuffer.Write(HashRageString("msgPaymentRequest")); |
| 127 | + |
| 128 | + // payload |
| 129 | + outBuffer.Write(data, length); |
| 130 | + |
| 131 | + // send along |
| 132 | + clientRef->SendPacket(0, outBuffer, NetPacketType_Reliable); |
| 133 | + } |
| 134 | + }); |
| 135 | +} |
| 136 | + |
| 137 | +std::optional<int> CommerceComponent::GetUserId() |
| 138 | +{ |
| 139 | + const auto& identifiers = m_client->GetIdentifiers(); |
| 140 | + |
| 141 | + for (const auto& identifier : identifiers) |
| 142 | + { |
| 143 | + if (identifier.find("fivem:") == 0) |
| 144 | + { |
| 145 | + int userId = atoi(identifier.substr(6).c_str()); |
| 146 | + |
| 147 | + if (userId != 0) |
| 148 | + { |
| 149 | + return userId; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return {}; |
| 155 | +} |
| 156 | + |
| 157 | +DECLARE_INSTANCE_TYPE(CommerceComponent); |
| 158 | + |
| 159 | +static InitFunction initFunction([]() |
| 160 | +{ |
| 161 | + fx::ScriptEngine::RegisterNativeHandler("CAN_PLAYER_START_COMMERCE_SESSION", MakeClientFunction([](fx::ScriptContext& context, const std::shared_ptr<fx::Client>& client) -> uint32_t |
| 162 | + { |
| 163 | + return client->GetComponent<CommerceComponent>()->GetUserId() ? true : false; |
| 164 | + })); |
| 165 | + |
| 166 | + fx::ServerInstanceBase::OnServerCreate.Connect([](fx::ServerInstanceBase* instance) |
| 167 | + { |
| 168 | + auto clientRegistry = instance->GetComponent<fx::ClientRegistry>(); |
| 169 | + |
| 170 | + clientRegistry->OnClientCreated.Connect([=](fx::Client* client) |
| 171 | + { |
| 172 | + client->SetComponent(new CommerceComponent(client)); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + fx::ScriptEngine::RegisterNativeHandler("LOAD_PLAYER_COMMERCE_DATA", MakeClientFunction([](fx::ScriptContext& context, const std::shared_ptr<fx::Client>& client) -> uint32_t |
| 177 | + { |
| 178 | + auto commerceData = client->GetComponent<CommerceComponent>(); |
| 179 | + |
| 180 | + commerceData->LoadCommerceData(); |
| 181 | + |
| 182 | + return commerceData->HasCommerceDataLoaded(); |
| 183 | + })); |
| 184 | + |
| 185 | + fx::ScriptEngine::RegisterNativeHandler("IS_PLAYER_COMMERCE_INFO_LOADED", MakeClientFunction([](fx::ScriptContext& context, const std::shared_ptr<fx::Client>& client) -> uint32_t |
| 186 | + { |
| 187 | + auto commerceData = client->GetComponent<CommerceComponent>(); |
| 188 | + |
| 189 | + return commerceData->HasCommerceDataLoaded(); |
| 190 | + })); |
| 191 | + |
| 192 | + fx::ScriptEngine::RegisterNativeHandler("DOES_PLAYER_OWN_SKU", MakeClientFunction([](fx::ScriptContext& context, const std::shared_ptr<fx::Client>& client) -> uint32_t |
| 193 | + { |
| 194 | + auto commerceData = client->GetComponent<CommerceComponent>(); |
| 195 | + |
| 196 | + return commerceData->OwnsSku(context.GetArgument<int>(1)); |
| 197 | + })); |
| 198 | + |
| 199 | + fx::ScriptEngine::RegisterNativeHandler("REQUEST_PLAYER_COMMERCE_SESSION", MakeClientFunction([](fx::ScriptContext& context, const std::shared_ptr<fx::Client>& client) -> bool |
| 200 | + { |
| 201 | + auto commerceData = client->GetComponent<CommerceComponent>(); |
| 202 | + commerceData->RequestSkuPurchase(context.GetArgument<int>(1)); |
| 203 | + |
| 204 | + return true; |
| 205 | + })); |
| 206 | +}); |
0 commit comments