Module: NBTFile::ReadMethods

Includes:
CommonMethods, Tokens
Included in:
CompoundReaderState, ListReaderState, TopReaderState
Defined in:
lib/nbtfile.rb

Instance Method Summary collapse

Methods included from CommonMethods

#sign_bit

Instance Method Details

#read_byte(io) ⇒ Object



119
120
121
# File 'lib/nbtfile.rb', line 119

def read_byte(io)
  read_integer(io, 1)
end

#read_byte_array(io) ⇒ Object



150
151
152
153
154
155
# File 'lib/nbtfile.rb', line 150

def read_byte_array(io)
  length = read_int(io)
  value = read_raw(io, length)
  value._nbtfile_force_encoding("BINARY")
  value
end

#read_double(io) ⇒ Object



139
140
141
# File 'lib/nbtfile.rb', line 139

def read_double(io)
  read_raw(io, 8).unpack("G").first
end

#read_float(io) ⇒ Object



135
136
137
# File 'lib/nbtfile.rb', line 135

def read_float(io)
  read_raw(io, 4).unpack("g").first
end

#read_int(io) ⇒ Object



127
128
129
# File 'lib/nbtfile.rb', line 127

def read_int(io)
  read_integer(io, 4)
end

#read_integer(io, n_bytes) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/nbtfile.rb', line 110

def read_integer(io, n_bytes)
  raw_value = read_raw(io, n_bytes)
  value = (0...n_bytes).reduce(0) do |accum, n|
    (accum << 8) | raw_value._nbtfile_getbyte(n)
  end
  value -= ((value & sign_bit(n_bytes)) << 1)
  value
end

#read_list_header(io) ⇒ Object



157
158
159
160
161
# File 'lib/nbtfile.rb', line 157

def read_list_header(io)
  list_type = read_type(io)
  list_length = read_int(io)
  [list_type, list_length]
end

#read_long(io) ⇒ Object



131
132
133
# File 'lib/nbtfile.rb', line 131

def read_long(io)
  read_integer(io, 8)
end

#read_raw(io, n_bytes) ⇒ Object

Raises:

  • (EOFError)


104
105
106
107
108
# File 'lib/nbtfile.rb', line 104

def read_raw(io, n_bytes)
  data = io.read(n_bytes)
  raise EOFError unless data and data.length == n_bytes
  data
end

#read_short(io) ⇒ Object



123
124
125
# File 'lib/nbtfile.rb', line 123

def read_short(io)
  read_integer(io, 2)
end

#read_string(io) ⇒ Object



143
144
145
146
147
148
# File 'lib/nbtfile.rb', line 143

def read_string(io)
  length = read_short(io)
  string = read_raw(io, length)
  string._nbtfile_force_encoding("UTF-8")
  string
end

#read_type(io) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/nbtfile.rb', line 163

def read_type(io)
  byte = read_byte(io)
  begin
    TOKEN_CLASSES_BY_INDEX.fetch(byte)
  rescue IndexError
    raise RuntimeError, "Unexpected tag ordinal #{byte}"
  end
end

#read_value(io, type, name, state, cont) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/nbtfile.rb', line 172

def read_value(io, type, name, state, cont)
  next_state = state

  case
  when type == TAG_End
    next_state = cont
    value = nil
  when type == TAG_Byte
    value = read_byte(io)
  when type == TAG_Short
    value = read_short(io)
  when type == TAG_Int
    value = read_int(io)
  when type == TAG_Long
    value = read_long(io)
  when type == TAG_Float
    value = read_float(io)
  when type == TAG_Double
    value = read_double(io)
  when type == TAG_Byte_Array
    value = read_byte_array(io)
  when type == TAG_String
    value = read_string(io)
  when type == TAG_List
    list_type, list_length = read_list_header(io)
    next_state = ListReaderState.new(state, list_type, list_length)
    value = list_type
  when type == TAG_Compound
    next_state = CompoundReaderState.new(state)
  end

  [next_state, type[name, value]]
end