Class: Table

Inherits:
Object
  • Object
show all
Defined in:
lib/RGSS.rb

Constant Summary collapse

MAX_ROW_LENGTH =
20

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ Table

Returns a new instance of Table.



24
25
26
27
28
29
30
31
32
# File 'lib/RGSS.rb', line 24

def initialize(bytes)
    @dim, @x, @y, @z, items, *@data = bytes.unpack('L5 S*')
    unless items == @data.length
        raise 'Size mismatch loading Table from data'
    end
    unless @x * @y * @z == items
        raise 'Size mismatch loading Table from data'
    end
end

Class Method Details

._load(bytes) ⇒ Object



79
80
81
# File 'lib/RGSS.rb', line 79

def self._load(bytes)
    Table.new(bytes)
end

Instance Method Details

#_dump(*ignored) ⇒ Object



75
76
77
# File 'lib/RGSS.rb', line 75

def _dump(*ignored)
    return [@dim, @x, @y, @z, @x * @y * @z, *@data].pack('L5 S*')
end

#encode_with(coder) ⇒ Object



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
# File 'lib/RGSS.rb', line 36

def encode_with(coder)
    coder.style = Psych::Nodes::Mapping::BLOCK

    coder['dim'] = @dim
    coder['x'] = @x
    coder['y'] = @y
    coder['z'] = @z

    if @x * @y * @z > 0
        stride = @x < 2 ? (@y < 2 ? @z : @y) : @x
        rows = @data.each_slice(stride).to_a
        if MAX_ROW_LENGTH != -1 && stride > MAX_ROW_LENGTH
            block_length = (stride + MAX_ROW_LENGTH - 1) / MAX_ROW_LENGTH
            row_length = (stride + block_length - 1) / block_length
            rows =
                rows
                    .collect { |x| x.each_slice(row_length).to_a }
                    .flatten(1)
        end
        rows = rows.collect { |x| x.collect { |y| '%04x' % y }.join(' ') }
        coder['data'] = rows
    else
        coder['data'] = []
    end
end

#init_with(coder) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/RGSS.rb', line 62

def init_with(coder)
    @dim = coder['dim']
    @x = coder['x']
    @y = coder['y']
    @z = coder['z']
    @data =
        coder['data'].collect { |x| x.split(' ').collect(&:hex) }.flatten
    items = @x * @y * @z
    unless items == @data.length
        raise 'Size mismatch loading Table from YAML'
    end
end