Class: RgssDb::DataManager

Inherits:
Object
  • Object
show all
Defined in:
lib/rgss_db/controller/data_manager.rb

Overview

Data manager class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_folder) ⇒ DataManager

Creates a data manager instance

Parameters:

  • data_folder (String)

    RPG Maker database folder



175
176
177
178
179
# File 'lib/rgss_db/controller/data_manager.rb', line 175

def initialize(data_folder)
  @path = File.expand_path(data_folder)
  database_detect_version
  database_load_model
end

Instance Attribute Details

#pathString (readonly)

Data folder path

Returns:

  • (String)


162
163
164
# File 'lib/rgss_db/controller/data_manager.rb', line 162

def path
  @path
end

#rgss_versionSymbol (readonly)

RGSS Version

Returns “nil“ if no version was detected

Returns:

  • (Symbol)


168
169
170
# File 'lib/rgss_db/controller/data_manager.rb', line 168

def rgss_version
  @rgss_version
end

Instance Method Details

#database_file?(file_path) ⇒ Boolean

Returns “true“ if “file_path“ is a RPG Maker binary data file

Returns “false“ if “file_path“ is not a binary data file

Returns “false“ if the RGSS version could not be determined

Parameters:

  • file_path (String)

    File path

Returns:

  • (Boolean)


214
215
216
# File 'lib/rgss_db/controller/data_manager.rb', line 214

def database_file?(file_path)
  File.extname(file_path).casecmp?(database_file_extension)
end

#json_file?(file_path) ⇒ Boolean

Returns “true“ if the file path is a JSON file

Parameters:

  • file_path (String)

    File path

Returns:

  • (Boolean)


225
226
227
# File 'lib/rgss_db/controller/data_manager.rb', line 225

def json_file?(file_path)
  File.extname(file_path).casecmp?(RGSS_FILE_EXT_JSON)
end

#load_database_file(database_file_type) ⇒ DataFile

Loads a single database data file based on the given type

Parameters:

  • database_file_type (String)

Returns:



247
248
249
250
251
252
253
# File 'lib/rgss_db/controller/data_manager.rb', line 247

def load_database_file(database_file_type)
  # Formats the database file path
  database_file_path = File.join(@path, database_file_type + database_file_extension)

  # Creates a data file instance for this database file
  DataFileFactory.create_data_file(database_file_path, load_file(database_file_path))
end

#load_database_filesArray<DataFile>

Gets a list of “DataFile“ instances based on the current path and detected version

This method should be used to read all RPG Maker binary data files

Returns an empty array if the operation is not possible

Returns:



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/rgss_db/controller/data_manager.rb', line 264

def load_database_files
  return [] unless version?

  # Formats the database file names adding the proper file extension
  database_files = database_file_names.map { |file_name| file_name + database_file_extension }

  # Scans the data folder for files
  detected_files = Dir.glob(database_files, File::FNM_CASEFOLD, base: @path)

  # Creates an array of data file instances with the detected database files
  detected_files.map do |data_file|
    data_file_path = File.expand_path(data_file, @path)
    DataFileFactory.create_data_file(data_file_path, load_file(data_file_path))
  end
end

#load_extracted_files(app_directory) ⇒ Array<DataFile>

Gets a list of “DataFile“ instances based on the given directory

All extracted files should be inside the given directory

This method reads extracted files and returns it as data files (RPG maker data)

Returns an empty array if the operation is not possible

Parameters:

  • app_directory (String)

    Application working directory

Returns:



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/rgss_db/controller/data_manager.rb', line 293

def load_extracted_files(app_directory)
  return [] unless version?

  # Determines the application working folder directory
  base_path = File.expand_path(app_directory, @path)

  # Creates a glob pattern to detect extracted data files using file extensions
  file_extensions_glob = "{#{RGSS_EXTRACTED_FILE_EXTENSIONS.values.join(",")}}"

  # Formats all supported database files adding the file extensions glob pattern
  extracted_files = database_file_names.map do |file_name|
    file_name + file_extensions_glob
  end

  # Scans the extracted folder for complete extracted files
  detected_files = Dir.glob(extracted_files, File::FNM_CASEFOLD, base: base_path)

  # Creates an array of data file instances with the detected database files
  detected_files.map do |data_file|
    data_file_path = File.expand_path(data_file, base_path)
    DataFileFactory.create_data_file(data_file_path, load_file(data_file_path))
  end
