MCPcopy Create free account
hub / github.com/ElyPrismLauncher/Launcher / executeTask

Method executeTask

launcher/meta/BaseEntity.cpp:112–183  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

110BaseEntityLoadTask::BaseEntityLoadTask(BaseEntity* parent, Net::Mode mode) : m_entity(parent), m_mode(mode) {}
111
112void BaseEntityLoadTask::executeTask()
113{
114 const QString fname = QDir("meta").absoluteFilePath(m_entity->localFilename());
115 auto hashMatches = false;
116 // the file exists on disk try to load it
117 if (QFile::exists(fname)) {
118 try {
119 QByteArray fileData;
120 // read local file if nothing is loaded yet
121 if (m_entity->m_load_status == BaseEntity::LoadStatus::NotLoaded || m_entity->m_file_sha256.isEmpty()) {
122 setStatus(tr("Loading local file"));
123 fileData = FS::read(fname);
124 m_entity->m_file_sha256 = Hashing::hash(fileData, Hashing::Algorithm::Sha256);
125 }
126
127 // on online the hash needs to match
128 hashMatches = m_entity->m_sha256 == m_entity->m_file_sha256;
129 if (m_mode == Net::Mode::Online && !m_entity->m_sha256.isEmpty() && !hashMatches) {
130 throw Exception("mismatched checksum");
131 }
132
133 // load local file
134 if (m_entity->m_load_status == BaseEntity::LoadStatus::NotLoaded) {
135 auto doc = Json::requireDocument(fileData, fname);
136 auto obj = Json::requireObject(doc, fname);
137 m_entity->parse(obj);
138 m_entity->m_load_status = BaseEntity::LoadStatus::Local;
139 }
140
141 } catch (const Exception& e) {
142 qDebug() << QString("Unable to parse file %1: %2").arg(fname, e.cause());
143 // just make sure it's gone and we never consider it again.
144 FS::deletePath(fname);
145 m_entity->m_load_status = BaseEntity::LoadStatus::NotLoaded;
146 }
147 }
148 // if we need remote update, run the update task
149 auto wasLoadedOffline = m_entity->m_load_status != BaseEntity::LoadStatus::NotLoaded && m_mode == Net::Mode::Offline;
150 // if has is not present allways fetch from remote(e.g. the main index file), else only fetch if hash doesn't match
151 auto wasLoadedRemote = m_entity->m_sha256.isEmpty() ? m_entity->m_load_status == BaseEntity::LoadStatus::Remote : hashMatches;
152 if (wasLoadedOffline || wasLoadedRemote) {
153 emitSucceeded();
154 return;
155 }
156 m_task.reset(new NetJob(QObject::tr("Download of meta file %1").arg(m_entity->localFilename()), APPLICATION->network()));
157 auto url = m_entity->url();
158 auto entry = APPLICATION->metacache()->resolveEntry("meta", m_entity->localFilename());
159 entry->setStale(true);
160 auto dl = Net::ApiDownload::makeCached(url, entry);
161 /*
162 * The validator parses the file and loads it into the object.
163 * If that fails, the file is not written to storage.
164 */
165 if (!m_entity->m_sha256.isEmpty())
166 dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Algorithm::Sha256, m_entity->m_sha256));
167 dl->addValidator(new ParsingValidator(m_entity));
168 m_task->addNetAction(dl);
169 m_task->setAskRetry(false);

Callers

nothing calls this directly

Calls 15

readFunction · 0.85
ExceptionClass · 0.85
requireDocumentFunction · 0.85
requireObjectFunction · 0.85
deletePathFunction · 0.85
causeMethod · 0.80
networkMethod · 0.80
resolveEntryMethod · 0.80
metacacheMethod · 0.80
addNetActionMethod · 0.80
setAskRetryMethod · 0.80
QDirClass · 0.50

Tested by

no test coverage detected