| 23 | namespace OpenKneeboard { |
| 24 | |
| 25 | std::string DCSWorld::GetModuleNameForLuaAircraft(const std::string& luaName) { |
| 26 | using mapping_t = std::unordered_map<std::string, std::string>; |
| 27 | static std::optional<mapping_t> sMapping; |
| 28 | if (sMapping) { |
| 29 | if (sMapping->contains(luaName)) { |
| 30 | return sMapping->at(luaName); |
| 31 | } |
| 32 | return luaName; |
| 33 | } |
| 34 | |
| 35 | const std::filesystem::path fileName = "DCS-Aircraft-Mapping.json"; |
| 36 | const std::vector<std::filesystem::path> dirs = { |
| 37 | // User-override |
| 38 | Filesystem::GetSettingsDirectory(), |
| 39 | // As-installed |
| 40 | Filesystem::GetImmutableDataDirectory(), |
| 41 | }; |
| 42 | |
| 43 | dprint("Loading DCS aircraft mapping..."); |
| 44 | for (const auto& dir: dirs) { |
| 45 | const auto path = dir / fileName; |
| 46 | dprint("Trying {}...", path); |
| 47 | if (!std::filesystem::exists(path)) { |
| 48 | dprint("... not found."); |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | dprint("... reading JSON."); |
| 53 | |
| 54 | try { |
| 55 | std::ifstream f(path.c_str()); |
| 56 | nlohmann::json json; |
| 57 | f >> json; |
| 58 | mapping_t data = json; |
| 59 | sMapping = std::move(data); |
| 60 | dprint("... done."); |
| 61 | break; |
| 62 | } catch (const nlohmann::json::exception& e) { |
| 63 | dprint("... JSON exception: {}", e.what()); |
| 64 | } catch (const std::exception& e) { |
| 65 | dprint("... standard exception: {}", e.what()); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (!sMapping) { |
| 70 | dprint("Failed to load any mapping file."); |
| 71 | sMapping = mapping_t {}; |
| 72 | } |
| 73 | |
| 74 | if (sMapping && sMapping->contains(luaName)) { |
| 75 | return sMapping->at(luaName); |
| 76 | } |
| 77 | |
| 78 | dprint("Aircraft mapping:"); |
| 79 | for (const auto& [key, value]: *sMapping) { |
| 80 | dprint("- {} -> {}", key, value); |
| 81 | } |
| 82 |
nothing calls this directly
no test coverage detected