Class: Bayonetta::SCRFile

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

Constant Summary collapse

ALIGNMENTS =
{
  'wmb' => 0x20,
  'wtb' => 0x1000,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f = nil, big = false) ⇒ SCRFile

Returns a new instance of SCRFile.



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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
301
302
303
304
305
306
307
308
309
310
# File 'lib/bayonetta/scr.rb', line 246

def initialize(f = nil, big = false)
  @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 = SCRFile.is_big?(f)

    f.rewind
    uint = get_uint

    @id = f.read(4)
    raise "Invalid id #{id.inspect}!" if @id != "SCR\0".b
    @num_models = f.read(4).unpack(uint).first
    @offset_texture = f.read(4).unpack(uint).first
    @unknown = f.read(4)
    flt = get_float
    sh = get_short
    @offsets_models = []
    @sizes_models = []
    @models_metadata = @num_models.times.collect {
      pos = f.tell
      name = f.read(16)
      offset = f.read(4).unpack(uint).first
      transform = f.read(4*9).unpack("#{flt}9")
      u_a = f.read(42*2).unpack("#{sh}42")
      @offsets_models.push(pos + offset)
      {
        name: name,
        transform: transform,
        u_a: u_a
      }
    }
    @num_models.times { |i|
      if i == ( @num_models - 1 )
        @sizes_models.push( @offset_texture - @offsets_models[i] )
      else
        @sizes_models.push( @offsets_models[i+1] - @offsets_models[i] )
      end
    }
    @size_textures = f.size - @offset_texture
    @models = @num_models.times.collect { |i|
      f.seek(@offsets_models[i])
      StringIO::new( f.read(@sizes_models[i]), "rb")
    }
    f.seek(@offset_texture)
    @textures = StringIO::new( f.read(@size_textures), "rb")
    @total_size = f.size
    f.close if file_name_input
  else
    @id = "SCR\0".b
    @num_models = 0
    @offset_texture = 0
    @size_textures = 0
    @unknown = "\1\0\0\0".b
    @offsets_models = []
    @sizes_models = []
    @models_metadata = []
    @models = []
    @textures = nil
    @total_size = 0
  end
end

Instance Attribute Details

#bigObject (readonly)

Returns the value of attribute big.



192
193
194
# File 'lib/bayonetta/scr.rb', line 192

def big
  @big
end

#models_metadataObject

Returns the value of attribute models_metadata.



194
195
196
# File 'lib/bayonetta/scr.rb', line 194

def 
  @models_metadata
end

#unknownObject

Returns the value of attribute unknown.



193
194
195
# File 'lib/bayonetta/scr.rb', line 193

def unknown
  @unknown
end

Class Method Details

.is_bayo2?(f) ⇒ Boolean

Returns:

  • (Boolean)


222
223
224
225
226
227
228
229
# File 'lib/bayonetta/scr.rb', line 222

def self.is_bayo2?(f)
  f.rewind
  id = f.read(4)
  raise "Invalid id #{id.inspect}!" if id != "SCR\0".b
  a, b = f.read(4).unpack("S2")
  f.rewind
  a > 0 && b > 0
end

.is_big?(f) ⇒ Boolean

Returns:

  • (Boolean)


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/bayonetta/scr.rb', line 205

def self.is_big?(f)
  f.rewind
  block = lambda { |int|
    id = f.read(4)
    raise "Invalid id #{id.inspect}!" if id != "SCR\0".b
    model_number = f.read(4).unpack(int).first
    offset_texture = f.read(4).unpack(int).first
    ( model_number >= 0 ) && ( (model_number * 0x8c + 0x10) < f.size  ) && ( offset_texture >= 0 ) && ( offset_texture < f.size )
  }
  big = block.call("l>")
  f.rewind
  small = block.call("l<")
  f.rewind
  raise "Invalid data!" unless big ^ small
  return big
end

.load(f) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bayonetta/scr.rb', line 231

def self.load(f)
  file_name_input = false
  unless f.respond_to?(:read) && f.respond_to?(:seek)
    f = File::new(f, "rb")
    file_name_input = true
  end
  if is_bayo2?(f)
    scr = SCR2File::new(f)
  else
    scr = SCRFile::new(f)
  end
  f.close if file_name_input
  scr
end

Instance Method Details

#[](i) ⇒ Object



322
323
324
# File 'lib/bayonetta/scr.rb', line 322

def [](i)
  return @models[i]
end

#bayo2?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/bayonetta/scr.rb', line 201

def bayo2?
  false
end

#compute_layoutObject



333
334
335
336
337
338
339
340
341
342
343
# File 'lib/bayonetta/scr.rb', line 333

def compute_layout
  current_offset = 0x10 + 0x8c * @num_models
  @offsets_models = @num_models.times.collect { |i|
     tmp = align(current_offset, ALIGNMENTS["wmb"])
     current_offset = tmp + @sizes_models[i]
     tmp
  }
  @offset_texture = align(current_offset, ALIGNMENTS["wtb"])
  @total_size = @offset_texture + @size_textures
  self
end

#dump(name) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/bayonetta/scr.rb', line 364

def dump(name)
  compute_layout
  uint = get_uint
  File.open(name,"wb") { |f|
    f.write("\0"*@total_size)
    f.rewind
    f.write([@id].pack("a4"))
    f.write([@num_models].pack(uint))
    f.write([@offset_texture].pack(uint))
    f.write([@unknown].pack("a4"))
    @num_models.times { |i|
      pos = f.tell
      f.write([@models_metadata[i][:name]].pack("a16"))
      f.write([@offsets_models[i] - pos].pack(uint))
      f.write(@models_metadata[i][:transform].pack("#{get_float}9"))
      f.write(@models_metadata[i][:u_a].pack("#{get_short}42"))
    }
    @offsets_models.each_with_index { |off, i|
      f.seek(off)
      @models[i].rewind
      f.write(@models[i].read)
    }
    f.seek(@offset_texture)
    @textures.rewind
    f.write(@textures.read)
  }
  self
end

#each_modelObject



312
313
314
315
316
317
318
319
320
# File 'lib/bayonetta/scr.rb', line 312

def each_model
  if block_given? then
    @num_models.times { |i|
      yield @models[i]
    }
  else
    to_enum(:each_model)
  end
end

#invalidate_layoutObject



326
327
328
329
330
331
# File 'lib/bayonetta/scr.rb', line 326

def invalidate_layout
  @offsets_models = []
  @offset_texture = 0
  @total_size = 0
  self
end

#push_model(file) ⇒ Object



345
346
347
348
349
350
351
# File 'lib/bayonetta/scr.rb', line 345

def push_model(file)
  invalidate_layout
  @models.push file
  @sizes_models.push file.size
  @num_models += 1
  self
end

#texturesObject



360
361
362
# File 'lib/bayonetta/scr.rb', line 360

def textures
  @textures
end

#textures=(file) ⇒ Object



353
354
355
356
357
358
# File 'lib/bayonetta/scr.rb', line 353

def textures=(file)
  invalidate_layout
  @textures = file
  @size_textures = file.size
  self
end