/// <summary> /// Sets the specified value in the specified Ini file to the given value. /// </summary> /// <remarks> /// This method writes the given value in the specified Ini value, if it is owned /// by the mod being upgraded. If the specified Ini edit is not owned by the mod /// being upgraded, the Ini edit is archived in the install log. /// /// If the Ini edit was not previously installed by the mod, then the normal install /// rules apply, including confirming overwrite if applicable. /// </remarks> /// <param name="p_strSettingsFileName">The name of the settings file to edit.</param> /// <param name="p_strSection">The section in the Ini file to edit.</param> /// <param name="p_strKey">The key in the Ini file to edit.</param> /// <param name="p_strValue">The value to which to set the key.</param> /// <returns><c>true</c> if the value was set; <c>false</c> /// if the user chose not to overwrite the existing value.</returns> public override bool EditIni(string p_strSettingsFileName, string p_strSection, string p_strKey, string p_strValue) { IList <IMod> lstInstallers = InstallLog.GetIniEditInstallers(p_strSettingsFileName, p_strSection, p_strKey); if (lstInstallers.Contains(Mod, ModComparer.Filename)) { if (!ModComparer.Filename.Equals(lstInstallers[lstInstallers.Count - 1], Mod)) { InstallLog.ReplaceIniEdit(Mod, p_strSettingsFileName, p_strSection, p_strKey, p_strValue); } else { if (!TouchedFiles.Contains(p_strSettingsFileName)) { TouchedFiles.Add(p_strSettingsFileName); TransactionalFileManager.Snapshot(p_strSettingsFileName); } IniMethods.WritePrivateProfileString(p_strSection, p_strKey, p_strValue, p_strSettingsFileName); } IniEdit iniEdit = new IniEdit(p_strSettingsFileName, p_strSection, p_strKey); OriginallyInstalledEdits.Remove(iniEdit); return(true); } return(base.EditIni(p_strSettingsFileName, p_strSection, p_strKey, p_strValue)); }
/// <summary> /// Adds the given original Ini value to the mod install log. /// </summary> /// <remarks> /// This backs up an original Ini value we are overwriting. /// </remarks> /// <param name="p_strFile">The Ini file that was edited.</param> /// <param name="p_strSection">The section in the Ini file that was edited.</param> /// <param name="p_strKey">The key in the Ini file that was edited.</param> /// <param name="p_strValue">The original value of the edited key.</param> internal void BackupOriginalIniValue(string p_strFile, string p_strSection, string p_strKey, string p_strValue) { var strLoweredFile = p_strFile.ToLowerInvariant(); var strLoweredSection = p_strSection.ToLowerInvariant(); var strLoweredKey = p_strKey.ToLowerInvariant(); var iniEdit = new IniEdit(strLoweredFile, strLoweredSection, strLoweredKey); var intIndex = ReplacedOriginalIniValues.IndexOf(iniEdit); if (intIndex == -1) { ReplacedOriginalIniValues.Add(iniEdit); } else { iniEdit = ReplacedOriginalIniValues[intIndex]; } iniEdit.Value = p_strValue; }
/// <summary> /// Adds the given Ini edit to the mod install log. /// </summary> /// <remarks> /// Adding an Ini edit to a mod's install log indicates that said edit was made /// as part of the mod. /// </remarks> /// <param name="p_strFile">The Ini file that was edited.</param> /// <param name="p_strSection">The section in the Ini file that was edited.</param> /// <param name="p_strKey">The key in the Ini file that was edited.</param> /// <param name="p_strValue">The value to which the key was set.</param> internal void AddIniEdit(string p_strFile, string p_strSection, string p_strKey, string p_strValue) { var strLoweredFile = p_strFile.ToLowerInvariant(); var strLoweredSection = p_strSection.ToLowerInvariant(); var strLoweredKey = p_strKey.ToLowerInvariant(); var iniEdit = new IniEdit(strLoweredFile, strLoweredSection, strLoweredKey); var intIndex = IniEdits.IndexOf(iniEdit); if (intIndex == -1) { IniEdits.Add(iniEdit); } else { iniEdit = IniEdits[intIndex]; } iniEdit.Value = p_strValue; }
/// <summary> /// Sets the specified value in the specified Ini file to the given value. /// </summary> /// <remarks> /// This method writes the given value in the specified Ini value, if it is owned /// by the mod being upgraded. If the specified Ini edit is not owned by the mod /// being upgraded, the Ini edit is archived in the install log. /// /// If the Ini edit was not previously installed by the mod, then the normal install /// rules apply, including confirming overwrite if applicable. /// </remarks> /// <param name="p_strSettingsFileName">The name of the settings file to edit.</param> /// <param name="p_strSection">The section in the Ini file to edit.</param> /// <param name="p_strKey">The key in the Ini file to edit.</param> /// <param name="p_strValue">The value to which to set the key.</param> /// <returns><c>true</c> if the value was set; <c>false</c> /// if the user chose not to overwrite the existing value.</returns> public override bool EditIni(string p_strSettingsFileName, string p_strSection, string p_strKey, string p_strValue) { IList<IMod> lstInstallers = InstallLog.GetIniEditInstallers(p_strSettingsFileName, p_strSection, p_strKey); if (lstInstallers.Contains(Mod, ModComparer.Filename)) { if (!ModComparer.Filename.Equals(lstInstallers[lstInstallers.Count - 1], Mod)) InstallLog.ReplaceIniEdit(Mod, p_strSettingsFileName, p_strSection, p_strKey, p_strValue); else { if (!TouchedFiles.Contains(p_strSettingsFileName)) { TouchedFiles.Add(p_strSettingsFileName); TransactionalFileManager.Snapshot(p_strSettingsFileName); } IniMethods.WritePrivateProfileString(p_strSection, p_strKey, p_strValue, p_strSettingsFileName); } IniEdit iniEdit = new IniEdit(p_strSettingsFileName, p_strSection, p_strKey); OriginallyInstalledEdits.Remove(iniEdit); return true; } return base.EditIni(p_strSettingsFileName, p_strSection, p_strKey, p_strValue); }
/// <summary> /// Gets all of the mods that have installed the specified Ini edit. /// </summary> /// <param name="p_strSettingsFileName">The Ini file containing the key whose installers are to be retrieved.</param> /// <param name="p_strSection">The section containing the key whose installers are to be retrieved.</param> /// <param name="p_strKey">The key whose installers are to be retrieved.</param> /// <returns>All of the mods that have installed the specified Ini edit.</returns> public IList<IMod> GetIniEditInstallers(string p_strSettingsFileName, string p_strSection, string p_strKey) { IniEdit iedEdit = new IniEdit(p_strSettingsFileName, p_strSection, p_strKey); InstallerStack<string> stkInstallers = GetCurrentIniEditInstallers(iedEdit); List<IMod> lstInstallers = new List<IMod>(); foreach (InstalledValue<string> ivlInstaller in stkInstallers) lstInstallers.Add(GetMod(ivlInstaller.InstallerKey)); return lstInstallers; }
/// <summary> /// Gets the value of the specified key before it was most recently overwritten. /// </summary> /// <param name="p_strSettingsFileName">The Ini file containing the key whose previous value is to be retrieved.</param> /// <param name="p_strSection">The section containing the key whose previous value is to be retrieved.</param> /// <param name="p_strKey">The key whose previous value is to be retrieved.</param> /// <returns>The value of the specified key before it was most recently overwritten, or /// <c>null</c> if there was no previous value.</returns> public string GetPreviousIniValue(string p_strSettingsFileName, string p_strSection, string p_strKey) { IniEdit iedEdit = new IniEdit(p_strSettingsFileName, p_strSection, p_strKey); InstallerStack<string> stkInstallers = GetCurrentIniEditInstallers(iedEdit); if (stkInstallers.Count < 2) return null; stkInstallers.Pop(); return stkInstallers.Peek().Value; }
/// <summary> /// Gets the mod that owns the specified INI edit. /// </summary> /// <param name="p_strSettingsFileName">The name of the edited INI file.</param> /// <param name="p_strSection">The section containting the INI edit.</param> /// <param name="p_strKey">The key of the edited INI value.</param> /// <returns>The mod that owns the specified INI edit.</returns> public IMod GetCurrentIniEditOwner(string p_strSettingsFileName, string p_strSection, string p_strKey) { IniEdit iedEdit = new IniEdit(p_strSettingsFileName, p_strSection, p_strKey); InstallerStack<string> stkInstallers = GetCurrentIniEditInstallers(iedEdit); if (stkInstallers.Count == 0) return null; return GetMod(stkInstallers.Peek().InstallerKey); }
/// <summary> /// Removes the specified ini edit as having been installed by the given mod. /// </summary> /// <param name="p_modUninstallingMod">The mod for which to remove the specified ini edit.</param> /// <param name="p_strSettingsFileName">The name of the edited INI file containing the INI edit being removed for the given mod.</param> /// <param name="p_strSection">The section containting the INI edit being removed for the given mod.</param> /// <param name="p_strKey">The key of the edited INI value whose edit is being removed for the given mod.</param> public void RemoveIniEdit(IMod p_modUninstallingMod, string p_strSettingsFileName, string p_strSection, string p_strKey) { string strUninstallingModKey = GetModKey(p_modUninstallingMod); if (String.IsNullOrEmpty(strUninstallingModKey)) return; IniEdit iedEdit = new IniEdit(p_strSettingsFileName, p_strSection, p_strKey); m_dicUninstalledIniEdits[iedEdit].Push(strUninstallingModKey, null); if (m_dicInstalledIniEdits.ContainsItem(iedEdit)) m_dicInstalledIniEdits[iedEdit].Remove(strUninstallingModKey); if (m_dicReplacedIniEdits.ContainsItem(iedEdit)) m_dicReplacedIniEdits[iedEdit].Remove(strUninstallingModKey); if (CurrentTransaction == null) Commit(); else Enlist(); }
/// <summary> /// Logs the specified INI edit as having been installed by the given mod. /// </summary> /// <param name="p_modInstallingMod">The mod installing the specified INI edit.</param> /// <param name="p_strSettingsFileName">The name of the edited INI file.</param> /// <param name="p_strSection">The section containing the INI edit.</param> /// <param name="p_strKey">The key of the edited INI value.</param> /// <param name="p_strValue">The value installed by the mod.</param> public void AddIniEdit(IMod p_modInstallingMod, string p_strSettingsFileName, string p_strSection, string p_strKey, string p_strValue) { string strInstallingModKey = AddActiveMod(p_modInstallingMod, false); IniEdit iedEdit = new IniEdit(p_strSettingsFileName, p_strSection, p_strKey); m_dicInstalledIniEdits[iedEdit].Push(strInstallingModKey, p_strValue); if (m_dicUninstalledIniEdits.ContainsItem(iedEdit)) m_dicUninstalledIniEdits[iedEdit].Remove(strInstallingModKey); if (CurrentTransaction == null) Commit(); else Enlist(); }
/// <summary> /// Gets the ordered list of mod that have installed the given ini edit. /// </summary> /// <param name="p_iedEdit">The ini edit for which to retrieve the list of installing mods.</param> /// <returns>The ordered list of mods that have installed the given ini edit.</returns> protected InstallerStack<string> GetCurrentIniEditInstallers(IniEdit p_iedEdit) { InstallerStack<string> stkInstallers = new InstallerStack<string>(); if (EnlistedInstallLog.m_dicInstalledIniEdits.ContainsItem(p_iedEdit)) stkInstallers.PushRange(EnlistedInstallLog.m_dicInstalledIniEdits[p_iedEdit]); if (m_dicInstalledIniEdits.ContainsItem(p_iedEdit)) foreach (InstalledValue<string> isvMod in m_dicInstalledIniEdits[p_iedEdit]) { stkInstallers.Remove(isvMod); stkInstallers.Push(isvMod); } if (m_dicReplacedIniEdits.ContainsItem(p_iedEdit)) foreach (InstalledValue<string> isvMod in m_dicReplacedIniEdits[p_iedEdit]) stkInstallers.Find(x => x.Equals(isvMod)).Value = isvMod.Value; if (m_dicUninstalledIniEdits.ContainsItem(p_iedEdit)) foreach (InstalledValue<string> isvMod in m_dicUninstalledIniEdits[p_iedEdit]) stkInstallers.Remove(isvMod); m_setRemovedModKeys.ForEach(x => stkInstallers.Remove(x)); return stkInstallers; }
/// <summary> /// Loads the data from the Install Log file. /// </summary> private void LoadInstallLog() { Trace.TraceInformation(String.Format("Path: {0}", LogPath)); if (!File.Exists(LogPath)) SaveInstallLog(); XDocument docLog = XDocument.Load(LogPath); Trace.TraceInformation("Loaded from XML."); string strLogVersion = docLog.Element("installLog").Attribute("fileVersion").Value; if (!CURRENT_VERSION.ToString().Equals(strLogVersion)) throw new Exception(String.Format("Invalid Install Log version: {0} Expecting {1}", strLogVersion, CURRENT_VERSION)); XElement xelModList = docLog.Descendants("modList").FirstOrDefault(); if (xelModList != null) { foreach (XElement xelMod in xelModList.Elements("mod")) { string strModPath = xelMod.Attribute("path").Value; Trace.Write("Found " + strModPath + "..."); if (OriginalValueMod.ModArchivePath.Equals(strModPath)) { m_amrModKeys.RegisterMod(OriginalValueMod, xelMod.Attribute("key").Value, true); Trace.WriteLine("OK"); } else if (ModManagerValueMod.ModArchivePath.Equals(strModPath)) { m_amrModKeys.RegisterMod(ModManagerValueMod, xelMod.Attribute("key").Value, true); Trace.WriteLine("OK"); } else { string strModName = xelMod.Element("name").Value; string strInstallDate = "<No Data>"; if (!(xelMod.Element("installDate") == null)) strInstallDate = xelMod.Element("installDate").Value; strModPath = Path.Combine(ModInstallDirectory, strModPath); XElement xelVersion = xelMod.Element("version"); string strVersion = xelVersion.Attribute("machineVersion").Value; Version verVersion = String.IsNullOrEmpty(strVersion) ? null : new Version(strVersion); strVersion = xelVersion.Value; IMod modMod = ManagedModRegistry.GetMod(strModPath) ?? new DummyMod(strModName, strModPath, verVersion, strVersion, "", strInstallDate); modMod.InstallDate = strInstallDate; try { m_amrModKeys.RegisterMod(modMod, xelMod.Attribute("key").Value); } catch (ArgumentException) { } if (modMod is DummyMod) Trace.WriteLine("Missing"); else Trace.WriteLine("OK"); } } } XElement xelFiles = docLog.Descendants("dataFiles").FirstOrDefault(); if (xelFiles != null) { foreach (XElement xelFile in xelFiles.Elements("file")) { string strPath = xelFile.Attribute("path").Value; foreach (XElement xelMod in xelFile.Descendants("mod")) m_dicInstalledFiles[strPath].Push(xelMod.Attribute("key").Value, null); } } XElement xelIniEdits = docLog.Descendants("iniEdits").FirstOrDefault(); if (xelIniEdits != null) { foreach (XElement xelIniEdit in xelIniEdits.Elements("ini")) { string strFile = xelIniEdit.Attribute("file").Value; string strSection = xelIniEdit.Attribute("section").Value; string strKey = xelIniEdit.Attribute("key").Value; IniEdit iniEntry = new IniEdit(strFile, strSection, strKey); foreach (XElement xelMod in xelIniEdit.Descendants("mod")) m_dicInstalledIniEdits[iniEntry].Push(xelMod.Attribute("key").Value, xelMod.Value); } } XElement xelGameSpecificValueEdits = docLog.Descendants("gameSpecificEdits").FirstOrDefault(); if (xelGameSpecificValueEdits != null) { foreach (XElement xelGameSpecificValueEdit in xelGameSpecificValueEdits.Elements("edit")) { string strKey = xelGameSpecificValueEdit.Attribute("key").Value; foreach (XElement xelMod in xelGameSpecificValueEdit.Descendants("mod")) m_dicInstalledGameSpecificValueEdits[strKey].Push(xelMod.Attribute("key").Value, Convert.FromBase64String(xelMod.Value)); } } }