Class: Yuki::VD

Inherits:
Object show all
Defined in:
scripts/00000 Dependencies/00200 GameData(dep)/00900 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



25
26
27
28
29
# File 'scripts/00000 Dependencies/00200 GameData(dep)/00900 Yuki__VD.rb', line 25

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



11
12
13
# File 'scripts/00000 Dependencies/00200 GameData(dep)/00900 Yuki__VD.rb', line 11

def filename
  @filename
end

Instance Method Details

#add_file(filename, ext_name = nil)

Add a file to the Yuki::VD

Parameters:

  • filename (String)

    the file name

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

    the file extension



63
64
65
66
# File 'scripts/00000 Dependencies/00200 GameData(dep)/00900 Yuki__VD.rb', line 63

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

#close

Close the VD



75
76
77
78
79
80
81
82
83
84
85
# File 'scripts/00000 Dependencies/00200 GameData(dep)/00900 Yuki__VD.rb', line 75

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:

Returns:

  • (Boolean)


46
47
48
# File 'scripts/00000 Dependencies/00200 GameData(dep)/00900 Yuki__VD.rb', line 46

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

#get_filenamesArray<String>

Get all the filename

Returns:



70
71
72
# File 'scripts/00000 Dependencies/00200 GameData(dep)/00900 Yuki__VD.rb', line 70

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



34
35
36
37
38
39
40
41
# File 'scripts/00000 Dependencies/00200 GameData(dep)/00900 Yuki__VD.rb', line 34

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)

Write a file with its data in the VD

Parameters:

  • filename (String)

    the file name

  • data (String)

    the data of the file



53
54
55
56
57
58
# File 'scripts/00000 Dependencies/00200 GameData(dep)/00900 Yuki__VD.rb', line 53

def write_data(filename, data)
  return unless @file
  @hash[filename] = @file.pos
  @file.write([data.bytesize].pack(UNPACK_METHOD))
  @file.write(data)
end