#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# DREAM controller for Save Files by Blizzard
# Version: 3.0b
# Type: Encryptor / Decryptor
# uses DREAM v4.1 or higher
# Date 22.7.2006
# Date v2.0: 25.1.2007
# Date v3.0: 20.8.2007
# Date v3.0b: 24.9.2007
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#   
#  This work is protected by the following license:
# #----------------------------------------------------------------------------
# #  
# #  Creative Commons - Attribution-NonCommercial-ShareAlike 3.0 Unported
# #  ( http://creativecommons.org/licenses/by-nc-sa/3.0/ )
# #  
# #  You are free:
# #  
# #  to Share - to copy, distribute and transmit the work
# #  to Remix - to adapt the work
# #  
# #  Under the following conditions:
# #  
# #  Attribution. You must attribute the work in the manner specified by the
# #  author or licensor (but not in any way that suggests that they endorse you
# #  or your use of the work).
# #  
# #  Noncommercial. You may not use this work for commercial purposes.
# #  
# #  Share alike. If you alter, transform, or build upon this work, you may
# #  distribute the resulting work only under the same or similar license to
# #  this one.
# #  
# #  - For any reuse or distribution, you must make clear to others the license
# #    terms of this work. The best way to do this is with a link to this web
# #    page.
# #  
# #  - Any of the above conditions can be waived if you get permission from the
# #    copyright holder.
# #  
# #  - Nothing in this license impairs or restricts the author's moral rights.
# #  
# #----------------------------------------------------------------------------
# 
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# 
#   DO TOUCH THE VALUE BELOW! If you change the required version to a lower
#   value your script WILL malfunction, because older DREAM version don't have
#   all the methods and functions required by this script.
Required_DREAM_Version = 4.1
# 
# 
# Special Thanks to:
# 
#   - the makers of the XVI32 Hex Editor
#     (without it, I couldn't have tested this script)
# 
# 
# Compatibility:
# 
#   98% compatible with SDK 1.x. 90% compatible with SDK 2.x. 99% compatible
#   with everything else. WILL corrupt your old savegames. Can cause
#   incompatibility issues with custom save screens. NEEDS DREAM of the
#   provided version or higher. Files will be saved into dream4 format with
#   .dream4 extension. Cannot decode dream2 and dream3 files where extra data
#   was added, but normal dream2 and dream3 files can be decoded.
# 
# 
# Features:
# 
#   - uses the Double Random Encryption Algorythm Module (DREAM) to encrypt
#     important save data every time it is saved
#   - makes it harder to hack saved files (even experienced hackers will have a
#     hard time hacking important data from save files encrypted with DREAM)
#   - great for RMXP MMORPGs as alternative for saving data on the server
# 
# new in v2.0:
#   - uses DREAM v3.0 which is far more powerful than DREAM v2.0, because the
#     .dream3 format is a format in which ALL the saved data is encrypted
#   - your savefiles will be saved into files with .dream3 extension
# 
# new in v3.0:
#   - uses the even more powerful DREAM v4.0
#   - faster, better, safer:
#     -> new, much faster algorithm which's speed doesn't depend on the chosen
#        encryption pattern anymore
#     -> re-coded to avoid the built-in RGSS bug with conditioning
#     -> new additional encryption of encrypted data
#   - your savefiles will be saved now into files with .dream4 extension
#   - displays "File corrupted!" instead of crashing when files are corrupted
#   - can decode dream2 and dream3 format
# 
# new in v3.0b:
#   - now uses DREAM v4.1
#   - direct implementation into DREAM
#   - now beta
# 
# 
# Configuration:
# 
#   Open the Scene_Title script and find these lines or similar:
# 
#     for i in 0..3
#       if FileTest.exist?("Save#{i+1}.rxdata")
#         @continue_enabled = true
#       end
#     end
# 
#   Change this line:
#       if FileTest.exist?("Save#{i+1}.rxdata")
#   into this one:
#       if FileTest.exist?("#{SAVE_NAME}#{i+1}.dream4")
# 
#   Change SAVE_NAME to the name your savefiles should have. i.e. Making it
#   "The Last_Elf" will make your savefiles look like "The Last_Elf.dream4".
# 
#   Find the part that needs to be configured and add additional data if you
#   are using additional savedata. Include all additional data AS STRINGS in
#   the array EXTRA_DATA. Example:
#   
#     EXTRA_DATA = ['$game_weapons', '$game_armors', '$game_items']
#
# 
# IMPORTANT NOTES:
# 
#   This script will encrypt all save data and render them 100% unusable by any
#   earlier version of your game that DOES NOT have this script. If you are
#   using save-game update of earlier versions, this script will make further
#   updates of old savegames IMPOSSIBLE, because they can't be loaded at all.
#   Please use a DREAM save file converter to convert usual save files into a
#   DREAM readable format. Note that this version of this script can also READ
#   save files encrypted in dream2 and dream3 format only if their extension is
#   being changed to .dream4 and their names are changed like configured below.
#   Note that any extra data from dream2 and dream3 files will not be read.
#   This script will save any files into .dream4 format. Changing this script
#   can easily destroy your savefiles and it is not recommended.
# 
# 
# If you find any bugs, please report them here:
# http://forum.chaos-project.com
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

EXTRA_DATA = []
SAVE_NAME = 'Save'

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

#==============================================================================
# module DREAM
#==============================================================================

module DREAM
  
  def self.self_extract
    data = load_data('Data/DREAM_ext.rb')
    file = File.open('DREAM_ext.rb', 'wb')
    file.write(data)
    file.close
    require File.expand_path('DREAM_ext.rb')
    load_DREAM
  end
  
end

DREAM.self_extract

#==============================================================================
# Scene_File
#==============================================================================

class Scene_File
  
  def make_filename(file_index)
    return "#{SAVE_NAME}#{file_index + 1}.dream4"
  end
  
end

#==============================================================================
# Scene_Save
#==============================================================================

class Scene_Save < Scene_File
  
  def write_save_data(file)
    DREAM.save_game(file)
  end

end

#==============================================================================
# Scene_Load
#==============================================================================

class Scene_Load < Scene_File
  
  def read_save_data(file)
    DREAM.load_game(file)
  end

end

#==============================================================================
# Window_SaveFile
#==============================================================================

class Window_SaveFile < Window_Base

  def initialize(file_index, filename)
    super(0, 64 + file_index%4*104, 640, 104)
    self.contents = Bitmap.new(width - 32, height - 32)
    if $fontface != nil
      self.contents.font.name = $fontface
      self.contents.font.size = $fontsize
    elsif $defaultfonttype != nil
      self.contents.font.name = $defaultfonttype
      self.contents.font.size = $defaultfontsize
    end
    @file_index, @filename = file_index, "#{SAVE_NAME}#{file_index + 1}.dream4"
    @time_stamp, @file_exist = Time.at(0), FileTest.exist?(@filename)
    if @file_exist
      begin
        file = File.open(@filename, 'r')
        @time_stamp = file.mtime
        @characters, @frame_count, @game_system, @game_switches,
        @game_variables, @game_party, @game_map, @game_player = DREAM.data(file)
        @total_sec = @frame_count / Graphics.frame_rate
        file.close
        refresh
      rescue
        @file_exist = false
        refresh
        self.contents.draw_text(4, 20, 600, 32, 'File corrupted!', 1)
        time_string = @time_stamp.strftime('%Y/%m/%d %H:%M')
        self.contents.draw_text(4, 40, 600, 32, time_string, 2)
      end
    else
      refresh
    end
    @selected = false
  end

end