Module: NBTFile::ReadMethods
- Includes:
- CommonMethods, Tokens
- Included in:
- CompoundReaderState, ListReaderState, TopReaderState
- Defined in:
- lib/nbtfile.rb
Instance Method Summary collapse
- #read_byte(io) ⇒ Object
- #read_byte_array(io) ⇒ Object
- #read_double(io) ⇒ Object
- #read_float(io) ⇒ Object
- #read_int(io) ⇒ Object
- #read_integer(io, n_bytes) ⇒ Object
- #read_list_header(io) ⇒ Object
- #read_long(io) ⇒ Object
- #read_raw(io, n_bytes) ⇒ Object
- #read_short(io) ⇒ Object
- #read_string(io) ⇒ Object
- #read_type(io) ⇒ Object
- #read_value(io, type, name, state, cont) ⇒ Object
Methods included from CommonMethods
Instance Method Details
#read_byte(io) ⇒ Object
117 118 119 |
# File 'lib/nbtfile.rb', line 117 def read_byte(io) read_integer(io, 1) end |
#read_byte_array(io) ⇒ Object
148 149 150 151 152 153 |
# File 'lib/nbtfile.rb', line 148 def read_byte_array(io) length = read_int(io) value = read_raw(io, length) value._nbtfile_force_encoding("BINARY") value end |
#read_double(io) ⇒ Object
137 138 139 |
# File 'lib/nbtfile.rb', line 137 def read_double(io) read_raw(io, 8).unpack("G").first end |
#read_float(io) ⇒ Object
133 134 135 |
# File 'lib/nbtfile.rb', line 133 def read_float(io) read_raw(io, 4).unpack("g").first end |
#read_int(io) ⇒ Object
125 126 127 |
# File 'lib/nbtfile.rb', line 125 def read_int(io) read_integer(io, 4) end |
#read_integer(io, n_bytes) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/nbtfile.rb', line 108 def read_integer(io, n_bytes) raw_value = read_raw(io, n_bytes) value = (0...n_bytes).reduce(0) do |accum, n| (accum << 8) | raw_value._nbtfile_getbyte(n) end value -= ((value & sign_bit(n_bytes)) << 1) value end |
#read_list_header(io) ⇒ Object
155 156 157 158 159 |
# File 'lib/nbtfile.rb', line 155 def read_list_header(io) list_type = read_type(io) list_length = read_int(io) [list_type, list_length] end |
#read_long(io) ⇒ Object
129 130 131 |
# File 'lib/nbtfile.rb', line 129 def read_long(io) read_integer(io, 8) end |
#read_raw(io, n_bytes) ⇒ Object
102 103 104 105 106 |
# File 'lib/nbtfile.rb', line 102 def read_raw(io, n_bytes) data = io.read(n_bytes) raise EOFError unless data and data.length == n_bytes data end |
#read_short(io) ⇒ Object
121 122 123 |
# File 'lib/nbtfile.rb', line 121 def read_short(io) read_integer(io, 2) end |
#read_string(io) ⇒ Object
141 142 143 144 145 146 |
# File 'lib/nbtfile.rb', line 141 def read_string(io) length = read_short(io) string = read_raw(io, length) string._nbtfile_force_encoding("UTF-8") string end |
#read_type(io) ⇒ Object
161 162 163 164 165 166 167 168 |
# File 'lib/nbtfile.rb', line 161 def read_type(io) byte = read_byte(io) begin TOKEN_CLASSES_BY_INDEX.fetch(byte) rescue IndexError raise RuntimeError, "Unexpected tag ordinal #{byte}" end end |
#read_value(io, type, name, state, cont) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/nbtfile.rb', line 170 def read_value(io, type, name, state, cont) next_state = state case when type == TAG_End next_state = cont value = nil when type == TAG_Byte value = read_byte(io) when type == TAG_Short value = read_short(io) when type == TAG_Int value = read_int(io) when type == TAG_Long value = read_long(io) when type == TAG_Float value = read_float(io) when type == TAG_Double value = read_double(io) when type == TAG_Byte_Array value = read_byte_array(io) when type == TAG_String value = read_string(io) when type == TAG_List list_type, list_length = read_list_header(io) next_state = ListReaderState.new(state, list_type, list_length) value = list_type when type == TAG_Compound next_state = CompoundReaderState.new(state) end [next_state, type[name, value]] end |