end

#load_extracted_files_custom(app_directory) ⇒ Array<DataFile>

Gets a list of “DataFile“ instances based on the given directory

All custom extracted files should be inside the given directory

This method reads extracted files and returns it as data files (RPG maker data)

Returns an empty array if the operation is not possible

Parameters:

  • app_directory (String)

    Application working directory

Returns:



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/rgss_db/controller/data_manager.rb', line 330

def load_extracted_files_custom(app_directory)
  return [] unless version?

  # Determines the application working folder directory
  base_path = File.expand_path(app_directory, @path)

  # Creates a glob pattern to detect extracted data files using file extensions
  file_extensions_glob = "{#{RGSS_EXTRACTED_FILE_EXTENSIONS.values.join(",")}}"

  # Formats all supported database files adding the file extensions glob pattern
  extracted_files = database_file_names.map do |file_name|
    file_name + DATA_FILE_CUSTOM_LABEL + file_extensions_glob
  end

  # Scans the extracted folder for complete extracted files
  detected_files = Dir.glob(extracted_files, File::FNM_CASEFOLD, base: base_path)

  # Creates an array of data file instances with the detected files
  detected_files.map do |data_file|
    data_file_path = File.expand_path(data_file, base_path)
    DataFileFactory.create_data_file(data_file_path, load_file(data_file_path))
  end
end

#save_data_file(data_file, app_directory, output_format_type) ⇒ Object

Saves the given data file instance

Parameters:

  • data_file (DataFile)

    Data file instance

  • app_directory (String)

    Application working directory

  • output_format_type (String)

    Output file format type

Raises:

  • (StandardError)

    RPG Maker version is not valid



363
364
365
366
367
368
369
370
371
# File 'lib/rgss_db/controller/data_manager.rb', line 363

def save_data_file(data_file, app_directory, output_format_type)
  raise "cannot save data file because rpg maker version is unknown: #{@rgss_version}" unless version?

  data_file_path = File.join(
    File.expand_path(app_directory, @path),
    data_file.serialize_file_name + determine_file_extension(output_format_type)
  )
  save_file(data_file_path, data_file.serialize)
end

#save_database_back_up(file_path, app_directory) ⇒ Object

Saves a back up of the given file

If “file_path“ is a path, the file’s base name is auto. extracted

If the database file does not exist back up creation is skipped

Parameters:

  • file_path (String)

    File path

  • app_directory (String)

    Application working directory

Raises:

  • (StandardError)

    RPG Maker version is not valid



385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/rgss_db/controller/data_manager.rb', line 385

def save_database_back_up(file_path, app_directory)
  raise "cannot save file backup because rpg maker version is unknown: #{@rgss_version}" unless version?

  # Gets the file's base name
  database_file = File.basename(file_path, ".*")

  # Determines the absolute path to the (possible) database file
  database_file_path = File.join(@path, database_file + database_file_extension)

  # Creates a back up file if the database file exists
  save_back_up(database_file_path, app_directory) if File.file?(database_file_path)
end

#version?Boolean

Checks if a valid RPG Maker version was detected or not

Returns:

  • (Boolean)


186
187
188
# File 'lib/rgss_db/controller/data_manager.rb', line 186

def version?
  !@rgss_version.nil?
end

#version_is?(version) ⇒ Boolean

Checks if the current RPG Maker version detected matches the given one

The argument is automatically casted into a Symbol instance

Parameters:

  • version (Symbol)

    RPG Maker version

Returns:

  • (Boolean)


199
200
201
# File 'lib/rgss_db/controller/data_manager.rb', line 199

def version_is?(version)
  @rgss_version == version.to_s.to_sym
end

#yaml_file?(file_path) ⇒ Boolean

Returns “true“ if the file path is a YAML file

Parameters:

  • file_path (String)

    File path

Returns:

  • (Boolean)


236
237
238
# File 'lib/rgss_db/controller/data_manager.rb', line 236

def yaml_file?(file_path)
  File.extname(file_path).casecmp?(RGSS_FILE_EXT_YAML)
end