|
| 1 | +#include "CDAIdBank.h" |
| 2 | +#include "SimConnect.h" |
| 3 | +#include "Logger.h" |
| 4 | + |
| 5 | +using namespace std; |
| 6 | +using namespace CDAIdBankMSFS; |
| 7 | +using namespace CPlusPlusLogging; |
| 8 | + |
| 9 | +CDAIdBank::CDAIdBank(int id, HANDLE hSimConnect) { |
| 10 | + nextId = id; |
| 11 | + this->hSimConnect = hSimConnect; |
| 12 | +} |
| 13 | + |
| 14 | +CDAIdBank::~CDAIdBank() { |
| 15 | + |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +pair<string, int> CDAIdBank::getId(int size) { |
| 20 | + char szLogBuffer[512]; |
| 21 | + pair<string, int> returnVal; |
| 22 | + std::map<int, pair<string, int>>::iterator it; |
| 23 | + |
| 24 | + // First, check if we have one available |
| 25 | + it = availableBank.find(size); |
| 26 | + if (it != availableBank.end()) { |
| 27 | + returnVal = make_pair(it->second.first, it->second.second); |
| 28 | + availableBank.erase(it); |
| 29 | + pair<int, pair<string, int>> out = make_pair(returnVal.second, make_pair(returnVal.first, size)); |
| 30 | + outBank.insert(out); |
| 31 | + } |
| 32 | + else { |
| 33 | + // Create new CDA |
| 34 | + string newName = string(CDA_NAME_TEMPLATE + to_string(nextId)); |
| 35 | + returnVal = getId(size, newName); |
| 36 | + } |
| 37 | + |
| 38 | + return returnVal; |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +pair<string, int> CDAIdBank::getId(int size, string name) { |
| 43 | + char szLogBuffer[512]; |
| 44 | + pair<string, int> returnVal; |
| 45 | + std::map<int, pair<string, int>>::iterator it; |
| 46 | + DWORD dwLastID; |
| 47 | + |
| 48 | + // First, check if we have one available |
| 49 | + it = availableBank.find(size); |
| 50 | + if (it != availableBank.end()) { |
| 51 | + returnVal = make_pair(it->second.first, it->second.second); |
| 52 | + availableBank.erase(it); |
| 53 | + pair<int, pair<string, int>> out = make_pair(returnVal.second, make_pair(returnVal.first, size)); |
| 54 | + outBank.insert(out); |
| 55 | + } |
| 56 | + else { |
| 57 | + // Create new CDA |
| 58 | + string newName = string(name); |
| 59 | + returnVal = make_pair(newName, nextId); |
| 60 | + pair<int, pair<string, int>> out = make_pair(nextId, make_pair(newName, size)); |
| 61 | + outBank.insert(out); |
| 62 | + |
| 63 | + // Set-up CDA |
| 64 | + if (!SUCCEEDED(SimConnect_MapClientDataNameToID(hSimConnect, newName.c_str(), nextId))) { |
| 65 | + sprintf_s(szLogBuffer, sizeof(szLogBuffer), "Error mapping CDA name %s to ID %d", newName.c_str(), nextId); |
| 66 | + LOG_ERROR(szLogBuffer); |
| 67 | + } |
| 68 | + else { |
| 69 | + SimConnect_GetLastSentPacketID(hSimConnect, &dwLastID); |
| 70 | + sprintf_s(szLogBuffer, sizeof(szLogBuffer), "CDA name %s mapped to ID %d [requestId=%lu]", newName.c_str(), nextId, dwLastID); |
| 71 | + LOG_DEBUG(szLogBuffer); |
| 72 | + } |
| 73 | + |
| 74 | + // Finally, Create the client data if it doesn't already exist |
| 75 | + if (!SUCCEEDED(SimConnect_CreateClientData(hSimConnect, nextId, size, SIMCONNECT_CREATE_CLIENT_DATA_FLAG_READ_ONLY))) { |
| 76 | + sprintf_s(szLogBuffer, sizeof(szLogBuffer), "Error creating client data area with id=%d and size=%d", nextId, size); |
| 77 | + LOG_ERROR(szLogBuffer); |
| 78 | + } |
| 79 | + else { |
| 80 | + SimConnect_GetLastSentPacketID(hSimConnect, &dwLastID); |
| 81 | + sprintf_s(szLogBuffer, sizeof(szLogBuffer), "Client data area created with id=%d (size=%d) [requestID=%lu]", nextId, size, dwLastID); |
| 82 | + LOG_DEBUG(szLogBuffer); |
| 83 | + } |
| 84 | + nextId++; |
| 85 | + } |
| 86 | + |
| 87 | + return returnVal; |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +void CDAIdBank::returnId(int id) { |
| 93 | + std::map<int, pair<string, int>>::iterator it; |
| 94 | + // First, check if we have one available |
| 95 | + it = outBank.find(id); |
| 96 | + if (it != outBank.end()) { |
| 97 | + pair<string, int> p = outBank.at(id); |
| 98 | + pair<int, pair<string, int>> returnedItem = make_pair(p.second, make_pair(p.first, id)); |
| 99 | + outBank.erase(it); |
| 100 | + availableBank.insert(returnedItem); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments