Class: Wad

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-doom.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verbose = false) ⇒ Wad

Returns a new instance of Wad.



665
666
667
668
669
# File 'lib/ruby-doom.rb', line 665

def initialize(verbose=false)
  @verbose = verbose
  @bytes = []
  @lumps = []
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



664
665
666
# File 'lib/ruby-doom.rb', line 664

def bytes
  @bytes
end

#directory_offsetObject (readonly)

Returns the value of attribute directory_offset.



664
665
666
# File 'lib/ruby-doom.rb', line 664

def directory_offset
  @directory_offset
end

#headerObject (readonly)

Returns the value of attribute header.



664
665
666
# File 'lib/ruby-doom.rb', line 664

def header
  @header
end

#lumpsObject (readonly)

Returns the value of attribute lumps.



664
665
666
# File 'lib/ruby-doom.rb', line 664

def lumps
  @lumps
end

Instance Method Details

#playerObject



690
691
692
# File 'lib/ruby-doom.rb', line 690

def player
  @lumps.find {|x| x.name == "THINGS"}.player
end

#read(filename) ⇒ Object



670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'lib/ruby-doom.rb', line 670

def read(filename)
  puts "Reading WAD into memory" unless !@verbose
  File.new(filename).each_byte {|b| 
    @bytes << b 
    puts "Read " + (@bytes.size/1000).to_s + " KB so far " unless (!@verbose or @bytes.size % 500000 != 0)
  }
  puts "Done reading, building the object model" unless !@verbose
  @header = Header.new
  @header.read(@bytes.slice(0,Header::BYTES_EACH))
  @header.lump_count.times {|directory_entry_index|
    de = DirectoryEntry.new
    de.read(@bytes.slice((directory_entry_index*DirectoryEntry::BYTES_EACH)+@header.directory_offset,DirectoryEntry::BYTES_EACH))
    lump = de.create_lump(@bytes)
    puts "Created " + lump.name unless !@verbose
    @lumps << lump
  }
  puts "Object model built" unless !@verbose
  puts "The file " + filename + " is a " + @bytes.size.to_s + " byte " + @header.type unless !@verbose
  puts "It's got " + @lumps.size.to_s + " lumps, the directory started at byte " + @header.directory_offset.to_s unless !@verbose
end

#write(filename = nil) ⇒ Object



693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
# File 'lib/ruby-doom.rb', line 693

def write(filename=nil)
  puts "Writing WAD" unless !@verbose
  out = []
  ptr = Header::BYTES_EACH
  entries = []
  @lumps.each {|lump|
    entries << DirectoryEntry.new(ptr, lump.size, lump.name)
    out += lump.write
    ptr += lump.size
  }
  entries.each {|e| out += e.write }
  # now go back and fill in the directory offset in the header
  h = Header.new("PWAD")
  h.directory_offset = ptr
  h.lump_count = @lumps.size
  out = h.write + out
  File.open(filename, "w") {|f| out.each {|b| f.putc(b) } } unless filename == nil
  puts "Done" unless !@verbose
  return out
end