Class: Osu::DB::StringIO

Inherits:
StringIO
  • Object
show all
Defined in:
lib/osu-db/common.rb

Constant Summary collapse

VERSION_MIN =
0x01330689
VERSION_MAX =
0x0133068D

Instance Method Summary collapse

Instance Method Details

#read_7bit_encoded_intObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/osu-db/common.rb', line 71

def read_7bit_encoded_int
  ret, off = 0, 0
  loop do
    byte = read_int(1)
    ret |= (byte & 0x7F) << off
    off += 7
    break if byte & 0x80 == 0
  end
  ret
end

#read_boolObject



62
63
64
65
66
67
68
69
# File 'lib/osu-db/common.rb', line 62

def read_bool
  flag = read_int(1)
  if flag == 0 || flag == 1
    flag != 0
  else
    raise DBCorruptError, "0x00 or 0x01 expected, got #{'0x%02x' % flag}"
  end
end

#read_doubleObject



58
59
60
# File 'lib/osu-db/common.rb', line 58

def read_double
  unpack(8, 'E')[0]
end

#read_floatObject



54
55
56
# File 'lib/osu-db/common.rb', line 54

def read_float
  unpack(4, 'e')[0]
end

#read_int(bytesize) ⇒ Object



44
45
46
# File 'lib/osu-db/common.rb', line 44

def read_int(bytesize)
  unpack(bytesize, "C#{bytesize}").reverse.inject{|h, l| h << 8 | l}
end

#read_signed_int(bytesize) ⇒ Object



48
49
50
51
52
# File 'lib/osu-db/common.rb', line 48

def read_signed_int(bytesize)
  len = 8 * bytesize
  ret = read_int(bytesize)
  ret[len - 1] == 0 ? ret : ret - (1 << len)
end

#read_strObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/osu-db/common.rb', line 87

def read_str
  tag = read_int(1)
  if tag == 0
    nil
  elsif tag == 0x0b
    len = read_7bit_encoded_int
    read(len)
  else
    raise DBCorruptError, "0x00 or 0x0b expected, got #{'0x%02x' % tag}"
  end
end

#read_timeObject



82
83
84
85
# File 'lib/osu-db/common.rb', line 82

def read_time
  ticks = read_int(8)
  ticks == 0 ? nil : TimeUtil.ticks_to_time(ticks)
end

#read_versionObject



102
103
104
105
106
107
108
109
# File 'lib/osu-db/common.rb', line 102

def read_version
  version = read_int(4)
  if (VERSION_MIN .. VERSION_MAX).include? version
    version
  else
    raise UnsupportedVersionError, "version = #{'0x%08x' % version}"
  end
end

#unpack(bytesize, format) ⇒ Object



40
41
42
# File 'lib/osu-db/common.rb', line 40

def unpack(bytesize, format)
  read(bytesize).unpack(format)
end