Class: Gen3PokemonSaveLoader

Inherits:
Object
  • Object
show all
Includes:
ByteCompiler
Defined in:
lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb

Instance Method Summary collapse

Methods included from ByteCompiler

#compile_bytes, #decompile_bytes, #set_bit, #unset_bit

Constructor Details

#initialize(file = nil) ⇒ Gen3PokemonSaveLoader

Returns a new instance of Gen3PokemonSaveLoader.



8
9
10
11
12
13
# File 'lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb', line 8

def initialize(file = nil)
  @game = nil 
	return if file.nil?	
  load_save(file) 
  detect_game_version
end

Instance Method Details

#calculate_section_checksum(id) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb', line 45

def calculate_section_checksum(id)
  num_data = 0
  case id
    when 0
      num_data = 3884
    when 1
      num_data = 3968
  end
  @save.pos = find_section(id)
  calc_checksum = 0
  num_data.times do |i|
    if i%4==0
      calc_checksum += compile_bytes([@save.readbyte,@save.readbyte,@save.readbyte,@save.readbyte])
    end
  end
  calc_checksum = (((calc_checksum&0xFFFF0000)>>16) + (calc_checksum&0xFFFF)) & 0xFFFF
  @save.pos = find_section(id) + 0xFF6
@save.pos = find_section(id) + 0xFF6
  @save.putc (calc_checksum&0xFF)
  @save.putc ((calc_checksum&0xFF00)>>8)
end

#closeObject



19
20
21
# File 'lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb', line 19

def close
  @save.close
end

#detect_game_versionObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb', line 127

def detect_game_version
  @save.pos = find_section(0) + 0xAC
  tempdata = 0
  4.times do
    tempdata << 8
    tempdata | @save.readbyte
  end
  
  puts tempdata
  case tempdata
  when 0
   @game = :rs
  when 1
   @game = :frlg
  else
   @game = :e
  end
  puts @game.to_s
  return tempdata
end

#find_recent_saveObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb', line 108

def find_recent_save
  @save.pos = 0xFFC
  game_a = 0
  game_b = 0
  4.times do |i|
    game_a += (@save.readbyte << 8*i)
  end
  @save.pos = 0xEFFC
  4.times do |i|
    game_b += (@save.readbyte << 8*i)
  end
  game_a == 0xFFFFFFFF ? game_a = 0 : game_b == 0xFFFFFFFF ? game_b = 0 : 0
  if game_a > game_b
    return 0
  else
    return 0xE000
  end
end

#find_section(id) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb', line 98

def find_section(id)
  cur_id = 255
  @save.pos = find_recent_save + 0xFF4
  while cur_id != id
    cur_id = @save.readbyte
    @save.pos += 0x1000 - 1
  end
  return @save.pos - 0x1000 - 0xFF4
end

#get_section_loc(id) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb', line 23

def get_section_loc(id)
	raise "Save file not loaded" if game.nil?
  case id
  when 0
    if @game == :rs
      return 0
    elsif @game == :frlg
      return 0
    elsif @game == :e
      return 0
    end
  when 1
    if @game == :rs
      return 0x234
    elsif @game == :frlg
      return 0x234
    elsif @game == :e
      return 0x34
    end
  end
end

#get_team_pokemon(id) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb', line 77

def get_team_pokemon(id)
  @save.pos = find_section(1) + get_section_loc(1) + 4 + ((id-1)*100)
  temp_pks = Gen3PokemonStruct.new
  100.times do |i|
    temp_pks.pdata[i] = @save.readbyte
  end
  return temp_pks
end

#get_team_sizeObject



67
68
69
70
# File 'lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb', line 67

def get_team_size
  @save.pos = find_section(1)
  return @save.readbyte
end

#load_save(file) ⇒ Object



15
16
17
# File 'lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb', line 15

def load_save(file)
  @save = File.new(file,"r+b")
end

#put_team_pokemon(id, pks) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb', line 86

def put_team_pokemon(id, pks)
  @save.pos = find_section(1) + get_section_loc(1) + 4 + ((id-1)*100)
  100.times do |i|
    if pks.pdata[i] != nil
      @save.putc pks.pdata[i]
    else 
      @save.pos = @save.pos + 1
    end
  end
  calculate_section_checksum(1)
end

#set_team_size(val) ⇒ Object



72
73
74
75
# File 'lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb', line 72

def set_team_size(val)
  @save.pos = find_section(1)
  @save.putc (val&7)
end