Class: Yuki::VD

Inherits:
Object
  • Object
show all
Defined in:
lib/psdk/helpers/900 Yuki__VD.rb

Overview

Class that helps to read Virtual Directories

In reading mode, the Virtual Directories can be loaded to RAM if MAX_SIZE >= VD.size

All the filenames inside the Yuki::VD has to be downcased filename in utf-8

Note : Encryption is up to the developper and no longer supported on the basic script

Constant Summary collapse

DEBUG_ON =

Is the debug info on ?

ARGV.include?('debug-yuki-vd')
MAX_SIZE =

The max size of the file that can be loaded in memory

10 * 1024 * 1024
ALLOWED_MODES =

List of allowed modes

%i[read write update]
POINTER_SIZE =

Size of the pointer at the begin of the file

4
UNPACK_METHOD =

Unpack method of the pointer at the begin of the file

'L'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, mode) ⇒ VD

Create a new Yuki::VD file or load it

Parameters:

  • filename (String)

    name of the Yuki::VD file

  • mode (:read, :write, :update)

    if we read or write the virtual directory



27
28
29
30
31
# File 'lib/psdk/helpers/900 Yuki__VD.rb', line 27

def initialize(filename, mode)
  @mode = mode = fix_mode(mode)
  @filename = filename
  send("initialize_#{mode}")
end

Instance Attribute Details

#filenameString (readonly)

Returns the filename of the current Yuki::VD.

Returns:

  • (String)

    the filename of the current Yuki::VD



12
13
14
# File 'lib/psdk/helpers/900 Yuki__VD.rb', line 12

def filename
  @filename
end

Instance Method Details

#add_file(filename, ext_name = nil) ⇒ Object

Add a file to the Yuki::VD

Parameters:

  • filename (String)

    the file name

  • ext_name (String, nil) (defaults to: nil)

    the file extension



68
69
70
71
# File 'lib/psdk/helpers/900 Yuki__VD.rb', line 68

def add_file(filename, ext_name = nil)
  sub_filename = ext_name ? "#{filename}.#{ext_name}" : filename
  write_data(filename, File.binread(sub_filename))
end

#closeObject

Close the VD



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/psdk/helpers/900 Yuki__VD.rb', line 80

def close
  return unless @file

  if @mode != :read
    pos = [@file.pos].pack(UNPACK_METHOD)
    @file.write(Marshal.dump(@hash))
    @file.pos = 0
    @file.write(pos)
  end
  @file.close
  @file = nil
end

#exists?(filename) ⇒ Boolean

Test if a file exists in the VD

Parameters:

  • filename (String)

Returns:

  • (Boolean)


50
51
52
# File 'lib/psdk/helpers/900 Yuki__VD.rb', line 50

def exists?(filename)
  @hash[filename] != nil
end

#get_filenamesArray<String>

Get all the filename

Returns:

  • (Array<String>)


75
76
77
# File 'lib/psdk/helpers/900 Yuki__VD.rb', line 75

def get_filenames
  @hash.keys
end

#read_data(filename) ⇒ String?

Read a file data from the VD

Parameters:

  • filename (String)

    the file we want to read its data

Returns:

  • (String, nil)

    the data of the file



36
37
38
39
40
41
42
43
44
45
# File 'lib/psdk/helpers/900 Yuki__VD.rb', line 36

def read_data(filename)
  return nil unless @file

  pos = @hash[filename]
  return nil unless pos

  @file.pos = pos
  size = @file.read(POINTER_SIZE).unpack1(UNPACK_METHOD)
  return @file.read(size)
end

#write_data(filename, data) ⇒ Object

Write a file with its data in the VD

Parameters:

  • filename (String)

    the file name

  • data (String)

    the data of the file



57
58
59
60
61
62
63
# File 'lib/psdk/helpers/900 Yuki__VD.rb', line 57

def write_data(filename, data)
  return unless @file

  @hash[filename] = @file.pos
  @file.write([data.bytesize].pack(UNPACK_METHOD))
  @file.write(data)
end