Skip to content

Commit 89476c1

Browse files
authored
Version command and console messages (#16)
1 parent a63c40c commit 89476c1

File tree

2 files changed

+56
-39
lines changed

2 files changed

+56
-39
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ The default channels for the MobiFlight client are auto created on startup. Each
2929
|```MF.SimVars.Clear``` |||
3030
|```MF.SimVars.Set(5 (>L:MyVar))```|||
3131
|```MF.Clients.Add.ClientName```|```MF.Clients.Add.ClientName.Finished```||
32+
|```MF.Config.MAX_VARS_PER_FRAME.Set.30```|||
33+
|```MF.Version.Get```|```MF.Version.0.6.0```||
34+
3235

3336
**MF.SimVars.Add.**
3437
The "SimVars.Add." command needs to be extended with a gauge calculator scipt for reading a variable, like shown in the table. Each added variable needs 4 reserved bytes to return its float value in the LVars channel. The bytes are allocated in the order of the LVars being added. The first variable starts at offset 0, the second at offset 4, the third at offset 8 and so on. To access each value, the external SimConnect clients needs a unique DataDefinitionId for each memory segment. It is recommended to start with ID 1000.

src/Sources/Code/Module.cpp

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,13 @@ void RegisterEvents() {
154154
hr = SimConnect_AddClientEventToNotificationGroup(g_hSimConnect, MOBIFLIGHT_GROUP::DEFAULT, eventID, false);
155155

156156
#if _DEBUG
157-
if (hr != S_OK) fprintf(stderr, "MobiFlight: Error on registering Event %s with ID %u for code %s", eventName.c_str(), eventID, eventCommand.c_str());
158-
else fprintf(stderr, "MobiFlight: Success on registering Event %s with ID %u for code %s", eventName.c_str(), eventID, eventCommand.c_str());
157+
if (hr != S_OK) {
158+
fprintf(stderr, "MobiFlight: Error on registering Event %s with ID %u for code %s", eventName.c_str(), eventID, eventCommand.c_str());
159+
}
160+
else {
161+
std::cout << "MobiFlight: Success on registering Event " << eventName.c_str();
162+
std::cout << " with ID " << eventID << " for code " << eventCommand.c_str() << std::endl;
163+
}
159164
#endif
160165

161166
eventID++;
@@ -171,9 +176,9 @@ void LoadEventDefinitions() {
171176
int eventDefinition = CodeEvents.size();
172177
LoadEventDefinitions(FileEventsUser);
173178

174-
fprintf(stderr, "MobiFlight: Loaded %u event defintions in total.", CodeEvents.size());
175-
fprintf(stderr, "MobiFlight: Loaded %u built-in event defintions.", eventDefinition);
176-
fprintf(stderr, "MobiFlight: Loaded %u user event defintions.", CodeEvents.size() - eventDefinition);
179+
std::cout << "MobiFlight: Loaded " << CodeEvents.size() << " event definitions in total." << std::endl;
180+
std::cout << "MobiFlight: Loaded " << eventDefinition << " built-in event definitions." << std::endl;
181+
std::cout << "MobiFlight: Loaded " << CodeEvents.size() - eventDefinition << " user event definitions." << std::endl;
177182
}
178183

179184
void SendResponse(const char * message, Client* client) {
@@ -193,7 +198,7 @@ void SendNewClientResponse(Client* client, Client* nc) {
193198
std::ostringstream oss;
194199
oss << "MF.Clients.Add." << nc->Name << ".Finished";
195200
std::string data = oss.str();
196-
fprintf(stderr, "MobiFlight[%s]: SendNewClientData > %s", client->Name.c_str(), data.c_str());
201+
std::cout << "MobiFlight[" << client->Name.c_str() << "]: SendNewClientData > " << data.c_str() << std::endl;
197202
SendResponse(data.c_str(), client);
198203
}
199204

@@ -214,7 +219,7 @@ void ListLVars(Client* client) {
214219
for (const auto& lVar : lVarList) {
215220
SendResponse(lVar.c_str(), client);
216221
#if _DEBUG
217-
fprintf(stderr, "MobiFlight[%s]: Available LVar > %s", client->Name.c_str(), lVar.c_str());
222+
std::cout << "MobiFlight[" << client->Name.c_str() << "]: Available LVar > " << lVar.c_str() << std::endl;
218223
#endif
219224
}
220225
}
@@ -233,7 +238,8 @@ void WriteSimVar(SimVar& simVar, Client* client) {
233238
fprintf(stderr, "MobiFlight[%s]: Error on Setting Client Data. %u, SimVar: %s (ID: %u)", client->Name.c_str(), hr, simVar.Name.c_str(), simVar.ID);
234239
}
235240
#if _DEBUG
236-
fprintf(stderr, "MobiFlight[%s]: SimVar %s with ID %u has value %f", client->Name.c_str(),simVar.Name.c_str(), simVar.ID, simVar.Value);
241+
std::cout << "MobiFlight[" << client->Name.c_str() << "]: SimVar " << simVar.Name.c_str();
242+
std::cout << " with ID " << simVar.ID << " has value " << simVar.Value << std::endl;
237243
#endif
238244
}
239245

@@ -261,7 +267,7 @@ void RegisterSimVar(const std::string code, Client* client) {
261267
client->MaxClientDataDefinition = SimVars->size();
262268
}
263269
#if _DEBUG
264-
fprintf(stderr, "MobiFlight[%s]: RegisterSimVar SimVars Size: %d\n", client->Name.c_str(), SimVars->size());
270+
std::cout << "MobiFlight[" << client->Name.c_str() << "]: RegisterSimVar SimVars Size: " << SimVars->size() << std::endl;
265271
#endif
266272

267273
FLOAT64 val;
@@ -273,14 +279,15 @@ void RegisterSimVar(const std::string code, Client* client) {
273279
WriteSimVar(var1, client);
274280

275281
#if _DEBUG
276-
fprintf(stderr, "MobiFlight[%s]: RegisterSimVar > %s ID [%u] : Offset(%u) : Value(%f)", client->Name.c_str(), var1.Name.c_str(), var1.ID, var1.Offset, var1.Value);
282+
std::cout << "MobiFlight[" << client->Name.c_str() << "]: RegisterSimVar > " << var1.Name.c_str();
283+
std::cout << " ID [" << var1.ID << "] : Offset(" << var1.Offset << ") : Value(" << var1.Value << ")" << std::endl;
277284
#endif
278285
}
279286

280287
// Clear the list of currently tracked SimVars
281288
void ClearSimVars(Client* client) {
282-
client->SimVars.clear();
283-
fprintf(stderr, "MobiFlight: Cleared SimVar tracking.");
289+
client->SimVars.clear();
290+
std::cout << "MobiFlight[" << client->Name.c_str() << "]: Cleared SimVar tracking." << std::endl;
284291
//client->RollingClientDataReadIndex = client->SimVars.begin();
285292
client->RollingClientDataReadIndex = 0;
286293
}
@@ -296,7 +303,8 @@ void ReadSimVar(SimVar &simVar, Client* client) {
296303
WriteSimVar(simVar, client);
297304

298305
#if _DEBUG
299-
fprintf(stderr, "MobiFlight[%s]: SimVar %s with ID %u has value %f", client->Name.c_str(), simVar.Name.c_str(), simVar.ID, simVar.Value);
306+
std::cout << "MobiFlight[" << client->Name.c_str() << "]: SimVar " << simVar.Name.c_str();
307+
std::cout << " with ID " << simVar.ID << " has value " << simVar.Value << std::endl;
300308
#endif
301309
}
302310

@@ -407,17 +415,17 @@ Client* RegisterNewClient(const std::string clientName) {
407415
}
408416

409417
#if _DEBUG
410-
fprintf(stderr, "MobiFlight: NewClient Name: %s\n", newClient->Name.c_str());
411-
fprintf(stderr, "MobiFlight: NewClient ID: %d\n", newClient->ID);
412-
fprintf(stderr, "MobiFlight: NewClient DataAreaIDSimvar: %d\n", newClient->DataAreaIDSimvar);
413-
fprintf(stderr, "MobiFlight: NewClient DataAreaIDResponse: %d\n", newClient->DataAreaIDResponse);
414-
fprintf(stderr, "MobiFlight: NewClient DataAreaIDCommand: %d\n", newClient->DataAreaIDCommand);
415-
fprintf(stderr, "MobiFlight: NewClient DataAreaNameSimVar: %s\n", newClient->DataAreaNameSimVar.c_str());
416-
fprintf(stderr, "MobiFlight: NewClient DataAreaNameResponse: %s\n", newClient->DataAreaNameResponse.c_str());
417-
fprintf(stderr, "MobiFlight: NewClient DataAreaNameCommand: %s\n", newClient->DataAreaNameCommand.c_str());
418-
fprintf(stderr, "MobiFlight: NewClient DataDefinitionIDStringResponse: %d\n", newClient->DataDefinitionIDStringResponse);
419-
fprintf(stderr, "MobiFlight: NewClient DataDefinitionIDStringCommand: %d\n", newClient->DataDefinitionIDStringCommand);
420-
fprintf(stderr, "MobiFlight: NewClient DataDefinitionIdSimVarsStart: %d\n", newClient->DataDefinitionIdSimVarsStart);
418+
std::cout << "MobiFlight: NewClient Name: " << newClient->Name.c_str() << std::endl;
419+
std::cout << "MobiFlight: NewClient ID: " << newClient->ID << std::endl;
420+
std::cout << "MobiFlight: NewClient DataAreaIDSimvar: " << newClient->DataAreaIDSimvar << std::endl;
421+
std::cout << "MobiFlight: NewClient DataAreaIDResponse: " << newClient->DataAreaIDResponse << std::endl;
422+
std::cout << "MobiFlight: NewClient DataAreaIDCommand: " << newClient->DataAreaIDCommand << std::endl;
423+
std::cout << "MobiFlight: NewClient DataAreaNameSimVar: " << newClient->DataAreaNameSimVar.c_str() << std::endl;
424+
std::cout << "MobiFlight: NewClient DataAreaNameResponse: " << newClient->DataAreaNameResponse.c_str() << std::endl;
425+
std::cout << "MobiFlight: NewClient DataAreaNameCommand: " << newClient->DataAreaNameCommand.c_str() << std::endl;
426+
std::cout << "MobiFlight: NewClient DataDefinitionIDStringResponse: " << newClient->DataDefinitionIDStringResponse << std::endl;
427+
std::cout << "MobiFlight: NewClient DataDefinitionIDStringCommand: " << newClient->DataDefinitionIDStringCommand << std::endl;
428+
std::cout << "MobiFlight: NewClient DataDefinitionIdSimVarsStart: " << newClient->DataDefinitionIdSimVarsStart << std::endl;
421429
#endif
422430

423431
return newClient;
@@ -460,15 +468,13 @@ extern "C" MSFS_CALLBACK void module_init(void)
460468
Client* client = RegisterNewClient(std::string(MOBIFLIGHT_CLIENT_DATA_NAME));
461469
RegisterEvents();
462470
ListLVars(client);
463-
464-
fprintf(stderr, "MobiFlight: Max Message size is %u", MOBIFLIGHT_MESSAGE_SIZE);
465-
fprintf(stderr, "MobiFlight: Module Init Complete. Version: %s", version);
466-
471+
472+
std::cout << "MobiFlight: Max Message size is " << MOBIFLIGHT_MESSAGE_SIZE << std::endl;
473+
std::cout << "MobiFlight: Module Init Complete.Version: " << version << std::endl;
467474
}
468475

469476
extern "C" MSFS_CALLBACK void module_deinit(void)
470477
{
471-
472478
if (!g_hSimConnect)
473479
return;
474480
HRESULT hr = SimConnect_Close(g_hSimConnect);
@@ -477,7 +483,6 @@ extern "C" MSFS_CALLBACK void module_deinit(void)
477483
fprintf(stderr, "Could not close SimConnect connection.\n");
478484
return;
479485
}
480-
481486
}
482487

