| 6 | #include "MaaUtils/Logger.h" |
| 7 | |
| 8 | MAA_CTRL_UNIT_NS_BEGIN |
| 9 | |
| 10 | std::optional<Recording> RecordParser::parse(const std::filesystem::path& path) |
| 11 | { |
| 12 | LogFunc << VAR(path); |
| 13 | |
| 14 | if (!std::filesystem::exists(path)) { |
| 15 | LogError << "File not found:" << path; |
| 16 | return std::nullopt; |
| 17 | } |
| 18 | |
| 19 | std::ifstream ifs(path); |
| 20 | if (!ifs.is_open()) { |
| 21 | LogError << "Failed to open file:" << path; |
| 22 | return std::nullopt; |
| 23 | } |
| 24 | |
| 25 | Recording recording; |
| 26 | |
| 27 | std::string line; |
| 28 | auto dir = path.parent_path(); |
| 29 | while (std::getline(ifs, line)) { |
| 30 | if (line.empty()) { |
| 31 | continue; |
| 32 | } |
| 33 | |
| 34 | auto json_opt = json::parse(line); |
| 35 | if (!json_opt) { |
| 36 | LogError << "Failed to parse json:" << line; |
| 37 | return std::nullopt; |
| 38 | } |
| 39 | |
| 40 | auto& record_json = json_opt.value(); |
| 41 | auto record_opt = parse_record(record_json, dir); |
| 42 | if (!record_opt) { |
| 43 | LogError << "Failed to parse record:" << line; |
| 44 | return std::nullopt; |
| 45 | } |
| 46 | |
| 47 | auto& record = record_opt.value(); |
| 48 | |
| 49 | if (record.action.type == RecordType::connect) { |
| 50 | auto& param = std::get<RecordConnect>(record.action.param); |
| 51 | recording.version = param.version; |
| 52 | recording.device_info.uuid = param.uuid; |
| 53 | recording.device_info.resolution = cv::Size(param.width, param.height); |
| 54 | } |
| 55 | |
| 56 | recording.records.emplace_back(std::move(record_opt.value())); |
| 57 | } |
| 58 | |
| 59 | return recording; |
| 60 | } |
| 61 | |
| 62 | std::optional<Record> RecordParser::parse_record(const json::value& record_json, const std::filesystem::path& dir) |
| 63 | { |