Szyu

SZS_Autosave

Sep 3rd, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.54 KB | None | 0 0
  1. #== Szyu Scripts presents =====================================================
  2. # * Szyu Scripts' Autosave
  3. # Version 1.0
  4. # by Szyu
  5. #
  6. #== Download ==================================================================
  7. # * pastebin:
  8. # http://adf.ly/4920670/autosave
  9. #
  10. #== About =====================================================================
  11. # * This script will keep track of the time played and save your game
  12. # automatically in custom defined intervals. You can also force an autosave or
  13. # load an autosaved game from the title screen.
  14. #
  15. #== How to Use ================================================================
  16. # * All customizable options are kept in the module "SZS_Autosave" with their
  17. # representive functionality.
  18. #
  19. # * To force an autosave, use a script call with following line:
  20. #     DataManager.autosave_game
  21. #
  22. # * To load the autosaved file, use a script call with following line:
  23. #     DataManager.load_autosave
  24. #
  25. #== Terms of Use ==============================================================
  26. # * You are free to use this script for commercial and non-commercial projects.
  27. # However I'd like you to inform me of the projects you are planning to use it
  28. # in, so I can keep track of where my scripts are used.
  29. #
  30. #== Import ====================================================================
  31. $imported = {} if $imported.nil?
  32. $imported["SZ-AUTOSAVE"] = true
  33. #==============================================================================
  34. module SZS_Autosave
  35.   # Name of autosave file
  36.   Filename = "Autosave"
  37.  
  38.   # Interval in minutes used for autosaves
  39.   Interval = 10
  40.  
  41.   # Term used in Scene_Title
  42.   Term = "Autoload"
  43.  
  44.   # Define, if this script automatically saves the game
  45.   Enabled = true
  46. end
  47.  
  48. #==============================================================================
  49. # ** DataManager
  50. #==============================================================================
  51. class << DataManager
  52.   #--------------------------------------------------------------------------
  53.   # * Execute Autosave
  54.   #--------------------------------------------------------------------------
  55.   def autosave_game
  56.     begin
  57.       autosave_game_without_rescue
  58.     rescue
  59.       delete_temp_autosave_file
  60.       false
  61.     end
  62.     apply_autosave
  63.   end
  64.   #--------------------------------------------------------------------------
  65.   # * Execute Load Autosave
  66.   #--------------------------------------------------------------------------
  67.   def load_autosave
  68.     load_autosave_without_rescue rescue false
  69.   end
  70.   #--------------------------------------------------------------------------
  71.   # * Load Save Header
  72.   #--------------------------------------------------------------------------
  73.   def load_autosave_header
  74.     load_autosave_header_without_rescue rescue nil
  75.   end
  76.   #--------------------------------------------------------------------------
  77.   # * Execute Save (No Exception Processing)
  78.   #--------------------------------------------------------------------------
  79.   def autosave_game_without_rescue
  80.     File.open(sprintf("%s.tmp", SZS_Autosave::Filename), "wb") do |file|
  81.       $game_system.on_before_save
  82.       Marshal.dump(make_save_header, file)
  83.       Marshal.dump(make_save_contents, file)
  84.     end
  85.     return true
  86.   end
  87.   #--------------------------------------------------------------------------
  88.   # * Execute Load (No Exception Processing)
  89.   #--------------------------------------------------------------------------
  90.   def load_autosave_without_rescue
  91.     File.open(sprintf("%s.rvdata2", SZS_Autosave::Filename), "rb") do |file|
  92.       Marshal.load(file)
  93.       extract_save_contents(Marshal.load(file))
  94.       reload_map_if_updated
  95.     end
  96.     return true
  97.   end
  98.   #--------------------------------------------------------------------------
  99.   # * Load Save Header (No Exception Processing)
  100.   #--------------------------------------------------------------------------
  101.   def load_autosave_header_without_rescue
  102.     File.open(sprintf("%s.rvdata2", SZS_Autosave::Filename), "rb") do |file|
  103.       return Marshal.load(file)
  104.     end
  105.     return nil
  106.   end
  107.   #--------------------------------------------------------------------------
  108.   # * Delete Save File
  109.   #--------------------------------------------------------------------------
  110.   def delete_temp_autosave_file
  111.     File.delete(sprintf("%s.tmp", SZS_Autosave::Filename)) rescue nil
  112.   end
  113.   #--------------------------------------------------------------------------
  114.   # * Apply temporary Autosave
  115.   #--------------------------------------------------------------------------
  116.   def apply_autosave
  117.     File.rename(sprintf("%s.tmp", SZS_Autosave::Filename),
  118.       sprintf("%s.rvdata2", SZS_Autosave::Filename)) rescue false
  119.   end
  120.   #--------------------------------------------------------------------------
  121.   # * Determine Existence of Autosave File
  122.   #--------------------------------------------------------------------------
  123.   def autosave_file_exists?
  124.     !Dir.glob(sprintf("%s.rvdata2", SZS_Autosave::Filename)).empty?
  125.   end
  126. end
  127.  
  128. #==============================================================================
  129. # ** Scene_Map
  130. #------------------------------------------------------------------------------
  131. #  This class performs the map screen processing.
  132. #==============================================================================
  133. class Scene_Map
  134.   alias :szs_asave_update :update
  135.   #--------------------------------------------------------------------------
  136.   # * Update
  137.   #--------------------------------------------------------------------------
  138.   def update
  139.     szs_asave_update
  140.     update_autosave if SZS_Autosave::Enabled && SZS_Autosave::Interval > 0
  141.   end
  142.   #--------------------------------------------------------------------------
  143.   # * Update Autosave
  144.   #--------------------------------------------------------------------------
  145.   def update_autosave
  146.     min = $game_system.playtime / 60.0
  147.     if min-$game_system.last_autosave >= SZS_Autosave::Interval
  148.       DataManager.autosave_game
  149.       $game_system.last_autosave = min
  150.     end
  151.   end
  152. end
  153.  
  154. #==============================================================================
  155. # ** Game_System
  156. #------------------------------------------------------------------------------
  157. #  This class handles system data. It saves the disable state of saving and
  158. # menus. Instances of this class are referenced by $game_system.
  159. #==============================================================================
  160. class Game_System
  161.   alias :szs_asave_initialize :initialize
  162.   attr_accessor :last_autosave
  163.   #--------------------------------------------------------------------------
  164.   # * Initialize
  165.   #--------------------------------------------------------------------------
  166.   def initialize
  167.     szs_asave_initialize
  168.     @last_autosave = 0
  169.   end
  170. end
  171.  
  172. #==============================================================================
  173. # ** Scene_Title
  174. #------------------------------------------------------------------------------
  175. #  This class performs the title screen processing.
  176. #==============================================================================
  177. class Scene_Title < Scene_Base
  178.   alias :szs_asave_create_command_window :create_command_window
  179.   #--------------------------------------------------------------------------
  180.   # * Create Command Window
  181.   #--------------------------------------------------------------------------
  182.   def create_command_window
  183.     szs_asave_create_command_window
  184.     @command_window.set_handler(:autosave, method(:command_load_autosave))
  185.   end
  186.   #--------------------------------------------------------------------------
  187.   # * Load Autosave
  188.   #--------------------------------------------------------------------------
  189.   def command_load_autosave
  190.     if DataManager.load_autosave
  191.       on_load_success
  192.     else
  193.       Sound.play_buzzer
  194.     end
  195.   end
  196.   #--------------------------------------------------------------------------
  197.   # * Processing When Load Is Successful
  198.   #--------------------------------------------------------------------------
  199.   def on_load_success
  200.     Sound.play_load
  201.     fadeout_all
  202.     $game_system.on_after_load
  203.     SceneManager.goto(Scene_Map)
  204.   end
  205. end
  206.  
  207. #==============================================================================
  208. # ** Window_TitleCommand
  209. #------------------------------------------------------------------------------
  210. #  This window is for selecting New Game/Continue on the title screen.
  211. #==============================================================================
  212. class Window_TitleCommand < Window_Command
  213.   alias :szs_asave_make_command_list :make_command_list
  214.   alias :sts_asave_process_ok :process_ok
  215.   #--------------------------------------------------------------------------
  216.   # * Create Command List
  217.   #--------------------------------------------------------------------------
  218.   def make_command_list
  219.     add_command(SZS_Autosave::Term, :autosave) if autosave_enabled
  220.     szs_asave_make_command_list
  221.   end
  222.   #--------------------------------------------------------------------------
  223.   # * Checks if an autosave file exists
  224.   #--------------------------------------------------------------------------
  225.   def autosave_enabled
  226.     DataManager.autosave_file_exists?
  227.   end
  228.   #--------------------------------------------------------------------------
  229.   # * Processing When OK Button Is Pressed
  230.   #--------------------------------------------------------------------------
  231.   def process_ok
  232.     if current_symbol == :autosave
  233.       Input.update
  234.       call_ok_handler
  235.     else
  236.       sts_asave_process_ok
  237.     end
  238.   end
  239. end
Advertisement
Add Comment
Please, Sign In to add comment