2019-10-12 21:37:16 +00:00
using System ;
using System.Linq ;
using Wabbajack.Common ;
2019-12-04 04:12:08 +00:00
using Wabbajack.Common.StatusFeed.Errors ;
2019-10-16 03:10:34 +00:00
using Wabbajack.Lib.NexusApi ;
using Wabbajack.Lib.Validation ;
2019-10-12 21:37:16 +00:00
2019-10-16 03:10:34 +00:00
namespace Wabbajack.Lib.Downloaders
2019-10-12 21:37:16 +00:00
{
public class NexusDownloader : IDownloader
{
2019-11-21 15:51:57 +00:00
public AbstractDownloadState GetDownloaderState ( dynamic archiveINI )
2019-10-12 21:37:16 +00:00
{
2019-11-21 15:51:57 +00:00
var general = archiveINI ? . General ;
2019-10-12 21:37:16 +00:00
if ( general . modID ! = null & & general . fileID ! = null & & general . gameName ! = null )
{
2019-11-08 01:36:01 +00:00
var name = ( string ) general . gameName ;
2019-11-09 14:57:35 +00:00
var gameMeta = GameRegistry . GetByMO2ArchiveName ( name ) ;
var game = gameMeta ! = null ? GameRegistry . GetByMO2ArchiveName ( name ) . Game : GameRegistry . GetByNexusName ( name ) . Game ;
2019-11-08 01:36:01 +00:00
var info = new NexusApiClient ( ) . GetModInfo ( game , general . modID ) ;
2019-10-12 21:37:16 +00:00
return new State
{
GameName = general . gameName ,
FileID = general . fileID ,
ModID = general . modID ,
Version = general . version ? ? "0.0.0.0" ,
Author = info . author ,
UploadedBy = info . uploaded_by ,
UploaderProfile = info . uploaded_users_profile_url ,
ModName = info . name ,
SlideShowPic = info . picture_url ,
2019-11-08 01:36:01 +00:00
NexusURL = NexusApiUtils . GetModURL ( game , info . mod_id ) ,
2019-10-12 21:37:16 +00:00
Summary = info . summary ,
Adult = info . contains_adult_content
} ;
}
return null ;
}
2019-10-12 22:15:20 +00:00
public void Prepare ( )
{
var client = new NexusApiClient ( ) ;
var status = client . GetUserStatus ( ) ;
if ( ! client . IsAuthenticated )
{
2019-12-05 04:58:02 +00:00
Utils . ErrorThrow ( new UnconvertedError ( $"Authenticating for the Nexus failed. A nexus account is required to automatically download mods." ) ) ;
2019-10-12 22:15:20 +00:00
return ;
}
2019-11-14 21:53:29 +00:00
if ( status . is_premium ) return ;
2019-12-05 04:58:02 +00:00
Utils . ErrorThrow ( new UnconvertedError ( $"Automated installs with Wabbajack requires a premium nexus account. {client.Username} is not a premium account." ) ) ;
2019-10-12 22:15:20 +00:00
}
2019-10-12 21:37:16 +00:00
public class State : AbstractDownloadState
{
public string Author ;
public string FileID ;
public string GameName ;
public string ModID ;
public string UploadedBy ;
public string UploaderProfile ;
public string Version ;
public string SlideShowPic ;
public string ModName ;
public string NexusURL ;
public string Summary ;
public bool Adult ;
public override bool IsWhitelisted ( ServerWhitelist whitelist )
{
// Nexus files are always whitelisted
return true ;
}
public override void Download ( Archive a , string destination )
{
string url ;
try
{
url = new NexusApiClient ( ) . GetNexusDownloadLink ( this , false ) ;
}
catch ( Exception ex )
{
Utils . Log ( $"{a.Name} - Error Getting Nexus Download URL - {ex.Message}" ) ;
return ;
}
Utils . Log ( $"Downloading Nexus Archive - {a.Name} - {GameName} - {ModID} - {FileID}" ) ;
new HTTPDownloader . State
{
Url = url
} . Download ( a , destination ) ;
}
public override bool Verify ( )
{
try
{
2019-12-01 14:44:01 +00:00
var gameMeta = GameRegistry . GetByMO2ArchiveName ( GameName ) ? ? GameRegistry . GetByNexusName ( GameName ) ;
if ( gameMeta = = null )
return false ;
var game = gameMeta . Game ;
if ( ! int . TryParse ( ModID , out var modID ) )
return false ;
var modFiles = new NexusApiClient ( ) . GetModFiles ( game , modID ) ;
if ( ! ulong . TryParse ( FileID , out var fileID ) )
return false ;
var found = modFiles . files
. FirstOrDefault ( file = > file . file_id = = fileID & & file . category_name ! = null ) ;
2019-11-21 05:57:48 +00:00
return found ! = null ;
2019-10-12 21:37:16 +00:00
}
catch ( Exception ex )
{
2019-11-22 05:19:42 +00:00
Utils . Log ( $"{ModName} - {GameName} - {ModID} - {FileID} - Error Getting Nexus Download URL - {ex}" ) ;
2019-10-12 21:37:16 +00:00
return false ;
}
}
2019-10-12 22:15:20 +00:00
public override IDownloader GetDownloader ( )
{
return DownloadDispatcher . GetInstance < NexusDownloader > ( ) ;
}
2019-10-12 22:54:25 +00:00
public override string GetReportEntry ( Archive a )
{
var profile = UploaderProfile . Replace ( "/games/" ,
"/" + NexusApiUtils . ConvertGameName ( GameName ) . ToLower ( ) + "/" ) ;
return string . Join ( "\n" ,
$"* [{a.Name}](http://nexusmods.com/{NexusApiUtils.ConvertGameName(GameName)}/mods/{ModID})" ,
$" * Author : [{UploadedBy}]({profile})" ,
$" * Version : {Version}" ) ;
}
2019-10-12 21:37:16 +00:00
}
}
}