vpk 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/README.rdoc +22 -0
  2. data/bin/vpk +50 -0
  3. data/lib/vpk.rb +12 -5
  4. data/lib/vpk/version.rb +1 -1
  5. metadata +6 -4
  6. data/README.md +0 -5
@@ -0,0 +1,22 @@
1
+ = vpk
2
+ VPK File Format Parser (extract and archive)
3
+
4
+ = Require
5
+ - Ruby 1.9
6
+
7
+ = What is VPK
8
+ VPK means "Valve Pak".
9
+ Description is here https://developer.valvesoftware.com/wiki/VPK
10
+
11
+ = Install
12
+ gem install vpk
13
+
14
+ = Usage
15
+ Extract from your vpk file
16
+ require 'vpk'
17
+ VPK::VPKFile.new("./path_to.vpk").extract_to("./")
18
+
19
+ Archive your directory
20
+ require 'vpk'
21
+ VPK::VPKFile.archive("./path_to_dir").write_to("./archive.vpk")
22
+
data/bin/vpk ADDED
@@ -0,0 +1,50 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+ # Author: kimoto
4
+ require 'vpk'
5
+ require 'optparse'
6
+
7
+ options = {}
8
+
9
+ opt = OptionParser.new
10
+ opt.banner = "#{File.basename($0)} [options] <dirname or vpkfile>"
11
+ opt.on("-c", "create archive"){ |v| options[:mode] = :archive }
12
+ opt.on("-x", "extract mode"){ |v| options[:mode] = :extract }
13
+ opt.parse! ARGV
14
+
15
+ #VPK::VPKFile.logger = Logger.new(STDERR)
16
+
17
+ case options[:mode]
18
+ when :archive
19
+ path = ARGV.shift
20
+
21
+ # trim last slashes
22
+ path = path.gsub(/(\/)+$/, "")
23
+
24
+ if File.directory? path
25
+ after_path = path + ".vpk"
26
+ if File.exists? after_path
27
+ STDERR.puts "already exist file: #{after_path}"
28
+ exit(1)
29
+ else
30
+ puts "compressed #{path} -> #{after_path}"
31
+ VPK::VPKFile.archive(path).write_to(after_path)
32
+ end
33
+ else
34
+ STDERR.puts "not directory: #{path}"
35
+ exit(1)
36
+ end
37
+ when :extract
38
+ path = ARGV.shift
39
+ if File.exists? path
40
+ VPK::VPKFile.new(path).extract_to("./")
41
+ puts "extracted!: #{path}"
42
+ else
43
+ STDERR.puts "not found specified file: #{path}"
44
+ exit(1)
45
+ end
46
+ else
47
+ puts opt.help
48
+ exit(1)
49
+ end
50
+
data/lib/vpk.rb CHANGED
@@ -3,6 +3,7 @@ require 'logger'
3
3
  require 'zlib'
4
4
  require 'find'
5
5
  require 'stringio'
6
+ require 'fileutils'
6
7
 
7
8
  module VPK
8
9
  class VPKHeader
@@ -30,7 +31,7 @@ module VPK
30
31
 
31
32
  def self.load_from(path)
32
33
  entry = VPKDirectoryEntry.new
33
- entry.payload = File.read(path)
34
+ entry.payload = File.binread(path)
34
35
  entry.full_path = path
35
36
  entry
36
37
  end
@@ -106,7 +107,7 @@ module VPK
106
107
  @@logger.info "try to read #{entry.full_path} (#{entry.entry_length} bytes)"
107
108
  entry.read_payload(io, end_of_header + @header.directory_length)
108
109
  unless entry.valid?
109
- @@logger.error "crc is invalid: #{entry.crc.inspect}"
110
+ @@logger.error "crc is invalid: @entry.payload:#{Zlib.crc32(entry.payload)} != @crc:#{entry.crc.inspect}"
110
111
  raise VPKFileInvalidCRCError
111
112
  end
112
113
  @@logger.info "done"
@@ -174,7 +175,7 @@ module VPK
174
175
  next if File.directory?(f)
175
176
  entry = VPKDirectoryEntry.new
176
177
  entry.full_path = f.gsub(/^#{target_dir}(\/)?/, "")
177
- entry.payload = File.read(f)
178
+ entry.payload = File.binread(f)
178
179
  vpk.dir_entries << entry
179
180
  }
180
181
  vpk
@@ -232,7 +233,7 @@ module VPK
232
233
 
233
234
  while true
234
235
  path = read_string(io)
235
- @@logger.debug "path = #{path.inspect}"
236
+ @@logger.debug "path = #{path.inspect} (#{path.unpack('H*')})"
236
237
  break if path == ""
237
238
 
238
239
  while true
@@ -257,9 +258,15 @@ module VPK
257
258
  total_offset = 0
258
259
  to_file_struct_tree.each{ |extension, path_map|
259
260
  write_string(io, extension)
261
+
260
262
  path_map.each{ |path, entries|
261
263
  @@logger.debug "write path: #{path.inspect}"
262
- write_string(io, path)
264
+ if path == "."
265
+ write_string(io, " ")
266
+ else
267
+ write_string(io, path)
268
+ end
269
+
263
270
  entries.each{ |entry|
264
271
  write_string(io, entry.file)
265
272
 
@@ -1,3 +1,3 @@
1
1
  module VPK
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vpk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,21 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-13 00:00:00.000000000 Z
12
+ date: 2012-07-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: VPK File Format Parser (extract and archive)
15
15
  email:
16
16
  - sub+peerler@gmail.com
17
- executables: []
17
+ executables:
18
+ - vpk
18
19
  extensions: []
19
20
  extra_rdoc_files: []
20
21
  files:
21
22
  - .gitignore
22
23
  - Gemfile
23
- - README.md
24
+ - README.rdoc
24
25
  - Rakefile
26
+ - bin/vpk
25
27
  - lib/vpk.rb
26
28
  - lib/vpk/version.rb
27
29
  - vpk.gemspec
data/README.md DELETED
@@ -1,5 +0,0 @@
1
- vpk
2
- ===
3
-
4
- VPK File Format Parser (extract and archive)
5
-