RMXP Script SaveFiles Updater

This code solves this problem some of you may have had:

The game you're working on is starting to get long, and you have save files on various parts of the game, for testing purposes. At some point, you decide to add a script that modifies in some way the data stored on save files. When you start a new game everything is working as expected, but you try to load a previous save file and now everything is broken including your dreams, also later that day you tell your therapist about that experience you just had, and he doesn't understand you, so it's all very traumatic.

In most times, the reason this happens is because the script adds a new variable to Game_System, which is stored when you save the game, but it can't read it on games saved prior to the script implementation. When that happens, the only thing you can do is delete those save files and make them again, which in some cases could mean hours of gameplay.

But not anymore!

If you have this problem, add this script over Main

Ruby:
#==============================================================================
# RMXP Script SaveFiles Updater v1.0
# Your savior: Wecoc (no credits required)
#==============================================================================

#==============================================================================
# ** Script module (Configuration)
#==============================================================================

module WEC_SSU
 
  ACTIVE = true           # Activate the script
 
  FULL_FOLDER = false     # Display the full name of the current folder
 
  SAVE_COPY = true        # Save a copy of the updated file
 
  SHOW_COPY = true        # Show the copy files on the folder
 
end

#==============================================================================
# ** Scene_Title
#==============================================================================

class Scene_Title
  #--------------------------------------------------------------------------
  # * Command: Continue
  #--------------------------------------------------------------------------
  alias wecssu_continue command_continue unless $@
  def command_continue
    # Load new scene if the script is active
    if WEC_SSU::ACTIVE == true
      $game_system.se_play($data_system.decision_se)
      $scene = Scene_Updater.new
    else
      wecssu_continue
    end
  end
end

#==============================================================================
# ** Dir
#==============================================================================

class Dir
  #--------------------------------------------------------------------------
  # * Dir.folders
  #--------------------------------------------------------------------------
  # Get the folders indide a given folder
  #--------------------------------------------------------------------------
  def self.folders(folder)
    all_files = Dir.open(folder).entries
    all_files.delete(".") ; all_files.delete("..")
    result_files = []
    for file in all_files
      if FileTest.exist?(folder + "/" + file)
        ftype = File.ftype(folder + "/" + file) rescue nil
        result_files.push(file) if ftype == "directory"
      end
    end
    return result_files
  end
  #--------------------------------------------------------------------------
  # * Dir.files
  #--------------------------------------------------------------------------
  # Get the files inside a given folder
  #   ext = file extension
  #--------------------------------------------------------------------------
  def self.files(folder, ext=nil)
    all_files = Dir.open(folder).entries
    result_files = []
    for file in all_files
      if FileTest.exist?(folder + "/" + file)
        ftype = File.ftype(folder + "/" + file) rescue nil
        result_files.push(file) if ftype == "file"
      end
    end
    return result_files if ext == nil
    return result_files.select {|d| d.include?("." + ext)}
  end
end

class String
  #--------------------------------------------------------------------------
  # * Inside Folders
  #--------------------------------------------------------------------------
  # Divide the complete name of the folder into a sorted array with all its
  # parent folders
  #--------------------------------------------------------------------------
  def inside_folders
    result = [""]
    n = 0
    return [self] if !self.include?("/")
    for i in 0...self.size
      if self[i].chr == "/"
        n += 1
        result[n] = ""
        next
      end
      result[n].concat(self[i].chr)
    end
    return result
  end
  #--------------------------------------------------------------------------
  # * File Ext
  #--------------------------------------------------------------------------
  # Get the extension of a file
  #--------------------------------------------------------------------------
  def file_ext
    r = self.rindex(".")
    return self[r..self.size]
  end
end

#==============================================================================
# ** Window_Files
#==============================================================================

