class Gnosis::Archive
Representation of an encrypted RGSS archive. This class provides a layer of abstraction over the archive itself, providing methods to help ease the traversal of encrypted archives and allow in-memory decryption of their contents.
Attributes
Full path to the encrypted archive this class represents. @return [String]
Hash of files with associated encryption information. @return [Hash{String => Hash{Symbol => Number}}]
The translator instance in use by this archive representation. @return [Translator]
Public Class Methods
Instantiates a new {Archive Archive} representing an encrypted RGSS archive.
@raise [InvalidArchiveError] if the given archive is invalid @param [String] archive the relative path to an encrypted archive
# File lib/gnosis/archive.rb, line 26 def initialize(archive) @archive = File.expand_path(archive) @translator = Translators.class_for(self) @files = @translator.scan_files end
Public Instance Methods
Convenience method for obtaining an array of all encrypted files within this RGSS archive representation.
@return [Array<String>] an array of files contained in the archive
# File lib/gnosis/archive.rb, line 36 def contents @files.keys end
Decrypts the given internal filename. The returned binary string may be converted into an IO object in memory via the StringIO class present in the Ruby standard library. Yields the decrypted binary data if a block is given.
@example Decrypt to StringIO
require 'stringio' archive = Gnosis::Archive.new('path/to/Game.rgss3a') io = StringIO.new(archive.decrypt('Graphics/Titles1/Title.png'))
@raise [Errno::ENOENT] if the given file does not exist in the archive @param [String] file internal path of encrypted file @return [String] the decrypted binary contents of the given file
# File lib/gnosis/archive.rb, line 69 def decrypt(file) data = @translator.decrypt(file) yield data if block_given? data end
Search for files within the encrypted archive with optional shell-style globbing. Yields the results of the given pattern if a block is given.
@note Globbing is performed via ‘File#fnmatch`.
@example Search with Globbing
archive = Gnosis::Archive.new('path/to/Game.rgss3a') archive.glob('**/Title.png') # => ["Graphics/Titles1/Title.png"]
@param [String] pattern file name pattern @yield [result] an array of file names matching the given pattern @return [Array<String>] an array of file names matching the given pattern
@see www.ruby-doc.org/core-1.9.3/File.html#method-c-fnmatch
File.fnmatch
@see search
# File lib/gnosis/archive.rb, line 112 def glob(pattern) pattern.force_encoding('utf-8') result = @files.keys.select { |file| File.fnmatch?(pattern, file) } yield result if block_given? result end
Search for files within the encrypted archive with a regular expression. Yields the results of the given expression if a block is given.
@example Search with Regular Expression
archive = Gnosis::Archive.new('path/to/Game.rgss3a') archive.search(/\.png$/) # => ["Graphics/Animations/Attack1.png", ... ]
@param [String, Regexp] expression substring or regular expression to
match files against
@yield [result] an array of file names matching the given expression @return [Array<String>] an array of file names matching the given
expression
@see glob
# File lib/gnosis/archive.rb, line 89 def search(expression) result = @files.keys.select { |file| file[expression] } yield result if block_given? result end
Convenience method for obtaining the number of encrypted files within this RGSS archive representation.
@return [Number] the number of files contained in the archive
# File lib/gnosis/archive.rb, line 44 def size contents.size end
A representation of this {Archive Archive} as a string.
@return [String] representation of this {Archive Archive} as a string.
# File lib/gnosis/archive.rb, line 122 def to_s "#{type} Archive: #{contents.size} files" end
Returns the type of archive represented by this {Archive Archive}.
@return [String] the type of archive represented
# File lib/gnosis/archive.rb, line 52 def type @translator.class.name.split(/::/).last end