rrb-common 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c0ce773dfdfb39eebd3eca206e2f583acc855bbbd4a00677d7bc0e4e7f3d228
4
- data.tar.gz: e7054853f2f0f99977ecb8a64fa18cfcd6159b07af9cf4815ad08988275d6353
3
+ metadata.gz: 77d064b5d3c6ebddbf4758b0c4848f35159c8181c503cd8c8438390605e3071c
4
+ data.tar.gz: 339bd670c3725bd9610ca0c247b3902c07bd8af7a6ad8bb894878dcf02ca8669
5
5
  SHA512:
6
- metadata.gz: 9a8566a6cccb99f6c084c2fb012e38b8d47893173de6305d4c48dd01cf07bbad3404e1e969512728c4358c41cf73fdb8d8e887d20d636e5f2a0a8cebb2546119
7
- data.tar.gz: '024422087af5f9646086bda2549af7cbac08facf82eb3cb76df32665136fc0dabcdb8bcf5d983e89dbe374811dcbbbb817a7f9d51fe20f040127e8ab2958f46c'
6
+ metadata.gz: 05c95f37e85863385d75c8bd9dca484a86a51bd45c84dab4e91abda6e948997c9bdaa21f96f8fa281d614644c21712497345a47f998cfa271e13059b524c73b9
7
+ data.tar.gz: 86fe15955d0fc6c5c702e2b4cbe6109e97d5d3987612eb6510c513df7a47425bcaee14df391697163fd9fd5d6a106b568fae893afe714e9b53fabeb57ee01f14
data/lib/rune/common.rb CHANGED
@@ -15,13 +15,14 @@ module RuneRb
15
15
 
16
16
  # The Core module contains the core classes and modules of the Rune.rb framework.
17
17
  module Core
18
- autoload :Identifiable, 'rune/core/identifiable'
19
- autoload :Logging, 'rune/core/logging'
20
- autoload :Unit, 'rune/core/unit'
18
+ autoload :Archive, 'rune/core/archive'
21
19
  autoload :Buffer, 'rune/core/buffer'
22
20
  autoload :Constants, 'rune/core/constants'
21
+ autoload :Identifiable, 'rune/core/identifiable'
23
22
  autoload :ISAAC, 'rune/core/isaac'
23
+ autoload :Logging, 'rune/core/logging'
24
24
  autoload :ReadableBuffer, 'rune/core/readable_buffer'
25
+ autoload :Unit, 'rune/core/unit'
25
26
  autoload :WriteableBuffer, 'rune/core/writeable_buffer'
26
27
 
27
28
  include Constants