483488
void CALLBACK MyDispatchProc(SIMCONNECT_RECV* pData, DWORD cbData, void* pContext)
@@ -495,33 +500,42 @@ void CALLBACK MyDispatchProc(SIMCONNECT_RECV* pData, DWORD cbData, void* pContex
495500
std::string str = std::string((char*)(&recv_data->dwData));
496501
DWORD clientID = (DWORD)recv_data->dwRequestID;
497502
#if _DEBUG
498-
fprintf(stderr, "MobiFlight: Received Command: %s\n", str.c_str());
499-
fprintf(stderr, "MobiFlight: Received ClientId: %ld\n", clientID);
503+
std::cout << "MobiFlight: Received Command: " << str.c_str() << std::endl;
504+
std::cout << "MobiFlight: Received ClientId: " << clientID << std::endl;
500505
#endif
501506

502507
Client* client = RegisteredClients[clientID];
503508
if (str == "MF.Ping") {
504509
SendResponse("MF.Pong", client);
505-
fprintf(stderr, "MobiFlight[%s]: Received ping\n", client->Name.c_str());
510+
std::cout << "MobiFlight[" << client->Name.c_str() << "]: Received ping" << std::endl;
506511
}
507512

508513
else if (str == "MF.SimVars.Clear") {
509514
ClearSimVars(client);
510515
break;
511516

512-
} else if (str == "MF.LVars.List") {
517+
}
518+
else if (str == "MF.LVars.List") {
513519
SendResponse("MF.LVars.List.Start", client);
514520
ListLVars(client);
515521
SendResponse("MF.LVars.List.End", client);
516522
break;
517523

524+
}
525+
else if (str == "MF.Version.Get")
526+
{
527+
std::string v = "MF.Version." + std::string(version);
528+
SendResponse(v.c_str(), client);
529+
std::cout << "MobiFlight[" << client->Name.c_str() << "]: Received get version" << std::endl;
530+
break;
531+
518532
}
519533
// MF.SimVars.Set(5 (>L:MyVar))
520534
else if (str.find("MF.SimVars.Set.") != std::string::npos) {
521535
std::string prefix = "MF.SimVars.Set.";
522536
str = str.substr(prefix.length());
523537
#if _DEBUG
524-
fprintf(stderr, "MobiFlight[%s]: Executing Code: %s\n", client->Name.c_str(), str.c_str());
538+
std::cout << "MobiFlight[" << client->Name.c_str() << "]: Executing Code: " << str.c_str() << std::endl;
525539
#endif
526540
execute_calculator_code(str.c_str(), 0, nullptr, nullptr);
527541
break;
@@ -533,7 +547,7 @@ void CALLBACK MyDispatchProc(SIMCONNECT_RECV* pData, DWORD cbData, void* pContex
533547
std::string prefix = "MF.SimVars.Add.";
534548
str = m_str.get()->substr(prefix.length());
535549
RegisterSimVar(str, client);
536-
fprintf(stderr, "MobiFlight[%s]: Received SimVar to register: %s.\n", client->Name.c_str(), str.c_str());
550+
std::cout << "MobiFlight[" << client->Name.c_str() << "]: Received SimVar to register: " << str.c_str() << std::endl;
537551
break;
538552
}
539553

@@ -542,15 +556,15 @@ void CALLBACK MyDispatchProc(SIMCONNECT_RECV* pData, DWORD cbData, void* pContex
542556
str= m_str.get()->substr(prefix.length());
543557
Client* newClient = RegisterNewClient(str);
544558
SendNewClientResponse(client, newClient);
545-
fprintf(stderr, "MobiFlight: Received Client to register: %s.\n", str.c_str());
559+
std::cout << "MobiFlight[" << client->Name.c_str() << "]: Received Client to register: " << str.c_str() << std::endl;
546560
}
547561

548562
if (m_str.get()->find("MF.Config.MAX_VARS_PER_FRAME.Set.") != std::string::npos) {
549563
std::string prefix = "MF.Config.MAX_VARS_PER_FRAME.Set.";
550564
str = m_str.get()->substr(prefix.length());
551565
uint16_t value = static_cast<uint16_t>(std::stoi(str));
552566
MOBIFLIGHT_MAX_VARS_PER_FRAME = value;
553-
fprintf(stderr, "MobiFlight: Set MF.Config.MAX_VARS_PER_FRAME to %u.\n", value);
567+
std::cout << "MobiFlight: Set MF.Config.MAX_VARS_PER_FRAME to " << value << std::endl;
554568
}
555569
break;
556570
}
@@ -572,7 +586,7 @@ void CALLBACK MyDispatchProc(SIMCONNECT_RECV* pData, DWORD cbData, void* pContex
572586
int CodeEventId = eventID;
573587
std::string command = std::string(CodeEvents[CodeEventId].second);
574588
#if _DEBUG
575-
fprintf(stderr, "execute %s\n", command.c_str());
589+
std::cout << "MobiFlight execute " << command.c_str() << std::endl;
576590
#endif
577591
execute_calculator_code(command.c_str(), nullptr, nullptr, nullptr);
578592
}

0 commit comments

Comments
 (0)