Class: WolfRpg::Database::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/wolfrpg/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coder) ⇒ Data

Returns a new instance of Data.



260
261
262
# File 'lib/wolfrpg/database.rb', line 260

def initialize(coder)
  @name = coder.read_string
end

Instance Attribute Details

#int_valuesObject

Returns the value of attribute int_values.



257
258
259
# File 'lib/wolfrpg/database.rb', line 257

def int_values
  @int_values
end

#nameObject

Returns the value of attribute name.



256
257
258
# File 'lib/wolfrpg/database.rb', line 256

def name
  @name
end

#string_valuesObject

Returns the value of attribute string_values.



258
259
260
# File 'lib/wolfrpg/database.rb', line 258

def string_values
  @string_values
end

Instance Method Details

#[](key) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/wolfrpg/database.rb', line 290

def [](key)
  if key.is_a? Field
    if key.string?
      @string_values[key.index]
    else
      @int_values[key.index]
    end
  elsif value.is_a? Integer
    self[@fields[key]]
  else
    raise "Data[] takes a Field, got a #{value.class}"
  end
end

#[]=(key, value) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/wolfrpg/database.rb', line 304

def []=(key, value)
  if key.is_a? Field
    if key.string?
      @string_values[key.index] = value
    else
      @int_values[key.index] = value
    end
  elsif value.is_a? Integer
    self[@fields[key]] = value
  else
    raise "Data[] takes a Field, got a #{value.class}"
  end
end

#dump_dat(coder) ⇒ Object



281
282
283
284
285
286
287
288
# File 'lib/wolfrpg/database.rb', line 281

def dump_dat(coder)
  @int_values.each do |i|
    coder.write_int(i)
  end
  @string_values.each do |i|
    coder.write_string(i)
  end
end

#dump_project(coder) ⇒ Object



264
265
266
# File 'lib/wolfrpg/database.rb', line 264

def dump_project(coder)
  coder.write_string(@name)
end

#each_filenameObject



326
327
328
329
330
331
332
333
# File 'lib/wolfrpg/database.rb', line 326

def each_filename
  @fields.each do |field|
    next unless field.string?
    self[field].scan(/^[^ ]*?[\/\\].*?\.[a-zA-Z0-9]+$/).each do |filename|
      yield filename.gsub('\\', '/')
    end
  end
end

#each_translatableObject



318
319
320
321
322
323
324
# File 'lib/wolfrpg/database.rb', line 318

def each_translatable
  @fields.each do |field|
    next unless field.string? && field.type == 0
    value = self[field]
    yield [value, field] unless value.empty? || value.include?("\n")
  end
end

#read_dat(coder, fields) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/wolfrpg/database.rb', line 268

def read_dat(coder, fields)
  @fields = fields
  @int_values = Array.new(fields.select(&:int?).size)
  @string_values = Array.new(fields.select(&:string?).size)

  @int_values.each_index do |i|
    @int_values[i] = coder.read_int
  end
  @string_values.each_index do |i|
    @string_values[i] = coder.read_string
  end
end