//Download the selected forge version and setup automatically public void DownloadSelectedVersion() { if (ForgeVersions.SelectedItem == null) //Make sure they selected a forge version { MessageBox.Show("You need to select a forge version for the selected minecraft version to download", "Please choose a forge version", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (ProjectName.Text == "Project Name") { MessageBox.Show("You need to set a name for your project", "Invalid Project Name", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (ProjectVersion.Text == "Project Version") { MessageBox.Show("You need to set the initial version for your project", "Invalid Version", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (ProjectGroupName.Text == "Project Group Name") { MessageBox.Show("You need to set a group name for your project", "Invalid Group Project Name", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (JavaVersion.Text == "Java Version") { MessageBox.Show("You need to set a java version for your project", "Invalid Java Version", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { HtmlWeb web = new HtmlWeb(); HtmlAgilityPack.HtmlDocument document = web.Load(ForgeDownloadsURL + "index_" + MinecraftVersions.SelectedItem + ".html"); //Find the download link from the slected version foreach (HtmlNode downloadNode in document.DocumentNode.SelectNodes("//tr")) { if (downloadNode.HasChildNodes && downloadNode.ChildNodes[1].InnerText.Contains(((string)ForgeVersions.SelectedItem).Split('★')[0])) //If it is the correct version { HtmlNode downloadLinkNode = null; //Find the correct download link try { downloadLinkNode = downloadNode.SelectSingleNode(".//i[contains(@class, 'classifier-mdk')]").ParentNode; } catch { } if (downloadLinkNode == null) { try { downloadLinkNode = downloadNode.SelectSingleNode(".//i[contains(@class, 'classifier-src')]").ParentNode; } catch { } } if (downloadLinkNode != null) { string DownloadLink = downloadLinkNode.Attributes["href"].Value.Substring(48); //Remove the adfocus link as it breaks the download - find the direct download link Console.WriteLine("Found download link: " + DownloadLink); Program.INSTANCE.AddConsoleText("Found download link: " + DownloadLink); FolderBrowserDialog fbd = new FolderBrowserDialog(); //Ask where they want to put the mod if (fbd.ShowDialog() == DialogResult.OK) { if (System.IO.Directory.EnumerateFileSystemEntries(fbd.SelectedPath).Any()) //Make sure the folder is empty { MessageBox.Show("The folder you selected has files in, please select another folder", "Invalid Folder", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ProgressBarForm form = ProgressBarForm.ShowProgressBar("Downloading Project Src", "Downloading Src", new Font(Font.FontFamily, 16, FontStyle.Regular)); using (WebClient wc = new WebClient()) { string[] Directory = fbd.SelectedPath.Split('\\'); Directory[Directory.Length - 1] = ""; //Get the folder above the chosen folder wc.DownloadProgressChanged += (sender, e) => { form.ProgressBar.Value = e.ProgressPercentage; //Update progress bar based on how much has been downloaded }; wc.DownloadFileCompleted += (sender, e) => { form.Close(); Close(); Console.WriteLine("Downloaded file to " + string.Join("\\", Directory) + "temp.zip"); Program.INSTANCE.AddConsoleText("Downloaded file to " + string.Join("\\", Directory) + "temp.zip"); if (e.Error != null) { Console.WriteLine("An error occurred:\n" + e.Error); Program.INSTANCE.AddConsoleText("An error occurred:\n" + e.Error); File.Delete(string.Join("\\", Directory) + "temp.zip"); //Delete the zip file return; } ZipFile.ExtractToDirectory(string.Join("\\", Directory) + "temp.zip", fbd.SelectedPath); //Extract the file Console.WriteLine("Extracting file to " + fbd.SelectedPath); Program.INSTANCE.AddConsoleText("Extracting file to " + fbd.SelectedPath); File.Delete(string.Join("\\", Directory) + "temp.zip"); //Delete the zip file SetupBuildFile(fbd.SelectedPath); //Setup the build.gradle file Program.INSTANCE.OpenProject(fbd.SelectedPath); //Open the project if (MessageBox.Show("Would you like to setup now?", "Setup now?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Program.INSTANCE.SetupProject(""); //Setup the project } }; wc.DownloadFileAsync(new Uri(DownloadLink), string.Join("\\", Directory) + "temp.zip"); //Start the download while (wc.IsBusy) { } } } else { return; } } } } } catch (Exception e) { Console.WriteLine("An error occurred:\n" + (string)e.Message); Program.INSTANCE.AddConsoleText("An error occurred:\n" + (string)e.Message); } }
//Load the versions from the forge web server public static void SyncVersions() { Versions.Clear(); LatestMinecraftVersion = ""; try { List <string> MinecraftVersions = new List <string>(); //Create a nice looking progress bar to show how much has been got off the website ProgressBarForm ProgressBar = ProgressBarForm.ShowProgressBar("Syncing Versions", "Syncing Versions", new Font(new FontFamily("Microsoft Sans Serif"), 16, FontStyle.Regular)); // Get the minecraft versions dynamic minecraftVersionsData = JObject.Parse(new WebClient().DownloadString(MinecraftVersionURL)); // List<string> minecraftVersions = new List<string>(); // Store the versions which are releases. Some of these versions may not have a forge version foreach (dynamic minecraftVersionData in minecraftVersionsData.versions) { if (minecraftVersionData.type == "release") { MinecraftVersions.Add((string)minecraftVersionData.id); } } Console.WriteLine("Loaded minecraft versions from " + MinecraftVersionURL); Program.INSTANCE.AddConsoleText("Loaded minecraft versions from " + MinecraftVersionURL); LatestMinecraftVersion = MinecraftVersions[0]; //Create something which will access the website HtmlWeb web = new HtmlWeb(); // HtmlAgilityPack.HtmlDocument document = web.Load(ForgeDownloadsURL); HtmlAgilityPack.HtmlDocument document; /* * LatestMinecraftVersion = document.DocumentNode.SelectSingleNode("//li[@class='elem-active']").InnerHtml; //Get the latest minecraft version * //MinecraftVersions.Add(LatestMinecraftVersion); * foreach (HtmlNode mcVersionNode in document.DocumentNode.SelectNodes("//li[@class='li-version-list']")) * { * foreach (HtmlNode subMcVersionNode in mcVersionNode.SelectNodes(".//li")) * { * if (subMcVersionNode.HasChildNodes) * { * foreach(HtmlNode node in subMcVersionNode.ChildNodes) * { * if(node.NodeType == HtmlNodeType.Element) * { * MinecraftVersions.Add(node.InnerHtml); //Add each minecraft version * } * } * } * } * } */ Version latestVersion = new Version(LatestMinecraftVersion); Version mc; foreach (string mcversion in MinecraftVersions.ToList()) { // mc = new Version(mcversion.Contains("_") ? mcversion.Substring(0, mcversion.IndexOf("_") - 1) : mcversion); mc = new Version(mcversion); if (latestVersion.CompareTo(mc) < 0) { MinecraftVersions.Insert(MinecraftVersions.IndexOf(mcversion), LatestMinecraftVersion); MinecraftVersions.Remove(mcversion); LatestMinecraftVersion = mcversion; MinecraftVersions.Insert(0, mcversion); } } if (latestVersion.CompareTo(new Version(LatestMinecraftVersion)) == 0) { MinecraftVersions.Insert(0, LatestMinecraftVersion); } foreach (string mcversion in MinecraftVersions) { Console.WriteLine(mcversion); List <string> forgeVersions = new List <string>(); try { document = web.Load(ForgeDownloadsURL + "index_" + mcversion + ".html"); foreach (HtmlNode forgeVersionNode in document.DocumentNode.SelectNodes("//td[@class='download-version']")) { //Load each forge version for every minecraft version ProgressBar.ProgressBar.Value = (int)((document.DocumentNode.SelectNodes("//td[@class='download-version']").IndexOf(forgeVersionNode) + 1F) / document.DocumentNode.SelectNodes("//td[@class='download-version']").Count + (int)((MinecraftVersions.IndexOf(mcversion) + 1F) / MinecraftVersions.Count * 100)) - 1; forgeVersions.Add(Regex.Replace(forgeVersionNode.InnerHtml.Split('<')[0].Replace(" ", string.Empty).Replace(Environment.NewLine, string.Empty), @"\s+", "") + (forgeVersionNode.InnerHtml.Contains("fa promo-recommended") ? "★" : "")); } Console.WriteLine(forgeVersions); if (Versions.ContainsKey(mcversion)) { Versions.Remove(mcversion); } Versions.Add(mcversion, forgeVersions); Console.WriteLine("Loaded forge versions from " + ForgeDownloadsURL + "index_" + mcversion + ".html"); ForgeModBuilder.Program.INSTANCE.AddConsoleText("Loaded forge versions from " + ForgeDownloadsURL + "index_" + mcversion + ".html"); } catch (NullReferenceException e) { Versions.Remove(mcversion); continue; } } ProgressBar.Close(); //Close the progress bar } catch (Exception e) { Console.WriteLine("An error occurred:\n" + e.ToString()); Program.INSTANCE.AddConsoleText("An error occurred:\n" + e.ToString()); } }