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
303 304 305 |
# File 'lib/nbtfile.rb', line 303 def emit_byte(io, value) emit_integer(io, 1, value) end |
#emit_byte_array(io, value) ⇒ Object
327 328 329 330 331 332 |
# File 'lib/nbtfile.rb', line 327 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
323 324 325 |
# File 'lib/nbtfile.rb', line 323 def emit_double(io, value) io.write([value].pack("G")) end |
#emit_float(io, value) ⇒ Object
319 320 321 |
# File 'lib/nbtfile.rb', line 319 def emit_float(io, value) io.write([value].pack("g")) end |
#emit_int(io, value) ⇒ Object
311 312 313 |
# File 'lib/nbtfile.rb', line 311 def emit_int(io, value) emit_integer(io, 4, value) end |
#emit_integer(io, n_bytes, value) ⇒ Object
295 296 297 298 299 300 301 |
# File 'lib/nbtfile.rb', line 295 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
347 348 349 350 |
# File 'lib/nbtfile.rb', line 347 def emit_list_header(io, type, count) emit_type(io, type) emit_int(io, count) end |
#emit_long(io, value) ⇒ Object
315 316 317 |
# File 'lib/nbtfile.rb', line 315 def emit_long(io, value) emit_integer(io, 8, value) end |
#emit_short(io, value) ⇒ Object
307 308 309 |
# File 'lib/nbtfile.rb', line 307 def emit_short(io, value) emit_integer(io, 2, value) end |
#emit_string(io, value) ⇒ Object
334 335 336 337 338 339 340 341 |
# File 'lib/nbtfile.rb', line 334 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
343 344 345 |
# File 'lib/nbtfile.rb', line 343 def emit_type(io, type) emit_byte(io, TOKEN_INDICES_BY_CLASS[type]) end |
#emit_value(io, type, value, capturing, state, cont) ⇒ Object
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 386 387 |
# File 'lib/nbtfile.rb', line 352 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 |