Example #1
0
        /// <summary>
        /// Loads the information about this nxmlink into this object. Subscribe to OnInitialized() to know when it has initialized and is ready for download to begin.
        /// THIS IS A BLOCKING CALL DO NOT RUN ON THE UI
        /// </summary>
        public void Initialize()
        {
            Log.Information($@"Initializing {NXMLink}");
            Task.Run(() =>
            {
                try
                {
                    DownloadLinks.Clear();

                    var nxmlink  = NXMLink.Substring(6);
                    var queryPos = nxmlink.IndexOf('?');

                    var info   = queryPos > 0 ? nxmlink.Substring(0, queryPos) : nxmlink;
                    var infos  = info.Split('/');
                    domain     = infos[0];
                    var modid  = int.Parse(infos[2]);
                    var fileid = int.Parse(infos[4]);

                    if (!NexusModsUtilities.AllSupportedNexusDomains.Contains(domain))
                    {
                        Log.Error($@"Cannot download file from unsupported domain: {domain}. Open your preferred mod manager from that game first");
                        Initialized           = true;
                        ProgressIndeterminate = false;
                        OnModDownloadError?.Invoke(this,
                                                   M3L.GetString(M3L.string_interp_dialog_modNotForThisModManager, domain));
                        return;
                    }

                    ModFile = NexusModsUtilities.GetClient().ModFiles.GetModFile(domain, modid, fileid).Result;
                    if (ModFile != null)
                    {
                        if (ModFile.Category != FileCategory.Deleted)
                        {
                            if (queryPos > 0)
                            {
                                // download with manager
                                string querystring = nxmlink.Substring(queryPos);
                                var parameters     = HttpUtility.ParseQueryString(querystring);

                                // Check if parameters are correct!
                                DownloadLinks.AddRange(NexusModsUtilities.GetDownloadLinkForFile(domain, modid, fileid,
                                                                                                 parameters[@"key"], int.Parse(parameters[@"expires"])).Result);
                            }
                            else
                            {
                                // premium?
                                if (!NexusModsUtilities.UserInfo.IsPremium)
                                {
                                    Log.Error(
                                        $@"Cannot download {ModFile.FileName}: User is not premium, but this link is not generated from NexusMods");
                                    Initialized           = true;
                                    ProgressIndeterminate = false;
                                    OnModDownloadError?.Invoke(this,
                                                               M3L.GetString(M3L.string_dialog_mustBePremiumUserToDownload));
                                    return;
                                }

                                DownloadLinks.AddRange(NexusModsUtilities.GetDownloadLinkForFile(domain, modid, fileid)
                                                       ?.Result);
                            }

                            ProgressMaximum = ModFile.Size * 1024; // Bytes
                            Initialized     = true;
                            Log.Error($@"ModDownload has initialized: {ModFile.FileName}");
                            OnInitialized?.Invoke(this, null);
                        }
                        else
                        {
                            Log.Error($@"Cannot download {ModFile.FileName}: File deleted from NexusMods");
                            Initialized           = true;
                            ProgressIndeterminate = false;
                            OnModDownloadError?.Invoke(this,
                                                       M3L.GetString(M3L.string_dialog_cannotDownloadDeletedFile));
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Error($@"Error downloading {ModFile?.FileName}: {e.Message}");
                    Initialized           = true;
                    ProgressIndeterminate = false;
                    OnModDownloadError?.Invoke(this, M3L.GetString(M3L.string_interp_errorDownloadingModX, e.Message));
                }
            });
        }
        /// <summary>
        /// Loads the information about this nxmlink into this object. Subscribe to OnInitialized() to know when it has initialized and is ready for download to begin.
        /// THIS IS A BLOCKING CALL DO NOT RUN ON THE UI
        /// </summary>
        public void Initialize()
        {
            Log.Information($@"Initializing {NXMLink}");
            Task.Run(() =>
            {
                try
                {
                    DownloadLinks.Clear();

                    ProtocolLink = NexusProtocolLink.Parse(NXMLink);
                    if (ProtocolLink == null)
                    {
                        return;                       // Parse failed.
                    }
                    if (!NexusModsUtilities.AllSupportedNexusDomains.Contains(ProtocolLink?.Domain))
                    {
                        Log.Error($@"Cannot download file from unsupported domain: {ProtocolLink?.Domain}. Open your preferred mod manager from that game first");
                        Initialized           = true;
                        ProgressIndeterminate = false;
                        OnModDownloadError?.Invoke(this,
                                                   M3L.GetString(M3L.string_interp_dialog_modNotForThisModManager, ProtocolLink.Domain));
                        return;
                    }

                    ModFile = NexusModsUtilities.GetClient().ModFiles.GetModFile(ProtocolLink.Domain, ProtocolLink.ModId, ProtocolLink.FileId).Result;
                    if (ModFile != null)
                    {
                        if (ModFile.Category != FileCategory.Deleted)
                        {
                            if (ProtocolLink.Key != null)
                            {
                                // Website click
                                if (ProtocolLink.Domain is @"masseffect" or @"masseffect2" && !IsDownloadWhitelisted(ProtocolLink.Domain, ModFile))
                                {
                                    // Check to see file has moddesc.ini the listing
                                    var fileListing = NexusModsUtilities.GetFileListing(ModFile);
                                    if (fileListing == null || !HasModdescIni(fileListing))
                                    {
                                        Log.Error($@"This file is not whitelisted for download and does not contain a moddesc.ini file, this is not a mod manager mod: {ModFile.FileName}");
                                        Initialized           = true;
                                        ProgressIndeterminate = false;
                                        OnModDownloadError?.Invoke(this, M3L.GetString(M3L.string_interp_nexusModNotCompatible, ModFile.Name));
                                        return;
                                    }
                                }


                                // download with manager was clicked.

                                // Check if parameters are correct!
                                DownloadLinks.AddRange(NexusModsUtilities.GetDownloadLinkForFile(ProtocolLink).Result);
                            }
                            else
                            {
                                // premium?
                                if (!NexusModsUtilities.UserInfo.IsPremium)
                                {
                                    Log.Error(
                                        $@"Cannot download {ModFile.FileName}: User is not premium, but this link is not generated from NexusMods");
                                    Initialized           = true;
                                    ProgressIndeterminate = false;
                                    OnModDownloadError?.Invoke(this,
                                                               M3L.GetString(M3L.string_dialog_mustBePremiumUserToDownload));
                                    return;
                                }

                                DownloadLinks.AddRange(NexusModsUtilities.GetDownloadLinkForFile(ProtocolLink)
                                                       ?.Result);
                            }

                            ProgressMaximum = ModFile.SizeInBytes ?? ModFile.SizeInKilobytes * 1024L; // SizeKb is the original version. They added SizeInBytes at my request
                            Initialized     = true;
                            Log.Information($@"ModDownload has initialized: {ModFile.FileName}");
                            OnInitialized?.Invoke(this, null);
                        }
                        else
                        {
                            Log.Error($@"Cannot download {ModFile.FileName}: File deleted from NexusMods");
                            Initialized           = true;
                            ProgressIndeterminate = false;
                            OnModDownloadError?.Invoke(this,
                                                       M3L.GetString(M3L.string_dialog_cannotDownloadDeletedFile));
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Error($@"Error downloading {ModFile?.FileName}: {e.Message}");
                    Initialized           = true;
                    ProgressIndeterminate = false;
                    OnModDownloadError?.Invoke(this, M3L.GetString(M3L.string_interp_errorDownloadingModX, e.Message));
                }
            });
        }