load mapping of channels to datarefs from a json file */
| 191 | load mapping of channels to datarefs from a json file |
| 192 | */ |
| 193 | bool XPlane::load_dref_map(const char *map_json) |
| 194 | { |
| 195 | char *fname = nullptr; |
| 196 | if (AP::FS().stat(map_json, &map_st) == 0) { |
| 197 | fname = strdup(map_json); |
| 198 | } else { |
| 199 | IGNORE_RETURN(asprintf(&fname, "@ROMFS/models/%s", map_json)); |
| 200 | if (AP::FS().stat(fname, &map_st) != 0) { |
| 201 | return false; |
| 202 | } |
| 203 | } |
| 204 | if (fname == nullptr) { |
| 205 | return false; |
| 206 | } |
| 207 | AP_JSON::value *obj = AP_JSON::load_json(fname); |
| 208 | if (obj == nullptr) { |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | free(map_filename); |
| 213 | map_filename = fname; |
| 214 | |
| 215 | // free old drefs |
| 216 | while (drefs) { |
| 217 | auto *d = drefs->next; |
| 218 | free(drefs->name); |
| 219 | delete drefs; |
| 220 | drefs = d; |
| 221 | } |
| 222 | |
| 223 | // free old joystick |
| 224 | while (joyinputs) { |
| 225 | auto *j = joyinputs->next; |
| 226 | delete joyinputs; |
| 227 | joyinputs = j; |
| 228 | } |
| 229 | |
| 230 | uint32_t count = 0; |
| 231 | // obtain a const reference to the map, and print the contents |
| 232 | const AP_JSON::value::object& o = obj->get<AP_JSON::value::object>(); |
| 233 | for (AP_JSON::value::object::const_iterator i = o.begin(); |
| 234 | i != o.end(); |
| 235 | ++i) { |
| 236 | const char *label = i->first.c_str(); |
| 237 | const auto &d = i->second; |
| 238 | if (strchr(label, '/') != nullptr) { |
| 239 | const char *type_s = d.get("type").to_str().c_str(); |
| 240 | if (strcmp(type_s, "angle") == 0) { |
| 241 | add_dref(label, DRefType::ANGLE, d); |
| 242 | } else if (strcmp(type_s, "range") == 0) { |
| 243 | add_dref(label, DRefType::RANGE, d); |
| 244 | } else if (strcmp(type_s, "fixed") == 0) { |
| 245 | add_dref(label, DRefType::FIXED, d); |
| 246 | } else { |
| 247 | ::printf("Invalid dref type %s for %s in %s", type_s, label, map_filename); |
| 248 | } |
| 249 | } else if (strcmp(label, "settings") == 0) { |
| 250 | handle_setting(d); |