nwn-lib 0.4.5 → 0.4.6

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/lib/nwn/infer.rb DELETED
@@ -1,125 +0,0 @@
1
- module NWN::Gff
2
- @YAMLStructDefaults = {}
3
-
4
- #:stopdoc:
5
- # This gets called for each parsed struct to set their container element
6
- # values (see also gff/reader.rb, line 233-ish).
7
- def self.__yaml_postparse parent, struct
8
- struct.each {|label,element|
9
- case element.field_type
10
- when :list, :struct
11
- [element.field_value].flatten.each {|item|
12
- __yaml_postparse(element, item)
13
- item.element = element
14
- }
15
- end
16
- }
17
- end
18
- #:startdoc:
19
-
20
- # This loads structs defaults from the given file, which will
21
- # be used for field_type inferring and autocompletion/filtering of default values.
22
- # A sample file has been provided with nwn-lib, called gff-bioware.yml
23
- def self.load_struct_defaults file
24
- @YAMLStructDefaults = YAML.load(IO.read(file))
25
- new = {}
26
- @YAMLStructDefaults.each {|k,v|
27
- new[Regexp.new(k + '$')] = v
28
- }
29
- @YAMLStructDefaults = new
30
- end
31
-
32
- def self.get_struct_defaults
33
- @YAMLStructDefaults || {}
34
- end
35
-
36
- def self.get_struct_defaults_for path, key
37
- sd = NWN::Gff.get_struct_defaults
38
- sd.keys.each {|rx|
39
- next unless path =~ rx
40
- return sd[rx][key] if sd[rx][key] != nil
41
- }
42
- nil
43
- end
44
-
45
- def self.get_struct_default_type path, key
46
- dd = get_struct_defaults_for(path, key)
47
- dd.is_a?(Array) ? dd[0] : dd
48
- end
49
-
50
- def self.get_struct_default_value path, key
51
- dd = get_struct_defaults_for(path, key)
52
- dd.is_a?(Array) ? dd[1] : nil
53
- end
54
- end
55
-
56
- module NWN::Gff::Struct
57
- # Returns true if we can later infer the struct_id with the given path.
58
- def can_infer_struct_id?
59
- v = NWN::Gff.get_struct_defaults_for(self.path, '__struct_id')
60
- v == @struct_id || v == "iterative" || v == "inline"
61
- end
62
-
63
- # Returns true if we can infer the data version of this struct (if it has parent).
64
- def can_infer_data_version?
65
- @data_version == DEFAULT_DATA_VERSION || (
66
- @element && @element.parent && @element.parent.data_version == @data_version
67
- )
68
- end
69
- end
70
-
71
- module NWN::Gff::Cexolocstr
72
- def field_value_as_compact
73
- !can_infer_str_ref? ? field_value.merge({'str_ref' => str_ref}) : field_value
74
- end
75
- end
76
-
77
- module NWN::Gff::Field
78
- YAMLCompactableFields = [:byte, :char, :word, :short, :dword, :int, :void,
79
- :dword64, :int64, :float, :double, :cexostr, :resref, :cexolocstr, :list]
80
-
81
- # Returns true if we can later infer the field type.
82
- def can_infer_type?
83
- expected = NWN::Gff.get_struct_default_type(@parent.path, field_label)
84
-
85
- raise NWN::Gff::GffError, "#{field_label} has field_type " +
86
- "#{field_type.inspect}, but infer data says #{expected.inspect}." if
87
- expected && expected != field_type
88
-
89
- expected == field_type
90
- end
91
-
92
- # Returns true if we can later infer the default value.
93
- def can_infer_value?
94
- NWN::Gff.get_struct_default_value(@parent.path, field_label) == field_value
95
- end
96
-
97
- # Returns true if we can infer the str ref later on.
98
- def can_infer_str_ref?
99
- !has_str_ref? || (d = NWN::Gff.get_struct_defaults_for(@parent.path, field_label) && d && d[2] != nil)
100
- end
101
-
102
- # Can we print this field without any syntactic gizmos?
103
- def can_compact_print?
104
-
105
- YAMLCompactableFields.index(field_type) &&
106
- # exolocs print their str_ref along with their language keys
107
- (field_type == :cexolocstr || can_infer_str_ref?) &&
108
- can_infer_type?
109
- end
110
-
111
- def can_compact_as_list?
112
- NWN::Gff.get_struct_defaults_for(self.path, '__compact') != nil &&
113
- field_value.reject {|x|
114
- x.can_infer_struct_id?
115
- }.size == 0
116
- end
117
-
118
- def get_compact_as_list_field
119
- NWN::Gff.get_struct_defaults_for(self.path, '__compact')
120
- end
121
-
122
- def field_value_as_compact
123
- field_value
124
- end
125
- end