Module: NBTFile

Defined in:
lib/nbtfile.rb

Defined Under Namespace

Modules: CommonMethods, ReadMethods, Tokens, WriteMethods Classes: BaseToken, CompoundReaderState, CompoundWriterState, EncodingError, EndReaderState, EndWriterState, ListReaderState, ListWriterState, Reader, TopReaderState, TopWriterState, Writer

Constant Summary collapse

TOKEN_CLASSES_BY_INDEX =
[]
TOKEN_INDICES_BY_CLASS =
{}

Class Method Summary collapse

Class Method Details

.emit(io) ⇒ Object



530
531
532
533
534
535
536
537
# File 'lib/nbtfile.rb', line 530

def self.emit(io)
  writer = Writer.new(io)
  begin
    yield writer
  ensure
    writer.finish
  end
end

.load(io) ⇒ Object



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/nbtfile.rb', line 539

def self.load(io)
  root = {}
  stack = [root]

  self.tokenize(io) do |token|
    case token
    when Tokens::TAG_Compound
      value = {}
    when Tokens::TAG_List
      value = []
    when Tokens::TAG_End
      stack.pop
      next
    else
      value = token.value
    end

    stack.last[token.name] = value

    case token
    when Tokens::TAG_Compound, Tokens::TAG_List
      stack.push value
    end
  end

  root.first
end

.tokenize(io) ⇒ Object



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/nbtfile.rb', line 514

def self.tokenize(io)
  case io
  when String
    io = StringIO.new(io, "rb")
  end
  reader = Reader.new(io)

  if block_given?
    reader.each_token { |token| yield token }
  else
    tokens = []
    reader.each_token { |token| tokens << token }
    tokens
  end
end