class Window_Files < Window_Selectable
 
  # Recognized extensions of the save files
  FILE_EXTENSIONS = ["rxdata"]
 
  # Current folder
  attr_reader :folder
 
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height)
    super(x, y, width, height)
    @folder = Dir.getwd
    refresh
  end
  #--------------------------------------------------------------------------
  # * Get Item
  #--------------------------------------------------------------------------
  def item
    return @data[self.index]
  end
  #--------------------------------------------------------------------------
  # * Set current folder
  #--------------------------------------------------------------------------
  def folder=(folder)
    if folder == nil
      folder = Dir.getwd
    end
    if @folder != folder
      @folder = folder
      refresh
    end
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    if self.contents != nil
      self.contents.dispose
      self.contents = nil
    end
    @data = []
    # Set current folders
    folders = Dir.folders(@folder)
    # Set current files
    files = []
    for ext in FILE_EXTENSIONS
      files.concat Dir.files(@folder, ext)
    end
    # Add folders
    for i in 0...folders.size
      @data.push(folders[i])
    end
    # Add files
    for i in 0...files.size
      # Ignore the files _copy generated for security
      next if files[i].include?("_copy")
      @data.push(files[i])
    end
    # Add "copy" files
    if WEC_SSU::SHOW_COPY == true
      for i in 0...files.size
        # Display the _copy generated files at the end
        next if !files[i].include?("_copy")
        @data.push(files[i])
      end
    end
    # If item count is not 0, make a bit map and draw all items
    @item_max = @data.size
    if @item_max > 0
      self.contents = Bitmap.new(width - 32, row_max * 32)
      for i in 0...@item_max
        draw_item(i)
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Draw Item
  #     index : item number
  #--------------------------------------------------------------------------
  def draw_item(index)
    item = @data[index]
    if folder?(index)
      self.contents.font.color = normal_color
    else
      self.contents.font.color = system_color
    end
    x = 4
    y = index * 32
    self.contents.draw_text(x, y, 640 - 32, 32, item, 0)
  end
 
  def folder?(index)
    return !file?(index)
  end
 
  def file?(index)
    item = @data[index]
    for ext in FILE_EXTENSIONS
      if item.include?(ext)
        return true
      end
    end
    return false
  end
end

#==============================================================================
# ** Scene_Updater
#==============================================================================

