rrb-common 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c0ce773dfdfb39eebd3eca206e2f583acc855bbbd4a00677d7bc0e4e7f3d228
4
- data.tar.gz: e7054853f2f0f99977ecb8a64fa18cfcd6159b07af9cf4815ad08988275d6353
3
+ metadata.gz: 8830b14ecfcf37cc6afb5779915d9beeaf84a137d38ca984d832d588ca7ecc1b
4
+ data.tar.gz: 8b4e8eced92a7c05e6161d9886c6f11e02614d82f9c0257b5ba9342834118867
5
5
  SHA512:
6
- metadata.gz: 9a8566a6cccb99f6c084c2fb012e38b8d47893173de6305d4c48dd01cf07bbad3404e1e969512728c4358c41cf73fdb8d8e887d20d636e5f2a0a8cebb2546119
7
- data.tar.gz: '024422087af5f9646086bda2549af7cbac08facf82eb3cb76df32665136fc0dabcdb8bcf5d983e89dbe374811dcbbbb817a7f9d51fe20f040127e8ab2958f46c'
6
+ metadata.gz: 6b350710235c41abad111df718f214c408a4ae69b223a7fbfb4f477ea1611720ce072429e8ebc19d6aa198e3d0f5a18c10f469ef5d5e4d988b98c25781ced03d
7
+ data.tar.gz: 9e83c03b05779b74f959689d8b5709dc83dbcb0ec2cc6f0ce43a90a6bfc90402cd54ff2334cd5b37e007d70f7eff3b6ac4612ee73706f20c2aad8ffa95d23079
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::Core::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,14 +1,14 @@
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.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick W.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-11 00:00:00.000000000 Z
11
+ date: 2023-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv
@@ -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