Class: WolfRpg::Database

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

Defined Under Namespace

Classes: Data, Field, Type

Constant Summary collapse

DAT_SEED_INDICES =
[0, 3, 9]
DAT_MAGIC_NUMBER =
[
  0x57, 0x00, 0x00, 0x4F, 0x4C, 0x00, 0x46, 0x4D, 0x00, 0xC1
].pack('C*')
DAT_TYPE_SEPARATOR =
[
  0xFE, 0xFF, 0xFF, 0xFF
].pack('C*')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_filename, dat_filename) ⇒ Database

Returns a new instance of Database.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# File 'lib/wolfrpg/database.rb', line 11

def initialize(project_filename, dat_filename)
  FileCoder.open(project_filename, :read) do |coder|
    @types = Array.new(coder.read_int)
    @types.each_index do |i|
      @types[i] = Type.new(coder)
    end
  end
  FileCoder.open(dat_filename, :read, DAT_SEED_INDICES) do |coder|
    if coder.encrypted?
      @crypt_header = coder.crypt_header
      @unknown_encrypted_1 = coder.read_byte
    else
      coder.verify(DAT_MAGIC_NUMBER)
    end
    num_types = coder.read_int
    unless num_types == @types.size
      raise "database project and dat Type count mismatch (#{@types.size} vs. #{num_types})"
    end
    @types.each do |type|
      type.read_dat(coder)
    end
    if coder.read_byte != 0xC1
      STDERR.puts "warning: no C1 terminator at the end of '#{dat_filename}'"
    end
  end

  def dump(project_filename, dat_filename)
    FileCoder.open(project_filename, :write) do |coder|
      coder.write_int(@types.size)
      @types.each do |type|
        type.dump_project(coder)
      end
    end
    FileCoder.open(dat_filename, :write, DAT_SEED_INDICES, @crypt_header) do |coder|
      if encrypted?
        coder.write_byte(@unknown_encrypted_1)
      else
        coder.write(DAT_MAGIC_NUMBER)
      end
      coder.write_int(@types.size)
      @types.each do |type|
        type.dump_dat(coder)
      end
      coder.write_byte(0xC1)
    end
  end

  def grep(needle)
    @types.each_with_index do |type, type_index|
      type.data.each_with_index do |datum, datum_index|
        datum.each_translatable do |value, field|
          next unless value =~ needle
          puts "DB:[#{type_index}]#{type.name}/[#{datum_index}]#{datum.name}/[#{field.index}]#{field.name}"
          puts "\t" + value
        end
      end
    end
  end
end

Instance Attribute Details

#typesObject

Returns the value of attribute types.



3
4
5
# File 'lib/wolfrpg/database.rb', line 3

def types
  @types
end

Instance Method Details

#dump(project_filename, dat_filename) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wolfrpg/database.rb', line 37

def dump(project_filename, dat_filename)
  FileCoder.open(project_filename, :write) do |coder|
    coder.write_int(@types.size)
    @types.each do |type|
      type.dump_project(coder)
    end
  end
  FileCoder.open(dat_filename, :write, DAT_SEED_INDICES, @crypt_header) do |coder|
    if encrypted?
      coder.write_byte(@unknown_encrypted_1)
    else
      coder.write(DAT_MAGIC_NUMBER)
    end
    coder.write_int(@types.size)
    @types.each do |type|
      type.dump_dat(coder)
    end
    coder.write_byte(0xC1)
  end
end

#encrypted?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/wolfrpg/database.rb', line 5

def encrypted?
  @crypt_header != nil
end

#grep(needle) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wolfrpg/database.rb', line 58

def grep(needle)
  @types.each_with_index do |type, type_index|
    type.data.each_with_index do |datum, datum_index|
      datum.each_translatable do |value, field|
        next unless value =~ needle
        puts "DB:[#{type_index}]#{type.name}/[#{datum_index}]#{datum.name}/[#{field.index}]#{field.name}"
        puts "\t" + value
      end
    end
  end
end