public bool MarkInstalled(ModMetadata meta, ModVersion version, ModDownloadResult downloadDetails) { ModStatus status = GetModStatus(meta); switch (status) { case ModStatus.Installed: ModVersions.Delete(m => m.MetadataId == meta.Id); ModVersions.Insert(version); return(true); case ModStatus.NotInstalled: Mods.Insert(meta); ModVersions.Insert(version); return(true); // Already added as requested, need to modify it case ModStatus.Requested: ModVersion requested = ModVersions.FindOne(v => v.GetModVersion() == version.GetModVersion()); requested.Filename = downloadDetails.Filename; requested.Checksum = downloadDetails.Checksum; return(ModVersions.Update(requested)); } return(false); }
/// <summary> /// Download a specific version of a mod. /// </summary> /// <exception cref="ModDownloadException"></exception> /// <exception cref="IOException"></exception> /// <returns></returns> public async Task <ModDownloadResult> DownloadMod(ModVersion version, string directory) { CurseforgeModVersion versionInfo; if (version is CurseforgeModVersion) { versionInfo = version as CurseforgeModVersion; } else { throw new ArgumentException("Version provided is not a curseforge mod version, cannot continue."); } // If we get here, the installed version was not found or corrupted if (versionInfo.FileId == null) { throw new ModDownloadException("Error during download: Mod metadata has not been fetched from Curseforge yet."); } if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } #region Perform Mod Download try { HttpWebRequest webRequest = WebRequest.CreateHttp(new Uri(versionInfo.DownloadURL + "/file")); using (WebResponse r = await webRequest.GetResponseAsync()) { Uri downloadUri = r.ResponseUri; if (!FILENAME_MATCH.IsMatch(downloadUri.AbsoluteUri)) { throw new ModDownloadException("The provided download URL does not appear to be valid."); } Match m = FILENAME_MATCH.Match(downloadUri.AbsoluteUri); string filename = m.Groups[1].Value; if (filename.ToLowerInvariant() == "download") { throw new ModDownloadException("Bad filename for a mod from Curseforge; got 'download' instead of filename."); } string finalFilename = Path.Combine(directory, filename); if (File.Exists(finalFilename)) { File.Delete(finalFilename); } FileStream fs = File.OpenWrite(finalFilename); byte[] buffer = new byte[1024]; using (Stream s = r.GetResponseStream()) { int size = s.Read(buffer, 0, buffer.Length); while (size > 0) { fs.Write(buffer, 0, size); size = s.Read(buffer, 0, buffer.Length); } fs.Flush(); fs.Close(); ModDownloadResult result = new ModDownloadResult(); result.Filename = filename; result.Checksum = ModUtilities.GetFileChecksum(finalFilename); return(result); } } } catch (Exception) { throw; } #endregion }
private static void DownloadPack() { using (Pack p = Modifi.DefaultPack) { ILogger log = Modifi.DefaultLogger; log.Information("Downloading modpack."); log.Information(Environment.NewLine); IDomain curseforge; try { curseforge = DomainHelper.LoadDomain(p, "curseforge"); } catch (DllNotFoundException) { log.Error("Cannot install mods; curseforge domain handler not found."); return; } IDomainHandler handler = curseforge.GetDomainHandler(); using (ModStorage storage = new ModStorage(p.Installed, curseforge)) { IEnumerable <ModMetadata> mods = storage.GetAllMods(); foreach (ModMetadata mod in mods) { log.Information("Installing: {0:l}", mod.GetName()); ModStatus status; try { status = storage.GetModStatus(mod); } catch (Exception) { log.Error("Error: Mod marked installed but checksum did not match."); log.Error("Please use the remove command and re-add it, or download the version manually."); continue; } switch (status) { case ModStatus.Installed: log.Information("Skipping, already installed."); log.Information(Environment.NewLine); continue; case ModStatus.Requested: ModVersion version = storage.GetMod(mod); log.Information("Requested Version: {0:l} ({1})", version.GetVersionName(), version.GetModVersion()); try { ModDownloadResult result = handler.DownloadMod(version, Settings.ModPath).Result; storage.MarkInstalled(mod, version, result); log.Information("Downloaded to {0}.", result.Filename); log.Information(Environment.NewLine); } catch (Exception e) { log.Error(e.Message); log.Error(Environment.NewLine); } break; } } } } }