Class: GamePlay::Save

Inherits:
Load show all
Defined in:
scripts/01450 Systems/00106 Save Load/00003 GamePlay/00400 Save.rb,
scripts/01450 Systems/09000 GTS/09000 GTS.rb,
scripts/01450 Systems/00106 Save Load/00003 GamePlay/00401 Save input.rb,
scripts/01450 Systems/00106 Save Load/00003 GamePlay/00402 Save logic.rb

Overview

Save game scene

Constant Summary collapse

MULTI_SAVE_FORMAT =

MultiSave file format

'%s-%d'
SAVE_ROOT_PATHS =

List of the usable root path for the save state

[
  '.',
  ENV['APPDATA'] || Dir.home,
  Dir.home
]
BEFORE_SAVE_HOOKS =

Returns all the before save hooks.

Returns:

  • (Hash)

    all the before save hooks

{ game_map: proc { $game_map.begin_save } }
AFTER_SAVE_HOOKS =

Returns all the after save hooks.

Returns:

  • (Hash)

    all the after save hooks

{ game_map: proc { $game_map.end_save } }

Constants inherited from Load

Load::ACTIONS

Constants inherited from BaseCleanUpdate

BaseCleanUpdate::AIU_KEY2METHOD

Constants inherited from Base

Base::DEFAULT_TRANSITION, Base::DEFAULT_TRANSITION_PARAMETER

Constants included from Input

Input::ALIAS_KEYS, Input::AXIS_MAPPING, Input::AXIS_SENSITIVITY, Input::DEAD_ZONE, Input::Keyboard, Input::Keys, Input::NON_TRIGGER_ZONE, Input::REPEAT_COOLDOWN, Input::REPEAT_SPACE

Constants included from DisplayMessage

DisplayMessage::MESSAGE_ERROR, DisplayMessage::MESSAGE_PROCESS_ERROR

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Base

#__last_scene, #__result_process, #running, #viewport

Attributes included from DisplayMessage

#message_window

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Load

#create_new_game, #should_make_new_game?, #update_graphics, #update_inputs, #update_mouse

Methods inherited from BaseCleanUpdate

#automatic_input_update, #update

Methods inherited from Base

#add_disposable, #call_scene, #dispose, #find_parent, #main, #return_to_scene, #snap_to_bitmap, #update, #visible, #visible=

Methods included from Input

dir4, dir8, get_text, joy_axis_position, press?, register_events, released?, repeat?, swap_states, trigger?

Methods included from DisplayMessage

#can_display_message_be_called?, #close_message_window, #display_message, #display_message_and_wait, #message_class, #message_processing?, #message_visible, #message_visible=

Constructor Details

#initializeSave

Create a new GamePlay::Save



18
19
20
21
22
23
24
# File 'scripts/01450 Systems/00106 Save Load/00003 GamePlay/00400 Save.rb', line 18

def initialize
  super
  make_save_directory
  @saved = false
  @index = Configs.save_config.single_save? ? 0 : Save.save_index - 1
  @index = 0 if @index < 0
end

Class Attribute Details

.save_indexInteger

Returns index of the save file (to allow multi-save).

Returns:

  • (Integer)

    index of the save file (to allow multi-save)



5
6
7
# File 'scripts/01450 Systems/00106 Save Load/00003 GamePlay/00402 Save logic.rb', line 5

def save_index
  @save_index
end

Instance Attribute Details

#savedBoolean (readonly)

Returns if the game was saved.

Returns:

  • (Boolean)

    if the game was saved



15
16
17
# File 'scripts/01450 Systems/00106 Save Load/00003 GamePlay/00400 Save.rb', line 15

def saved
  @saved
end

Class Method Details

.load(filename = nil, no_load_parameter: false) ⇒ PFM::GameState?

Note:

Change PFM.game_state

Load a game

Parameters:

  • filename (String, nil) (defaults to: nil)

    name of the save file (nil = auto name the save file)

  • no_load_parameter (Boolean) (defaults to: false)

    if the system should not call load_parameters

Returns:

  • (PFM::GameState, nil)

    The save data (nil = no save data / data corruption)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'scripts/01450 Systems/00106 Save Load/00003 GamePlay/00402 Save logic.rb', line 37

def load(filename = nil, no_load_parameter: false)
  filename ||= Save.save_filename
  return nil unless File.exist?(filename)

  header = Configs.save_config.save_header
  data = File.binread(filename)
  file_header = data[0...(header.size)]
  return nil if file_header != header

  PFM.game_state = Marshal.load(encrypt(data[header.size..-1]))
  PFM.game_state.load_parameters unless no_load_parameter
  return PFM.game_state
rescue LoadError, StandardError
  log_error("Corrupted save error: #{$!.class} => #{$!.message}")
  return nil
end

.save(filename = nil, no_file = false)

Save a game

Parameters:

  • filename (String, nil) (defaults to: nil)

    name of the save file (nil = auto name the save file)

  • no_file (Boolean) (defaults to: false)

    tell if the save should not be saved to file and just be returned



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'scripts/01450 Systems/00106 Save Load/00003 GamePlay/00402 Save logic.rb', line 15

def save(filename = nil, no_file = false)
  return 'NONE' unless $game_temp

  clear_states
  update_save_info
  # Call the hooks that make the save data safer and lighter
  BEFORE_SAVE_HOOKS.each_value(&:call)
  # Build the save data
  save_data = Configs.save_config.save_header.dup.force_encoding(Encoding::ASCII_8BIT)
  save_data << encrypt(Marshal.dump(PFM.game_state))
  # Save the game
  save_file(filename || Save.save_filename, save_data) unless no_file
  # Call the hooks that restore all the data
  AFTER_SAVE_HOOKS.each_value(&:call)
  return save_data
end

.save_filename



58
59
60
61
62
63
64
# File 'scripts/01450 Systems/00106 Save Load/00003 GamePlay/00402 Save logic.rb', line 58

def save_filename
  root = save_root_path.tr('\\', '/').encode(Encoding::UTF_8)
  game_name = root.start_with?('.') ? '' : ".#{Configs.infos.game_title}/"
  base_filename = Configs.save_config.base_filename
  filename = (@save_index > 0 ? format(MULTI_SAVE_FORMAT, base_filename, @save_index) : base_filename)
  return format('%<root>s/%<game_name>s%<filename>s', root: root, game_name: game_name, filename: filename)
end

.save_root_path



54
55
56
# File 'scripts/01450 Systems/00106 Save Load/00003 GamePlay/00402 Save logic.rb', line 54

def save_root_path
  SAVE_ROOT_PATHS.find(&File.method(:writable?)) || ''
end

Instance Method Details

#current_game_stateGameState?

Return the current GameState object

Returns:

  • (GameState, nil)


28
29
30
# File 'scripts/01450 Systems/00106 Save Load/00003 GamePlay/00400 Save.rb', line 28

def current_game_state
  PFM.game_state || Save.load
end

#gts_save_game



1013
# File 'scripts/01450 Systems/09000 GTS/09000 GTS.rb', line 1013

alias gts_save_game save_game

#save_game

Save the game (method allowing hooks on the save)



33
34
35
36
37
38
39
40
# File 'scripts/01450 Systems/00106 Save Load/00003 GamePlay/00400 Save.rb', line 33

def save_game
  potential_old_party = current_game_state
  if potential_old_party&.raw_online_id && potential_old_party&.online_pokemon &&
     potential_old_party.raw_online_id != PFM.game_state.raw_online_id
    GTS::Core.delete_pokemon(true, potential_old_party)
  end
  gts_save_game
end