Exemple #1
0
        /// <summary>
        /// Uninstalls the specified file.
        /// </summary>
        /// <remarks>
        /// If the mod we are uninstalling doesn't own the file, then its version is removed
        /// from the overwrites directory. If the mod we are uninstalling overwrote a file when it
        /// installed the specified file, then the overwritten file is restored. Otherwise
        /// the file is deleted.
        /// </remarks>
        /// <param name="p_strPath">The path to the file that is to be uninstalled.</param>
        /// <param name="p_booSecondaryInstallPath">Whether to use the secondary install path.</param>
        public void UninstallDataFile(string p_strPath, bool p_booSecondaryInstallPath)
        {
            string strInstallFilePath = String.Empty;

            DataFileUtility.AssertFilePathIsSafe(p_strPath);
            string strUninstallingModKey = InstallLog.GetModKey(Mod);

            if (p_booSecondaryInstallPath && !(String.IsNullOrEmpty(GameModeInfo.SecondaryInstallationPath)))
            {
                strInstallFilePath = Path.Combine(GameModeInfo.SecondaryInstallationPath, p_strPath);
            }
            else
            {
                strInstallFilePath = Path.Combine(GameModeInfo.InstallationPath ?? "", p_strPath);
            }

            string strBackupDirectory = Path.Combine(GameModeInfo.OverwriteDirectory, Path.GetDirectoryName(p_strPath));
            string strFile;
            string strRestoreFromPath = string.Empty;
            bool   booRestoreFile     = false;

            FileInfo fiInfo = null;

            if (File.Exists(strInstallFilePath))
            {
                string strCurrentOwnerKey = InstallLog.GetCurrentFileOwnerKey(p_strPath);
                //if we didn't install the file, then leave it alone
                if (strUninstallingModKey.Equals(strCurrentOwnerKey))
                {
                    //if we did install the file, replace it with the file we overwrote
                    // when we installed the file
                    // if we didn't overwrite a file, then just delete the current file
                    fiInfo = new FileInfo(strInstallFilePath);
                    if (fiInfo.IsReadOnly)
                    {
                        m_lstErrorMods.Add(strInstallFilePath);
                    }
                    else
                    {
                        TransactionalFileManager.Delete(strInstallFilePath);
                    }

                    string strPreviousOwnerKey = InstallLog.GetPreviousFileOwnerKey(p_strPath);
                    if (strPreviousOwnerKey != null)
                    {
                        strFile            = strPreviousOwnerKey + "_" + Path.GetFileName(p_strPath);
                        strRestoreFromPath = Path.Combine(strBackupDirectory, strFile);
                        if (File.Exists(strRestoreFromPath))
                        {
                            booRestoreFile = true;
                        }
                    }

                    if (IsPlugin)
                    {
                        if ((PluginManager.IsActivatiblePluginFile(strInstallFilePath)) && !booRestoreFile)
                        {
                            PluginManager.RemovePlugin(strInstallFilePath);
                        }
                    }

                    if (booRestoreFile)
                    {
                        //we get the file name this way in order to preserve the file name's case
                        string strBackupFileName = Path.GetFileName(Directory.GetFiles(Path.GetDirectoryName(strRestoreFromPath), Path.GetFileName(strRestoreFromPath))[0]);
                        strBackupFileName = strBackupFileName.Substring(strBackupFileName.IndexOf('_') + 1);
                        string strNewDataPath = Path.Combine(Path.GetDirectoryName(strInstallFilePath), strBackupFileName);

                        fiInfo = new FileInfo(strRestoreFromPath);
                        try
                        {
                            TransactionalFileManager.Copy(strRestoreFromPath, strNewDataPath, true);
                        }
                        catch { }

                        if (fiInfo.IsReadOnly)
                        {
                            m_lstErrorMods.Add(strInstallFilePath);
                        }
                        else
                        {
                            TransactionalFileManager.Delete(strRestoreFromPath);
                        }
                    }

                    //remove any empty directories from the data folder we may have created
                    TrimEmptyDirectories(Path.GetDirectoryName(strInstallFilePath), GameModeInfo.InstallationPath);
                }
            }

            //remove our version of the file from the backup directory
            string strOverwritePath = Path.Combine(strBackupDirectory, strUninstallingModKey + "_" + Path.GetFileName(p_strPath));

            if (File.Exists(strOverwritePath))
            {
                fiInfo = new FileInfo(strOverwritePath);
                if (((fiInfo.Attributes | FileAttributes.Hidden) == fiInfo.Attributes) || (fiInfo.IsReadOnly))
                {
                    m_lstErrorMods.Add(strInstallFilePath);
                }
                else
                {
                    TransactionalFileManager.Delete(strOverwritePath);
                }
            }

            //remove any empty directories from the overwrite folder we may have created
            string strStopDirectory = GameModeInfo.OverwriteDirectory;
            string strFileName      = Path.GetFileName(strOverwritePath);

            TrimEmptyDirectories(strOverwritePath.Replace(strFileName, ""), strStopDirectory);

            InstallLog.RemoveDataFile(Mod, p_strPath);
        }