/// <summary> /// Saves the changes of the encapsulated addon to its file stream. /// </summary> /// <exception cref="IOException">Happens if there is a problem with creating the addon into its stream.</exception> public void Save() { OpenAddon.Sort(); try { // It is needed to create a new, temporary file where we write the addon first // Without it, we would "undermount" the current file // And end up overwriting the addon from where ContentFile.Content gets the data we would write. using (FileStream newAddon = new FileStream(AddonStream.Name + "_create", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) { Writer.Create(OpenAddon, newAddon); // Copy the contents to the real file newAddon.Seek(0, SeekOrigin.Begin); AddonStream.Seek(0, SeekOrigin.Begin); AddonStream.SetLength(0); newAddon.CopyTo(AddonStream); AddonStream.Flush(); } } catch (IOException) { throw; } // If there were no errors creating and copying the temporary file, // I assume it is safe to delete. File.Delete(AddonStream.Name + "_create"); _modified = false; // Reload the content database of the open addon if (AddonReader == null) { try { AddonReader = new Reader(AddonStream); } catch (IOException) { throw; } } else { AddonReader.Reparse(); } // Convert all files in the open addon to addon-backed content storages // So after save, the application knows the file is now in the addon. // This also updates the fileIDs in case of a file was reordered when Sort() happened. foreach (Reader.IndexEntry entry in AddonReader.Index) { OpenAddon.Files.Where(f => f.Path == entry.Path).First().SwitchToAddonInstance(AddonReader, entry); } }
/// <summary> /// Gets the ContentFile entry for the specified path. /// </summary> /// <param name="path">The path of the file WITHIN the addon.</param> /// <returns>The ContentFile instance.</returns> /// <exception cref="FileNotFoundException">The specified file is not in the collection.</exception> public ContentFile GetFile(string path) { try { return(OpenAddon.GetFile(path)); } catch (FileNotFoundException) { throw; } }
/// <summary> /// Removes the specified file from the encapsulated addon. /// </summary> /// <param name="path">The path of the file.</param> /// <exception cref="FileNotFoundException">Thrown if the specified file does not exist.</exception> public void RemoveFile(string path) { try { OpenAddon.RemoveFile(path); } catch (FileNotFoundException) { throw; } _modified = true; }
/// <summary> /// Adds an array of bytes to the encapsulated addon using the specified internal path. /// </summary> /// <param name="path">The path which the file should be added as.</param> /// <param name="content">The array of bytes containing the actual content.</param> /// <exception cref="ArgumentException">Happens if a file with the same path is already added.</exception> /// <exception cref="WhitelistException">The file is prohibited from storing by the global whitelist.</exception> /// <exception cref="IgnoredException">The file is prohibited from storing by the addon's ignore list.</exception> public void AddFile(string path, byte[] content) { try { OpenAddon.AddFile(path, content); } catch (IgnoredException) { throw; } catch (WhitelistException) { throw; } catch (ArgumentException) { throw; } _modified = true; }
/// <summary> /// Adds the specified file from the local filesystem to the encapsulated addon. /// </summary> /// <param name="filename">The path of the file to add.</param> /// <exception cref="FileNotFoundException">Thrown if the specified file does not exist.</exception> /// <exception cref="IOException">Thrown if a problem happens with opening the file.</exception> /// <exception cref="ArgumentException">Happens if a file with the same path is already added.</exception> /// <exception cref="WhitelistException">The file is prohibited from storing by the global whitelist.</exception> /// <exception cref="IgnoredException">The file is prohibited from storing by the addon's ignore list.</exception> public void AddFile(string filename) { if (!File.Exists(filename)) { throw new FileNotFoundException("The specified file " + filename + " does not exist."); } // Prevent the need to read the contents of a file if it cannot be added. string path = Whitelist.GetMatchingString(filename); try { OpenAddon.CheckRestrictions(path); } catch (IgnoredException) { throw; } catch (WhitelistException) { throw; } catch (ArgumentException) { throw; } byte[] bytes; try { bytes = File.ReadAllBytes(filename); } catch (IOException) { throw; } AddFile(Whitelist.GetMatchingString(filename), bytes); }