예제 #1
0
        public bool UnInstallFiles(List <string> paths, bool needWarn)
        {
            if (needWarn)
            {
                string warningText;
                warningText = "确认要卸载以下文件?" + Environment.NewLine;
                foreach (string path in paths)
                {
                    warningText += path + Environment.NewLine;
                }

                DialogResult result = MessageBox.Show(warningText, "警告",
                                                      MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (result == DialogResult.No)
                {
                    return(true);
                }
            }
            try
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(PathOperation.GetReplacedFilePath());

                foreach (string path in paths)
                {
                    foreach (XmlNode node in xml.SelectNodes("replaced/file"))
                    {
                        Console.WriteLine(path);
                        if (path == node.InnerText)
                        {
                            xml.DocumentElement.RemoveChild(node);
                            break;
                        }
                    }
                    File.Delete(PathOperation.GetGamePath(false) + "\\mods\\" + path);
                }
                xml.Save(PathOperation.GetReplacedFilePath());
                return(true);
            } catch (Exception e)
            {
                ExceptionHandler.ShowError(e, "卸载所选文件出错");
                return(false);
            }
        }
예제 #2
0
        /// <summary>
        /// 安装所给文件
        /// </summary>
        /// <param name="name">欲安装文件的Mod名称</param>
        /// <param name="paths">欲安装的文件列表</param>
        /// <returns>若成功安装,返回真</returns>
        public bool InstallFiles(string name, List <string> paths)
        {
            XmlDocument xml = new XmlDocument();

            xml.Load(PathOperation.GetReplacedFilePath());
            if (paths.Count == 0)
            {
                return(true);
            }
            try
            {
                //检测冲突
                List <ReplacedInformation> replacedFiles = GetReplacedFiles();
                List <ReplacedInformation> conflictFiles = new List <ReplacedInformation>();
                foreach (ReplacedInformation i in replacedFiles)
                {
                    foreach (string path in paths)
                    {
                        if (path == i.path)
                        {
                            conflictFiles.Add(i);
                            break;
                        }
                    }
                }
                //若有冲突,弹出警告
                if (conflictFiles.Count > 0)
                {
                    string warningText = "以下Mod文件有冲突:" + Environment.NewLine;
                    foreach (ReplacedInformation i in conflictFiles)
                    {
                        warningText += i.name + " 中的 " + i.path + Environment.NewLine;
                    }
                    warningText += "是否要覆盖上述文件?";
                    DialogResult result = MessageBox.Show(warningText, "警告",
                                                          MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (result == DialogResult.No)
                    {
                        return(true);
                    }
                    //若选择继续覆盖,删除原有文件及XML信息
                    foreach (ReplacedInformation i in conflictFiles)
                    {
                        File.Delete(PathOperation.GetGamePath(false) + "\\mods\\" + i.path);
                        foreach (XmlNode node in xml.SelectNodes("replaced/file"))
                        {
                            if (node.InnerText == i.path)
                            {
                                xml.DocumentElement.RemoveChild(node);
                                break;
                            }
                        }
                    }
                }


                //保存配置文件
                foreach (string path in paths)
                {
                    XmlElement ele = xml.CreateElement("file");
                    ele.InnerText = path;
                    ele.SetAttribute("mod", name);
                    xml.DocumentElement.AppendChild(ele);
                }
                xml.Save(PathOperation.GetReplacedFilePath());
                //解压文件
                string targetPath = PathOperation.GetGamePath(false) + @"\mods";
                string sourcePath = PathOperation.GetModsStorePath() + "\\" + name + ".mod";
                //解压至临时目录
                string tempPath = Path.GetTempPath() + "smm";
                if (Directory.Exists(tempPath))
                {
                    Directory.Delete(tempPath, true);
                }
                ZipFile.ExtractToDirectory(sourcePath, tempPath);
                Console.WriteLine("已经 " + sourcePath + "解压至" + tempPath);
                foreach (string path in paths)
                {
                    if (!Directory.Exists(targetPath + "\\" + Path.GetDirectoryName(path)))
                    {
                        Directory.CreateDirectory(targetPath + "\\" + Path.GetDirectoryName(path));
                    }
                    File.Copy(tempPath + "\\" + path, targetPath + "\\" + path);
                }
                Directory.Delete(tempPath, true);
                MessageBox.Show("所选文件已安装成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return(true);
            } catch (Exception e)
            {
                ExceptionHandler.ShowError(e, "安装文件出现错误");
                return(false);
            }
        }