Class: VPK::VPKFile
- Inherits:
-
Object
- Object
- VPK::VPKFile
- Defined in:
- lib/vpk.rb
Constant Summary collapse
- @@logger =
Logger.new(nil)
Instance Attribute Summary collapse
-
#dir_entries ⇒ Object
Returns the value of attribute dir_entries.
-
#header ⇒ Object
Returns the value of attribute header.
Class Method Summary collapse
- .archive(target_dir) ⇒ Object
- .entries(path, &block) ⇒ Object
- .logger=(new_logger) ⇒ Object
- .open(path, &block) ⇒ Object
Instance Method Summary collapse
- #extract_to(base_dir) ⇒ Object
-
#initialize(path = nil) ⇒ VPKFile
constructor
A new instance of VPKFile.
- #to_blob ⇒ Object
- #to_file_struct_tree ⇒ Object
- #to_s ⇒ Object
- #write_to(path) ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ VPKFile
Returns a new instance of VPKFile.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/vpk.rb', line 85 def initialize(path=nil) return if path.nil? @@logger.info "extract vpk file #{path.inspect}" File.open(path, "rb") do |io| @header = VPKHeader.new @header.signature = io.read(4).unpack("V*").first @header.version = io.read(4).unpack("V*").first @header.directory_length = io.read(4).unpack("V*").first @@logger.info "reading header: #{@header.inspect}" end_of_header = io.pos unless @header.signature == "0x55aa1234".hex @@logger.error "signature is invalid: #{@header.signature.inspect}" raise VPKFileFormatError end @@logger.info "signature is valid" @dir_entries = read_directory(io) @dir_entries.each do |entry| @@logger.info "try to read #{entry.full_path} (#{entry.entry_length} bytes)" entry.read_payload(io, end_of_header + @header.directory_length) unless entry.valid? @@logger.error "crc is invalid: @entry.payload:#{Zlib.crc32(entry.payload)} != @crc:#{entry.crc.inspect}" raise VPKFileInvalidCRCError end @@logger.info "done" end end end |
Instance Attribute Details
#dir_entries ⇒ Object
Returns the value of attribute dir_entries.
78 79 80 |
# File 'lib/vpk.rb', line 78 def dir_entries @dir_entries end |
#header ⇒ Object
Returns the value of attribute header.
77 78 79 |
# File 'lib/vpk.rb', line 77 def header @header end |
Class Method Details
.archive(target_dir) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/vpk.rb', line 166 def self.archive(target_dir) vpk = VPKFile.new vpk.header = VPKHeader.new vpk.header.signature = VPKHeader::VPK_SIGNATURE vpk.header.version = VPKHeader::VERSION vpk.header.directory_length = nil vpk.dir_entries = [] Find.find(target_dir){ |f| next if File.directory?(f) entry = VPKDirectoryEntry.new entry.full_path = f.gsub(/^#{target_dir}(\/)?/, "") entry.payload = File.binread(f) vpk.dir_entries << entry } vpk end |
.entries(path, &block) ⇒ Object
123 124 125 126 127 |
# File 'lib/vpk.rb', line 123 def self.entries(path, &block) file = self.new(path) file.dir_entries end |
.logger=(new_logger) ⇒ Object
81 82 83 |
# File 'lib/vpk.rb', line 81 def self.logger=(new_logger) @@logger = new_logger end |
.open(path, &block) ⇒ Object
118 119 120 121 |
# File 'lib/vpk.rb', line 118 def self.open(path, &block) file = self.new(path) block.call file end |
Instance Method Details
#extract_to(base_dir) ⇒ Object
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/vpk.rb', line 155 def extract_to(base_dir) @dir_entries.each{ |entry| unless entry.path == " " path = File.join(base_dir, entry.path) FileUtils.mkdir_p path end file_path = File.join(base_dir, entry.full_path) File.write file_path, entry.payload } end |
#to_blob ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/vpk.rb', line 139 def to_blob @header.directory_length = calc_directory_length @@logger.debug "calculated directory length: #{@header.directory_length}" StringIO.open("") do |io| io.write([@header.signature].pack("I*")) io.write([@header.version].pack("I*")) io.write([@header.directory_length].pack("I*")) write_directory(io) io.rewind return io.read end end |
#to_file_struct_tree ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/vpk.rb', line 129 def to_file_struct_tree map = {} @dir_entries.each{ |entry| map[entry.extension] ||= {} map[entry.extension][entry.path] ||= [] map[entry.extension][entry.path] << entry } map end |
#to_s ⇒ Object
188 189 190 |
# File 'lib/vpk.rb', line 188 def to_s "#<VPKFile: #{self.object_id} @header=#{@header.inspect} @files=#{self.dir_entries.size}>" end |
#write_to(path) ⇒ Object
184 185 186 |
# File 'lib/vpk.rb', line 184 def write_to(path) File.write path, to_blob end |