/// <summary> /// Copies the contents of the "packed" folder into the game mod directory /// </summary> public void InstallMod() { var activeMod = _projectManager.ActiveProject; var logPath = Path.Combine(activeMod.ProjectDirectory, "install_log.xml"); try { //Check if we have installed this mod before. If so do a little cleanup. if (File.Exists(logPath)) { var log = XDocument.Load(logPath); var dirs = log.Root.Element("Files")?.Descendants("Directory").ToList(); if (dirs != null) { //Loop throught dirs and delete the old files in them. foreach (var d in dirs) { foreach (var f in d.Elements("file")) { if (File.Exists(f.Value)) { File.Delete(f.Value); Debug.WriteLine("File delete: " + f.Value); } } } //Delete the empty directories. foreach (var d in dirs) { if (d.Attribute("Path") != null && Directory.Exists(d.Attribute("Path").Value) && !Directory.GetFiles(d.Attribute("Path").Value, "*", SearchOption.AllDirectories).Any()) { Directory.Delete(d.Attribute("Path").Value, true); Debug.WriteLine("Directory delete: " + d.Attribute("Path").Value); } } } //Delete the old install log. We will make a new one so this is not needed anymore. File.Delete(logPath); } var installlog = new XDocument( new XElement("InstalLog", new XAttribute("Project", activeMod.Name), new XAttribute("Build_date", DateTime.Now.ToString()) )); var fileroot = new XElement("Files"); //Copy and log the files. var packedmoddir = activeMod.PackedRootDirectory; if (!Directory.Exists(packedmoddir)) { _loggerService.Error("Failed to install the mod! The packed directory doesn't exist!"); return; } fileroot.Add(Commonfunctions.DirectoryCopy(packedmoddir, _settingsManager.GetRED4GameRootDir(), true)); //var packeddlcdir = Path.Combine(ActiveMod.ProjectDirectory, "packed", "DLC"); //if (Directory.Exists(packeddlcdir)) // fileroot.Add(Commonfunctions.DirectoryCopy(packeddlcdir, MainController.Get().Configuration.CP77GameDlcDir, true)); installlog.Root.Add(fileroot); installlog.Save(logPath); _loggerService.Success($"{activeMod.Name} installed!"); _notificationService.Success($"{activeMod.Name} installed!"); } catch (Exception ex) { //If we screwed up something. Log it. _loggerService.Error(ex); } }
private static void InstallMod() { var ActiveMod = MainController.Get().ActiveMod; var _logger = ServiceLocator.Default.ResolveType <ILoggerService>(); try { //Check if we have installed this mod before. If so do a little cleanup. if (File.Exists(ActiveMod.ProjectDirectory + "\\install_log.xml")) { XDocument log = XDocument.Load(ActiveMod.ProjectDirectory + "\\install_log.xml"); var dirs = log.Root.Element("Files")?.Descendants("Directory"); if (dirs != null) { //Loop throught dirs and delete the old files in them. foreach (var d in dirs) { foreach (var f in d.Elements("file")) { if (File.Exists(f.Value)) { File.Delete(f.Value); Debug.WriteLine("File delete: " + f.Value); } } } //Delete the empty directories. foreach (var d in dirs) { if (d.Attribute("Path") != null) { if (Directory.Exists(d.Attribute("Path").Value)) { if (!(Directory.GetFiles(d.Attribute("Path").Value, "*", SearchOption.AllDirectories).Any())) { Directory.Delete(d.Attribute("Path").Value, true); Debug.WriteLine("Directory delete: " + d.Attribute("Path").Value); } } } } } //Delete the old install log. We will make a new one so this is not needed anymore. File.Delete(ActiveMod.ProjectDirectory + "\\install_log.xml"); } var installlog = new XDocument(new XElement("InstalLog", new XAttribute("Project", ActiveMod.Name), new XAttribute("Build_date", DateTime.Now.ToString()))); var fileroot = new XElement("Files"); //Copy and log the files. if (!Directory.Exists(Path.Combine(ActiveMod.ProjectDirectory, "packed"))) { _logger.LogString("Failed to install the mod! The packed directory doesn't exist! You forgot to tick any of the packing options?", Logtype.Important); return; } var packedmoddir = Path.Combine(ActiveMod.ProjectDirectory, "packed", "Mods"); if (Directory.Exists(packedmoddir)) { fileroot.Add(Commonfunctions.DirectoryCopy(packedmoddir, MainController.Get().Configuration.GameModDir, true)); } var packeddlcdir = Path.Combine(ActiveMod.ProjectDirectory, "packed", "DLC"); if (Directory.Exists(packeddlcdir)) { fileroot.Add(Commonfunctions.DirectoryCopy(packeddlcdir, MainController.Get().Configuration.GameDlcDir, true)); } installlog.Root.Add(fileroot); //Save the log. installlog.Save(ActiveMod.ProjectDirectory + "\\install_log.xml"); _logger.LogString(ActiveMod.Name + " installed!" + "\n", Logtype.Success); } catch (Exception ex) { //If we screwed up something. Log it. _logger.LogString(ex.ToString() + "\n", Logtype.Error); } }