Class: MppReader::Blocks::Props

Inherits:
Object
  • Object
show all
Defined in:
lib/mpp_reader/blocks/props.rb

Overview

A Props block: a map of integer keys to raw byte values, used for project-level settings and the field-map definitions. Layout (ported from MPXJ Props14): 16-byte header with the entry count as a u16 at offset 12, then entries of (size:u32, key:u32, unknown:u32, data), each padded to a 2-byte boundary.

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Props

Returns a new instance of Props.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mpp_reader/blocks/props.rb', line 9

def initialize(data)
  @map = {}
  count = data.byteslice(12, 2).to_s.unpack1("v").to_i
  pos = 16
  count.times do
    break if pos + 12 > data.bytesize

    size, key = data.byteslice(pos, 8).unpack("VV")
    pos += 12
    break if size < 1 || pos + size > data.bytesize

    @map[key] = data.byteslice(pos, size)
    pos += size + (size.odd? ? 1 : 0)
  end
end

Instance Method Details

#[](key) ⇒ Object



25
# File 'lib/mpp_reader/blocks/props.rb', line 25

def [](key) = @map[key]

#int(key) ⇒ Object



27
28
29
30
# File 'lib/mpp_reader/blocks/props.rb', line 27

def int(key)
  bytes = @map[key]
  bytes && bytes.bytesize >= 4 ? bytes.unpack1("V") : nil
end

#short(key) ⇒ Object



32
33
34
35
# File 'lib/mpp_reader/blocks/props.rb', line 32

def short(key)
  bytes = @map[key]
  bytes && bytes.bytesize >= 2 ? bytes.unpack1("v") : nil
end

#unicode_string(key) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/mpp_reader/blocks/props.rb', line 37

def unicode_string(key)
  bytes = @map[key]
  return nil unless bytes

  bytes.force_encoding(Encoding::UTF_16LE)
       .encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
       .sub(/\0.*\z/m, "")
end