/// <summary> /// Loads a Mod from a Manifest /// </summary> /// <param name="uri">the URI where the Manifest lives</param> /// <returns>a Mod</returns> public static Mod LoadFromUrl(string uri) { Mod ret = new Mod(); XDocument manifest = XDocument.Load(uri); if (manifest.Root.Name != Namespace.GetName("manifest")) { throw new Exception("That's not a McModManager manifest"); } ret.Name = manifest.Root.Element(Namespace.GetName("name")).Value; ret.Id = manifest.Root.Element(Namespace.GetName("id")); if (ret.Id.Version != null) { throw new Exception("Manifest IDs cannot include versions"); } if (manifest.Root.Element(Namespace.GetName("url")) != null) { ret.Url = manifest.Root.Element(Namespace.GetName("url")).Value; } using (var dbConn = Database.GetConnection()) using (var tx = dbConn.BeginTransaction()) { ret.Save(dbConn, tx); foreach (var ver in manifest.Root.Element(Namespace.GetName("versions")).Elements(Namespace.GetName("version"))) { var v = ModVersion.LoadVersion(ret, ver, dbConn, tx); v.Save(dbConn, tx); } tx.Commit(); } return ret; }
/// <summary> /// Creates a ModVersion from an XML manifest node /// </summary> /// <param name="parent">the Mod that owns this version</param> /// <param name="ver">the XML from which to create the node</param> /// <param name="dbConn">The database connection</param> /// <param name="tx">The Transaction scope</param> /// <returns>a ModVersion</returns> internal static ModVersion LoadVersion(Mod parent, XElement ver, IDbConnection dbConn, IDbTransaction tx) { var ret = new ModVersion(); var ns = Mod.Namespace; ret.ParentId = parent.Id; ret.Url = ver.Element(ns.GetName("url")).Value; ret.Ver = ver.Element(ns.GetName("ver")).Value; if (ver.Element(ns.GetName("hash")) != null) { ret.Hash = ver.Element(ns.GetName("hash")).Value.ToLower(); } var packing = ver.Element(ns.GetName("packing")).Value.ToLower(); ret.Packing = (PackingType)Enum.Parse(typeof(PackingType), packing); ret.Url = UrlHelpers.GetRealUrl(ret.Url); Uri temp = new Uri(ret.Url); ret.FileName = Path.GetFileName(temp.AbsolutePath); if (string.IsNullOrEmpty(ret.FileName)) { ret.FileName = "mod.zip"; } ret.Save(dbConn, tx); if (ver.Element(ns.GetName("depends")) != null) { foreach (var dep in ver.Element(ns.GetName("depends")).Elements(ns.GetName("depend")).Select(d => ID.Parse(d))) { dbConn.Execute("REPLACE INTO moddependency (modid, version, depmodid, depversion) VALUES (@Id, @Ver, @DepId, @DepVer)", new { Id = (string)ret.ParentId, ret.Ver, DepId = dep.Root + ":" + dep.Value, DepVer = dep.Version }, tx); } } return ret; }