/// <summary> /// Downloads the mod. /// </summary> /// <param name="modInfo">The infos of the mod. Must have at least ModURL and LocalPath</param> /// <param name="downloadProgressCallback">Callback function for download progress.</param> /// <returns>True if the mod was downloaded.</returns> public bool DownloadMod(ref ModInfo modInfo, DownloadProgressCallback downloadProgressCallback = null) { if (modInfo == null) { return(false); } string downloadURL = GetDownloadURL(modInfo.ModURL); modInfo.LocalPath = Www.DownloadFile2(downloadURL, OptionsController.DownloadPath, downloadProgressCallback); return(!string.IsNullOrEmpty(modInfo.LocalPath) && File.Exists(modInfo.LocalPath)); }
/// <summary> /// Gets the content of the site of the passed URL and parses it for ModInfos. /// </summary> /// <param name="url">The URL to the KSP forum site to parse the ModInfos from.</param> /// <returns>The ModInfos parsed from the site of the passed URL.</returns> public static ModInfo GetModInfo(string url) { ModInfo modInfo = new ModInfo(); modInfo.SiteHandlerName = Name; modInfo.ModURL = url; if (ParseSite(Www.Load(url), ref modInfo)) { return(modInfo); } else { return(null); } }
/// <summary> /// Loads the AVC file from the web url, parses the json response and creates a AVCInfo object. /// </summary> /// <returns>The new created AVCInfo object or null.</returns> public static AVCInfo ReadFromWeb(string url) { if (string.IsNullOrEmpty(url)) { return(null); } string content = Www.Load(url); if (string.IsNullOrEmpty(content)) { return(null); } return(ReadFromString(content)); }
/// <summary> /// Downloads the mod. /// </summary> /// <param name="modInfo">The infos of the mod. Must have at least ModURL and LocalPath</param> /// <param name="downloadProgressCallback">Callback function for download progress.</param> /// <returns>True if the mod was downloaded.</returns> public bool DownloadMod(ref ModInfo modInfo, DownloadProgressCallback downloadProgressCallback = null) { if (modInfo == null) { return(false); } HtmlWeb web = new HtmlWeb(); HtmlDocument htmlDoc = web.Load(modInfo.ModURL); htmlDoc.OptionFixNestedTags = true; // get filename from hover text HtmlNode fileNode = htmlDoc.DocumentNode.SelectSingleNode("//*[@id='content']/section[2]/div[4]/div[2]/ul/li[1]/div[2]/p/a"); HtmlNode fileNode2 = htmlDoc.DocumentNode.SelectSingleNode("//*[@id='content']/section[2]/div[4]/div[2]/ul/li/div[2]/p/a/span"); string downloadURL = GetDownloadURL(modInfo.ModURL); if (fileNode == null || (fileNode.InnerHtml.Contains("...") && fileNode2 == null)) { modInfo.LocalPath = Www.DownloadFile2(downloadURL, OptionsController.DownloadPath, downloadProgressCallback); return(!string.IsNullOrEmpty(modInfo.LocalPath) && File.Exists(modInfo.LocalPath)); } string filename = string.Empty; if (fileNode.InnerHtml.Contains("...")) { filename = fileNode2.Attributes["title"].Value; // Long filename was truncated } else { filename = fileNode.InnerHtml; } modInfo.LocalPath = Path.Combine(OptionsController.DownloadPath, filename); Www.DownloadFile(downloadURL, modInfo.LocalPath, downloadProgressCallback); return(File.Exists(modInfo.LocalPath)); }
/// <summary> /// Parse the download URL from site. /// </summary> /// <param name="url">The URL to parse the download URL from.</param> /// <param name="filename">The Filename of the mod archive.</param> /// <returns>The parsed download URL from site.</returns> private string GetDownloadURL(string url, ref string filename) { string siteContent = Www.Load(url); int index1 = siteContent.IndexOf("<li class=\"newest-file\">Newest File: ") + 37; if (index1 < 0) { return(string.Empty); } siteContent = siteContent.Substring(index1); index1 = siteContent.IndexOf("</li>"); if (index1 < 0) { return(string.Empty); } filename = siteContent.Substring(0, index1); index1 = siteContent.IndexOf("<em class=\"cta-button download-large\">"); if (index1 < 0) { return(string.Empty); } index1 = siteContent.IndexOf("href=\"", index1) + 6; siteContent = siteContent.Substring(index1); index1 = siteContent.IndexOf("\""); if (index1 < 0) { return(string.Empty); } // /ksp-mods/220221-mechjeb/download // http://kerbal.curseforge.com/ksp-mods/220221-mechjeb/files/latest string downloadURL = "http://kerbal.curseforge.com" + siteContent.Substring(0, index1).Replace("/kerbal/", "/").Replace("/download", "/") + "files/latest"; return((downloadURL.StartsWith("http:/") || downloadURL.StartsWith("https:/")) ? downloadURL : string.Empty); }