-
-
Notifications
You must be signed in to change notification settings - Fork 21
Optimize RPN execution to reduce FPS overhead via deduplication #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -44,19 +44,22 @@ uint16_t MOBIFLIGHT_MAX_VARS_PER_FRAME = 30; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // due to the maximum client-data-array-size (SIMCONNECT_CLIENTDATA_MAX_SIZE) of 8kB! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| constexpr uint16_t MOBIFLIGHT_STRING_SIMVAR_VALUE_MAX_LEN = 128; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //declare struct Client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct Client; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // data struct for dynamically registered SimVars | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct SimVar { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int ID; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int Offset; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::string Name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| float Value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct Client * client; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct StringSimVar { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int ID; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int Offset; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::string Name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::string Value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct Client * client; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // data struct for client accessing SimVars | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -81,12 +84,23 @@ struct Client { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // This is an optimization to be able to re-use already defined data definition IDs & request IDs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // after resetting registered SimVars | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uint16_t MaxClientDataDefinition = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Runtime Rolling CLient Data reading Index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //std::vector<SimVar>::iterator RollingClientDataReadIndex; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uint16_t RollingClientDataReadIndex; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Runtime Rolling CLient Data reading Index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uint16_t RollingDataReadIndex = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //RPN code execution for reading values in every frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct ReadRPNCode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::string Code; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //RetType: 0:float 1:integer 2:string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int RetType; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<SimVar> SimVars; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<StringSimVar> StringSimVars; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<ReadRPNCode> RPNCodelist; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // The list of currently registered clients | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<Client*> RegisteredClients; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -255,10 +269,10 @@ void WriteSimVar(StringSimVar& simVar, Client* client) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (hr != S_OK) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fprintf(stderr, "MobiFlight[%s]: Error on Setting String Client Data. %lu, SimVar: %s (String-ID: %ul)\n", client->Name.c_str(), hr, simVar.Name.c_str(), simVar.ID); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fprintf(stderr, "MobiFlight[%s]: Error on Setting String Client Data. %lu, SimVar: (String-ID: %ul)\n", client->Name.c_str(), hr, simVar.ID); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if _DEBUG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: Written String-SimVar " << simVar.Name.c_str(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: Written String-SimVar"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << " with String-ID " << simVar.ID << " has value " << simVar.Value.c_str() << std::endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -276,27 +290,49 @@ void WriteSimVar(SimVar& simVar, Client* client) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (hr != S_OK) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fprintf(stderr, "MobiFlight[%s]: Error on Setting Client Data. %lu, SimVar: %s (ID: %u)", client->Name.c_str(), hr, simVar.Name.c_str(), simVar.ID); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fprintf(stderr, "MobiFlight[%s]: Error on Setting Client Data. %lu, SimVar: (ID: %u)", client->Name.c_str(), hr, simVar.ID); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if _DEBUG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: Written SimVar " << simVar.Name.c_str(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: Written SimVar"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << " with ID " << simVar.ID << " has value " << simVar.Value << std::endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //check whether SimVar has already registered in SimVar list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ReadRPNCode* IsDuplicatedSimVar(const std::string code) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (auto &rpn : RPNCodelist) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (rpn.Code == code) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return &rpn; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+302
to
+309
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Register a single Float-SimVar and send the current value to SimConnect Clients | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void RegisterFloatSimVar(const std::string code, Client* client) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<SimVar>* SimVars = &(client->SimVars); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<StringSimVar>* StringSimVars = &(client->StringSimVars); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SimVar newSimVar; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ReadRPNCode* pdupRpn = IsDuplicatedSimVar(code); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HRESULT hr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newSimVar.Name = code; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newSimVar.ID = SimVars->size() + client->DataDefinitionIdSimVarsStart; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newSimVar.Offset = SimVars->size() * (sizeof(float)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newSimVar.Value = 0.0F; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newSimVar.client = client; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SimVars->push_back(newSimVar); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //duplicated SimVar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (pdupRpn) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pdupRpn->SimVars.push_back(newSimVar); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ReadRPNCode rpnCode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rpnCode.Code = code; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rpnCode.RetType = 0;//hardcoded type id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rpnCode.SimVars.push_back(newSimVar); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RPNCodelist.push_back(rpnCode); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
323
to
+334
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (client->MaxClientDataDefinition < (SimVars->size() + StringSimVars->size())) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hr = SimConnect_AddToClientDataDefinition( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| g_hSimConnect, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -307,11 +343,11 @@ void RegisterFloatSimVar(const std::string code, Client* client) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (hr != S_OK) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fprintf(stderr, "MobiFlight[%s]: Error on adding Client Data \"%s\" with ID: %u, Offset: %u and Size: %lu\n", client->Name.c_str(), newSimVar.Name.c_str(), newSimVar.ID, newSimVar.Offset, sizeof(float)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fprintf(stderr, "MobiFlight[%s]: Error on adding Client Data \"%s\" with ID: %u, Offset: %u and Size: %lu\n", client->Name.c_str(), code.c_str(), newSimVar.ID, newSimVar.Offset, sizeof(float)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if _DEBUG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: Added SimVar > " << newSimVar.Name.c_str(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: Added SimVar > " << code.c_str(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << " with ID: " << newSimVar.ID << ", Offset: " << newSimVar.Offset << " and Size: " << sizeof(float) << std::endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: RegisterFloatSimVar SimVars Size: " << SimVars->size() << std::endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -324,7 +360,7 @@ void RegisterFloatSimVar(const std::string code, Client* client) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newSimVar.Value = floatVal; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| WriteSimVar(newSimVar, client); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if _DEBUG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: RegisterFloatSimVar > " << newSimVar.Name.c_str(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: RegisterFloatSimVar > " << code.c_str(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << " ID [" << newSimVar.ID << "] : Offset(" << newSimVar.Offset << ") : Value(" << newSimVar.Value << ")" << std::endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -334,14 +370,26 @@ void RegisterStringSimVar(const std::string code, Client* client) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<SimVar>* SimVars = &(client->SimVars); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<StringSimVar>* StringSimVars = &(client->StringSimVars); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StringSimVar newStringSimVar; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ReadRPNCode* pdupRpn = IsDuplicatedSimVar(code); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HRESULT hr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newStringSimVar.Name = code; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newStringSimVar.ID = StringSimVars->size() + client->DataDefinitionIdStringVarsStart; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newStringSimVar.Offset = StringSimVars->size() * MOBIFLIGHT_STRING_SIMVAR_VALUE_MAX_LEN; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newStringSimVar.Value.empty(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newStringSimVar.client = client; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StringSimVars->push_back(newStringSimVar); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //duplicated SimVar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (pdupRpn) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pdupRpn->StringSimVars.push_back(newStringSimVar); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ReadRPNCode rpnCode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rpnCode.Code = code; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rpnCode.RetType = 2;//hardcoded type id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rpnCode.StringSimVars.push_back(newStringSimVar); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RPNCodelist.push_back(rpnCode); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
380
to
+391
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (client->MaxClientDataDefinition < (SimVars->size() + StringSimVars->size())) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hr = SimConnect_AddToClientDataDefinition( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| g_hSimConnect, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -352,11 +400,11 @@ void RegisterStringSimVar(const std::string code, Client* client) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (hr != S_OK) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fprintf(stderr, "MobiFlight[%s]: Error on adding Client Data \"%s\" with String-ID: %u, String-Offset: %u and Size: %u\n", client->Name.c_str(), newStringSimVar.Name.c_str(), newStringSimVar.ID, newStringSimVar.Offset, MOBIFLIGHT_STRING_SIMVAR_VALUE_MAX_LEN); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fprintf(stderr, "MobiFlight[%s]: Error on adding Client Data \"%s\" with String-ID: %u, String-Offset: %u and Size: %u\n", client->Name.c_str(), code.c_str(), newStringSimVar.ID, newStringSimVar.Offset, MOBIFLIGHT_STRING_SIMVAR_VALUE_MAX_LEN); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if _DEBUG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: Added String-SimVar > " << newStringSimVar.Name.c_str(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: Added String-SimVar > " << code.c_str(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << " with String-ID: " << newStringSimVar.ID << ", String-Offset: " << newStringSimVar.Offset << " and Size: " << MOBIFLIGHT_STRING_SIMVAR_VALUE_MAX_LEN << std::endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: RegisterStringSimVar StringSimVars Size: " << StringSimVars->size() << std::endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -369,7 +417,7 @@ void RegisterStringSimVar(const std::string code, Client* client) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newStringSimVar.Value = std::string(charVal, strnlen(charVal, MOBIFLIGHT_STRING_SIMVAR_VALUE_MAX_LEN)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| WriteSimVar(newStringSimVar, client); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if _DEBUG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: RegisterStringSimVar > " << newStringSimVar.Name.c_str(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << "MobiFlight[" << client->Name.c_str() << "]: RegisterStringSimVar > " << code.c_str(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::cout << " ID [" << newStringSimVar.ID << "] : Offset(" << newStringSimVar.Offset << ") : Value(" << newStringSimVar.Value << ")" << std::endl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -391,66 +439,99 @@ void ClearSimVars(Client* client) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| WriteSimVar(simVar, client); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| client->StringSimVars.clear(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // clear RNP code list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // clear RNP code list | |
| // clear RPN code list |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in comment: "RNP" should be "RPN" to match the actual data structure name (ReadRPNCode).
| //remove empty RNP code | |
| //remove empty RPN code |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Outdated comment reference. The comment refers to client->RollingDataReadIndex but the code now uses the global RollingDataReadIndex. Either update the comment to reflect the current implementation or remove it as it may cause confusion.
| //client->RollingDataReadIndex = client->SimVars.begin(); |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The floating-point comparison logic is flawed. The condition (simVar.Value > floatVal) && (simVar.Value - floatVal < 0.00001F) will skip updates when values are nearly equal AND the old value is greater. However, if simVar.Value is exactly equal to floatVal, or if they differ by more than 0.00001, the update proceeds. The logic should use std::abs(simVar.Value - floatVal) < 0.00001F to properly check if values are approximately equal regardless of which is larger. Also note the epsilon threshold may be too small for some float ranges.
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing case for RetType == 1 (integer). According to the comment on line 95, RetType can be 0 (float), 1 (integer), or 2 (string). However, the code only handles types 0 and 2. If a ReadRPNCode entry has RetType == 1, it will be silently ignored without executing the RPN code or updating any values. Either add handling for integer types or remove the integer type from the comment if it's not supported.
| // Read a single SimVar and send the current value to SimConnect Clients (overloaded for float SimVars) | |
| void ReadSimVar(ReadRPNCode &rpn) { | |
| if (rpn.RetType == 0) { | |
| ReadSimVarFloat(rpn); | |
| // Read a single SimVar and send the current value to SimConnect Clients (overloaded for float SimVars) | |
| void ReadSimVarInt(ReadRPNCode &rpn) { | |
| int intVal = 0; | |
| execute_calculator_code(std::string(rpn.Code).c_str(), &intVal, nullptr, nullptr); | |
| for (auto& simVar : rpn.SimVars) { | |
| if (simVar.Value == static_cast<double>(intVal)) continue; | |
| simVar.Value = static_cast<double>(intVal); | |
| WriteSimVar(simVar, simVar.client); | |
| #if _DEBUG | |
| std::cout << "MobiFlight[" << simVar.client->Name.c_str() << "]: IntSimVar " << rpn.Code.c_str(); | |
| std::cout << " with ID " << simVar.ID << " has value " << simVar.Value << std::endl; | |
| #endif | |
| } | |
| } | |
| void ReadSimVar(ReadRPNCode &rpn) { | |
| if (rpn.RetType == 0) { | |
| ReadSimVarFloat(rpn); | |
| } else if (rpn.RetType == 1) { | |
| ReadSimVarInt(rpn); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only float and string, there is no int
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential out-of-bounds access. If RPNCodelist is empty (totalSimVars == 0), maxVarsPerFrame will be 0, and the loop won't execute. However, if the list becomes empty during execution (e.g., all clients disconnect between frames), and RollingDataReadIndex is non-zero from a previous frame, the next frame could attempt to access RPNCodelist.at(RollingDataReadIndex) with an invalid index. Add a check: if (totalSimVars == 0) { RollingDataReadIndex = 0; return; } before the loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in comment: "CLient" should be "Client".