Module: NBTFile::WriteMethods
- Includes:
- CommonMethods, Tokens
- Included in:
- CompoundWriterState, ListWriterState, TopWriterState
- Defined in:
- lib/nbtfile.rb
Instance Method Summary collapse
- #emit_byte(io, value) ⇒ Object
- #emit_byte_array(io, value) ⇒ Object
- #emit_double(io, value) ⇒ Object
- #emit_float(io, value) ⇒ Object
- #emit_int(io, value) ⇒ Object
- #emit_integer(io, n_bytes, value) ⇒ Object
- #emit_list_header(io, type, count) ⇒ Object
- #emit_long(io, value) ⇒ Object
- #emit_short(io, value) ⇒ Object
- #emit_string(io, value) ⇒ Object
- #emit_type(io, type) ⇒ Object
- #emit_value(io, type, value, capturing, state, cont) ⇒ Object
Methods included from CommonMethods
Instance Method Details
#emit_byte(io, value) ⇒ Object
301 302 303 |
# File 'lib/nbtfile.rb', line 301 def emit_byte(io, value) emit_integer(io, 1, value) end |
#emit_byte_array(io, value) ⇒ Object
325 326 327 328 329 330 |
# File 'lib/nbtfile.rb', line 325 def emit_byte_array(io, value) value = value.dup value._nbtfile_force_encoding("BINARY") emit_int(io, value._nbtfile_bytesize) io.write(value) end |
#emit_double(io, value) ⇒ Object
321 322 323 |
# File 'lib/nbtfile.rb', line 321 def emit_double(io, value) io.write([value].pack("G")) end |
#emit_float(io, value) ⇒ Object
317 318 319 |
# File 'lib/nbtfile.rb', line 317 def emit_float(io, value) io.write([value].pack("g")) end |
#emit_int(io, value) ⇒ Object
309 310 311 |
# File 'lib/nbtfile.rb', line 309 def emit_int(io, value) emit_integer(io, 4, value) end |
#emit_integer(io, n_bytes, value) ⇒ Object
293 294 295 296 297 298 299 |
# File 'lib/nbtfile.rb', line 293 def emit_integer(io, n_bytes, value) value -= ((value & sign_bit(n_bytes)) << 1) bytes = (1..n_bytes).map do |n| byte = (value >> ((n_bytes - n) << 3) & 0xff) end io.write(bytes.pack("C*")) end |
#emit_list_header(io, type, count) ⇒ Object
345 346 347 348 |
# File 'lib/nbtfile.rb', line 345 def emit_list_header(io, type, count) emit_type(io, type) emit_int(io, count) end |
#emit_long(io, value) ⇒ Object
313 314 315 |
# File 'lib/nbtfile.rb', line 313 def emit_long(io, value) emit_integer(io, 8, value) end |
#emit_short(io, value) ⇒ Object
305 306 307 |
# File 'lib/nbtfile.rb', line 305 def emit_short(io, value) emit_integer(io, 2, value) end |
#emit_string(io, value) ⇒ Object
332 333 334 335 336 337 338 339 |
# File 'lib/nbtfile.rb', line 332 def emit_string(io, value) value = value._nbtfile_encode("UTF-8") unless value._nbtfile_valid_encoding? raise EncodingError, "Invalid character sequence" end emit_short(io, value._nbtfile_bytesize) io.write(value) end |
#emit_type(io, type) ⇒ Object
341 342 343 |
# File 'lib/nbtfile.rb', line 341 def emit_type(io, type) emit_byte(io, TOKEN_INDICES_BY_CLASS[type]) end |
#emit_value(io, type, value, capturing, state, cont) ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/nbtfile.rb', line 350 def emit_value(io, type, value, capturing, state, cont) next_state = state case when type == TAG_Byte emit_byte(io, value) when type == TAG_Short emit_short(io, value) when type == TAG_Int emit_int(io, value) when type == TAG_Long emit_long(io, value) when type == TAG_Float emit_float(io, value) when type == TAG_Double emit_double(io, value) when type == TAG_Byte_Array emit_byte_array(io, value) when type == TAG_String emit_string(io, value) when type == TAG_Float emit_float(io, value) when type == TAG_Double emit_double(io, value) when type == TAG_List next_state = ListWriterState.new(state, value, capturing) when type == TAG_Compound next_state = CompoundWriterState.new(state, capturing) when type == TAG_End next_state = cont else raise RuntimeError, "Unexpected token #{type}" end next_state end |