Module: NBTFile::Private::EmitMethods

Includes:
CommonMethods, Tokens
Included in:
CompoundEmitterState, ListEmitterState, TopEmitterState
Defined in:
lib/nbtfile.rb

Instance Method Summary collapse

Methods included from CommonMethods

#sign_bit

Instance Method Details

#emit_byte(io, value) ⇒ Object



331
332
333
# File 'lib/nbtfile.rb', line 331

def emit_byte(io, value)
  emit_integer(io, 1, value)
end

#emit_byte_array(io, value) ⇒ Object



355
356
357
358
359
360
# File 'lib/nbtfile.rb', line 355

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



351
352
353
# File 'lib/nbtfile.rb', line 351

def emit_double(io, value)
  io.write([value].pack("G"))
end

#emit_float(io, value) ⇒ Object



347
348
349
# File 'lib/nbtfile.rb', line 347

def emit_float(io, value)
  io.write([value].pack("g"))
end

#emit_int(io, value) ⇒ Object



339
340
341
# File 'lib/nbtfile.rb', line 339

def emit_int(io, value)
  emit_integer(io, 4, value)
end

#emit_integer(io, n_bytes, value) ⇒ Object



323
324
325
326
327
328
329
# File 'lib/nbtfile.rb', line 323

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



375
376
377
378
# File 'lib/nbtfile.rb', line 375

def emit_list_header(io, type, count)
  emit_type(io, type)
  emit_int(io, count)
end

#emit_long(io, value) ⇒ Object



343
344
345
# File 'lib/nbtfile.rb', line 343

def emit_long(io, value)
  emit_integer(io, 8, value)
end

#emit_short(io, value) ⇒ Object



335
336
337
# File 'lib/nbtfile.rb', line 335

def emit_short(io, value)
  emit_integer(io, 2, value)
end

#emit_string(io, value) ⇒ Object



362
363
364
365
366
367
368
369
# File 'lib/nbtfile.rb', line 362

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



371
372
373
# File 'lib/nbtfile.rb', line 371

def emit_type(io, type)
  emit_byte(io, TOKEN_INDICES_BY_CLASS[type])
end

#emit_value(io, type, value, capturing, state, cont) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/nbtfile.rb', line 380

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 = ListEmitterState.new(state, value, capturing)
  when type == TAG_Compound
    next_state = CompoundEmitterState.new(state, capturing)
  when type == TAG_End
    next_state = cont
  else
    raise RuntimeError, "Unexpected token #{type}"
  end

  next_state
end