Class: RuneRb::Cache::Archive

Inherits:
Object
  • Object
show all
Includes:
RuneRb::Core::Logging
Defined in:
lib/rune/cache/archive.rb

Overview

An archive of data in the Cache.

Since:

  • 0.1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RuneRb::Core::Logging

#err, #err!, #log, #log!

Constructor Details

#initialize(entries) ⇒ Archive

Constructs a new CacheArchive

Parameters:

  • entries (Array<Hash>)

    the entries in the archive

Since:

  • 0.1.0



12
13
14
# File 'lib/rune/cache/archive.rb', line 12

def initialize(entries)
  @entries = entries
end

Instance Attribute Details

#entriesArray<Hash> (readonly)

Returns the entries in the archive.

Returns:

  • (Array<Hash>)

    the entries in the archive

Since:

  • 0.1.0



8
9
10
# File 'lib/rune/cache/archive.rb', line 8

def entries
  @entries
end

Class Method Details

.decode(buffer) ⇒ Object

Decodes an archive from the specified buffer.

Parameters:

Since:

  • 0.1.0



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rune/cache/archive.rb', line 18

def self.decode(buffer)
  extracted_size = buffer.read(type: :int24)
  size = buffer.read(type: :int24)
  extracted = false

  if size != extracted_size
    data = buffer.data.slice!(0..size)
    data = StringIO.new(data)
    decompressor = RBzip2.default_adapter::Decompressor.new(data)
    buffer = RuneRb::Core::Buffer.new(mode: 'r')
    buffer << decompressor.read
    extracted = true
  end

  entry_count = buffer.read(type: :short)
  identifiers = Array.new(entry_count)
  extracted_sizes = Array.new(entry_count)
  sizes = Array.new(entry_count)

  entry_count.times do |i|
    identifiers[i] = buffer.read(type: :int)
    extracted_sizes[i] = buffer.read(type: :int24)
    sizes[i] = buffer.read(type: :int24)
  end

  entries = Array.new(entry_count)
  entry_count.times do |entry|
    entry_buffer = RuneRb::Core::Buffer.new(mode: 'r')

    if extracted == false
      data = buffer.data.slice!(0..sizes[entry])
      data = StringIO.new(data)
      decompressor = RBzip2.default_adapter::Decompressor.new(data)
      entry_buffer << decompressor.read
    else
      data = buffer.data.slice!(0..extracted_sizes[entry])
      entry_buffer << data 
    end

    entries[entry] = { id: identifiers[entry], buffer: entry_buffer }
  end

  RuneRb::Cache::Archive.new(entries)
end