mirror of
https://github.com/PrismLauncher/PrismLauncher.git
synced 2026-07-05 21:06:58 +03:00
chore(clang-tidy): modernize the code
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
18f04b1e29
commit
9c88eb9bc0
36 changed files with 713 additions and 632 deletions
|
|
@ -15,6 +15,8 @@
|
|||
|
||||
#include "BaseEntity.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "Exception.h"
|
||||
#include "FileSystem.h"
|
||||
#include "Json.h"
|
||||
|
|
@ -26,19 +28,18 @@
|
|||
#include "net/NetJob.h"
|
||||
|
||||
#include "Application.h"
|
||||
#include "settings/SettingsObject.h"
|
||||
#include "BuildConfig.h"
|
||||
#include "settings/SettingsObject.h"
|
||||
#include "tasks/Task.h"
|
||||
|
||||
namespace Meta {
|
||||
|
||||
namespace {
|
||||
class ParsingValidator : public Net::Validator {
|
||||
public: /* con/des */
|
||||
ParsingValidator(BaseEntity* entity) : m_entity(entity) {};
|
||||
virtual ~ParsingValidator() = default;
|
||||
explicit ParsingValidator(Meta::BaseEntity* entity) : m_entity(entity) {};
|
||||
~ParsingValidator() override = default;
|
||||
|
||||
public: /* methods */
|
||||
bool init(QNetworkRequest&) override
|
||||
bool init(QNetworkRequest& /*request*/) override
|
||||
{
|
||||
m_data.clear();
|
||||
return true;
|
||||
|
|
@ -53,7 +54,7 @@ class ParsingValidator : public Net::Validator {
|
|||
m_data.clear();
|
||||
return true;
|
||||
}
|
||||
bool validate(QNetworkReply&) override
|
||||
bool validate(QNetworkReply& /*reply*/) override
|
||||
{
|
||||
auto fname = m_entity->localFilename();
|
||||
try {
|
||||
|
|
@ -69,12 +70,14 @@ class ParsingValidator : public Net::Validator {
|
|||
|
||||
private: /* data */
|
||||
QByteArray m_data;
|
||||
BaseEntity* m_entity;
|
||||
Meta::BaseEntity* m_entity;
|
||||
};
|
||||
} // namespace
|
||||
namespace Meta {
|
||||
|
||||
QUrl BaseEntity::url() const
|
||||
{
|
||||
auto s = APPLICATION->settings();
|
||||
auto* s = APPLICATION->settings();
|
||||
QString metaOverride = s->get("MetaURLOverride").toString();
|
||||
if (metaOverride.isEmpty()) {
|
||||
return QUrl(BuildConfig.META_URL).resolved(localFilename());
|
||||
|
|
@ -82,33 +85,33 @@ QUrl BaseEntity::url() const
|
|||
return QUrl(metaOverride).resolved(localFilename());
|
||||
}
|
||||
|
||||
Task::Ptr BaseEntity::loadTask(Net::Mode mode, bool forceReload)
|
||||
Task::Ptr BaseEntity::loadTask(Net::Mode loadType, bool forceReload)
|
||||
{
|
||||
if (m_task && m_task->isRunning()) {
|
||||
return m_task;
|
||||
}
|
||||
m_task.reset(new BaseEntityLoadTask(this, mode, forceReload));
|
||||
m_task.reset(new BaseEntityLoadTask(this, loadType, forceReload));
|
||||
return m_task;
|
||||
}
|
||||
|
||||
bool BaseEntity::isLoaded() const
|
||||
{
|
||||
// consider it loaded only if the main hash is either empty and was remote loadded or the hashes match and was loaded
|
||||
return m_sha256.isEmpty() ? m_load_status == LoadStatus::Remote : m_load_status != LoadStatus::NotLoaded && m_sha256 == m_file_sha256;
|
||||
return m_sha256.isEmpty() ? m_loadStatus == LoadStatus::Remote : m_loadStatus != LoadStatus::NotLoaded && m_sha256 == m_fileSha256;
|
||||
}
|
||||
|
||||
void BaseEntity::setSha256(QString sha256)
|
||||
{
|
||||
m_sha256 = sha256;
|
||||
m_sha256 = std::move(sha256);
|
||||
}
|
||||
|
||||
BaseEntity::LoadStatus BaseEntity::status() const
|
||||
{
|
||||
return m_load_status;
|
||||
return m_loadStatus;
|
||||
}
|
||||
|
||||
BaseEntityLoadTask::BaseEntityLoadTask(BaseEntity* parent, Net::Mode mode, bool forceReload)
|
||||
: m_entity(parent), m_mode(mode), m_force_reload(forceReload)
|
||||
: m_entity(parent), m_mode(mode), m_forceReload(forceReload)
|
||||
{}
|
||||
|
||||
void BaseEntityLoadTask::executeTask()
|
||||
|
|
@ -120,47 +123,47 @@ void BaseEntityLoadTask::executeTask()
|
|||
try {
|
||||
QByteArray fileData;
|
||||
// read local file if nothing is loaded yet
|
||||
if (m_entity->m_load_status == BaseEntity::LoadStatus::NotLoaded || m_entity->m_file_sha256.isEmpty()) {
|
||||
if (m_entity->m_loadStatus == BaseEntity::LoadStatus::NotLoaded || m_entity->m_fileSha256.isEmpty()) {
|
||||
setStatus(tr("Loading local file"));
|
||||
fileData = FS::read(fname);
|
||||
m_entity->m_file_sha256 = Hashing::hash(fileData, Hashing::Algorithm::Sha256);
|
||||
m_entity->m_fileSha256 = Hashing::hash(fileData, Hashing::Algorithm::Sha256);
|
||||
}
|
||||
|
||||
// on online the hash needs to match
|
||||
const auto& expected = m_entity->m_sha256;
|
||||
const auto& actual = m_entity->m_file_sha256;
|
||||
const auto& actual = m_entity->m_fileSha256;
|
||||
hashMatches = expected == actual;
|
||||
if (m_mode == Net::Mode::Online && !m_entity->m_sha256.isEmpty() && !hashMatches) {
|
||||
throw Exception(QString("Checksum mismatch, expected sha256: %1, got: %2").arg(expected, actual));
|
||||
}
|
||||
|
||||
// load local file
|
||||
if (m_entity->m_load_status == BaseEntity::LoadStatus::NotLoaded) {
|
||||
if (m_entity->m_loadStatus == BaseEntity::LoadStatus::NotLoaded) {
|
||||
auto doc = Json::requireDocument(fileData, fname);
|
||||
auto obj = Json::requireObject(doc, fname);
|
||||
m_entity->parse(obj);
|
||||
m_entity->m_load_status = BaseEntity::LoadStatus::Local;
|
||||
m_entity->m_loadStatus = BaseEntity::LoadStatus::Local;
|
||||
}
|
||||
|
||||
} catch (const Exception& e) {
|
||||
qCritical() << QString("Unable to parse file %1: %2").arg(fname, e.cause());
|
||||
// just make sure it's gone and we never consider it again.
|
||||
FS::deletePath(fname);
|
||||
m_entity->m_load_status = BaseEntity::LoadStatus::NotLoaded;
|
||||
m_entity->m_loadStatus = BaseEntity::LoadStatus::NotLoaded;
|
||||
}
|
||||
}
|
||||
// if we need remote update, run the update task
|
||||
auto wasLoadedOffline = m_entity->m_load_status != BaseEntity::LoadStatus::NotLoaded && m_mode == Net::Mode::Offline;
|
||||
auto wasLoadedOffline = m_entity->m_loadStatus != BaseEntity::LoadStatus::NotLoaded && m_mode == Net::Mode::Offline;
|
||||
// if has is not present allways fetch from remote(e.g. the main index file), else only fetch if hash doesn't match
|
||||
auto wasLoadedRemote = m_entity->m_sha256.isEmpty() ? m_entity->m_load_status == BaseEntity::LoadStatus::Remote : hashMatches;
|
||||
if (wasLoadedOffline || (wasLoadedRemote && !m_force_reload)) {
|
||||
auto wasLoadedRemote = m_entity->m_sha256.isEmpty() ? m_entity->m_loadStatus == BaseEntity::LoadStatus::Remote : hashMatches;
|
||||
if (wasLoadedOffline || (wasLoadedRemote && !m_forceReload)) {
|
||||
emitSucceeded();
|
||||
return;
|
||||
}
|
||||
m_task.reset(new NetJob(QObject::tr("Download of meta file %1").arg(m_entity->localFilename()), APPLICATION->network()));
|
||||
auto url = m_entity->url();
|
||||
auto entry = APPLICATION->metacache()->resolveEntry("meta", m_entity->localFilename());
|
||||
if (m_force_reload) {
|
||||
if (m_forceReload) {
|
||||
// clear validators so manual refreshes fetch a fresh body
|
||||
entry->setETag({});
|
||||
entry->setRemoteChangedTimestamp({});
|
||||
|
|
@ -171,15 +174,16 @@ void BaseEntityLoadTask::executeTask()
|
|||
* The validator parses the file and loads it into the object.
|
||||
* If that fails, the file is not written to storage.
|
||||
*/
|
||||
if (!m_entity->m_sha256.isEmpty())
|
||||
if (!m_entity->m_sha256.isEmpty()) {
|
||||
dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Algorithm::Sha256, m_entity->m_sha256));
|
||||
}
|
||||
dl->addValidator(new ParsingValidator(m_entity));
|
||||
m_task->addNetAction(dl);
|
||||
m_task->setAskRetry(false);
|
||||
connect(m_task.get(), &Task::failed, this, &BaseEntityLoadTask::emitFailed);
|
||||
connect(m_task.get(), &Task::succeeded, this, [this]() {
|
||||
m_entity->m_load_status = BaseEntity::LoadStatus::Remote;
|
||||
m_entity->m_file_sha256 = m_entity->m_sha256;
|
||||
m_entity->m_loadStatus = BaseEntity::LoadStatus::Remote;
|
||||
m_entity->m_fileSha256 = m_entity->m_sha256;
|
||||
emitSucceeded();
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue