Class: Mikunyan::TypeTree
- Inherits:
-
Object
- Object
- Mikunyan::TypeTree
- Defined in:
- lib/mikunyan/type_tree.rb
Overview
Class for representing TypeTree
Defined Under Namespace
Classes: Node
Instance Attribute Summary collapse
-
#nodes ⇒ Array<Mikunyan::TypeTree::Node>
list of all nodes.
Class Method Summary collapse
-
.deserialize(obj) ⇒ Object
Creates TypeTree from serialized object.
-
.load(br, version) ⇒ Mikunyan::TypeTree
Creates TypeTree from binary string.
-
.load_default(class_id, hash) ⇒ Mikunyan::TypeTree?
Create default TypeTree from hash string (if exists).
Instance Method Summary collapse
-
#serialize ⇒ Object
Generates JSON-compatible serialized representation of typetree information.
-
#tree ⇒ Mikunyan::TypeTree::Node?
Returns the root node of the typetree.
Instance Attribute Details
#nodes ⇒ Array<Mikunyan::TypeTree::Node>
list of all nodes
9 10 11 |
# File 'lib/mikunyan/type_tree.rb', line 9 def nodes @nodes end |
Class Method Details
.deserialize(obj) ⇒ Object
Creates TypeTree from serialized object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/mikunyan/type_tree.rb', line 129 def self.deserialize(obj) stack = [] ret = TypeTree.new ret.nodes = obj['nodes'].map.with_index do |e, index| level = e['level'] || e['depth'] parent = level > 0 ? stack[level - 1] : nil n = Node.new( version: e['version'], level: level, array?: e['is_array'], type: e['type'], name: e['name'], size: e['size'], index: index, flags: e['flags'], v18meta: e['v18meta'], parent: parent, children: [] ) parent.children << n if parent stack[level] = n end ret end |
.load(br, version) ⇒ Mikunyan::TypeTree
Creates TypeTree from binary string
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/mikunyan/type_tree.rb', line 59 def self.load(br, version) if version == 10 || version >= 12 node_count = br.i32u buffer_size = br.i32u nodes = Array.new(node_count) do Node.new( version: br.i16u, level: br.i8u, array?: br.bool, type: br.i32u, name: br.i32u, size: br.i32s, index: br.i32u, flags: br.i32u, v18meta: version >= 18 ? br.i64u : nil, children: [] ) end buffer = br.read(buffer_size) stack = [] nodes.each do |n| n.type = Mikunyan::Constants.get_string_or_default(n.type, buffer) n.name = Mikunyan::Constants.get_string_or_default(n.name, buffer) if n.level > 0 n.parent = stack[n.level - 1] n.parent.children << n end stack[n.level] = n end br.adv(4) if version >= 21 else nodes = [] stack = [] until stack.empty? && !nodes.empty? parent = stack.pop node = Node.new( type: br.cstr, name: br.cstr, size: br.i32s, index: br.i32u, array?: br.i32 != 0, version: br.i32u, flags: br.i32u, level: parent ? parent.level + 1 : 0, parent: parent, children: [] ) nodes << node parent.children << node if parent stack += Array.new(br.i32u, node) end end ret = TypeTree.new ret.nodes = nodes ret end |
.load_default(class_id, hash) ⇒ Mikunyan::TypeTree?
Create default TypeTree from hash string (if exists)
120 121 122 123 124 125 |
# File 'lib/mikunyan/type_tree.rb', line 120 def self.load_default(class_id, hash) file = File.("../typetrees/#{class_id}/#{hash.unpack1('H*')}.json", __FILE__) return nil unless File.file?(file) TypeTree.deserialize(JSON.parse(File.read(file))) end |
Instance Method Details
#serialize ⇒ Object
Generates JSON-compatible serialized representation of typetree information
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mikunyan/type_tree.rb', line 37 def serialize { 'nodes' => nodes.map do |e| { 'version' => e.version, 'level' => e.level, 'is_array' => e.array?, 'type' => e.type, 'name' => e.name, 'size' => e.size, 'flags' => e.flags, 'v18meta' => e. } end } end |
#tree ⇒ Mikunyan::TypeTree::Node?
Returns the root node of the typetree
32 33 34 |
# File 'lib/mikunyan/type_tree.rb', line 32 def tree nodes&.[](0) end |