Example #1
0
        public bool InstallFile(string fnFrom, string fnTo = "")
        {
            var    ret = false;
            string strDataPath;

            /*
             * There were two differences between InstallFileFromFomod and CopyDataFile
             * 1. IFFF checked permissions, CDF did not.
             * 2. IFFF used the same src and dst filenames, CDF used different names.
             */
            PermissionsManager.CurrentPermissions.Assert();

            if (fnTo == "")
            {
                fnTo = fnFrom;
            }

            FileManagement.AssertFilePathIsSafe(fnTo);

            #region refactor me

            #region original code

            //      byte[] bteFomodFile = Fomod.GetFile(fnFrom);
            //      ret = GenerateDataFile(fnTo, bteFomodFile);

            #endregion

            #region newcode

            /*
             * New code
             * 1. Extract the file from the archive to the temp file
             * 2. Copy the temp file to the destination
             * 3. Delete the temp file
             */

            var tmpFN = Fomod.ExtractToTemp(fnFrom);
            if (GenerateDataFilePrep(fnTo, out strDataPath))
            {
                Installer.TransactionalFileManager.Copy(tmpFN, strDataPath, true);
                Installer.MergeModule.AddFile(fnTo);
                ret = true;
            }

            #endregion

            #endregion

            return(ret);
        }
Example #2
0
        /// <summary>
        ///   Writes the file represented by the given byte array to the given path.
        /// </summary>
        /// <remarks>
        ///   This method writes the given data as a file at the given path. If the file
        ///   already exists the user is prompted to overwrite the file.
        /// </remarks>
        /// <param name="p_strPath">The path where the file is to be created.</param>
        /// <param name="p_bteData">The data that is to make up the file.</param>
        /// <returns>
        ///   <lang langref="true" /> if the file was written; <lang langref="false" /> if the user chose
        ///   not to overwrite an existing file.
        /// </returns>
        /// <exception cref="IllegalFilePathException">
        ///   Thrown if <paramref name="p_strPath" /> is
        ///   not safe.
        /// </exception>
        public virtual bool GenerateDataFile(string p_strPath, byte[] p_bteData)
        {
            PermissionsManager.CurrentPermissions.Assert();
            FileManagement.AssertFilePathIsSafe(p_strPath);
            string strDataPath;
            var    ret = false;

            if (GenerateDataFilePrep(p_strPath, out strDataPath))
            {
                Installer.TransactionalFileManager.WriteAllBytes(strDataPath, p_bteData);
                Installer.MergeModule.AddFile(p_strPath);
                ret = true;
            }

            return(ret);
        }
Example #3
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.
        ///   This variant of <see cref="UninstallDataFile" /> is for use when uninstalling a file
        ///   for a mod whose FOMod is missing.
        /// </remarks>
        /// <param name="p_strFomodBaseName">
        ///   The base name of the <see cref="fomod" /> whose file
        ///   is being uninstalled.
        /// </param>
        /// <param name="p_strPath">The path to the file that is to be uninstalled.</param>
        /// <seealso cref="UninstallDataFile(string)" />
        public void UninstallDataFile(string p_strFomodBaseName, string p_strFile)
        {
            PermissionsManager.CurrentPermissions.Assert();
            FileManagement.AssertFilePathIsSafe(p_strFile);
            var strDataPath        = Path.Combine(Program.GameMode.PluginsPath, p_strFile);
            var strKey             = InstallLog.Current.GetModKey(p_strFomodBaseName);
            var strDirectory       = Path.GetDirectoryName(p_strFile);
            var strBackupDirectory = Path.Combine(Program.GameMode.OverwriteDirectory, strDirectory);

            if (File.Exists(strDataPath))
            {
                var strCurrentOwnerKey = InstallLog.Current.GetCurrentFileOwnerKey(p_strFile);
                //if we didn't install the file, then leave it alone
                if (strKey.Equals(strCurrentOwnerKey))
                {
                    //if we did install the file, replace it with the file we overwrote
                    // if we didn't overwrite a file, then just delete it
                    Installer.TransactionalFileManager.Delete(strDataPath);

                    var strPreviousOwnerKey = InstallLog.Current.GetPreviousFileOwnerKey(p_strFile);
                    if (strPreviousOwnerKey != null)
                    {
                        var strFile            = strPreviousOwnerKey + "_" + Path.GetFileName(p_strFile);
                        var strRestoreFromPath = Path.Combine(strBackupDirectory, strFile);
                        if (File.Exists(strRestoreFromPath))
                        {
                            var strBackupFileName =
                                Path.GetFileName(
                                    Directory.GetFiles(Path.GetDirectoryName(strRestoreFromPath), Path.GetFileName(strRestoreFromPath))[0]);
                            var strCasedFileName = strBackupFileName.Substring(strBackupFileName.IndexOf('_') + 1);
                            var strNewDataPath   = Path.Combine(Path.GetDirectoryName(strDataPath), strCasedFileName);
                            Installer.TransactionalFileManager.Copy(strRestoreFromPath, strNewDataPath, true);
                            Installer.TransactionalFileManager.Delete(strRestoreFromPath);
                        }

                        //remove anny empty directories from the overwrite folder we may have created
                        var strStopDirectory = Program.GameMode.OverwriteDirectory;
                        strStopDirectory = strStopDirectory.Remove(0, strStopDirectory.LastIndexOfAny(new[]
                        {
                            Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar
                        }));
                        TrimEmptyDirectories(strRestoreFromPath, strStopDirectory);
                    }
                    else
                    {
                        //remove any empty directories from the data folder we may have created
                        var strStopDirectory = Program.GameMode.PluginsPath;
                        strStopDirectory = strStopDirectory.Remove(0, strStopDirectory.LastIndexOfAny(new[]
                        {
                            Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar
                        }));
                        TrimEmptyDirectories(strDataPath, strStopDirectory);
                    }
                }
            }

            //remove our version of the file from the backup directory
            var strOverwriteFile = strKey + "_" + Path.GetFileName(p_strFile);
            var strOverwritePath = Path.Combine(strBackupDirectory, strOverwriteFile);

            if (File.Exists(strOverwritePath))
            {
                Installer.TransactionalFileManager.Delete(strOverwritePath);
                //remove anny empty directories from the overwrite folder we may have created
                TrimEmptyDirectories(strOverwritePath, Program.GameMode.OverwriteDirectory);
            }
        }