2016-08-14 02:33:31 +02:00
# include "LibrariesTask.h"
2021-11-21 23:21:12 +01:00
2017-07-24 09:01:37 +02:00
# include "minecraft/MinecraftInstance.h"
2020-06-27 12:02:31 +02:00
# include "minecraft/PackProfile.h"
2016-08-14 02:33:31 +02:00
2021-11-21 23:21:12 +01:00
# include "Application.h"
2017-07-24 09:01:37 +02:00
LibrariesTask : : LibrariesTask ( MinecraftInstance * inst )
2016-08-14 02:33:31 +02:00
{
2018-07-15 14:51:05 +02:00
m_inst = inst ;
2016-08-14 02:33:31 +02:00
}
void LibrariesTask : : executeTask ( )
{
2022-12-11 16:11:32 -08:00
setStatus ( tr ( " Downloading required library files... " ) ) ;
2018-07-15 14:51:05 +02:00
qDebug ( ) < < m_inst - > name ( ) < < " : downloading libraries " ;
MinecraftInstance * inst = ( MinecraftInstance * ) m_inst ;
2016-08-14 02:33:31 +02:00
2018-07-15 14:51:05 +02:00
// Build a list of URLs that will need to be downloaded.
2020-06-27 12:02:31 +02:00
auto components = inst - > getPackProfile ( ) ;
2018-07-15 14:51:05 +02:00
auto profile = components - > getProfile ( ) ;
2016-08-14 02:33:31 +02:00
2023-01-24 16:52:09 -03:00
NetJob : : Ptr job { new NetJob ( tr ( " Libraries for instance %1 " ) . arg ( inst - > name ( ) ) , APPLICATION - > network ( ) ) } ;
2018-07-15 14:51:05 +02:00
downloadJob . reset ( job ) ;
2016-08-14 02:33:31 +02:00
2021-11-21 23:21:12 +01:00
auto metacache = APPLICATION - > metacache ( ) ;
2018-11-26 03:06:58 +01:00
2018-11-26 09:57:51 +01:00
auto processArtifactPool = [ & ] ( const QList < LibraryPtr > & pool , QStringList & errors , const QString & localPath )
2018-07-15 14:51:05 +02:00
{
2018-11-26 09:57:51 +01:00
for ( auto lib : pool )
2018-07-15 14:51:05 +02:00
{
2018-11-26 09:57:51 +01:00
if ( ! lib )
{
emitFailed ( tr ( " Null jar is specified in the metadata, aborting. " ) ) ;
return false ;
}
2022-07-11 09:01:07 +02:00
auto dls = lib - > getDownloads ( inst - > runtimeContext ( ) , metacache . get ( ) , errors , localPath ) ;
2018-11-26 09:57:51 +01:00
for ( auto dl : dls )
{
downloadJob - > addNetAction ( dl ) ;
}
2018-07-15 14:51:05 +02:00
}
2018-11-26 09:57:51 +01:00
return true ;
} ;
2017-04-06 23:31:23 +02:00
2018-11-26 09:57:51 +01:00
QStringList failedLocalLibraries ;
QList < LibraryPtr > libArtifactPool ;
libArtifactPool . append ( profile - > getLibraries ( ) ) ;
libArtifactPool . append ( profile - > getNativeLibraries ( ) ) ;
2020-03-27 02:23:15 +01:00
libArtifactPool . append ( profile - > getMavenFiles ( ) ) ;
2022-04-05 23:22:24 -07:00
for ( auto agent : profile - > getAgents ( ) )
{
libArtifactPool . append ( agent - > library ( ) ) ;
}
2018-11-26 09:57:51 +01:00
libArtifactPool . append ( profile - > getMainJar ( ) ) ;
processArtifactPool ( libArtifactPool , failedLocalLibraries , inst - > getLocalLibraryPath ( ) ) ;
QStringList failedLocalJarMods ;
2018-12-06 00:33:49 +01:00
processArtifactPool ( profile - > getJarMods ( ) , failedLocalJarMods , inst - > jarModsDir ( ) ) ;
2018-11-26 09:57:51 +01:00
if ( ! failedLocalJarMods . empty ( ) | | ! failedLocalLibraries . empty ( ) )
2018-07-15 14:51:05 +02:00
{
downloadJob . reset ( ) ;
2018-11-26 09:57:51 +01:00
QString failed_all = ( failedLocalLibraries + failedLocalJarMods ) . join ( " \n " ) ;
2018-12-06 00:36:31 +01:00
emitFailed ( tr ( " Some artifacts marked as 'local' are missing their files: \n %1 \n \n You need to either add the files, or removed the packages that require them. \n You'll have to correct this problem manually. " ) . arg ( failed_all ) ) ;
2018-07-15 14:51:05 +02:00
return ;
}
2018-11-26 09:57:51 +01:00
2018-07-15 14:51:05 +02:00
connect ( downloadJob . get ( ) , & NetJob : : succeeded , this , & LibrariesTask : : emitSucceeded ) ;
connect ( downloadJob . get ( ) , & NetJob : : failed , this , & LibrariesTask : : jarlibFailed ) ;
2022-06-22 19:56:24 -03:00
connect ( downloadJob . get ( ) , & NetJob : : aborted , this , [ this ] { emitFailed ( tr ( " Aborted " ) ) ; } ) ;
2018-07-15 14:51:05 +02:00
connect ( downloadJob . get ( ) , & NetJob : : progress , this , & LibrariesTask : : progress ) ;
2021-12-31 05:27:59 +01:00
downloadJob - > start ( ) ;
2016-08-14 02:33:31 +02:00
}
bool LibrariesTask : : canAbort ( ) const
{
2018-07-15 14:51:05 +02:00
return true ;
2016-08-14 02:33:31 +02:00
}
void LibrariesTask : : jarlibFailed ( QString reason )
{
2018-07-15 14:51:05 +02:00
emitFailed ( tr ( " Game update failed: it was impossible to fetch the required libraries. \n Reason: \n %1 " ) . arg ( reason ) ) ;
2016-08-14 02:33:31 +02:00
}
bool LibrariesTask : : abort ( )
{
2018-07-15 14:51:05 +02:00
if ( downloadJob )
{
return downloadJob - > abort ( ) ;
}
else
{
qWarning ( ) < < " Prematurely aborted LibrariesTask " ;
}
return true ;
2016-08-14 02:33:31 +02:00
}