2014-04-24 13:09:25 +00:00
# include "loader.hpp"
2016-06-10 16:10:14 +00:00
# include <iostream>
2014-04-24 13:09:25 +00:00
2014-05-10 10:04:36 +00:00
# include "../tools/reportmodel.hpp"
2014-04-24 13:09:25 +00:00
# include "document.hpp"
2014-06-26 09:41:21 +00:00
CSMDoc : : Loader : : Stage : : Stage ( ) : mFile ( 0 ) , mRecordsLoaded ( 0 ) , mRecordsLeft ( false ) { }
2014-05-03 10:07:05 +00:00
2014-04-24 13:09:25 +00:00
CSMDoc : : Loader : : Loader ( )
2016-06-10 16:10:14 +00:00
: mShouldStop ( false )
2014-04-24 13:09:25 +00:00
{
2016-06-10 16:10:14 +00:00
mTimer = new QTimer ( this ) ;
2014-04-24 13:09:25 +00:00
2022-08-23 02:28:58 +00:00
connect ( mTimer , & QTimer : : timeout , this , & Loader : : load ) ;
2016-06-10 16:10:14 +00:00
mTimer - > start ( ) ;
2014-04-24 13:09:25 +00:00
}
QWaitCondition & CSMDoc : : Loader : : hasThingsToDo ( )
{
return mThingsToDo ;
}
2016-06-10 16:10:14 +00:00
void CSMDoc : : Loader : : stop ( )
{
mShouldStop = true ;
}
2014-04-24 13:09:25 +00:00
void CSMDoc : : Loader : : load ( )
{
if ( mDocuments . empty ( ) )
{
mMutex . lock ( ) ;
mThingsToDo . wait ( & mMutex ) ;
mMutex . unlock ( ) ;
2016-06-10 16:10:14 +00:00
if ( mShouldStop )
mTimer - > stop ( ) ;
2014-04-24 13:09:25 +00:00
return ;
}
2014-05-03 10:07:05 +00:00
std : : vector < std : : pair < Document * , Stage > > : : iterator iter = mDocuments . begin ( ) ;
2014-04-24 13:09:25 +00:00
Document * document = iter - > first ;
2014-05-03 10:07:05 +00:00
int size = static_cast < int > ( document - > getContentFiles ( ) . size ( ) ) ;
2014-06-17 09:01:17 +00:00
int editedIndex = size - 1 ; // index of the file to be edited/created
2014-05-03 10:07:05 +00:00
if ( document - > isNew ( ) )
- - size ;
bool done = false ;
2014-04-24 13:09:25 +00:00
try
{
2014-05-03 13:05:02 +00:00
if ( iter - > second . mRecordsLeft )
{
2015-06-20 17:04:19 +00:00
Messages messages ( Message : : Severity_Error ) ;
2016-01-03 17:20:34 +00:00
const int batchingSize = 50 ;
2014-05-03 13:33:35 +00:00
for ( int i = 0 ; i < batchingSize ; + + i ) // do not flood the system with update signals
2014-05-10 10:04:36 +00:00
if ( document - > getData ( ) . continueLoading ( messages ) )
2014-05-03 13:33:35 +00:00
{
iter - > second . mRecordsLeft = false ;
break ;
}
2014-06-26 09:41:21 +00:00
else
+ + ( iter - > second . mRecordsLoaded ) ;
2014-05-03 13:33:35 +00:00
2014-05-10 10:04:36 +00:00
CSMWorld : : UniversalId log ( CSMWorld : : UniversalId : : Type_LoadErrorLog , 0 ) ;
2014-06-26 09:41:21 +00:00
{ // silence a g++ warning
2016-10-15 16:34:54 +00:00
for ( CSMDoc : : Messages : : Iterator messageIter ( messages . begin ( ) ) ;
messageIter ! = messages . end ( ) ; + + messageIter )
2014-05-10 11:18:40 +00:00
{
2016-10-15 16:34:54 +00:00
document - > getReport ( log ) - > add ( * messageIter ) ;
emit loadMessage ( document , messageIter - > mMessage ) ;
2014-05-10 11:18:40 +00:00
}
2014-06-26 09:41:21 +00:00
}
2014-05-10 10:04:36 +00:00
2014-06-26 09:41:21 +00:00
emit nextRecord ( document , iter - > second . mRecordsLoaded ) ;
2014-05-03 13:05:02 +00:00
return ;
}
2021-07-23 07:34:27 +00:00
if ( iter - > second . mFile < size ) // start loading the files
2014-05-03 10:07:05 +00:00
{
2022-06-08 21:25:50 +00:00
std : : filesystem : : path path = document - > getContentFiles ( ) [ iter - > second . mFile ] ;
2014-05-03 11:01:29 +00:00
2021-07-23 07:34:27 +00:00
int steps = document - > getData ( ) . startLoading ( path , iter - > second . mFile ! = editedIndex , /*project*/ false ) ;
2014-05-03 13:05:02 +00:00
iter - > second . mRecordsLeft = true ;
2014-06-26 09:41:21 +00:00
iter - > second . mRecordsLoaded = 0 ;
2014-05-03 13:33:35 +00:00
2022-06-19 11:28:33 +00:00
emit nextStage ( document , path . filename ( ) . string ( ) , steps ) ; //TODO(Project579): let's hope unicode characters are never used in these filenames on windows or this will break
2014-05-03 10:07:05 +00:00
}
2021-07-23 07:34:27 +00:00
else if ( iter - > second . mFile = = size ) // start loading the last (project) file
2014-05-03 10:07:05 +00:00
{
2021-07-23 07:34:27 +00:00
int steps = document - > getData ( ) . startLoading ( document - > getProjectPath ( ) , /*base*/ false , true ) ;
2014-05-03 13:05:02 +00:00
iter - > second . mRecordsLeft = true ;
2014-06-26 09:41:21 +00:00
iter - > second . mRecordsLoaded = 0 ;
2014-05-03 13:33:35 +00:00
2014-06-26 09:41:21 +00:00
emit nextStage ( document , " Project File " , steps ) ;
2014-05-03 10:07:05 +00:00
}
else
{
done = true ;
}
+ + ( iter - > second . mFile ) ;
2014-04-24 13:09:25 +00:00
}
catch ( const std : : exception & e )
{
2014-05-03 10:07:05 +00:00
mDocuments . erase ( iter ) ;
2014-04-24 13:09:25 +00:00
emit documentNotLoaded ( document , e . what ( ) ) ;
2014-05-03 10:07:05 +00:00
return ;
}
if ( done )
{
mDocuments . erase ( iter ) ;
emit documentLoaded ( document ) ;
2014-04-24 13:09:25 +00:00
}
}
2014-04-29 12:27:44 +00:00
void CSMDoc : : Loader : : loadDocument ( CSMDoc : : Document * document )
2014-04-24 13:09:25 +00:00
{
2020-10-17 08:26:35 +00:00
mDocuments . emplace_back ( document , Stage ( ) ) ;
2014-04-24 13:09:25 +00:00
}
2014-05-03 14:44:50 +00:00
void CSMDoc : : Loader : : abortLoading ( CSMDoc : : Document * document )
2014-04-24 13:09:25 +00:00
{
2014-05-03 10:07:05 +00:00
for ( std : : vector < std : : pair < Document * , Stage > > : : iterator iter = mDocuments . begin ( ) ;
2014-04-24 13:09:25 +00:00
iter ! = mDocuments . end ( ) ; + + iter )
{
if ( iter - > first = = document )
{
mDocuments . erase ( iter ) ;
emit documentNotLoaded ( document , " " ) ;
break ;
}
}
2014-05-03 14:44:50 +00:00
}