private void GetMainFilesUrls(RootGameVersion versionJsnon, string jsnonUrl) { HashSet <string> filePaths = null; string downloadPath = SettingsControl.MainDirectory + "\\versions\\" + downloadVersion + "\\"; filePaths = new HashSet <string>(); filePaths.Add(downloadPath + downloadVersion + ".json"); downloadQueue.Enqueue(new DownloadFileInfo(filePaths, jsnonUrl, 0)); filePaths = new HashSet <string>(); filePaths.Add(downloadPath + downloadVersion + ".jar"); downloadQueue.Enqueue(new DownloadFileInfo(filePaths, versionJsnon.downloads.client.url, versionJsnon.downloads.client.size)); if (versionJsnon.downloads.server != null && !string.IsNullOrEmpty(versionJsnon.downloads.server.url)) { filePaths = new HashSet <string>(); filePaths.Add(downloadPath + "minecraft_server." + downloadVersion + ".jar"); downloadQueue.Enqueue(new DownloadFileInfo(filePaths, versionJsnon.downloads.server.url, versionJsnon.downloads.server.size)); } if (versionJsnon.downloads.windows_server != null && !string.IsNullOrEmpty(versionJsnon.downloads.windows_server.url)) { filePaths = new HashSet <string>(); filePaths.Add(downloadPath + "minecraft_server." + downloadVersion + ".exe"); downloadQueue.Enqueue(new DownloadFileInfo(filePaths, versionJsnon.downloads.windows_server.url, versionJsnon.downloads.windows_server.size)); } }
private void GetLibrariesUrls(RootGameVersion versionJsnon) { bool skipLibrary = false; HashSet <string> filePaths = null; string libariesPath = SettingsControl.MainDirectory + "\\libraries\\"; var librariesList = versionJsnon.libraries.GroupBy(x => x.name).Select(y => y.First()).ToList(); for (int x = librariesList.Count - 1; x >= 0; x--) { skipLibrary = false; if (librariesList[x].rules != null) { foreach (var rule in librariesList[x].rules) { if (rule.action != null && rule.action == "allow" && rule.os != null && rule.os.name != "windows") { skipLibrary = true; break; } else if (rule.action != null && rule.action == "disallow" && rule.os != null && rule.os.name == "windows") { skipLibrary = true; break; } } } if (skipLibrary) { librariesList.RemoveAt(x); continue; } if (librariesList[x].downloads.artifact != null) { filePaths = new HashSet <string>(); filePaths.Add(libariesPath + librariesList[x].downloads.artifact.path.Replace("/", "\\")); downloadQueue.Enqueue(new DownloadFileInfo(filePaths, librariesList[x].downloads.artifact.url, librariesList[x].downloads.artifact.size)); } if (librariesList[x].downloads.classifiers != null) { foreach (var classifier in librariesList[x].downloads.classifiers) { if (classifier.Key.Contains("windows")) { filePaths = new HashSet <string>(); filePaths.Add(libariesPath + classifier.Value.path.Replace("/", "\\")); downloadQueue.Enqueue(new DownloadFileInfo(filePaths, classifier.Value.url, classifier.Value.size)); } } } librariesList.RemoveAt(x); } }
public bool DownloadVersion(string newDownloadVersion) { ResetTempVariables(); downloadVersion = newDownloadVersion; string content = ""; bool downloaded = false; RootVersionsManifest versionsManifest = null; try { string mainDir = SettingsControl.MainDirectory; if (!File.Exists(mainDir + "\\version_manifest.json")) { DownloadUtils.DownloadFile(Constants.URL_VERSIONS_MANIFEST, mainDir + "\\version_manifest.json"); } versionsManifest = JsonConvert.DeserializeObject <RootVersionsManifest>(File.ReadAllText(mainDir + "\\version_manifest.json")); } catch (Exception ex) { MessageBox.Show(string.Format(Messages.ERROR_READ_FILE_F, "version_manifest.json") + ex.Message, Messages.CAPTION_COMMON); return(false); } ManifestVersion versionManifest = null; try { versionManifest = versionsManifest.versions.Where(x => x.id == downloadVersion).First(); if (versionManifest == null) { MessageBox.Show(string.Format(Messages.ERROR_GET_VERSION_FROM_MANIFEST_F, downloadVersion), Messages.CAPTION_COMMON); return(false); } } catch (Exception ex) { MessageBox.Show(string.Format(Messages.ERROR_GET_VERSION_FROM_MANIFEST_F, downloadVersion) + ex.Message, Messages.CAPTION_COMMON); return(false); } try { using (StreamReader reader = new StreamReader(DownloadUtils.DownloadToStream(versionManifest.url))) { content = reader.ReadToEnd(); } } catch (Exception ex) { MessageBox.Show(Messages.ERROR_GET_VERSION_JSON + ex.Message, Messages.CAPTION_COMMON); return(false); } RootGameVersion versionJsnon = null; try { versionJsnon = JsonConvert.DeserializeObject <RootGameVersion>(content); } catch (Exception ex) { MessageBox.Show(string.Format(Messages.ERROR_READ_FILE_F, downloadVersion + ".json") + ex.Message, Messages.CAPTION_COMMON); return(false); } SetTotalBytesToDownload(versionJsnon); StartDownloadFilesFromQueue(Environment.ProcessorCount); Task getAssetsUrls = Task.Factory.StartNew(new Action(() => GetAssetsUrls(versionJsnon)), cancellationTokenSource.Token); getAssetsUrls.Start(); Task getLibrariesUrls = Task.Factory.StartNew(new Action(() => GetLibrariesUrls(versionJsnon)), cancellationTokenSource.Token); getLibrariesUrls.Start(); Task getMainFilesUrls = Task.Factory.StartNew(new Action(() => GetMainFilesUrls(versionJsnon, versionManifest.url)), cancellationTokenSource.Token); getMainFilesUrls.Start(); if (WaitTasks(new Task[] { getAssetsUrls, getLibrariesUrls, getMainFilesUrls }, -1)) { downloadQueue.Enqueue(null); if (WaitCurrentDownloadTasks(-1)) { downloaded = true; } } ResetTempVariables(); GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); return(downloaded); }
private void GetAssetsUrls(RootGameVersion versionJsnon) { HashSet <string> savePaths = null; string content = ""; string assetsDirPath = ""; string assetUrl = ""; string mainFileHash = ""; ulong totalBytesToDownload = 0; string mainDir = SettingsControl.MainDirectory; try { using (StreamReader reader = new StreamReader(DownloadUtils.DownloadToStream(versionJsnon.assetIndex.url))) { content = reader.ReadToEnd(); } } catch (Exception ex) { MessageBox.Show(Messages.ERROR_GET_ASSETS_URLS + ex.Message, Messages.CAPTION_COMMON); throw ex; } savePaths = new HashSet <string>(); if (versionJsnon.assets == "legacy") { savePaths.Add(mainDir + "\\assets\\indexes\\legacy.json"); } else { savePaths.Add(mainDir + "\\assets\\indexes\\" + versionJsnon.assets + ".json"); } downloadQueue.Enqueue(new DownloadFileInfo(savePaths, versionJsnon.assetIndex.url, versionJsnon.assetIndex.size)); if (versionJsnon.assets == "legacy") { assetsDirPath = mainDir + "\\assets\\virtual\\legacy\\"; } else { assetsDirPath = mainDir + "\\assets\\objects\\"; } var assetsInfo = JsonConvert.DeserializeObject <RootAssets>(content).objects.ToList(); for (int x = assetsInfo.Count - 1; x >= 0; x--) { savePaths = new HashSet <string>(); mainFileHash = assetsInfo[x].Value.hash; totalBytesToDownload = 0; assetUrl = Constants.URL_ASSETS + mainFileHash.Substring(0, 2) + "/" + mainFileHash; for (int i = x; i >= 0; i--) { if (assetsInfo[i].Value.hash == mainFileHash) { totalBytesToDownload += assetsInfo[i].Value.size; savePaths.Add(GetAssetSavePath(assetsInfo[i], versionJsnon.assetIndex.id, assetsDirPath)); assetsInfo.RemoveAt(i); } } x = assetsInfo.Count - 1; downloadQueue.Enqueue(new DownloadFileInfo(savePaths, assetUrl, totalBytesToDownload)); } }
private List <KeyValuePair <KeyValuePair <string, string>, string> > GetLibrariesPath(RootGameVersion versionJson) { List <KeyValuePair <KeyValuePair <string, string>, string> > libraries = new List <KeyValuePair <KeyValuePair <string, string>, string> >(50); KeyValuePair <string, string> libraryInfo; HashSet <string> filePaths = null; bool skipLibrary = false; string mainDir = SettingsControl.MainDirectory; try { foreach (var item in versionJson.libraries) { skipLibrary = false; if (item.rules != null) { foreach (var rule in item.rules) { if (rule.action != null && rule.action == "allow" && rule.os != null && rule.os.name != "windows") { skipLibrary = true; } else if (rule.action != null && rule.action == "disallow" && rule.os != null && rule.os.name == "windows") { skipLibrary = true; } } } if (skipLibrary) { continue; } if (item.downloads != null) { if (item.downloads.artifact != null) { libraryInfo = CommonUtils.LibraryFullNameToInfoConvert(item.name + "_A"); libraries.Add(new KeyValuePair <KeyValuePair <string, string>, string>(libraryInfo, mainDir + "\\libraries\\" + GetLibraryPath(item.downloads.artifact, item.name))); } } else { libraryInfo = CommonUtils.LibraryFullNameToInfoConvert(item.name + "_A"); string convertedName = CommonUtils.LibraryFullNameToPathConvert(item.name); string libraryPath = mainDir + "\\libraries\\" + convertedName; string url = ""; libraries.Add(new KeyValuePair <KeyValuePair <string, string>, string>(libraryInfo, libraryPath)); if (!File.Exists(libraryPath)) { if (item.url != null && item.name.Contains("forge")) { convertedName = convertedName.Insert(convertedName.Length - 4, "-universal"); url = item.url + convertedName.Replace('\\', '/'); } else if (item.url != null) { url = item.url + convertedName.Replace('\\', '/'); } else { url = Constants.URL_LIBRARIES + convertedName.Replace('\\', '/'); } filePaths = new HashSet <string>(); filePaths.Add(libraryPath); downloadQueue.Enqueue(new DownloadFileInfo(filePaths, url, 0)); } } } } catch (Exception ex) { MessageBox.Show(Messages.ERROR_GET_LIBRARIES + ex.Message, Messages.CAPTION_COMMON); cancellationTokenSource.Cancel(); } return(libraries); }
public string GetStartString(string version) { ResetTempVariables(); StringBuilder startParams = new StringBuilder(5000); List <KeyValuePair <KeyValuePair <string, string>, string> > libraries = new List <KeyValuePair <KeyValuePair <string, string>, string> >(50); List <KeyValuePair <KeyValuePair <string, string>, string> > tempInfo; List <string> minecraftArguments = new List <string>(10); string startParamsString = ""; string mainClass = ""; string assets = ""; string nativesPath = ""; string usedNatives = ""; char quote = '"'; ExtractionResult extrResult; string mainDir = SettingsControl.MainDirectory; var coresCount = Environment.ProcessorCount; if (coresCount == 1 || coresCount == 2) { StartDownloadFilesFromQueue(1); } else { StartDownloadFilesFromQueue(coresCount - 1); } RootGameVersion versionJson = null; do { try { versionJson = JsonConvert.DeserializeObject <RootGameVersion>(File.ReadAllText(mainDir + "\\versions\\" + version + "\\" + version + ".json")); if (versionJson == null) { throw new Exception("Can't read data in " + mainDir + "\\versions\\" + version + "\\" + version + ".json"); } } catch (Exception ex) { MessageBox.Show(string.Format(Messages.ERROR_READ_FILE_F, version + ".json") + ex.Message, Messages.CAPTION_COMMON); return(""); } foreach (var item in GetLibrariesPath(versionJson)) { libraries.Add(new KeyValuePair <KeyValuePair <string, string>, string>(item.Key, item.Value)); } nativesPath = mainDir + "\\versions\\" + versionJson.id + "\\" + versionJson.id + "-natives"; extrResult = ExtractNatives(versionJson, nativesPath); if (extrResult == ExtractionResult.Successfully) { usedNatives = versionJson.id; } else if (extrResult == ExtractionResult.Error) { return(""); } if (versionJson.minecraftArguments != null) { minecraftArguments.AddRange(versionJson.minecraftArguments.Split(new string[] { "--" }, StringSplitOptions.RemoveEmptyEntries)); } if (mainClass == "" && versionJson.mainClass != null) { mainClass = versionJson.mainClass; } if (assets == "" && versionJson.assets != null) { assets = versionJson.assets; } if (versionJson.inheritsFrom == null) { break; } version = versionJson.inheritsFrom; } while (true); downloadQueue.Enqueue(null); if (!WaitCurrentDownloadTasks(-1)) { ResetTempVariables(); return(""); } ResetTempVariables(); libraries = libraries.OrderBy(x => x.Key.Key).ThenBy(x => x.Key.Value).ToList(); for (int x = libraries.Count() - 1; x >= 0; x--) { tempInfo = libraries.Where(z => z.Key.Key == libraries[x].Key.Key).ToList(); if (tempInfo.Count == 1) { startParams.Append(libraries[x].Value); startParams.Append(";"); } else { startParams.Append(libraries[x].Value); startParams.Append(";"); x -= tempInfo.Count - 1; } } for (int x = 0; x < minecraftArguments.Count; x++) { minecraftArguments[x] = minecraftArguments[x].Trim(); } startParamsString = startParams.ToString(); startParams.Clear(); startParams.Append("-Djava.library.path=\""); startParams.Append(mainDir); startParams.Append("\\versions\\"); startParams.Append(usedNatives); startParams.Append("\\"); startParams.Append(usedNatives); startParams.Append("-natives\" -cp \""); startParams.Append(startParamsString); startParams.Append(mainDir); startParams.Append("\\versions\\"); startParams.Append(version); startParams.Append("\\"); startParams.Append(version); startParams.Append(".jar\" "); startParams.Append(mainClass); startParams.Append(" --"); startParams.Append(string.Join(" --", minecraftArguments.Distinct())); startParams.Replace("${game_directory}", quote + mainDir + quote); if (assets == "legacy") { startParams.Replace("${game_assets}", quote + mainDir + "\\assets\\virtual\\legacy\""); } else { startParams.Replace("${assets_index_name}", quote + assets + quote); startParams.Replace("${assets_root}", quote + mainDir + "\\assets\""); } startParams.Replace(" --versionType ${version_type}", ""); return(startParams.ToString()); }
private ExtractionResult ExtractNatives(RootGameVersion versionJson, string extractionPath) { ExtractionResult result = ExtractionResult.NothingToExtract; bool skipLibrary = false; string deletePath = ""; string mainDir = SettingsControl.MainDirectory; foreach (var item in versionJson.libraries) { skipLibrary = false; if (item.rules != null) { foreach (var rule in item.rules) { if (rule.action != null && rule.action == "allow" && rule.os != null && rule.os.name != "windows") { skipLibrary = true; } else if (rule.action != null && rule.action == "disallow" && rule.os != null && rule.os.name == "windows") { skipLibrary = true; } } } if (skipLibrary) { continue; } if (item.extract != null) { if (item.downloads.classifiers != null) { try { foreach (var classifier in item.downloads.classifiers) { if (classifier.Key.Contains("natives-windows")) { if (Constants.IS_64_BIT && classifier.Key == "natives-windows-64") { CommonUtils.ExtractZip(mainDir + "\\libraries\\" + GetLibraryPath(classifier.Value, item.name), extractionPath, false); result = ExtractionResult.Successfully; } else if (!Constants.IS_64_BIT && classifier.Key == "natives-windows-32") { CommonUtils.ExtractZip(mainDir + "\\libraries\\" + GetLibraryPath(classifier.Value, item.name), extractionPath, false); result = ExtractionResult.Successfully; } else if (classifier.Key == "natives-windows") { CommonUtils.ExtractZip(mainDir + "\\libraries\\" + GetLibraryPath(classifier.Value, item.name), extractionPath, false); result = ExtractionResult.Successfully; } } } } catch (Exception ex) { MessageBox.Show(string.Format(Messages.ERROR_EXTRACTION_FILES_F, extractionPath) + ex.Message, Messages.CAPTION_COMMON); return(ExtractionResult.Error); } if (item.extract.exclude != null && (item.downloads.classifiers != null || item.downloads.artifact != null)) { foreach (var excludeItem in item.extract.exclude) { if (excludeItem[excludeItem.Length - 1] == '/') { deletePath = extractionPath + "\\" + excludeItem.Replace("/", "\\"); if (Directory.Exists(deletePath)) { try { Directory.Delete(deletePath, true); } catch (Exception ex) { MessageBox.Show(string.Format(Messages.ERROR_DELETE_FOLDER_F, deletePath) + ex.Message, Messages.CAPTION_COMMON); } } } else { if (excludeItem.Contains("/")) { deletePath = extractionPath + "\\" + excludeItem.Replace("/", "\\"); } else { deletePath = extractionPath + "\\" + excludeItem; } if (File.Exists(deletePath)) { try { File.Delete(extractionPath + "\\" + excludeItem); } catch (Exception ex) { MessageBox.Show(string.Format(Messages.ERROR_DELETE_FILE_F, extractionPath + "\\" + excludeItem) + ex.Message, Messages.CAPTION_COMMON); } } } } } } } } return(result); }
protected void SetTotalBytesToDownload(RootGameVersion versionJsnon) { totalBytesToDownload = 0; if (versionJsnon.downloads.client != null && string.IsNullOrWhiteSpace(versionJsnon.downloads.client.url) && versionJsnon.downloads.client.size > 0) { totalBytesToDownload += versionJsnon.downloads.client.size; } if (versionJsnon.downloads.server != null && string.IsNullOrWhiteSpace(versionJsnon.downloads.server.url) && versionJsnon.downloads.server.size > 0) { totalBytesToDownload += versionJsnon.downloads.server.size; } if (versionJsnon.downloads.windows_server != null && string.IsNullOrWhiteSpace(versionJsnon.downloads.windows_server.url) && versionJsnon.downloads.windows_server.size > 0) { totalBytesToDownload += versionJsnon.downloads.windows_server.size; } if (versionJsnon.assetIndex != null && string.IsNullOrWhiteSpace(versionJsnon.assetIndex.url) && versionJsnon.assetIndex.totalSize > 0) { totalBytesToDownload += versionJsnon.assetIndex.totalSize; } bool skipLibrary = false; var librariesList = versionJsnon.libraries; if (versionJsnon.libraries != null) { for (int x = librariesList.Count - 1; x >= 0; x--) { skipLibrary = false; if (librariesList[x].rules != null) { foreach (var rule in librariesList[x].rules) { if (rule.action != null && rule.action == "allow" && rule.os != null && rule.os.name != "windows") { skipLibrary = true; break; } else if (rule.action != null && rule.action == "disallow" && rule.os != null && rule.os.name == "windows") { skipLibrary = true; break; } } } if (skipLibrary) { continue; } if (librariesList[x].downloads.artifact != null && librariesList[x].downloads.artifact.size > 0) { totalBytesToDownload += librariesList[x].downloads.artifact.size; } if (librariesList[x].downloads.classifiers != null) { foreach (var classifier in librariesList[x].downloads.classifiers) { if (classifier.Key.Contains("natives-windows")) { if (Constants.IS_64_BIT && classifier.Key == "natives-windows-64" && classifier.Value.size > 0) { totalBytesToDownload += classifier.Value.size; } else if (!Constants.IS_64_BIT && classifier.Key == "natives-windows-32" && classifier.Value.size > 0) { totalBytesToDownload += classifier.Value.size; } else if (classifier.Value.size > 0) { totalBytesToDownload += classifier.Value.size; } } } } } } totalBytesToDownload *= 8; }