Class: SSO::Elements::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/sso/elements/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollection

Returns a new instance of Collection.



11
12
13
14
# File 'lib/sso/elements/collection.rb', line 11

def initialize
  @addon_index = {}
  @lists = []
end

Instance Attribute Details

#configsObject

Returns the value of attribute configs.



6
7
8
# File 'lib/sso/elements/collection.rb', line 6

def configs
  @configs
end

#indexObject

Returns the value of attribute index.



6
7
8
# File 'lib/sso/elements/collection.rb', line 6

def index
  @index
end

#signatureObject

Returns the value of attribute signature.



6
7
8
# File 'lib/sso/elements/collection.rb', line 6

def signature
  @signature
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/sso/elements/collection.rb', line 6

def version
  @version
end

Class Method Details

.compact_string(s) ⇒ Object



229
230
231
# File 'lib/sso/elements/collection.rb', line 229

def self.compact_string(s)
  s.gsub("\u0000", '')
end

.expand_string_utf16(base, type) ⇒ Object



233
234
235
236
237
238
# File 'lib/sso/elements/collection.rb', line 233

def self.expand_string_utf16(base, type)
  sz = type.split(':').last.to_i
  s = base
  s += "\u0000".encode('UTF-16LE') while s.bytes.size < sz
  s
end

.expand_string_utf8(base, type) ⇒ Object



240
241
242
243
244
245
# File 'lib/sso/elements/collection.rb', line 240

def self.expand_string_utf8(base, type)
  sz = type.split(':').last.to_i
  s = base
  s += "\u0000" while s.bytes.size < sz
  s
end

Instance Method Details

#build_index!Object



20
21
22
23
24
25
26
# File 'lib/sso/elements/collection.rb', line 20

def build_index!
  @index = {}
  @configs.each do |config|
    config_name = config.name.split(' - ').last.downcase.split('_').map(&:capitalize).join('')
    @index[config_name] = config
  end
end

#load_config!(path) ⇒ Object



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
# File 'lib/sso/elements/collection.rb', line 77

def load_config!(path)
  if path.is_a?(Integer)
    cfg = Config::CFG_AVAIL[path]
    raise "Incompatible version #{path}!" unless cfg
    dir = File.dirname(__FILE__)
    path = File.expand_path("./configs/#{cfg}", dir)
  end

  buffer = File.readlines(path, chomp: true)

  lists_size = buffer.shift.to_i

  lists = []
  @conversation_list_index = buffer.shift.to_i
  @conversation_list_index = 58 if @conversation_list_index == 0

  lists_size.times do
    line = ''

    loop do
      line = buffer.shift
      break unless line == ''
    end

    offset = buffer.shift

    list = Config.new
    list.name = line
    list.offset = Array.new(offset.to_i) if offset != 'AUTO'
    list.fields = buffer.shift.split(';')
    list.types = buffer.shift.split(';')
    lists << list
  end

  @configs = lists
  true
end

#load_elements!(path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sso/elements/collection.rb', line 28

def load_elements!(path)
  sstat = [ 0, 0, 0, 0, 0 ]

  file = File.open(path, 'rb')

  @version = file.read(2).unpack('s<').first

  if @configs.nil?
    puts '[INFO] No config provided. Searching in default CFG files'
    if SSO::Elements::Config::CFG_AVAIL.keys.include?(@version)
      puts "[INFO] Using #{SSO::Elements::Config::CFG_AVAIL[@version]}!"
      load_config!(@version)
    else
      raise "[ERROR] No config provided and no default cfg file to match with elements version #{@version}!"
    end
  end

  @signature = file.read(2).unpack('s<').first

  @configs.each_with_index do |config, i|
    puts "[INFO] (#{i+1}/#{@configs.size}) Reading #{config.name} ... "
    sstat[0] = i

    if config.offset.size > 0
      config.offset = file.read(config.offset.size).bytes
    else
      puts 'CRITICAL: NO OFFSET FOUND!'
      return false
    end

    config.values = Array.new(read_value(file, 'int32'))
    sstat[1] = config.values.size

    (0...config.values.size).each do |e|
      config.values[e] = Array.new(config.types.size)

      (0...config.values[e].size).each do |f|
        config.values[e][f] = read_value(file, config.types[f])
      end
    end
  end

  file.close

  build_index!

  true
end

#load_rules(path) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/sso/elements/collection.rb', line 115

def load_rules(path)
  buffer = File.readlines(path, chomp: true)

  result = {}

  buffer.each do |line|
    next if line.empty? || line[0] == '#'

    if line.include?('|')
      key, value = line.split('|')
    else
      key = line
      value = ''
    end

    result[key] = value
  end

  result['SETCONVERSATIONLISTINDEX'] ||= 58

  result
end

#read_value(file, type) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/sso/elements/collection.rb', line 178

def read_value(file, type)
  if type == 'int16'
    file.read(2).unpack('s<')[0]
  elsif type == 'int32'
    file.read(4).unpack("l<")[0]
  elsif type == 'int64'
    file.read(8).unpack('q<')[0]
  elsif type == 'float'
    file.read(4).unpack('f')[0]
  elsif type == 'double'
    file.read(8).unpack('d')[0]
  elsif type.start_with?('byte:')
    byte_count = type[5..-1].to_i
    file.read(byte_count)
  elsif type.start_with?('wstring:')
    byte_count = type[8..-1].to_i
    Collection.compact_string(file.read(byte_count).force_encoding('UTF-16LE').encode('UTF-8'))
  elsif type.start_with?('string:')
    byte_count = type[7..-1].to_i
    Collection.compact_string(file.read(byte_count).force_encoding('UTF-8'))
  else
    nil
  end
end

#save!(path) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/sso/elements/collection.rb', line 138

def save!(path)
  File.delete(path) if File.exist?(path)
  file = File.open(path, 'wb')

  file.write([@version].pack('s<'))
  file.write([@signature].pack('s<'))

  @configs.each_with_index do |config, index|
    puts "[INFO] (#{index+1}/#{@configs.size}) Saving #{config.name} ... "
    file.write(config.offset.pack("C*")) if config.offset.size > 0

    if @conversation_list_index != index
      file.write([config.values.size].pack('l<'))
    end

    if config.elements_loaded?
      (0...config.values.size).each do |config_values_index|
        config.values[config_values_index] =  config.elements[config_values_index].to_value
      end
    end

    config.values.each do |values|
      values.each_with_index do |value, value_index|
        begin
          write_value(file, value, config.types[value_index])
        rescue => e
          pp values
          raise e
        end
      end
    end
  end

  file.close
  true
rescue => e
  p e
  false
end

#table(name) ⇒ Object



16
17
18
# File 'lib/sso/elements/collection.rb', line 16

def table(name)
  index[name]
end

#write_value(file, value, type) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/sso/elements/collection.rb', line 203

def write_value(file, value, type)
  if type == 'int16'
    file.write([value.to_i].pack('s<'))
  elsif type == 'int32'
    file.write([value.to_i].pack('l<'))
  elsif type == 'int64'
    file.write([value.to_f].pack('q<'))
  elsif type == 'float'
    file.write([value.to_f].pack('f'))
  elsif type == 'double'
    file.write([value.to_f].pack('d'))
  elsif type.start_with?('byte:')
    file.write(value)
  elsif type.start_with?('wstring:')
    file.write(Collection.expand_string_utf16((value || 'BUG').encode('UTF-16LE'), type))
  elsif type.start_with?('string:')
    file.write(Collection.expand_string_utf8((value || 'BUG'), type))
  else
    nil
  end
rescue => e
  pp value
  pp type
  raise e
end