/// <summary> /// Verifies if the given file can be written. /// </summary> /// <remarks> /// This method checks if the given path is valid. If so, and the file does not /// exist, the file can be written. If the file does exist, than the user is /// asked to overwrite the file. /// </remarks> /// <param name="p_strPath">The file path, relative to the Data folder, whose writability is to be verified.</param> /// <returns><c>true</c> if the location specified by <paramref name="p_strPath"/> /// can be written; <c>false</c> otherwise.</returns> protected bool TestDoOverwrite(string p_strPath) { string strDataPath = Path.Combine(GameModeInfo.InstallationPath, p_strPath); if (!File.Exists(strDataPath)) { return(true); } string strLoweredPath = strDataPath.ToLowerInvariant(); if (m_lstOverwriteFolders.Contains(Path.GetDirectoryName(strLoweredPath))) { return(true); } if (m_lstDontOverwriteFolders.Contains(Path.GetDirectoryName(strLoweredPath))) { return(false); } if (m_booOverwriteAll) { return(true); } if (m_booDontOverwriteAll) { return(false); } IMod modOld = InstallLog.GetCurrentFileOwner(p_strPath); if (modOld == Mod) { return(true); } string strMessage = null; if (modOld != null) { strMessage = String.Format("Data file '{{0}}' has already been installed by '{0}'" + Environment.NewLine + "Overwrite with this mod's file?", modOld.ModName); } else { strMessage = "Data file '{0}' already exists." + Environment.NewLine + "Overwrite with this mod's file?"; } switch (m_dlgOverwriteConfirmationDelegate(String.Format(strMessage, p_strPath), true, (modOld != null))) { case OverwriteResult.Yes: return(true); case OverwriteResult.No: return(false); case OverwriteResult.NoToAll: m_booDontOverwriteAll = true; return(false); case OverwriteResult.YesToAll: m_booOverwriteAll = true; return(true); case OverwriteResult.NoToGroup: Queue <string> folders = new Queue <string>(); folders.Enqueue(Path.GetDirectoryName(strLoweredPath)); while (folders.Count > 0) { strLoweredPath = folders.Dequeue(); if (!m_lstOverwriteFolders.Contains(strLoweredPath)) { m_lstDontOverwriteFolders.Add(strLoweredPath); foreach (string s in Directory.GetDirectories(strLoweredPath)) { folders.Enqueue(s.ToLowerInvariant()); } } } return(false); case OverwriteResult.YesToGroup: folders = new Queue <string>(); folders.Enqueue(Path.GetDirectoryName(strLoweredPath)); while (folders.Count > 0) { strLoweredPath = folders.Dequeue(); if (!m_lstDontOverwriteFolders.Contains(strLoweredPath)) { m_lstOverwriteFolders.Add(strLoweredPath); foreach (string s in Directory.GetDirectories(strLoweredPath)) { folders.Enqueue(s.ToLowerInvariant()); } } } return(true); default: throw new Exception("Sanity check failed: OverwriteDialog returned a value not present in the OverwriteResult enum"); } }
/// <summary> /// Verifies if the given file can be written. /// </summary> /// <remarks> /// This method checks if the given path is valid. If so, and the file does not /// exist, the file can be written. If the file does exist, than the user is /// asked to overwrite the file. /// </remarks> /// <param name="p_strPath">The file path, relative to the Data folder, whose writability is to be verified.</param> /// <returns><c>true</c> if the location specified by <paramref name="p_strPath"/> /// can be written; <c>false</c> otherwise.</returns> protected bool TestDoOverwrite(string p_strPath) { string strDataPath = Path.Combine(GameModeInfo.InstallationPath, p_strPath); bool booFIDataPath = false; if (!File.Exists(strDataPath)) { return(true); } else { booFIDataPath = new FileInfo(strDataPath).IsReadOnly; if (booFIDataPath) { m_booOverwriteAll = false; } } if (m_mmModManager.IsBackupActive) { ModBackupInfo mbInfo = m_mmModManager.lstMBInfo.Where(x => x.Mod == Mod).FirstOrDefault(); if (mbInfo == null) { return(false); } else { KeyValuePair <string, bool> Dictionary = mbInfo.ModFileDictionary.Where(x => x.Key == p_strPath).FirstOrDefault(); if (Dictionary.Key == null) { return(false); } else { return(Dictionary.Value); } } } string strLoweredPath = strDataPath.ToLowerInvariant(); if (m_lstOverwriteFolders.Contains(Path.GetDirectoryName(strLoweredPath))) { return(true); } if (m_lstDontOverwriteFolders.Contains(Path.GetDirectoryName(strLoweredPath))) { return(false); } if (m_booOverwriteAll) { return(true); } if (m_booDontOverwriteAll) { return(false); } IMod modOld = InstallLog.GetCurrentFileOwner(p_strPath); if (modOld == Mod) { return(true); } string strModFile = String.Empty; string strModFileID = String.Empty; string strMessage = null; if (modOld != null) { strModFile = modOld.Filename; strModFileID = modOld.Id; if (!String.IsNullOrEmpty(strModFileID)) { if (m_lstOverwriteMods.Contains(strModFileID)) { return(true); } if (m_lstDontOverwriteMods.Contains(strModFileID)) { return(false); } } else { if (m_lstOverwriteMods.Contains(strModFile)) { return(true); } if (m_lstDontOverwriteMods.Contains(strModFile)) { return(false); } } strMessage = String.Format("Data file '{{0}}' has already been installed by '{0}'", modOld.ModName); if (booFIDataPath) { strMessage += " and is ReadOnly."; } strMessage += Environment.NewLine + "Overwrite with this mod's file?"; } else { strMessage = "Data file '{0}' already exists"; if (booFIDataPath) { strMessage += " and is ReadOnly."; } else { strMessage += "."; } strMessage += Environment.NewLine + "Overwrite with this mod's file?"; } if (booFIDataPath) { switch (m_dlgOverwriteConfirmationDelegate(String.Format(strMessage, p_strPath), true, (modOld != null))) { case OverwriteResult.Yes: return(true); case OverwriteResult.YesToAll: m_booOverwriteAll = true; return(true); case OverwriteResult.NoToAll: m_booDontOverwriteAll = true; return(false); case OverwriteResult.No: return(false); default: throw new Exception("Sanity check failed: OverwriteDialog returned a value not present in the OverwriteResult enum"); } } else { switch (m_dlgOverwriteConfirmationDelegate(String.Format(strMessage, p_strPath), true, (modOld != null))) { case OverwriteResult.Yes: return(true); case OverwriteResult.No: return(false); case OverwriteResult.NoToAll: m_booDontOverwriteAll = true; return(false); case OverwriteResult.YesToAll: m_booOverwriteAll = true; return(true); case OverwriteResult.NoToGroup: Queue <string> folders = new Queue <string>(); folders.Enqueue(Path.GetDirectoryName(strLoweredPath)); while (folders.Count > 0) { strLoweredPath = folders.Dequeue(); if (!m_lstOverwriteFolders.Contains(strLoweredPath)) { m_lstDontOverwriteFolders.Add(strLoweredPath); foreach (string s in Directory.GetDirectories(strLoweredPath)) { folders.Enqueue(s.ToLowerInvariant()); } } } return(false); case OverwriteResult.YesToGroup: folders = new Queue <string>(); folders.Enqueue(Path.GetDirectoryName(strLoweredPath)); while (folders.Count > 0) { strLoweredPath = folders.Dequeue(); if (!m_lstDontOverwriteFolders.Contains(strLoweredPath)) { m_lstOverwriteFolders.Add(strLoweredPath); foreach (string s in Directory.GetDirectories(strLoweredPath)) { folders.Enqueue(s.ToLowerInvariant()); } } } return(true); case OverwriteResult.NoToMod: strModFile = modOld.Filename; strModFileID = modOld.Id; if (!String.IsNullOrEmpty(strModFileID)) { if (!m_lstOverwriteMods.Contains(strModFileID)) { m_lstDontOverwriteMods.Add(strModFileID); } } else { if (!m_lstOverwriteMods.Contains(strModFile)) { m_lstDontOverwriteMods.Add(strModFile); } } return(false); case OverwriteResult.YesToMod: strModFile = modOld.Filename; strModFileID = modOld.Id; if (!String.IsNullOrEmpty(strModFileID)) { if (!m_lstDontOverwriteMods.Contains(strModFileID)) { m_lstOverwriteMods.Add(strModFileID); } } else { if (!m_lstDontOverwriteMods.Contains(strModFile)) { m_lstOverwriteMods.Add(strModFile); } } return(true); default: throw new Exception("Sanity check failed: OverwriteDialog returned a value not present in the OverwriteResult enum"); } } }