* Returns true on success, with index populated * index is undefined otherwise */
| 87 | * index is undefined otherwise |
| 88 | */ |
| 89 | bool loadAssetsIndexJson(const QString &assetsId, const QString &path, AssetsIndex& index) |
| 90 | { |
| 91 | /* |
| 92 | { |
| 93 | "objects": { |
| 94 | "icons/icon_16x16.png": { |
| 95 | "hash": "bdf48ef6b5d0d23bbb02e17d04865216179f510a", |
| 96 | "size": 3665 |
| 97 | }, |
| 98 | ... |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | */ |
| 103 | |
| 104 | QFile file(path); |
| 105 | |
| 106 | // Try to open the file and fail if we can't. |
| 107 | // TODO: We should probably report this error to the user. |
| 108 | if (!file.open(QIODevice::ReadOnly)) |
| 109 | { |
| 110 | qCritical() << "Failed to read assets index file" << path; |
| 111 | return false; |
| 112 | } |
| 113 | index.id = assetsId; |
| 114 | |
| 115 | // Read the file and close it. |
| 116 | QByteArray jsonData = file.readAll(); |
| 117 | file.close(); |
| 118 | |
| 119 | QJsonParseError parseError; |
| 120 | QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &parseError); |
| 121 | |
| 122 | // Fail if the JSON is invalid. |
| 123 | if (parseError.error != QJsonParseError::NoError) |
| 124 | { |
| 125 | qCritical() << "Failed to parse assets index file:" << parseError.errorString() |
| 126 | << "at offset " << QString::number(parseError.offset); |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | // Make sure the root is an object. |
| 131 | if (!jsonDoc.isObject()) |
| 132 | { |
| 133 | qCritical() << "Invalid assets index JSON: Root should be an array."; |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | QJsonObject root = jsonDoc.object(); |
| 138 | |
| 139 | QJsonValue isVirtual = root.value("virtual"); |
| 140 | if (!isVirtual.isUndefined()) |
| 141 | { |
| 142 | index.isVirtual = isVirtual.toBool(false); |
| 143 | } |
| 144 | |
| 145 | QJsonValue mapToResources = root.value("map_to_resources"); |
| 146 | if (!mapToResources.isUndefined()) |