Module: RgssDb::DataFileFactory

Defined in:
lib/rgss_db/model/data_file_factory.rb

Overview

Data file factory module

Constant Summary collapse

FACTORY_ALL_TYPES =

List of all data files types

Returns:

  • (Array<String>)
[
  DATA_FILE_ACTORS,
  DATA_FILE_ANIMATIONS,
  DATA_FILE_AREAS,
  DATA_FILE_ARMORS,
  DATA_FILE_CLASSES,
  DATA_FILE_COMMON_EVENTS,
  DATA_FILE_ENEMIES,
  DATA_FILE_ITEMS,
  DATA_FILE_MAP_INFOS,
  DATA_FILE_MAPS,
  DATA_FILE_SKILLS,
  DATA_FILE_STATES,
  DATA_FILE_SYSTEM,
  DATA_FILE_TILESETS,
  DATA_FILE_TROOPS,
  DATA_FILE_WEAPONS
].freeze
FACTORY_ARRAY =

List of data files handled using an array

Some RPG Maker database files are saved as arrays (Actors, Weapons, Items…)

Returns:

  • (Array<String>)
[
  DATA_FILE_ACTORS, DATA_FILE_ANIMATIONS,
  DATA_FILE_ARMORS, DATA_FILE_CLASSES, DATA_FILE_COMMON_EVENTS,
  DATA_FILE_ENEMIES, DATA_FILE_ITEMS, DATA_FILE_SKILLS,
  DATA_FILE_STATES, DATA_FILE_TILESETS, DATA_FILE_TROOPS,
  DATA_FILE_WEAPONS
].freeze
FACTORY_HASH =

List of data files handled using a hash

Returns:

  • (Array<String>)
[].freeze
FACTORY_HASH_NUMBER =

List of data files handled using a number hash

Returns:

  • (Array<String>)
[
  DATA_FILE_AREAS,
  DATA_FILE_MAP_INFOS
].freeze

Class Method Summary collapse

Class Method Details

.create_data_file(data_file, object) ⇒ DataFile

Creates a data file instance based on the given file entry

The database file type is auto. determined using the “data_file“ path

Parameters:

  • data_file (String)

    Data file entry

  • object (Object)

    Deserialized data file object

Returns:

Raises:

  • (StandardError)

    No type found



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rgss_db/model/data_file_factory.rb', line 66

def self.create_data_file(data_file, object)
  type = determine_data_file_type(data_file)
  raise "could not find a valid data file type for the file: '#{data_file}'" if type.nil?

  # Checks for a specific data file usage (bulk-check)
  return DataFileArray.new(type, data_file, object) if FACTORY_ARRAY.any? { |f| f.casecmp?(type) }
  return DataFileHash.new(type, data_file, object) if FACTORY_HASH.any? { |f| f.casecmp?(type) }
  return DataFileHashNumber.new(type, data_file, object) if FACTORY_HASH_NUMBER.any? { |f| f.casecmp?(type) }

  # Assume a base data file (map data files will use this)
  DataFile.new(type, data_file, object)
end

.determine_data_file_type(data_file) ⇒ String

Determines the type of the data file

Returns “nil“ if a valid type cannot be found

Parameters:

  • data_file (String)

    Data file entry

Returns:

  • (String)


88
89
90
91
92
# File 'lib/rgss_db/model/data_file_factory.rb', line 88

def self.determine_data_file_type(data_file)
  # Gets the data file name without extensions (and custom label, if any)
  data_file_name = File.basename(data_file, ".*").gsub(DATA_FILE_CUSTOM_LABEL, "")
  FACTORY_ALL_TYPES.find { |type| File.fnmatch(type, data_file_name, File::FNM_CASEFOLD) }
end