class Gen3PokemonSaveLoader

Public Class Methods

new(file = nil) click to toggle source
# 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

Public Instance Methods

calculate_section_checksum(id) click to toggle source
# 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
close() click to toggle source
# File lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb, line 19
def close
  @save.close
end
detect_game_version() click to toggle source
# 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_save() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_size() click to toggle source
# File lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb, line 67
def get_team_size
  @save.pos = find_section(1)
  return @save.readbyte
end
load_save(file) click to toggle source
# File lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb, line 15
def load_save(file)
  @save = File.new(file,"r+b")
end
put_team_pokemon(id, pks) click to toggle source
# 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) click to toggle source
# File lib/gen3_pokeedit/Gen3PokemonSaveLoader.rb, line 72
def set_team_size(val)
  @save.pos = find_section(1)
  @save.putc (val&7)
end