/
pak.rb
executable file
·68 lines (49 loc) · 1.55 KB
/
pak.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /usr/bin/env ruby
# http://wiki.xentax.com/index.php?title=Rising_Kingdoms
require 'thor'
require 'fffs'
require 'stringio'
class PakFile < FFFS::FileSystem
def parse (content)
stream = StringIO.new(content)
# header
if stream.read(16) != "HMMSYS PackFile\n"
raise ArgumentError.new 'This is not a PAK file'
end
version = stream.read(4).unpack('V').shift
stream.read 12
fileNumber = stream.read(4).unpack('V').shift
directoryLength = stream.read(4).unpack('V').shift
current = 0
previous = nil
1.upto(fileNumber) do
nameLength = stream.read(1).ord
nameReuseLength = stream.read(1).ord
path = String.new
if nameReuseLength > 0 && previous
path << previous[0, nameReuseLength]
end
path << stream.read(nameLength - nameReuseLength)
path.gsub!('\\', '/')
offset = stream.read(4).unpack('V').shift
length = stream.read(4).unpack('V').shift
into = self
File.dirname(path).split('/').each {|dir|
into = into[dir] || (into << FFFS::Directory.new(dir))
}
back = stream.tell; stream.seek(offset)
into << FFFS::File.new(::File.basename(path), stream.read(length))
stream.seek(back)
previous = path
end
end
end
class Application < Thor
include Thor::Actions
class_option :help, type: :boolean, desc: 'Show help usage'
desc 'extract FILE [PATH]', 'Extract a PAK file'
def extract (file, path='.')
PakFile.parse(File.read(file)).save(path)
end
end
Application.start(ARGV)