Class: Bayonetta::EFFFile

Inherits:
Object
  • Object
show all
Includes:
Alignment, Endianness
Defined in:
lib/bayonetta/eff.rb

Defined Under Namespace

Classes: Directory

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f = nil, big = false, id = "EF2\0".b) ⇒ EFFFile

Returns a new instance of EFFFile.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/bayonetta/eff.rb', line 213

def initialize(f = nil, big = false, id = "EF2\0".b)
  @big = big
  if f
    file_name_input = false
    unless f.respond_to?(:read) && f.respond_to?(:seek)
      file_name_input = true
      f = File::new(f, "rb")
    end
    @big = EFFFile.is_big?(f)
    uint = get_uint
    @id = f.read(4)
    check_id
    @directory_number = f.read(4).unpack(uint).first
    @directory_number += 1 if @id == "IDD\0".b
    @directory_infos = @directory_number.times.collect {
      f.read(8).unpack("#{uint}2")
    }
    sorted_directory_infos = @directory_infos.sort { |e1, e2| e1[1] <=> e2[1] }
    sorted_directory_infos.each_with_index { |info, i|
      id, offset = info
      if sorted_directory_infos[i+1]
        size = sorted_directory_infos[i+1][1] - offset
      else
        size = f.size - offset
      end
      info.push(size)
    }
    @directories = @directory_infos.collect { |id, offset, size|
      f.seek(offset)
      of = StringIO::new( f.read(size), "rb")
      Directory::new(of, @big)
    }
    @layout = @directories.each_with_index.collect { |d, i|
      [ @directory_infos[i][0] , d.name]
    }
  else
    @id = id
    @directory_number = 0
    @directory_infos = []
    @directories = []
    @layout = nil
  end

end

Instance Attribute Details

#bigObject (readonly)

Directory



176
177
178
# File 'lib/bayonetta/eff.rb', line 176

def big
  @big
end

#idObject

Returns the value of attribute id.



178
179
180
# File 'lib/bayonetta/eff.rb', line 178

def id
  @id
end

#layoutObject

Returns the value of attribute layout.



177
178
179
# File 'lib/bayonetta/eff.rb', line 177

def layout
  @layout
end

Class Method Details

.check_id(id) ⇒ Object



180
181
182
# File 'lib/bayonetta/eff.rb', line 180

def self.check_id(id)
  raise "Invalid id #{id.inspect}!" unless id == "EF2\0".b || id == "IDD\0".b
end

.is_big?(f) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/bayonetta/eff.rb', line 184

def self.is_big?(f)
  f.rewind
  block = lambda { |int|
    id = f.read(4)
    self.check_id(id)
    directory_number = f.read(4).unpack(int).first
    if directory_number * 8 + 8 < f.size
      directory_infos = directory_number.times.collect {
        f.read(8).unpack("#{int}2")
      }
      ( directory_number >= 0 ) && directory_infos.reduce(true) { |memo, (index, offset)|
        memo && index >= 0 && offset > 0 && offset < f.size
      }
    else
      false
    end
  }
  big = block.call("l>")
  f.rewind
  small = block.call("l<")
  f.rewind
  raise "Invalid data!" unless big ^ small
  return big
end

Instance Method Details

#check_idObject



209
210
211
# File 'lib/bayonetta/eff.rb', line 209

def check_id
  EFFFile.check_id(@id)
end

#compute_layoutObject



265
266
267
268
269
270
271
272
273
# File 'lib/bayonetta/eff.rb', line 265

def compute_layout
  current_offset = 0x1000
  @directory_number.times { |i|
    @directory_infos[i][1] = current_offset
    current_offset += @directories[i].size
    current_offset = align(current_offset, 0x1000)
  }
  current_offset
end

#each_directoryObject



302
303
304
305
306
307
308
309
310
# File 'lib/bayonetta/eff.rb', line 302

def each_directory
  if block_given?
    @directory_number.times { |i|
      yield @directory_infos[i][0], @directories[i]
    }
  else
    to_enum(:each_directory)
  end
end

#push(id, d) ⇒ Object



258
259
260
261
262
263
# File 'lib/bayonetta/eff.rb', line 258

def push(id, d)
  @directories.push(d)
  @directory_infos.push [id, nil]
  @directory_number += 1
  self
end

#to_stringioObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/bayonetta/eff.rb', line 275

def to_stringio
  total_size = compute_layout
  str = StringIO::new("\x00".b*total_size, "w+b")
  str.rewind

  uint = get_uint

  str.write @id
  if @id == "IDD\0".b
    str.write [@directory_number-1].pack(uint)
  else
    str.write [@directory_number].pack(uint)
  end
  @directory_infos.each { |inf|
    str.write inf.pack("#{uint}*")
  }
  @directory_number.times { |i|
    str.seek(@directory_infos[i][1])
    f = @directories[i].to_stringio
    str.write f.read
  }

  str.rewind
  str.close_write
  str
end