#
#    クイックロード(RGSS3)
#  (C)2012 TYPE74RX-T
#
 
#==============================================================================
# ■ SceneManager
#------------------------------------------------------------------------------
#  シーン遷移を管理するモジュールです。たとえばメインメニューからアイテム画面
# を呼び出し、また戻るというような階層構造を扱うことができます。
#==============================================================================
 
class << SceneManager # module内のエイリアス
  alias rx3_1201202_first_scene_class first_scene_class
  def first_scene_class
    # ★ クイックロードモードの条件を満たしていたらクイックロードに飛ぶ
    return Scene_RX_QuickLoad if rx_quick_load?
    rx3_1201202_first_scene_class  # メソッド呼び戻し
  end
end
 
module SceneManager
  #--------------------------------------------------------------------------
  # ★ クイックロードモードの条件を満たしているか
  #--------------------------------------------------------------------------
  def self.rx_quick_load?
    $RX_QLOAD = 0
    Input.update
    $RX_QLOAD = 1 if Input.press?(:C) # 最後にロードしたファイルから始める
    $RX_QLOAD = 2 if Input.press?(:B) # 最後にセーブしたファイルから始める
    chk = Dir.glob('RX_SaveFileInfos.dat').size > 0 && $RX_QLOAD > 0
    return chk
  end
end
 
#==============================================================================
# ■ Scene_Save
#------------------------------------------------------------------------------
#  セーブ画面の処理を行うクラスです。
#==============================================================================
 
class Scene_Save < Scene_File
  #--------------------------------------------------------------------------
  # ● セーブ成功時の処理
  #--------------------------------------------------------------------------
  alias rx3_1201202_on_save_success on_save_success
  def on_save_success
    index_save         # ★ セーブしたファイルインデックス情報を書き込む
    rx3_1201202_on_save_success # メソッド呼び戻し
  end
  #--------------------------------------------------------------------------
  # ★ セーブしたファイルインデックス情報を書き込む
  #--------------------------------------------------------------------------
  def index_save
    chk = rx_read_load_index # インデックス情報があれば、それを読み込む
    file = File.open("RX_SaveFileInfos.dat", "wb")
    # ファイルインデックス情報ファイルがなければ、仮に -1 を記録
    chk ? Marshal.dump(@rx_load_index, file) : Marshal.dump(-1, file)
    Marshal.dump(@index, file)
    file.close
  end
  #--------------------------------------------------------------------------
  # ★ ロードしたファイルインデックス情報を読み込む
  #--------------------------------------------------------------------------
  def rx_read_load_index
    chk = Dir.glob('RX_SaveFileInfos.dat').size > 0
    return false unless chk
    file = File.open("RX_SaveFileInfos.dat", "rb")
    @rx_load_index = Marshal.load(file)
    sav_id         = Marshal.load(file)
    file.close
    return true
  end
end
 
#==============================================================================
# ■ Scene_Load
#------------------------------------------------------------------------------
#  ロード画面の処理を行うクラスです。
#==============================================================================
 
class Scene_Load < Scene_File
  #--------------------------------------------------------------------------
  # ● ロード成功時の処理
  #--------------------------------------------------------------------------
  alias rx3_1201202_on_load_success on_load_success
  def on_load_success
    index_save         # ★ ロードしたファイルインデックス情報を書き込む
    rx3_1201202_on_load_success # メソッド呼び戻し
  end
  #--------------------------------------------------------------------------
  # ★ ロードしたファイルインデックス情報を書き込む
  #--------------------------------------------------------------------------
  def index_save
    rx_read_save_index # 先にセーブしたファイルインデックス情報を読み込む
    file = File.open("RX_SaveFileInfos.dat", "wb")
    Marshal.dump(@index, file)
    Marshal.dump(@rx_save_index, file)
    file.close
  end
  #--------------------------------------------------------------------------
  # ★ セーブしたファイルインデックス情報を読み込む
  #--------------------------------------------------------------------------
  def rx_read_save_index
    file = File.open("RX_SaveFileInfos.dat", "rb")
    rx_load_index  = Marshal.load(file)
    @rx_save_index = Marshal.load(file)
    file.close
  end
end
 
#==============================================================================
# ★ Scene_RX_QuickLoad
#------------------------------------------------------------------------------
#  クイックロードの処理を行うクラスです。
#==============================================================================
 
class Scene_RX_QuickLoad < Scene_MenuBase
  #--------------------------------------------------------------------------
  # ● 開始処理
  #--------------------------------------------------------------------------
  def start
    super
    quick_load
  end
  #--------------------------------------------------------------------------
  # ● クイックロード
  #--------------------------------------------------------------------------
  def quick_load
    index = read_index
    if index >= 0
      $game_system.on_after_load if DataManager.load_game(index)
      SceneManager.goto(Scene_Map)
    else
      SceneManager.goto(Scene_Title)
    end
  end
  #--------------------------------------------------------------------------
  # ● インデックス情報をロード
  #--------------------------------------------------------------------------
  def read_index
    file = File.open("RX_SaveFileInfos.dat", "rb")
    load_index     = Marshal.load(file)
    save_index     = Marshal.load(file)
    file.close
    return load_index if $RX_QLOAD == 1
    return save_index if $RX_QLOAD == 2
  end
end