Module: NWN

Included in:
Gff::Scripting::Sandbox
Defined in:
lib/nwn/erf.rb,
lib/nwn/gff.rb,
lib/nwn/key.rb,
lib/nwn/res.rb,
lib/nwn/tlk.rb,
lib/nwn/twoda.rb,
lib/nwn/version.rb,
lib/nwn/settings.rb

Defined Under Namespace

Modules: Erf, Gff, Key, Resources, Tlk, TwoDA

Constant Summary collapse

VERSION =
"0.5.0"
SETTING_DEFAULT_VALUES =
{
  'NWN_LIB_IN_ENCODING' => 'ISO-8859-1',
  'NWN_LIB_OUT_ENCODING' => 'UTF-8'
}
IconvState =

:nodoc:

{}

Class Method Summary collapse

Class Method Details

.iconv_gff_to_native(text) ⇒ Object

Converts text from Gff format to native/external, such as json (usually UTF-8).



56
57
58
# File 'lib/nwn/settings.rb', line 56

def self.iconv_gff_to_native text
  text.encode('UTF-8')
end

.iconv_native_to_gff(text) ⇒ Object

Converts text from native format (such as json) to Gff (required by NWN).



51
52
53
# File 'lib/nwn/settings.rb', line 51

def self.iconv_native_to_gff text
  text.encode(NWN.setting(:out_encoding))
end

.log_debug(msg) ⇒ Object

This writes a internal warnings and debug messages to stderr.

Leaving this on is recommended, since it usually points to (fixable) errors in your resource files. You can turn this off anyways by setting the environment variable NWN_LIB_DEBUG to “0” or “off”.

Will return true when printed, false otherwise.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nwn/settings.rb', line 15

def self.log_debug msg
  # Do not print debug messages if explicitly turned off
  return false if [false, "off"].index(setting(:debug))

  if NWN.setting(:debug_traces)
    $stderr.puts "(nwn-lib): %s" % [msg]
    $stderr.puts "  " + caller.join("\n  ") + "\n"
  else
    dir = File.expand_path(File.dirname(File.expand_path(__FILE__)) + "/../../")
    pa = caller.reject {|x| x.index(dir) }[0]
    pa ||= caller[0]
    pa ||= "(no frames)"
    pa = pa[(pa.size - 36) .. -1] if pa.size > 36
    $stderr.puts "(nwn-lib) %s: %s" % [pa, msg]
  end

  true
end

.setting(sym, value = :_invalid_) ⇒ Object

Get or set a ENV var setting. Returns false for “0” values. Returns the old value for new assignments.



37
38
39
40
41
42
43
44
45
46
# File 'lib/nwn/settings.rb', line 37

def self.setting sym, value = :_invalid_
  name = "NWN_LIB_#{sym.to_s.upcase}"
  if value != :_invalid_
    ret = setting(sym)
    ENV[name] = value.to_s if value != :_invalid_
    ret
  else
    ENV[name] == "0" ? false : (ENV[name] || SETTING_DEFAULT_VALUES[name])
  end
end