Class: Mix1::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/mix1.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path:, package:, memory_buffer: false, buffer_size: DEFAULT_BUFFER_SIZE) ⇒ Writer

Returns a new instance of Writer.



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/mix1.rb', line 273

def initialize(file_path:, package:, memory_buffer: false, buffer_size: DEFAULT_BUFFER_SIZE)
  @package = package

  @buffer = MemoryBuffer.new(file_path: file_path, mode: :write, buffer_size: buffer_size)
  @buffer.pos = 0

  @buffer.write("MIX1")

  files = @package.files.sort_by(&:file_crc)

  @buffer.pos = 16

  files.each do |file|
    file.content_offset = @buffer.pos
    file.content_length = file.data.length
    @buffer.write(file.data)

    @buffer.pos += -@buffer.pos & 7
  end

  file_data_offset = @buffer.pos
  write_i32(files.count)

  files.each do |file|
    write_u32(file.file_crc.to_i(16))
    write_u32(file.content_offset)
    write_u32(file.content_length)
  end

  file_name_offset = @buffer.pos
  write_i32(files.count)

  files.each do |file|
    write_byte(file.name.length + 1)
    @buffer.write("#{file.name}\0")
  end

  @buffer.write_header(data_offset: file_data_offset, name_offset: file_name_offset)
ensure
  @buffer&.close
end

Instance Attribute Details

#packageObject (readonly)

Returns the value of attribute package.



271
272
273
# File 'lib/mix1.rb', line 271

def package
  @package
end

Instance Method Details

#write_byte(byte) ⇒ Object



323
324
325
# File 'lib/mix1.rb', line 323

def write_byte(byte)
  @buffer.write([byte].pack("c"))
end

#write_i32(int) ⇒ Object



315
316
317
# File 'lib/mix1.rb', line 315

def write_i32(int)
  @buffer.write([int].pack("l"))
end

#write_u32(uint) ⇒ Object



319
320
321
# File 'lib/mix1.rb', line 319

def write_u32(uint)
  @buffer.write([uint].pack("L"))
end