Class: Doom::Wad::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/doom/wad/reader.rb

Defined Under Namespace

Classes: DirectoryEntry

Constant Summary collapse

IWAD =
'IWAD'
PWAD =
'PWAD'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Reader

Returns a new instance of Reader.



13
14
15
16
17
# File 'lib/doom/wad/reader.rb', line 13

def initialize(path)
  @file = File.open(path, 'rb')
  read_header
  read_directory
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



11
12
13
# File 'lib/doom/wad/reader.rb', line 11

def directory
  @directory
end

#num_lumpsObject (readonly)

Returns the value of attribute num_lumps.



11
12
13
# File 'lib/doom/wad/reader.rb', line 11

def num_lumps
  @num_lumps
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/doom/wad/reader.rb', line 11

def type
  @type
end

Instance Method Details

#closeObject



48
49
50
# File 'lib/doom/wad/reader.rb', line 48

def close
  @file.close
end

#find_lump(name) ⇒ Object



19
20
21
# File 'lib/doom/wad/reader.rb', line 19

def find_lump(name)
  @directory.find { |entry| entry.name == name.upcase }
end

#lumps_between(start_marker, end_marker) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/doom/wad/reader.rb', line 40

def lumps_between(start_marker, end_marker)
  start_idx = @directory.index { |e| e.name == start_marker }
  end_idx = @directory.index { |e| e.name == end_marker }
  return [] unless start_idx && end_idx

  @directory[start_idx + 1...end_idx]
end

#read_lump(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/doom/wad/reader.rb', line 23

def read_lump(name)
  return @lump_cache[name] if @lump_cache.key?(name)

  entry = find_lump(name)
  return nil unless entry

  @file.seek(entry.offset)
  data = @file.read(entry.size)
  @lump_cache[name] = data
  data
end

#read_lump_at(entry) ⇒ Object



35
36
37
38
# File 'lib/doom/wad/reader.rb', line 35

def read_lump_at(entry)
  @file.seek(entry.offset)
  @file.read(entry.size)
end