class Core::Config

Attributes

hash[R]

Public Class Methods

new() click to toggle source
# File lib/config.rb, line 8
def initialize
  @hash = {
    :opengl => true,
    :language => "de",
    :volume => 0.5, # 0 = normal, 1 = twice as loud
    :contrast => 1.0, # 1.0 = off
    :log => true, # whether to log to file or print everything on stdout
    :text_speed => 5, # higher = slower
  }
end

Public Instance Methods

load(file=Core::DEFAULT_CONFIG) click to toggle source
# File lib/config.rb, line 19
def load(file=Core::DEFAULT_CONFIG)
  if !File.exists?(Core::HOME_PATH + file)
    File.new(Core::HOME_PATH + file, "w")
  else
    if File.size(Core::HOME_PATH + file) > 0
      begin
        @hash = Marshal.load(File.open(Core::HOME_PATH + file, "r"))
        validate
      rescue TypeError
        puts("WARNING: Corrupted config file (#{file}), reverting to default")
      rescue NoMethodError # this is likely caused by outdated configs
          warn("WARNING: Config file #{file} is outdated, deleting")
          File.delete(Core::HOME_PATH + file)
      end
    end
  end
end
save(file=Core::DEFAULT_CONFIG) click to toggle source
# File lib/config.rb, line 37
def save(file=Core::DEFAULT_CONFIG)
  validate
  Marshal.dump(@hash, File.open(Core::HOME_PATH + file, "w"))
rescue NoMethodError # this is likely caused by outdated configs
    warn("WARNING: Config file #{file} is outdated, deleting")
    File.delete(Core::HOME_PATH + file)
end
validate() click to toggle source
# File lib/config.rb, line 45
def validate
  if !["en", "de"].include?(@hash[:language])
    @hash[:language] = "de"
  end
  vol = @hash[:volume]
  if vol < 0
    @hash[:volume] = 0.0
  elsif vol > 1
    @hash[:volume] = 1.0
  end
  speed = @hash[:text_speed]
  if speed < 1
    @hash[:text_speed] = 1
  elsif speed > 10
    @hash[:text_speed] = 10
  end
end