public void DownloadJAR(DownloadingEventHandler eve) { Dictionary <NeedDownloadFile, string> files = new Dictionary <NeedDownloadFile, string>(); JARPath = Path.Combine(GameSettings.SelectedGameDirectory.ToString(), "versions", VersionName, VersionName + ".jar"); files.Add(new NeedDownloadFile() { FileName = AddrTranslater.TranslateURL(JSON["downloads"]["client"]["url"].ToString()), FileName2 = JSON["downloads"]["client"]["url"].ToString(), Hash = "" }, JARPath); var v = new DownloadTask(files).Download((float percent) => { eve(percent * 0.2f); }); if (v.Count > 0) { throw v.First().Value; } }
public void InstallVersion(DownloadingEventHandler eve) { RefreshJSON(); DownloadJAR(eve); IndexAssets(); Dictionary <NeedDownloadFile, string> files = new Dictionary <NeedDownloadFile, string>(); foreach (var lib in Assets.GetNeedToInstallFiles()) { files.Add(lib.Key, lib.Value); } foreach (var lib in Libraries.GetNeedToInstallFiles()) { files.Add(lib.Key, lib.Value); } new DownloadTask(files).Download((float f) => { eve(f * 0.8f + 0.2f); }); }
/// <summary> /// 开始下载 /// </summary> public void Download(DownloadingEventHandler eve) { percent = 0; DownloadProgressRefreshed(this, new EventArgs()); if (MinecraftVersion == null) { throw new Exception("Minecraft Version Can't be NULL"); } else { MinecraftProfile profile = MinecraftProfile.GetFromVersion(MinecraftVersion, VersionName); profile.SaveToLocalPath(); profile.InstallVersion((float f) => { eve(f * (1f - ((float)APIVersions.Count / 10f))); }); if (APIVersions.Count == 0) { } else { int count = APIVersions.Values.Count; int dldd = 0; foreach (var api in APIVersions.Values) { MinecraftAPI mcapi = api.GetMinecraftAPI(); mcapi.profile = profile; profile.APIConfig.InstallAPI(mcapi, (float f) => { eve((1f - ((float)APIVersions.Count / 10f)) + (float)(f / 10f) + (float)(dldd / 10f)); }); dldd++; } } } }
public Dictionary <KeyValuePair <NeedDownloadFile, string>, Exception> Download(DownloadingEventHandler downloadingEvent) { Dictionary <KeyValuePair <NeedDownloadFile, string>, Exception> FAILURES = new Dictionary <KeyValuePair <NeedDownloadFile, string>, Exception>(); Task distri = new Task(() => { List <Task> threads = new List <Task>(); Dictionary <NeedDownloadFile, string> unDownloadFiles = new Dictionary <NeedDownloadFile, string>(); int unDownloadedFiles = 0; float percentall = 0; int addedcount = 0; foreach (var file in keyValuePairs) { unDownloadFiles.Add(file.Key, file.Value); unDownloadedFiles++; PERcents.Add(file, 0f); } Dictionary <NeedDownloadFile, string> downloadedFiles = new Dictionary <NeedDownloadFile, string>(); for (int i = 0; i < DownloadSettings.MaxThreads; i++) { threads.Add(new Task(() => { while (true) { bool needDownload = true; KeyValuePair <NeedDownloadFile, string> file = new KeyValuePair <NeedDownloadFile, string>(); lock (unDownloadFiles) { if (unDownloadFiles.Count == 0) { return; } file = unDownloadFiles.First(); if (File.Exists(file.Value)) { string hash = HashHelper.ComputeSHA1(file.Value); if (hash == file.Key.Hash) { needDownload = false; percentall += 1; Logger.LogInfo("Need not Download : " + file.Key.FileName); downloadedFiles.Add(file.Key, file.Value); unDownloadFiles.Remove(unDownloadFiles.First().Key); } } if (needDownload) { unDownloadFiles.Remove(unDownloadFiles.First().Key); } } if (needDownload) { Directory.CreateDirectory(Path.GetDirectoryName(file.Value)); Logger.LogInfo("Download File:" + file.Key.FileName); //Download Paragraph { try { System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(file.Key.FileName); System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.FileStream(file.Value, System.IO.FileMode.Create); long totalDownloadedByte = 0; byte[] by = new byte[1024]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { totalDownloadedByte = osize + totalDownloadedByte; so.Write(by, 0, osize); osize = st.Read(by, 0, (int)by.Length); PERcents[file] = (float)totalDownloadedByte / (float)totalBytes; addedcount++; } so.Close(); st.Close(); } catch (System.Exception ex) { if (file.Key.FileName2 == null) { FAILURES.Add(file, ex); Logger.LogInfo("DownloadFileFAIL:" + file.Value + ex); } else { try { System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(file.Key.FileName2); System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.FileStream(file.Value, System.IO.FileMode.Create); long totalDownloadedByte = 0; byte[] by = new byte[1024]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { totalDownloadedByte = osize + totalDownloadedByte; so.Write(by, 0, osize); osize = st.Read(by, 0, (int)by.Length); PERcents[file] = (float)totalDownloadedByte / (float)totalBytes; addedcount++; } so.Close(); st.Close(); } catch (System.Exception exm) { FAILURES.Add(file, exm); Logger.LogInfo("DownloadFileFAIL:" + file.Value + exm); } } } finally { downloadedFiles.Add(file.Key, file.Value); Logger.LogInfo("DownloadedFile:" + file.Value); } } } PERcents[file] = 1; unDownloadedFiles--; } })); } Thread report = new Thread(() => { float lp = 0; while (true) { float pa = 0; try { foreach (var p in PERcents) { pa += p.Value; } lp = pa; } catch { pa = lp; } downloadingEvent(pa / PERcents.Count); Thread.Sleep(600); } }); report.Start(); foreach (var thread in threads) { thread.Start(); } while (downloadedFiles.Count != keyValuePairs.Count) { Thread.Sleep(600); } report.Abort(); report.DisableComObjectEagerCleanup(); Logger.LogInfo("Event That"); }); distri.Start(); distri.Wait(); Thread.Sleep(100); return(FAILURES); }
public override void Install(DownloadingEventHandler dlevent) { Forge forgeType = GetDownloadAPI(); Logger.LogInfo("Start to Install Forge " + forgeType.VersionName); string installerFilePath = Path.Combine(@"OMCLC\Plugins\Data\EDGW\Downloader-Temps", Guid.NewGuid().ToString()) + "-forge-installer.jar"; string installerDirectoryPath = Path.Combine(@"OMCLC\Plugins\Data\EDGW\Downloader-Temps", Guid.NewGuid().ToString()); //Install Jar { Logger.LogInfo("Install Forge JAR"); var jarList = new Dictionary <NeedDownloadFile, string>() { }; jarList.Add(new NeedDownloadFile() { FileName = AddrTranslater.TranslateURL(forgeType.DownloadAddr), FileName2 = forgeType.DownloadAddr, Hash = "" }, installerFilePath); DownloadTask jarDownloadTask = new DownloadTask(jarList); var downloadFaiures = jarDownloadTask.Download((float percent) => { }); if (downloadFaiures.Count > 0) { foreach (var failure in downloadFaiures) { Logger.LogError("Failed to Download File:" + failure.Value); } throw downloadFaiures.First().Value; } dlevent(0.2f); } //Resolve Jar { Logger.LogInfo("Resolve Installer"); var installerJAR = ZipFile.Open(installerFilePath, ZipArchiveMode.Update); Directory.CreateDirectory(installerDirectoryPath + "-extract"); installerJAR.ExtractToDirectory(installerDirectoryPath + "-extract"); { var profileEntry = installerJAR.GetEntry("install_profile.json"); profileEntry.ExtractToFile(installerFilePath + "-profile.tmp"); } { var versionEntry = installerJAR.GetEntry("version.json"); versionEntry.ExtractToFile(installerFilePath + "-version.tmp"); } JObject profile = JObject.Parse(File.ReadAllText(installerFilePath + "-profile.tmp")); JObject injectData = JObject.Parse(File.ReadAllText(installerFilePath + "-version.tmp")); Logger.LogInfo("Resolve Install-Profile"); if (profile["install"] == null) { //>=1.12.2 version switch ((int)profile["spec"]) { case 1: { //>=1.17 { Logger.LogInfo("Download Necessary Libraries"); VersionLibraries libraries = new VersionLibraries(); foreach (JObject libJSON in profile["libraries"] as JArray) { VersionLibrary library = VersionLibrary.FromJSON(libJSON, GameProfile); if (libJSON["downloads"]["artifact"]["url"].ToString() == "") { Logger.LogInfo("Extract Maven"); { var mavenEntry = installerJAR.GetEntry("maven/" + libJSON["downloads"]["artifact"]["path"].ToString()); Directory.CreateDirectory(Path.Combine(GameSettings.SelectedGameDirectory.FullName , "libraries" , Path.GetDirectoryName(libJSON["downloads"]["artifact"]["path"].ToString()))); mavenEntry.ExtractToFile(Path.Combine(GameSettings.SelectedGameDirectory.FullName , "libraries" , libJSON["downloads"]["artifact"]["path"].ToString())); } Logger.LogInfo("Extract Maven Successfully"); } else { libraries.Add(library); } } //Download JARs; { DownloadTask jarDownloadTask = new DownloadTask(libraries.GetNeedToInstallFiles()); var failures = jarDownloadTask.Download((float percent) => { dlevent(0.2f + (0.5f * percent)); }); if (failures.Count > 0) { foreach (var failure in failures) { Logger.LogError("Failed to Download File:" + failure.Value); } throw failures.First().Value; } } //Run Mappings { Logger.LogInfo("Start Mapping Process"); JObject dataKeys = profile["data"] as JObject; dataKeys["MINECRAFT_VERSION"] = new JObject(); dataKeys["INSTALLER"] = new JObject(); dataKeys["SIDE"] = new JObject(); dataKeys["MINECRAFT_JAR"] = new JObject(); dataKeys["ROOT"] = new JObject(); dataKeys["MINECRAFT_VERSION"]["client"] = GameProfile.VersionName; dataKeys["INSTALLER"]["client"] = installerFilePath; dataKeys["SIDE"]["client"] = "client"; dataKeys["MINECRAFT_JAR"]["client"] = GameProfile.JARPath; dataKeys["ROOT"]["client"] = GameSettings.SelectedGameDirectory.FullName; foreach (JObject precessorObject in profile["processors"]) { Logger.LogInfo("Run Processor " + precessorObject); if (precessorObject["sides"] == null) { precessorObject["sides"] = new JArray(); (precessorObject["sides"] as JArray).Add("client"); } if (precessorObject["sides"] != null) { Logger.LogInfo("Check Side"); string mainClass = ""; bool isClient = false; foreach (string side in precessorObject["sides"] as JArray) { if (side == "client") { isClient = true; } } if (isClient) { var javainstance = JavaInstance.CreateInstance(Java.JavaList[0]); var classpathArgs = new ClasspathArgument(); //Add MainJAR foreach (var library in libraries) { if (library.LibraryName.ToString() == precessorObject["jar"].ToString()) { Logger.LogInfo("Get MainClass"); ZipArchive libJAR = ZipFile.OpenRead((library.Downloads.First() as ArtifactLibrary).GetLocalAbsolutePath()); var manifest = libJAR.GetEntry("META-INF/MANIFEST.MF"); var stream = manifest.Open(); StreamReader reader = new StreamReader(stream); string manifestText = reader.ReadToEnd(); foreach (var manifestLine in manifestText.Split('\n')) { if (manifestLine.StartsWith("Main-Class:")) { string mainClassData = manifestLine.Substring("Main-Class:".Length, manifestLine.Length - "Main-Class:".Length - 1); mainClass = mainClassData.Replace("\n", ""); } } classpathArgs.AddClasspath((library.Downloads.First() as ArtifactLibrary).GetLocalAbsolutePath()); } } //Add ClassPaths foreach (string classpath in precessorObject["classpath"] as JArray) { Logger.LogInfo("Add ClassPath: " + classpath); classpathArgs.AddClasspath(new ArtifactLibrary() { LibraryName = LibraryName.Parse(classpath) }.GetLocalAbsolutePath()); } //Add ProcessorArgs javainstance.Arguments.AddArgument(classpathArgs); javainstance.Arguments.AddArgument(new StringArgument(mainClass)); foreach (string a in precessorObject["args"] as JArray) { string argument = a; List <string> replaces = new List <string>(); string replacingText = ""; bool needCollect = false; foreach (var character in argument) { if (character == '{') { needCollect = true; } else if (character == '}') { needCollect = false; replaces.Add(replacingText); replacingText = ""; } else if (needCollect) { replacingText += character; } } foreach (var replaceObject in replaces) { string replacedData = dataKeys[replaceObject]["client"].ToString(); if (replacedData.StartsWith("[") && replacedData.EndsWith("]")) { string libName = replacedData.Substring(1, replacedData.Length - 2); replacedData = "\"" + new ArtifactLibrary() { LibraryName = LibraryName.Parse(libName) }.GetLocalAbsolutePath() + "\""; } if (argument.StartsWith("/data/")) { argument = installerDirectoryPath + "-extract" + argument; } argument = argument.Replace("{" + replaceObject + "}", replacedData); } if (argument.StartsWith("[") && argument.EndsWith("]")) { string libname = argument.Substring(1, argument.Length - 2); argument = "\"" + new ArtifactLibrary() { LibraryName = LibraryName.Parse(libname) }.GetLocalAbsolutePath() + "\""; } if (argument.StartsWith("/data/")) { argument = installerDirectoryPath + "-extract" + argument; } javainstance.Arguments.AddArgument(new StringArgument(argument)); Logger.LogInfo("Add Argument: " + argument); } javainstance.Launch(); javainstance.JavaProcess.WaitForExit(); Logger.LogInfo("Processor Completed with ExitCode " + javainstance.JavaProcess.ExitCode.ToString()); if (javainstance.JavaProcess.ExitCode != 0) { throw new Exception("Run Processor Failed"); } } } } } //Inject Profile { Logger.LogInfo("Inject Forge Profile into Version Profile"); foreach (var jvmArg in injectData["arguments"]["jvm"] as JArray) { (GameProfile.JSON["arguments"]["jvm"] as JArray).Add(jvmArg); Logger.LogInfo("Inject JVM Argument:" + jvmArg + " Successfully"); } foreach (var gameArg in injectData["arguments"]["game"] as JArray) { (GameProfile.JSON["arguments"]["game"] as JArray).Add(gameArg); Logger.LogInfo("Inject Game Argument:" + gameArg + " Successfully"); } foreach (var libArg in injectData["libraries"] as JArray) { (GameProfile.JSON["libraries"] as JArray).Add(libArg); Logger.LogInfo("Inject Library:" + libArg + " Successfully"); } GameProfile.JSON["mainClass"] = injectData["mainClass"]; Logger.LogInfo("Inject MainClass:" + injectData["mainClass"] + " Successfully"); GameProfile.JSON["id"] = injectData["id"]; Logger.LogInfo("Inject GameID:" + injectData["id"] + " Successfully"); GameProfile.SaveToLocalPath(); Logger.LogInfo("Save Inject Profile Successfully"); Logger.LogInfo("Injected Profile!"); Logger.LogInfo("Installed Forge!"); } } break; } case 0: { //1.13-1.16.5 { Logger.LogInfo("Download Necessary Libraries"); VersionLibraries libraries = new VersionLibraries(); foreach (JObject libJSON in profile["libraries"] as JArray) { VersionLibrary library = VersionLibrary.FromJSON(libJSON, GameProfile); if (libJSON["downloads"]["artifact"]["url"].ToString() == "") { Logger.LogInfo("Extract Maven"); { var mavenEntry = installerJAR.GetEntry("maven/" + libJSON["downloads"]["artifact"]["path"].ToString()); Directory.CreateDirectory(Path.Combine(GameSettings.SelectedGameDirectory.FullName , "libraries" , Path.GetDirectoryName(libJSON["downloads"]["artifact"]["path"].ToString()))); mavenEntry.ExtractToFile(Path.Combine(GameSettings.SelectedGameDirectory.FullName , "libraries" , libJSON["downloads"]["artifact"]["path"].ToString())); } Logger.LogInfo("Extract Maven Successfully"); } else { libraries.Add(library); } } //Download JARs; { DownloadTask jarDownloadTask = new DownloadTask(libraries.GetNeedToInstallFiles()); var failures = jarDownloadTask.Download((float percent) => { dlevent(0.2f + (0.5f * percent)); }); if (failures.Count > 0) { foreach (var failure in failures) { Logger.LogError("Failed to Download File:" + failure.Value); } throw failures.First().Value; } } //Run Mappings { Logger.LogInfo("Start Mapping Process"); JObject dataKeys = profile["data"] as JObject; dataKeys["MINECRAFT_VERSION"] = new JObject(); dataKeys["INSTALLER"] = new JObject(); dataKeys["SIDE"] = new JObject(); dataKeys["MINECRAFT_JAR"] = new JObject(); dataKeys["ROOT"] = new JObject(); dataKeys["MINECRAFT_VERSION"]["client"] = GameProfile.VersionName; dataKeys["INSTALLER"]["client"] = installerFilePath; dataKeys["SIDE"]["client"] = "client"; dataKeys["MINECRAFT_JAR"]["client"] = GameProfile.JARPath; dataKeys["ROOT"]["client"] = GameSettings.SelectedGameDirectory.FullName; foreach (JObject precessorObject in profile["processors"]) { Logger.LogInfo("Run Processor " + precessorObject); if (precessorObject["sides"] == null) { precessorObject["sides"] = new JArray(); (precessorObject["sides"] as JArray).Add("client"); } if (precessorObject["sides"] != null) { Logger.LogInfo("Check Side"); string mainClass = ""; bool isClient = false; foreach (string side in precessorObject["sides"] as JArray) { if (side == "client") { isClient = true; } } if (isClient) { var javainstance = JavaInstance.CreateInstance(Java.JavaList[0]); var classpathArgs = new ClasspathArgument(); //Add MainJAR foreach (var library in libraries) { if (library.LibraryName.ToString() == precessorObject["jar"].ToString()) { Logger.LogInfo("Get MainClass"); ZipArchive libJAR = ZipFile.OpenRead((library.Downloads.First() as ArtifactLibrary).GetLocalAbsolutePath()); var manifest = libJAR.GetEntry("META-INF/MANIFEST.MF"); var stream = manifest.Open(); StreamReader reader = new StreamReader(stream); string manifestText = reader.ReadToEnd(); foreach (var manifestLine in manifestText.Split('\n')) { if (manifestLine.StartsWith("Main-Class:")) { string mainClassData = manifestLine.Substring("Main-Class:".Length, manifestLine.Length - "Main-Class:".Length - 1); mainClass = mainClassData.Replace("\n", ""); } } classpathArgs.AddClasspath((library.Downloads.First() as ArtifactLibrary).GetLocalAbsolutePath()); } } //Add ClassPaths foreach (string classpath in precessorObject["classpath"] as JArray) { Logger.LogInfo("Add ClassPath: " + classpath); classpathArgs.AddClasspath(new ArtifactLibrary() { LibraryName = LibraryName.Parse(classpath) }.GetLocalAbsolutePath()); } //Add ProcessorArgs javainstance.Arguments.AddArgument(classpathArgs); javainstance.Arguments.AddArgument(new StringArgument(mainClass)); foreach (string a in precessorObject["args"] as JArray) { string argument = a; List <string> replaces = new List <string>(); string replacingText = ""; bool needCollect = false; foreach (var character in argument) { if (character == '{') { needCollect = true; } else if (character == '}') { needCollect = false; replaces.Add(replacingText); replacingText = ""; } else if (needCollect) { replacingText += character; } } foreach (var replaceObject in replaces) { string replacedData = dataKeys[replaceObject]["client"].ToString(); if (replacedData.StartsWith("[") && replacedData.EndsWith("]")) { string libName = replacedData.Substring(1, replacedData.Length - 2); replacedData = "\"" + new ArtifactLibrary() { LibraryName = LibraryName.Parse(libName) }.GetLocalAbsolutePath() + "\""; } if (argument.StartsWith("/data/")) { argument = installerDirectoryPath + "-extract" + argument; } argument = argument.Replace("{" + replaceObject + "}", replacedData); } if (argument.StartsWith("[") && argument.EndsWith("]")) { string libname = argument.Substring(1, argument.Length - 2); argument = "\"" + new ArtifactLibrary() { LibraryName = LibraryName.Parse(libname) }.GetLocalAbsolutePath() + "\""; } if (argument.StartsWith("/data/")) { argument = installerDirectoryPath + "-extract" + argument; } javainstance.Arguments.AddArgument(new StringArgument(argument)); Logger.LogInfo("Add Argument: " + argument); } javainstance.Launch(); javainstance.JavaProcess.WaitForExit(); Logger.LogInfo("Processor Completed with ExitCode " + javainstance.JavaProcess.ExitCode.ToString()); if (javainstance.JavaProcess.ExitCode != 0) { throw new Exception("Run Processor Failed"); } } } } } //Inject Profile { Logger.LogInfo("Inject Forge Profile into Version Profile"); foreach (var jvmArg in injectData["arguments"]["jvm"] as JArray) { (GameProfile.JSON["arguments"]["jvm"] as JArray).Add(jvmArg); Logger.LogInfo("Inject JVM Argument:" + jvmArg + " Successfully"); } foreach (var gameArg in injectData["arguments"]["game"] as JArray) { (GameProfile.JSON["arguments"]["game"] as JArray).Add(gameArg); Logger.LogInfo("Inject Game Argument:" + gameArg + " Successfully"); } foreach (var libArg in injectData["libraries"] as JArray) { (GameProfile.JSON["libraries"] as JArray).Add(libArg); Logger.LogInfo("Inject Library:" + libArg + " Successfully"); } GameProfile.JSON["mainClass"] = injectData["mainClass"]; Logger.LogInfo("Inject MainClass:" + injectData["mainClass"] + " Successfully"); GameProfile.JSON["id"] = injectData["id"]; Logger.LogInfo("Inject GameID:" + injectData["id"] + " Successfully"); GameProfile.SaveToLocalPath(); Logger.LogInfo("Save Inject Profile Successfully"); Logger.LogInfo("Injected Profile!"); Logger.LogInfo("Installed Forge!"); } } break; } } } else { //<=1.12.2 Version } } }