Class: Sc2::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sc2/magick/parser.rb

Class Method Summary collapse

Class Method Details

.parse(data) ⇒ Object



3
4
5
# File 'lib/sc2/magick/parser.rb', line 3

def self.parse data
  self.parse_recur String.new data
end

.parse_recur(data) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sc2/magick/parser.rb', line 7

def self.parse_recur data
  case data.slice!(0).bytes.next
    when 2
      data.slice! 0, vlf(data)
    when 4
      data.slice! 0, 2
      (0...vlf(data)).map {|i| parse_recur data }
    when 5
      Hash.[]((0...vlf(data)).map do |i| 
        [vlf(data), parse_recur(data)]
      end)
    when 6
      data.slice! 0
    when 7
      data.slice!(0, 4).unpack("V")[0]
    when 9
      vlf data
    else
      nil
  end
end

.vlf(data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sc2/magick/parser.rb', line 29

def self.vlf data
  ret, shift = 0, 0
  loop do
    char = data.slice!(0)
    return nil unless char
    byte = char.bytes.next
    ret += (byte & 0x7F) << (7 * shift)
    break if byte & 0x80 == 0
    shift += 1
  end
  (ret >> 1) * ((ret & 0x1) == 0 ? 1 : -1)
end