Class: Factorix::MODSettings

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

Overview

Class for handling MOD settings

MODSettings manages the settings from mod-settings.dat file, which contains three sections: startup, runtime-global, and runtime-per-user.

Defined Under Namespace

Classes: Section

Constant Summary collapse

VALID_SECTIONS =

Valid section names

%w[startup runtime-global runtime-per-user].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game_version, sections) ⇒ MODSettings

Create a new MODSettings instance

Parameters:



203
204
205
206
# File 'lib/factorix/mod_settings.rb', line 203

def initialize(game_version, sections)
  @game_version = game_version
  @sections = sections
end

Instance Attribute Details

#game_versionFactorix::GameVersion (readonly)

Get the game version

Returns:



211
212
213
# File 'lib/factorix/mod_settings.rb', line 211

def game_version
  @game_version
end

Class Method Details

.load(path = Container[:runtime].mod_settings_path) ⇒ MODSettings

Load MOD settings from file

Parameters:

  • path (Pathname) (defaults to: Container[:runtime].mod_settings_path)

    Path to the MOD settings file (default: runtime.mod_settings_path)

Returns:



111
112
113
114
115
116
# File 'lib/factorix/mod_settings.rb', line 111

def self.load(path=Container[:runtime].mod_settings_path)
  path.open("rb") do |io|
    game_version, sections = load_settings_from_io(io)
    new(game_version, sections)
  end
end

Instance Method Details

#[](name) ⇒ Section

Get a section by name from the MOD settings

Parameters:

  • name (String)

    The section name

Returns:

Raises:



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/factorix/mod_settings.rb', line 219

def [](name)
  unless VALID_SECTIONS.include?(name)
    raise MODSettingsError, "Invalid MOD section name: #{name}"
  end

  section = @sections[name]
  unless section
    raise MODSectionNotFoundError, "MOD section not found: #{name}"
  end

  section
end

#each_section {|section| ... } ⇒ Enumerator

Iterate over all sections in the MOD settings

Yields:

  • (section)

    Block to be called for each section

Yield Parameters:

  • section (Section)

    The section

Returns:

  • (Enumerator)

    If no block is given



237
238
239
240
241
# File 'lib/factorix/mod_settings.rb', line 237

def each_section(&)
  return @sections.values.to_enum(:each) unless block_given?

  @sections.each_value(&)
end

#save(path = Container[:runtime].mod_settings_path) ⇒ void

This method returns an undefined value.

Save MOD settings to file

Parameters:

  • path (Pathname) (defaults to: Container[:runtime].mod_settings_path)

    Path to save the MOD settings file (default: runtime.mod_settings_path)



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/factorix/mod_settings.rb', line 247

def save(path=Container[:runtime].mod_settings_path)
  path.open("wb") do |file|
    serializer = SerDes::Serializer.new(file)

    # 1. Write version
    serializer.write_game_version(@game_version)

    # 2. Write a boolean value (seems to be always false)
    serializer.write_bool(false)

    # 3. Write property tree
    settings_hash = build_settings_hash
    serializer.write_property_tree(settings_hash)
  end
end