@@ -0,0 +1,116 @@
1
+ module RuneRb::Core
2
+ class Archive
3
+ include RuneRb::Core::Logging
4
+
5
+ # Constructs a new Archive object.
6
+ # @param path [String] the path to the cache directory.
7
+ def initialize(path = 'cache/')
8
+ unless check_for_archives(path)
9
+ error "Cache archive not found. Please install them into the #{path} directory."
10
+ return
11
+ end
12
+
13
+ # Find how many index files exist
14
+ count = 0.upto(255).each do |i|
15
+ break i unless File.exist?(File.join(path, "main_file_cache.idx#{i}"))
16
+ end
17
+
18
+ # Gather file objects
19
+ @data_file = File.open(File.join(path, "main_file_cache.dat"), "r")
20
+ @index_files = []
21
+
22
+ count.times do |i|
23
+ @index_files << File.open(File.join(path, "main_file_cache.idx#{i}"), "r")
24
+ end
25
+ end
26
+
27
+ # Gets a file from the cache.
28
+ # @param cache [Integer] the cache archive to get the file from.
29
+ # @param file [Integer] the file to get.
30
+ def get(cache, file)
31
+ # then convert crc table stuff (easy with array pack)
32
+ # then make server send data back to the client
33
+
34
+ index_file = @index_files[cache]
35
+ cache += 1
36
+
37
+ index = IO.read(index_file.path, RuneRb::Core::INDEX_SIZE, RuneRb::Core::INDEX_SIZE * file)
38
+ idx_buffer = RuneRb::Core::Buffer.new(mode: 'r')
39
+ idx_buffer << index
40
+
41
+ file_size = (idx_buffer.read(type: :byte, signed: false) << 16) | (idx_buffer.read(type: :byte, signed: false) << 8) | idx_buffer.read(type: :byte, signed: false)
42
+ file_block = (idx_buffer.read(type: :byte, signed: false) << 16) | (idx_buffer.read(type: :byte, signed: false) << 8) | idx_buffer.read(type: :byte, signed: false)
43
+
44
+ remaining_bytes = file_size
45
+ current_block = file_block
46
+ payload = ""
47
+ cycles = 0
48
+
49
+ while remaining_bytes > 0
50
+ size = RuneRb::Core::DATA_SIZE
51
+ rem = @data_file.size - current_block * RuneRb::Core::DATA_SIZE
52
+ size = rem if rem < RuneRb::Core::DATA_SIZE
53
+
54
+ header = IO.read(@data_file.path, RuneRb::Core::DATA_HEADER_SIZE, current_block * RuneRb::Cache::DATA_SIZE)
55
+ header_buffer = RuneRb::Core::Buffer.new(mode: 'r')
56
+ header_buffer << header
57
+
58
+ next_file_id = header_buffer.read(type: :short, signed: false)
59
+ current_part_id = header_buffer.read(type: :short, signed: false)
60
+ next_block_id = (header_buffer.read(type: :byte, signed: false) << 16) | (header_buffer.read(type: :byte, signed: false) << 8) | header_buffer.read(type: :byte, signed: false)
61
+ next_cache_id = header_buffer.read(type: :byte, signed: false)
62
+
63
+ size -= 8
64
+
65
+ cycle_bytes = remaining_bytes > RuneRb::Core::DATA_BLOCK_SIZE ? RuneRb::Core::DATA_BLOCK_SIZE : remaining_bytes
66
+
67
+ payload << IO.read(@data_file.path, cycle_bytes, current_block * RuneRb::Core::DATA_SIZE + RuneRb::Core::DATA_HEADER_SIZE)
68
+
69
+ remaining_bytes -= cycle_bytes
70
+
71
+ raise "Cycle does not match part id." if cycles != current_part_id
72
+
73
+ if remaining_bytes > 0
74
+ raise "Unexpected next cache id." if next_cache_id != cache
75
+ raise "Unexpected next file id." if next_file_id != file
76
+ end
77
+
78
+ cycles += 1
79
+ current_block = next_block_id
80
+ end
81
+
82
+ payload
83
+ end
84
+
85
+ # Closes the archive file streams.
86
+ def close
87
+ @data_file.close
88
+ @index_files.each {|file| file.close }
89
+ end
90
+
91
+ # The number of index files in the cache.
92
+ # @return [Integer] the number of index files in the cache.
93
+ def cache_count
94
+ @index_files.size
95
+ end
96
+
97
+ # The number of files in a cache.
98
+ # @param cache [Integer] the cache to check.
99
+ def file_count(cache)
100
+ (@index_files[cache].size / RuneRb::Core::INDEX_SIZE) - 1
101
+ end
102
+
103
+ private
104
+
105
+ # Checks for the existence of cache archives in a path.
106
+ # @param path [String] the path to the cache directory.
107
+ def check_for_archives(path)
108
+ return false if path.nil?
109
+ return false if !File.directory?(path)
110
+ return false if !File.exist?(path)
111
+ return false if !File.readable?(path)
112
+ return false if Dir.empty?(path)
113
+ true
114
+ end
115
+ end
116
+ end
@@ -18,4 +18,20 @@ module RuneRb::Core::Constants
18
18
  # Bit masks for bit packing
19
19
  # @return [Array<Integer>]
20
20
  BIT_MASK_OUT = (0...32).collect { (1 << _1) - 1 }.freeze
21
+
22
+ # The size of a single index
23
+ # @return [Integer]
24
+ INDEX_SIZE = 0x6
25
+
26
+ # The size of a single data block
27
+ # @return [Integer]
28
+ DATA_BLOCK_SIZE = 0x200
29
+
30
+ # The size of a single data header
31
+ # @return [Integer]
32
+ DATA_HEADER_SIZE = 0x8
33
+
34
+ # The size of a chunk of data
35
+ # @return [Integer]
36
+ DATA_SIZE = DATA_BLOCK_SIZE + DATA_HEADER_SIZE
21
37
  end
@@ -141,7 +141,7 @@ module RuneRb::Core::WriteableBuffer
141
141
  def write_bytes(values)
142
142
  case values
143
143
  when Array then values.each { |byte| write_byte(byte.to_i) }
144
- when RuneRb::Core::Buffer then send(:put, values)
144
+ when RuneRb::Core::Buffer then @data.concat(values.instance_variable_get(:@data))
145
145
  when String then send(:<<, values)
146
146
  end
147
147
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rrb-common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick W.
@@ -157,6 +157,7 @@ extensions: []
157
157
  extra_rdoc_files: []
158
158
  files:
159
159
  - lib/rune/common.rb
160
+ - lib/rune/core/archive.rb
160
161
  - lib/rune/core/buffer.rb
161
162
  - lib/rune/core/constants.rb
162
163
  - lib/rune/core/identifiable.rb