Read a top-level string field from /mod.json, or "" if absent/unreadable.
| 23 | } |
| 24 | |
| 25 | // Read a top-level string field from <modDir>/mod.json, or "" if absent/unreadable. |
| 26 | std::string ReadModJsonString(const std::filesystem::path& modDir, const char* key) |
| 27 | { |
| 28 | const std::filesystem::path metadata = modDir / "mod.json"; |
| 29 | std::error_code ec; |
| 30 | if (!std::filesystem::exists(metadata, ec)) |
| 31 | return {}; |
| 32 | std::ifstream in(metadata, std::ios::binary); |
| 33 | if (!in) |
| 34 | return {}; |
| 35 | std::ostringstream buffer; |
| 36 | buffer << in.rdbuf(); |
| 37 | cJSON* root = cJSON_Parse(buffer.str().c_str()); |
| 38 | std::string out; |
| 39 | if (root != nullptr) |
| 40 | { |
| 41 | const cJSON* item = cJSON_GetObjectItemCaseSensitive(root, key); |
| 42 | if (cJSON_IsString(item) && item->valuestring != nullptr) |
| 43 | out = item->valuestring; |
| 44 | cJSON_Delete(root); |
| 45 | } |
| 46 | return out; |
| 47 | } |
| 48 | |
| 49 | // Total size of every regular file under dir (recursive). Best-effort: I/O errors |
no test coverage detected