1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include "loader.hpp"
#include <algorithm>
#include <exception>
#include <filesystem>
#include <apps/opencs/model/doc/messages.hpp>
#include <apps/opencs/model/world/data.hpp>
#include <apps/opencs/model/world/universalid.hpp>
#include <components/debug/debuglog.hpp>
#include <components/files/conversion.hpp>
#include <QTimer>
#include "../tools/reportmodel.hpp"
#include "document.hpp"
CSMDoc::Loader::Loader()
: mShouldStop(false)
{
mTimer = new QTimer(this);
connect(mTimer, &QTimer::timeout, this, &Loader::load);
mTimer->start();
}
QWaitCondition& CSMDoc::Loader::hasThingsToDo()
{
return mThingsToDo;
}
void CSMDoc::Loader::stop()
{
mShouldStop = true;
}
void CSMDoc::Loader::load()
{
if (mDocuments.empty())
{
mMutex.lock();
mThingsToDo.wait(&mMutex);
mMutex.unlock();
if (mShouldStop)
mTimer->stop();
return;
}
if (!mStart.has_value())
mStart = std::chrono::steady_clock::now();
std::vector<std::pair<Document*, Stage>>::iterator iter = mDocuments.begin();
Document* document = iter->first;
int size = static_cast<int>(document->getContentFiles().size());
int editedIndex = size - 1; // index of the file to be edited/created
if (document->isNew())
--size;
bool done = false;
try
{
if (iter->second.mRecordsLeft)
{
Messages messages(Message::Severity_Error);
const int batchingSize = 50;
for (int i = 0; i < batchingSize; ++i) // do not flood the system with update signals
if (document->getData().continueLoading(messages))
{
iter->second.mRecordsLeft = false;
break;
}
else
++(iter->second.mRecordsLoaded);
CSMWorld::UniversalId log(CSMWorld::UniversalId::Type_LoadErrorLog, 0);
{ // silence a g++ warning
for (CSMDoc::Messages::Iterator messageIter(messages.begin()); messageIter != messages.end();
++messageIter)
{
document->getReport(log)->add(*messageIter);
emit loadMessage(document, messageIter->mMessage);
}
}
emit nextRecord(document, iter->second.mRecordsLoaded);
return;
}
if (iter->second.mFile < size) // start loading the files
{
const std::filesystem::path& path = document->getContentFiles()[iter->second.mFile];
int steps = document->getData().startLoading(path, iter->second.mFile != editedIndex, /*project*/ false);
iter->second.mRecordsLeft = true;
iter->second.mRecordsLoaded = 0;
emit nextStage(document, Files::pathToUnicodeString(path.filename()), steps);
}
else if (iter->second.mFile == size) // start loading the last (project) file
{
int steps = document->getData().startLoading(document->getProjectPath(), /*base*/ false, true);
iter->second.mRecordsLeft = true;
iter->second.mRecordsLoaded = 0;
emit nextStage(document, "Project File", steps);
}
else
{
document->getData().finishLoading();
done = true;
}
++(iter->second.mFile);
}
catch (const std::exception& e)
{
mDocuments.erase(iter);
emit documentNotLoaded(document, e.what());
return;
}
if (done)
{
if (mStart.has_value())
{
const auto duration = std::chrono::steady_clock::now() - *mStart;
Log(Debug::Verbose) << "Loaded content files in "
<< std::chrono::duration_cast<std::chrono::duration<double>>(duration).count() << 's';
mStart.reset();
}
mDocuments.erase(iter);
emit documentLoaded(document);
}
}
void CSMDoc::Loader::loadDocument(CSMDoc::Document* document)
{
mDocuments.emplace_back(document, Stage());
}
void CSMDoc::Loader::abortLoading(CSMDoc::Document* document)
{
for (std::vector<std::pair<Document*, Stage>>::iterator iter = mDocuments.begin(); iter != mDocuments.end(); ++iter)
{
if (iter->first == document)
{
mDocuments.erase(iter);
emit documentNotLoaded(document, "");
break;
}
}
}
|