/// <summary> /// 获取特定Mod信息 /// </summary> /// <param name="name">欲获取Mod的名称</param> /// <param name="shouldHave">应该存在,若为真则产生报错信息</param> /// <returns>返回ModInformation</returns> public ModInformation GetModInformation(string name, bool shouldHave) { try { XmlDocument xml = new XmlDocument(); xml.Load(PathOperation.GetModsIndexPath()); string time = xml.SelectSingleNode("mods/mod_" + name + "/time").InnerText; string size = xml.SelectSingleNode("mods/mod_" + name + "/size").InnerText; List <string> contain = new List <string>(); XmlNodeList fileNodes = xml.SelectNodes("mods/mod_" + name + "/files/file"); foreach (XmlNode node in fileNodes) { contain.Add(node.InnerText); } ModInformation information = new ModInformation(name, time, size, contain); return(information); } catch (Exception e) { if (shouldHave) { ExceptionHandler.ShowError(e, "获取 " + name + " 信息出错"); } return(null); } }
/// <summary> /// 删除Mod /// </summary> /// <param name="name">欲删除Mod的文件名</param> /// <returns>成功则返回真</returns> public bool DeleteMod(string name) { List <ReplacedInformation> informations = GetReplacedFiles(); List <string> installedFilesPaths = new List <string>(); foreach (ReplacedInformation i in informations) { if (i.name == name) { installedFilesPaths.Add(i.path); } } if (installedFilesPaths.Count > 0) { string warningText = "该Mod的部分文件已经被安装" + Environment.NewLine; foreach (string path in installedFilesPaths) { warningText += path + Environment.NewLine; } warningText += "是否仍要删除该Mod与已安装文件?"; DialogResult result = MessageBox.Show(warningText, "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (result == DialogResult.No) { return(true); } } try { File.Delete(PathOperation.GetModsStorePath() + "\\" + name + ".mod"); //删除Index中的节点 XmlDocument xml = new XmlDocument(); xml.Load(PathOperation.GetModsIndexPath()); XmlNode node = xml.SelectSingleNode("mods/mod_" + name); if (node != null) { xml.DocumentElement.RemoveChild(node); } xml.Save(PathOperation.GetModsIndexPath()); //删除已替换文件 UnInstallFiles(installedFilesPaths, false); return(true); } catch (Exception e) { ExceptionHandler.ShowError(e, "无法删除Mod"); return(false); } }
private void InitNewMod(string sourcePath) { string name = Path.GetFileNameWithoutExtension(sourcePath); XmlDocument xml = new XmlDocument(); xml.Load(PathOperation.GetModsIndexPath()); XmlElement basic = xml.CreateElement("mod_" + name); XmlElement size = xml.CreateElement("size"); XmlElement time = xml.CreateElement("time"); //文件大小 FileInfo fileInfo = new FileInfo(sourcePath); size.InnerText = ((float)fileInfo.Length / 1024 / 1024).ToString("0.##"); basic.AppendChild(size); //文件时间 time.InnerText = fileInfo.CreationTime.ToString(); basic.AppendChild(time); //文件列表 XmlElement fileBasic = xml.CreateElement("files"); List <string> fileList = InitNewModFiles(sourcePath); if (fileList.Count == 0) { MessageBox.Show("这不是只狼Mod文件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); DeleteMod(name); return; } foreach (string path in fileList) { XmlElement file = xml.CreateElement("file"); file.InnerText = path; fileBasic.AppendChild(file); } basic.AppendChild(fileBasic); xml.DocumentElement.AppendChild(basic); xml.Save(PathOperation.GetModsIndexPath()); }