nwn-lib 0.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,7 +1,17 @@
1
+ === 0.2.1
2
+
3
+ * TwoDA allows creation of tables and proper writing, HAS NEW API. (sorry!)
4
+ * NWN::Gff.kivinen_print takes more options, compatibility updates.
5
+ * nwn-gff-print takes option --type: override file type, prints a custom header.
6
+ * nwn-gff-print takes option -f: print full path. (formerly -k)
7
+ * nwn-gff-print takes option --struct: override struct id
8
+ * Added CHEATSHEET
9
+
1
10
  === 0.2
2
11
 
3
12
  * Add TwoDA reading/writing support.
4
13
  * nwn-gff-print supports kivinen-style ____type printing.
14
+
5
15
  === 0.1
6
16
 
7
17
  * Add basic functionality for reading and writing arbitary gff files.
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ include FileUtils
9
9
  # Configuration
10
10
  ##############################################################################
11
11
  NAME = "nwn-lib"
12
- VERS = "0.2"
12
+ VERS = "0.2.1"
13
13
  CLEAN.include ["**/.*.sw?", "pkg", ".config", "rdoc", "coverage"]
14
14
  RDOC_OPTS = ["--quiet", "--line-numbers", "--inline-source", '--title', \
15
15
  'nwn-lib: a ruby library for accessing NWN resource files', \
@@ -18,7 +18,7 @@ RDOC_OPTS = ["--quiet", "--line-numbers", "--inline-source", '--title', \
18
18
  Rake::RDocTask.new do |rdoc|
19
19
  rdoc.rdoc_dir = "rdoc"
20
20
  rdoc.options += RDOC_OPTS
21
- rdoc.rdoc_files.add ["README", "CHANGELOG", "COPYING", "doc/*.rdoc", "lib/**/*.rb"]
21
+ rdoc.rdoc_files.add ["README", "CHEATSHEET", "CHANGELOG", "COPYING", "doc/*.rdoc", "lib/**/*.rb"]
22
22
  end
23
23
 
24
24
  desc "Packages up nwn-lib"
data/bin/nwn-gff-print CHANGED
@@ -6,6 +6,9 @@ require 'yaml'
6
6
 
7
7
  format = nil
8
8
  types_too = false
9
+ full = false
10
+ file_type = nil
11
+ struct_id = nil
9
12
 
10
13
  OptionParser.new do |o|
11
14
  o.banner = "Usage: nwn-gff-print [options] file/- [path]"
@@ -15,9 +18,24 @@ OptionParser.new do |o|
15
18
  o.on "-k", "--kivinen", "Dump as kivinens dump format (like the perl tools)" do
16
19
  format = :kivinen
17
20
  end
21
+ o.on "-f", "--kivinen-full-path", "Print the full path (implies -k)" do
22
+ format = :kivinen
23
+ full = true
24
+ end
18
25
  o.on "-t", "--types", "Dump types as well (only applies to -k, for now)" do
19
26
  types_too = true
20
27
  end
28
+ o.on "--type filetype", "Override the file type (implies --struct 0xffffffff)" do |t|
29
+ if t.size != 3
30
+ $stderr.puts "Invalid type #{t} passwd."
31
+ exit 1
32
+ end
33
+ file_type = t.upcase + " "
34
+ struct_id = 0xffffffff
35
+ end
36
+ o.on "--struct id", "Override struct id (as hex, please)" do |s|
37
+ struct_id = s.hex
38
+ end
21
39
  end.parse!
22
40
 
23
41
  file = ARGV.shift or begin
@@ -48,7 +66,7 @@ case format
48
66
  when :yaml
49
67
  y g
50
68
  when :kivinen
51
- NWN::Gff.kivinen_format g, "/", types_too do |label, value|
69
+ NWN::Gff.kivinen_format g, "/", types_too, full, file_type, struct_id do |label, value|
52
70
  puts "%s:\t%s" % [label, value]
53
71
  end
54
72
  else
data/lib/nwn/gff.rb CHANGED
@@ -58,27 +58,38 @@ module NWN
58
58
 
59
59
  # Parses +s+ as an arbitary GFF object and yields for each field found,
60
60
  # with the proper prefix.
61
- def self.kivinen_format s, prefix = "/", types_too = false, &block
61
+ #
62
+ # [+s+] The gff object to yield pairs for; can be one of NWN::Gff::Gff, NWN::Gff::Struct, Array (for lists), or NWN::Gff::Element.
63
+ # [+prefix+] Supply a prefix to add to the output.
64
+ # [+types_too+] Yield type definitions as well (gffprint.pl -t).
65
+ # [+add_prefix+] Add a prefix <tt>(unknown type)</tt> of no type information can be derived from the input.
66
+ # [+file_type+] File type override. If non-null, add a global struct header with the given file type (useful for passing to gffencode.pl)
67
+ # [+struct_id+] Provide a struct_id override (if printing a struct).
68
+ def self.kivinen_format s, prefix = "/", types_too = false, add_prefix = true, file_type = nil, struct_id = nil, &block
62
69
  if s.is_a?(NWN::Gff::Gff)
63
70
  if types_too
64
- yield("/ ____file_type", s.type)
71
+ yield("/", "")
72
+ yield("/ ____file_type", file_type.nil? ? s.type : file_type)
65
73
  yield("/ ____file_version", s.version)
66
74
  end
67
75
  s = NWN::Gff::Element.new("", :struct, s.root_struct)
76
+ elsif file_type != nil
77
+ yield("/", "")
78
+ yield("/ ____file_type", file_type)
79
+ yield("/ ____file_version", "V3.2")
68
80
  end
69
81
 
70
82
  if s.is_a?(Array)
71
- v = NWN::Gff::Element.new("(unlabeled list)", :list, s)
83
+ v = NWN::Gff::Element.new(add_prefix ? "(unlabeled list)" : "", :list, s)
72
84
  end
73
85
 
74
86
  if s.is_a?(NWN::Gff::Struct)
75
- yield(prefix + " ____struct_type", s.struct_id) if types_too
76
- s = NWN::Gff::Element.new("(unlabeled struct)", :struct, s)
87
+ s = NWN::Gff::Element.new(add_prefix ? "(unlabeled struct)" : "", :struct, s)
77
88
  end
78
89
 
79
90
  case s.type
80
91
  when :struct
81
- yield(prefix + " ____struct_type", s.value.struct_id) if types_too
92
+ yield(prefix + " ____struct_type", struct_id.nil? ? s.value.struct_id : struct_id) if types_too
82
93
  s.value.each {|k,v|
83
94
  kivinen_format v, prefix + s.label + (s.label == "" ? "" : "/"), types_too do |l,v|
84
95
  yield(l, v)
@@ -88,18 +99,24 @@ module NWN
88
99
  when :cexolocstr
89
100
 
90
101
  s.value.each {|vv|
91
- yield(prefix + s.label + "/" + vv.language.to_s, vv.text)
102
+ yield(prefix + s.label + "/" + vv.language.to_s, vv.text.gsub(/([\000-\037\177-\377%])/) {|v| "%" + v.unpack("H2")[0] })
92
103
  }
93
- yield(prefix + s.label + ". ___string_ref", s._str_ref)
104
+ yield(prefix + s.label + ". ____string_ref", s._str_ref)
94
105
 
95
106
  when :list
96
107
  s.value.each_with_index {|vv, idx|
108
+ if types_too
109
+ yield(prefix + s.label + "[#{idx}]/", prefix + s.label + "[#{idx}]")
110
+ yield(prefix + s.label + "[#{idx}]/" + " ____struct_type", 0)
111
+ end
97
112
  vv.each {|kkk, vvv|
98
113
  kivinen_format vvv, prefix + s.label + "[#{idx}]/", types_too do |l,v|
99
114
  yield(l,v)
100
115
  end
101
116
  }
102
117
  }
118
+ when :cexostr
119
+ yield(prefix + s.label, s.value.gsub(/([\000-\037\177-\377%])/) {|v| "%" + v.unpack("H2")[0] })
103
120
  else
104
121
  yield(prefix + s.label, s.value)
105
122
  end
@@ -158,7 +175,6 @@ class NWN::Gff::Gff
158
175
  # gff['/PropertiesList[0]'] = 'Test'
159
176
  # This will raise an error (obviously)
160
177
  def get_or_set k, new_value = nil, new_type = nil, new_label = nil, new_str_ref = nil
161
- puts "get_or_set(#{k} = #{new_value})"
162
178
  h = self.root_struct
163
179
  path = []
164
180
  value_path = [h]
@@ -274,10 +290,12 @@ class NWN::Gff::Gff
274
290
  old_value
275
291
  end
276
292
 
293
+ # A alias for get_or_set(key).
277
294
  def [] k
278
295
  get_or_set k
279
296
  end
280
297
 
298
+ # A alias for get_or_set(key, value).
281
299
  def []= k, v
282
300
  get_or_set k, v
283
301
  end
@@ -299,7 +317,7 @@ class NWN::Gff::Element
299
317
  def initialize label = nil, type = nil, value = nil
300
318
  @label, @type, @value = label, type, value
301
319
  end
302
-
320
+
303
321
  def validate path_prefix = "/"
304
322
  raise NWN::Gff::GffTypeError, "#{path_prefix}#{self.label}: New value #{self.value} is not compatible with the current type #{self.type}" unless
305
323
  self.class.valid_for?(self.value, self.type)
data/lib/nwn/twoda.rb CHANGED
@@ -5,22 +5,40 @@ module NWN
5
5
  class Table
6
6
 
7
7
  # An array of all column names present in this 2da table.
8
- attr_reader :columns
8
+ attr_accessor :columns
9
9
 
10
10
  # An array of row arrays, without headers.
11
- attr_reader :rows
11
+ attr_accessor :rows
12
12
 
13
+ # Create a new, empty 2da table.
14
+ def initialize
15
+ @columns = []
16
+ @rows = []
17
+ end
13
18
 
14
- # Creates a new Table object from a given file.
19
+ # Creates a new Table object from a given IO source.
15
20
  #
16
- # [+file+] A readable, valid .2da file.
17
- def self.new_from_file file
18
- self.new IO.read(file)
21
+ # [+file+] A IO object pointing to a 2da file.
22
+ def self.read_from io
23
+ self.parse io.read()
24
+ end
25
+
26
+ # Dump this table to a IO object.
27
+ def write_to io
28
+ io.write(self.to_2da)
19
29
  end
20
30
 
31
+ # Parse a existing string containing a full 2da table.
32
+ # Returns a TwoDA::Table.
33
+ def self.parse bytes
34
+ obj = self.new
35
+ obj.parse bytes
36
+ obj
37
+ end
21
38
 
22
39
  # Parses a string that represents a valid 2da definition.
23
- def initialize bytes
40
+ # Replaces any content this table may already have.
41
+ def parse bytes
24
42
  magic, empty, header, *data = *bytes.split(/\r?\n/).map {|v| v.strip }
25
43
 
26
44
  raise ArgumentError, "Not valid 2da: No valid header found" if
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: nwn-lib
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.2"
7
- date: 2008-06-29 00:00:00 +02:00
6
+ version: 0.2.1
7
+ date: 2008-07-03 00:00:00 +02:00
8
8
  summary: a ruby library for accessing Neverwinter Nights resource files
9
9
  require_paths:
10
10
  - lib