class Scene_Updater
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Create the help window
    @help_window = Window_Help.new
    @help_window.set_text("What file do you want to update?")
    # The default starting folder is the project's main folder
    @folder = Dir.getwd
    # Create the folder window
    @folder_window = Window_Help.new
    @folder_window.y = 64
    @folder_window.set_text(folder_name)
    # Create the files window
    @files_window = Window_Files.new(0, 128, 640, 480 - 128)
    @files_window.index = 0
    @files_window.active = true
    # Execute transition
    Graphics.transition
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of windows
    @help_window.dispose
    @folder_window.dispose
    @files_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Folder Name
  #--------------------------------------------------------------------------
  def folder_name
    if WEC_SSU::FULL_FOLDER == true
      return @folder.to_s
    else
      f = @folder.inside_folders
      wd_f = Dir.getwd.inside_folders
      f.delete("")
      folders = f[0] + "/.../"
      folders += wd_f[wd_f.size - 1] + "/"
      f -= wd_f
      for i in 0...f.size
        folders += f[i] + "/"
      end
      return folders
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    @folder_window.update
    @files_window.update
    # Update current folder
    if @folder != @files_window.folder
      @folder = @files_window.folder
      @folder_window.set_text(folder_name)
    end
    # If B button was pressed
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      # If current folder is the project's folder, go back to title
      if @folder == Dir.getwd
        $scene = Scene_Title.new
        return
      end
      # Get to the parent folder
      folders = @folder.inside_folders
      last_folder = folders[folders.size-1]
      new_folder = folders[0...folders.size-1]
      new_folder.delete("")
      new_folder = new_folder.join("/")
      @files_window.folder = new_folder
      @files_window.index = Dir.folders(new_folder).index(last_folder)
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # If the selected item is a folder
      if @files_window.folder?(@files_window.index)
        $game_system.se_play($data_system.decision_se)
        # Open the folder
        new_folder = @folder + "/" + @files_window.item
        @files_window.folder = new_folder
        @files_window.index = 0
        return
      # If the selected item is a file
      else
        filename = @folder + "/" + @files_window.item
        # Update the file
        restore_file(filename)
      end
    end
  end
 
  def restore_file(filename)
    # Load the file
    begin
      file = File.open(filename, "rb")
        characters = Marshal.load(file)
        Graphics.frame_count = Marshal.load(file)
        game_system        = Marshal.load(file)
        game_switches      = Marshal.load(file)
        game_variables     = Marshal.load(file)
        game_self_switches = Marshal.load(file)
        game_screen        = Marshal.load(file)
        game_actors        = Marshal.load(file)
        game_party         = Marshal.load(file)
        game_troop         = Marshal.load(file)
        game_map           = Marshal.load(file)
        game_player        = Marshal.load(file)
      file.close
    rescue
      # Invalid file
      p "Invalid file! - This is not a Save File"
      return
    end
    # Create a copy of the file
    if WEC_SSU::SAVE_COPY == true
      file_ext = filename.file_ext
      filename_copy = filename[0...filename.size - file_ext.size]
      filename_copy = filename_copy + "_copy" + file_ext
      file = File.open(filename_copy, "wb")
        characters = []
        for i in 0...game_party.actors.size
          actor = game_party.actors[i]
          characters.push([actor.character_name, actor.character_hue])
        end
        Marshal.dump(characters, file)
        Marshal.dump(Graphics.frame_count, file)
        Marshal.dump(game_system, file)
        Marshal.dump(game_switches, file)
        Marshal.dump(game_variables, file)
        Marshal.dump(game_self_switches, file)
        Marshal.dump(game_screen, file)
        Marshal.dump(game_actors, file)
        Marshal.dump(game_party, file)
        Marshal.dump(game_troop, file)
        Marshal.dump(game_map, file)
        Marshal.dump(game_player, file)
      file.close
    end
    # Create a new game
    $game_temp          = Game_Temp.new
    $game_system        = Game_System.new
    $game_switches      = Game_Switches.new
    $game_variables     = Game_Variables.new
    $game_self_switches = Game_SelfSwitches.new
    $game_screen        = Game_Screen.new
    $game_actors        = Game_Actors.new
    $game_party         = Game_Party.new
    $game_troop         = Game_Troop.new
    $game_map           = Game_Map.new
    $game_player        = Game_Player.new
    $game_party.setup_starting_members
    $game_map.setup($data_system.start_map_id)
    $game_player.moveto($data_system.start_x, $data_system.start_y)
    $game_player.refresh
    $game_map.autoplay
    $game_map.update
    
    # Define all data to the new game from the previous one
    data_names = ["system", "switches", "variables", "self_switches",
                  "screen", "actors", "party", "troop", "map", "player"]
    for data in data_names
      new_variables = eval("$game_#{data}.instance_variables")
      variables     = eval("game_#{data}.instance_variables")
      for i in new_variables
        if variables.include?(i)
          s = eval("game_#{data}.instance_variable_get(i)")
          e = eval("$game_#{data}.instance_variable_get(i)")
          if e != s
            eval("$game_#{data}.instance_variable_set(i, s)")
          end
        end
      end
    end
    # Rewrite the loaded file for the new one
    file = File.open(filename, "wb")
      characters = []
      for i in 0...$game_party.actors.size
        actor = $game_party.actors[i]
        characters.push([actor.character_name, actor.character_hue])
      end
      Marshal.dump(characters, file)
      Marshal.dump(Graphics.frame_count, file)
      Marshal.dump($game_system, file)
      Marshal.dump($game_switches, file)
      Marshal.dump($game_variables, file)
      Marshal.dump($game_self_switches, file)
      Marshal.dump($game_screen, file)
      Marshal.dump($game_actors, file)
      Marshal.dump($game_party, file)
      Marshal.dump($game_troop, file)
      Marshal.dump($game_map, file)
      Marshal.dump($game_player, file)
    file.close
    # Refresh the window
    @files_window.refresh
    $game_system.se_play($data_system.save_se)
  end
end

Terms of use
- Use this freely, no credits are required. (You will delete the code after using it anyway)
- You can share and edit this code.

Instructions
Copy the script above Main. Start the game from the editor. A window will appear with the folders and save files of the game. If your game stores the files inside a custom folder you can navigate through this using C (Accept) to open them and X (Cancel) to go back.

file1.png


Once you've selected the desired file to update, press C (Accept) to update it, and you should hear the "Save" sound. By default it will also generate a copy of the save file. Then close the game back to the editor, search on the beginning of the script the line ACTIVE = true and set it to false. Test the game again; everything should be fine now. Your file has been updated!

Once all changes have been made, you can delete this script.

Spoiler: Cases where it may not work
In some complicated cases (like a more serious rewriting of the structures of the game, or scripts that implicitely modify the load/save methods) it won't work. In those cases it usually doesn't even recognize the save file so it won't try to update it.
That means at least it won't make the problem worse 😅

If for some reason it tried to update it but it didn't work, all you have to do is delete that save file, search the copy and change its name back.

In summary, I can't guarantee it will work in every case, but in some cases it can save you a lot of time and trouble.

I don't have a nice catchphrase to end my posts, so:
Bone apple script!
[Yeah, I'll keep working on that]

Original thread: https://forums.rpgmakerweb.com/threads/rmxp-script-savefiles-updater.138415/