/// <summary> /// Writes the file represented by the given file stream 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_fstData">The data that is to make up the file.</param> /// <returns><c>true</c> if the file was written; <c>false</c> if the user chose /// not to overwrite an existing file.</returns> public virtual bool GenerateDataFile(string p_strPath, FileStream p_fstData) { string strInstallFilePath = p_strPath; if (!Directory.Exists(Path.GetDirectoryName(strInstallFilePath))) { TransactionalFileManager.CreateDirectory(Path.GetDirectoryName(strInstallFilePath)); } else { if (!TestDoOverwrite(p_strPath)) { return(false); } } TransactionalFileManager.WriteFileStream(strInstallFilePath, p_fstData); InstallLog.AddDataFile(Mod, p_strPath); return(true); }
/// <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> /// <param name="p_booSecondaryInstallPath">Whether to use the secondary install path.</param> /// <returns><c>true</c> if the file was written; <c>false</c> 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, bool p_booSecondaryInstallPath) { DataFileUtility.AssertFilePathIsSafe(p_strPath); string strInstallFilePath = String.Empty; if (p_booSecondaryInstallPath && !(String.IsNullOrEmpty(GameModeInfo.SecondaryInstallationPath))) { strInstallFilePath = Path.Combine(GameModeInfo.SecondaryInstallationPath, p_strPath); } else { strInstallFilePath = Path.Combine(GameModeInfo.InstallationPath, p_strPath); } FileInfo Info = new FileInfo(strInstallFilePath); if (!Directory.Exists(Path.GetDirectoryName(strInstallFilePath))) { TransactionalFileManager.CreateDirectory(Path.GetDirectoryName(strInstallFilePath)); } else { if (!TestDoOverwrite(p_strPath)) { return(false); } if (File.Exists(strInstallFilePath)) { if (Info.IsReadOnly == true) { File.SetAttributes(strInstallFilePath, File.GetAttributes(strInstallFilePath) & ~FileAttributes.ReadOnly); } string strInstallDirectory = Path.GetDirectoryName(p_strPath); string strBackupDirectory = Path.Combine(GameModeInfo.OverwriteDirectory, strInstallDirectory); string strOldModKey = InstallLog.GetCurrentFileOwnerKey(p_strPath); if (strOldModKey == null) { InstallLog.LogOriginalDataFile(p_strPath); strOldModKey = InstallLog.OriginalValuesKey; } string strInstallingModKey = InstallLog.GetModKey(Mod); //if this mod has installed this file already we just replace it and don't // need to back it up. if (!strOldModKey.Equals(strInstallingModKey)) { //back up the current version of the file if the current mod // didn't install it if (!Directory.Exists(strBackupDirectory)) { TransactionalFileManager.CreateDirectory(strBackupDirectory); } //we get the file name this way in order to preserve the file name's case string strFile = Path.GetFileName(Directory.GetFiles(Path.GetDirectoryName(strInstallFilePath), Path.GetFileName(strInstallFilePath))[0]); strFile = strOldModKey + "_" + strFile; string strBackupFilePath = Path.Combine(strBackupDirectory, strFile); Info = new FileInfo(strBackupFilePath); if ((Info.IsReadOnly == true) && (File.Exists(strBackupFilePath))) { File.SetAttributes(strBackupFilePath, File.GetAttributes(strBackupFilePath) & ~FileAttributes.ReadOnly); } TransactionalFileManager.Copy(strInstallFilePath, strBackupFilePath, true); } TransactionalFileManager.Delete(strInstallFilePath); } } TransactionalFileManager.WriteAllBytes(strInstallFilePath, p_bteData); // Checks whether the file is a gamebryo plugin if (IsPlugin) { if (PluginManager.IsActivatiblePluginFile(strInstallFilePath)) { if (!PluginManager.CanActivatePlugins()) { string strTooManyPlugins = String.Format("The requested change to the active plugins list would result in over {0} plugins being active.", PluginManager.MaxAllowedActivePluginsCount); strTooManyPlugins += Environment.NewLine + String.Format("The current game doesn't support more than {0} active plugins, you need to disable at least one plugin to continue.", PluginManager.MaxAllowedActivePluginsCount); strTooManyPlugins += Environment.NewLine + Environment.NewLine + String.Format("NOTE: This is a game engine limitation.") + Environment.NewLine; throw new Exception(strTooManyPlugins); } PluginManager.AddPlugin(strInstallFilePath); } } InstallLog.AddDataFile(Mod, p_strPath); return(IsPlugin); }
/// <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><c>true</c> if the file was written; <c>false</c> 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, bool p_booSecondaryInstallPath) { DataFileUtility.AssertFilePathIsSafe(p_strPath); string strInstallFilePath = null; if (p_booSecondaryInstallPath && !(String.IsNullOrEmpty(GameModeInfo.SecondaryInstallationPath))) { strInstallFilePath = Path.Combine(GameModeInfo.SecondaryInstallationPath, p_strPath); } else { strInstallFilePath = Path.Combine(GameModeInfo.InstallationPath, p_strPath); } FileInfo Info = new FileInfo(strInstallFilePath); if (!Directory.Exists(Path.GetDirectoryName(strInstallFilePath))) { TransactionalFileManager.CreateDirectory(Path.GetDirectoryName(strInstallFilePath)); } else { if (!TestDoOverwrite(p_strPath)) { return(false); } if (File.Exists(strInstallFilePath)) { if (Info.IsReadOnly == true) { File.SetAttributes(strInstallFilePath, File.GetAttributes(strInstallFilePath) & ~FileAttributes.ReadOnly); } string strInstallDirectory = Path.GetDirectoryName(p_strPath); string strBackupDirectory = Path.Combine(GameModeInfo.OverwriteDirectory, strInstallDirectory); string strOldModKey = InstallLog.GetCurrentFileOwnerKey(p_strPath); if (strOldModKey == null) { InstallLog.LogOriginalDataFile(p_strPath); strOldModKey = InstallLog.OriginalValuesKey; } string strInstallingModKey = InstallLog.GetModKey(Mod); //if this mod has installed this file already we just replace it and don't // need to back it up. if (!strOldModKey.Equals(strInstallingModKey)) { //back up the current version of the file if the current mod // didn't install it if (!Directory.Exists(strBackupDirectory)) { TransactionalFileManager.CreateDirectory(strBackupDirectory); } //we get the file name this way in order to preserve the file name's case string strFile = Path.GetFileName(Directory.GetFiles(Path.GetDirectoryName(strInstallFilePath), Path.GetFileName(strInstallFilePath))[0]); strFile = strOldModKey + "_" + strFile; string strBackupFilePath = Path.Combine(strBackupDirectory, strFile); Info = new FileInfo(strBackupFilePath); if ((Info.IsReadOnly == true) && (File.Exists(strBackupFilePath))) { File.SetAttributes(strBackupFilePath, File.GetAttributes(strBackupFilePath) & ~FileAttributes.ReadOnly); } TransactionalFileManager.Copy(strInstallFilePath, strBackupFilePath, true); } TransactionalFileManager.Delete(strInstallFilePath); } } TransactionalFileManager.WriteAllBytes(strInstallFilePath, p_bteData); // Checks whether the file is a gamebryo plugin if (IsPlugin) { if (PluginManager.IsActivatiblePluginFile(strInstallFilePath)) { PluginManager.AddPlugin(strInstallFilePath); } } InstallLog.AddDataFile(Mod, p_strPath); return(IsPlugin); }
/// <summary> /// Installs the specified file from the Mod to the file system. /// </summary> /// <param name="p_strModFilePath">The path of the file in the Mod to install.</param> /// <param name="p_strInstallPath">The path on the file system where the file is to be installed.</param> /// <returns><c>true</c> if the file was written; <c>false</c> if the user chose /// not to overwrite an existing file.</returns> public bool InstallFileFromMod(string p_strModFilePath, string p_strInstallPath, bool p_booSecondaryInstallPath) { Console.WriteLine("install " + p_strModFilePath + " to " + p_strInstallPath); string destinationPath = installPath(p_strInstallPath, p_booSecondaryInstallPath); if (!Directory.Exists(Path.GetDirectoryName(destinationPath))) { TransactionalFileManager.CreateDirectory(Path.GetDirectoryName(destinationPath)); } else { if (!TestDoOverwrite(p_strInstallPath)) { return(false); } if (File.Exists(destinationPath)) { FileInfo Info = new FileInfo(destinationPath); if (Info.IsReadOnly == true) { File.SetAttributes(destinationPath, File.GetAttributes(destinationPath) & ~FileAttributes.ReadOnly); } string strInstallDirectory = Path.GetDirectoryName(p_strInstallPath); string strBackupDirectory = Path.Combine(GameModeInfo.OverwriteDirectory, strInstallDirectory); string strOldModKey = InstallLog.GetCurrentFileOwnerKey(p_strInstallPath); if (strOldModKey == null) { InstallLog.LogOriginalDataFile(p_strInstallPath); strOldModKey = InstallLog.OriginalValuesKey; } string strInstallingModKey = InstallLog.GetModKey(Mod); //if this mod has installed this file already we just replace it and don't // need to back it up. if (!strOldModKey.Equals(strInstallingModKey)) { //back up the current version of the file if the current mod // didn't install it if (!Directory.Exists(strBackupDirectory)) { TransactionalFileManager.CreateDirectory(strBackupDirectory); } //we get the file name this way in order to preserve the file name's case string strFile = Path.GetFileName(Directory.GetFiles(Path.GetDirectoryName(destinationPath), Path.GetFileName(destinationPath))[0]); strFile = strOldModKey + "_" + strFile; string strBackupFilePath = Path.Combine(strBackupDirectory, strFile); Info = new FileInfo(strBackupFilePath); if ((Info.IsReadOnly == true) && (File.Exists(strBackupFilePath))) { File.SetAttributes(strBackupFilePath, File.GetAttributes(strBackupFilePath) & ~FileAttributes.ReadOnly); } TransactionalFileManager.Copy(destinationPath, strBackupFilePath, true); } TransactionalFileManager.Delete(destinationPath); } } try { using (FileStream stream = File.Create(destinationPath)) { Mod.ExtractFileTo(p_strModFilePath, stream); } } catch (FileNotFoundException e) { MessageBox.Show("File " + p_strModFilePath + " couldn't be extracted.\n" + "This probably means the mod is broken though you may still get partial functionality.\n" + "Please inform the mod author.\n" + "Detailed Error: " + e.Message, "File not found in FOMod", MessageBoxButtons.OK, MessageBoxIcon.Error); File.Delete(destinationPath); throw; } catch (Exception ex) { MessageBox.Show("Exception: " + ex.ToString()); throw; } // Checks whether the file is a gamebryo plugin if (IsPlugin) { if (PluginManager.IsActivatiblePluginFile(destinationPath)) { PluginManager.AddPlugin(destinationPath); } } InstallLog.AddDataFile(Mod, p_strInstallPath); return(IsPlugin); }