-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.cpp
More file actions
179 lines (146 loc) · 4.94 KB
/
plugin.cpp
File metadata and controls
179 lines (146 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#define IBM 1
#define XPLM200 1
#define XPLM_MSG_AIRCRAFT_LOADED 103
#include "XPLMPlugin.h"
#include "XPLMUtilities.h"
#include "XPLMDataAccess.h"
#include "XPLMMenus.h"
#include <filesystem>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
struct Command {
XPLMCommandRef original{};
int state{0};
XPLMCommandRef beginRef{};
XPLMCommandRef endRef{};
XPLMDataRef pressDR{};
};
static std::vector<Command *> commands;
static int getDataRef(void *ref) {
return static_cast<Command *>(ref)->state;
}
static void setDataRef(void *ref, int v) {
auto *c = static_cast<Command *>(ref);
if (v != c->state) {
c->state = v;
if (v) XPLMCommandBegin(c->original);
else XPLMCommandEnd(c->original);
}
}
static int beginCommand(XPLMCommandRef, XPLMCommandPhase phase, void *ref) {
auto *c = static_cast<Command *>(ref);
if (phase != xplm_CommandBegin)
return 0;
XPLMCommandBegin(c->original);
return 0;
}
static int endCommand(XPLMCommandRef, XPLMCommandPhase phase, void *ref) {
auto *c = static_cast<Command *>(ref);
if (phase != xplm_CommandBegin)
return 0;
XPLMCommandEnd(c->original);
return 0;
}
static void removeRegisteredCommands() {
for (auto *c: commands) {
if (c->beginRef)
XPLMUnregisterCommandHandler(c->beginRef, beginCommand, 0, c);
if (c->endRef)
XPLMUnregisterCommandHandler(c->endRef, endCommand, 0, c);
if (c->pressDR)
XPLMUnregisterDataAccessor(c->pressDR);
delete c;
}
commands.clear();
}
static std::string getPluginDir() {
char filePath[600];
XPLMGetPluginInfo(XPLMGetMyID(), nullptr, filePath, nullptr, nullptr);
return std::filesystem::path(filePath).parent_path().parent_path().string();
}
static void registerCommands() {
removeRegisteredCommands();
XPLMDebugString("[NVAN] Registering commands...\n");
std::ifstream file((getPluginDir() + "/commands.txt").c_str());
if (!file.is_open()) {
XPLMDebugString("[NVAN] Cannot open commands.txt\n");
return;
}
std::string line;
while (std::getline(file, line)) {
auto trim = [](std::string &s) {
size_t a = s.find_first_not_of(" \t\r\n");
size_t b = s.find_last_not_of(" \t\r\n");
if (a == std::string::npos) {
s.clear();
return;
}
s = s.substr(a, b - a + 1);
};
trim(line);
if (line.empty() || line[0] == '#') continue;
XPLMCommandRef original = XPLMFindCommand(line.c_str());
if (!original) {
XPLMDebugString("[NVAN] Command not found: ");
XPLMDebugString(line.c_str());
XPLMDebugString("\n");
continue;
}
auto *command = new Command();
command->original = original;
char buf[600];
snprintf(buf, sizeof(buf), "%s_press", line.c_str());
command->pressDR = XPLMRegisterDataAccessor(
buf, xplmType_Int, 1,
getDataRef, setDataRef,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr,
command, command
);
snprintf(buf, sizeof(buf), "%s_begin", line.c_str());
command->beginRef = XPLMCreateCommand(buf, buf);
XPLMRegisterCommandHandler(command->beginRef, beginCommand, 0, command);
snprintf(buf, sizeof(buf), "%s_end", line.c_str());
command->endRef = XPLMCreateCommand(buf, buf);
XPLMRegisterCommandHandler(command->endRef, endCommand, 0, command);
commands.push_back(command);
XPLMDebugString("[NVAN] Registered command: ");
XPLMDebugString(line.c_str());
XPLMDebugString("\n");
}
XPLMDebugString("[NVAN] Finished registering commands!");
}
void menuHandler(void* inMenuRef, void* inItemRef) {
if (inItemRef == (void*)1) {
registerCommands();
}
}
extern "C" {
PLUGIN_API int XPluginStart(char *outName, char *outSig, char *outDesc) {
strcpy(outName, "NVAN X-Plane Command Aliases");
strcpy(outSig, "es.nvan.xPlaneCommandAliasesPlugin");
strcpy(outDesc, "Registers _press/_begin/_end aliases for commands to implement press and hold via UDP");
XPLMMenuID pluginsMenu = XPLMFindPluginsMenu();
int menuItem = XPLMAppendMenuItem(XPLMFindPluginsMenu(), "NVAN Command Aliases", nullptr, 0);
XPLMMenuID menu = XPLMCreateMenu("NVAN Command Aliases", pluginsMenu, menuItem, menuHandler, nullptr);
XPLMAppendMenuItem(menu, "Reload commands.txt", (void*)1, 0);
return 1;
}
PLUGIN_API int XPluginEnable(void) {
registerCommands();
return 1;
}
PLUGIN_API void XPluginDisable(void) {
removeRegisteredCommands();
}
PLUGIN_API void XPluginStop(void) {
XPluginDisable();
}
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID, int msg, void *) {
if (msg == XPLM_MSG_AIRCRAFT_LOADED) {
registerCommands();
}
}
}