=begin #=============================================================================== Title: Compressed Save Files Author: Hime Date: Feb 1, 2014 URL: http://himeworks.com/2014/02/01/compressed-save-files/ -------------------------------------------------------------------------------- ** Change log Feb 1, 2014 - Initial release -------------------------------------------------------------------------------- ** Terms of Use * Free to use in non-commercial projects * Contact me for commercial use * No real support. The script is provided as-is * Will do bug fixes, but no compatibility patches * Features may be requested but no guarantees, especially if it is non-trivial * Credits to Hime Works in your project * Preserve this header -------------------------------------------------------------------------------- ** Description This script compresses all save files, resulting in smaller save files. For backwards compatibility, if a compressed file is not available, it will try to load the uncompressed file. -------------------------------------------------------------------------------- ** Installation In the script editor, place this script below Materials and above Main -------------------------------------------------------------------------------- ** Usage Plug and play. #=============================================================================== =end $imported = {} if $imported.nil? $imported[:TH_CompressedSaveFiles] = true #=============================================================================== # ** Rest of script #=============================================================================== module DataManager class << self alias :th_compressed_saves_load_game_without_rescue :load_game_without_rescue alias :th_compressed_saves_load_header_without_rescue :load_header_without_rescue end def self.make_compressed_filename(index) make_filename(index).split(".")[0] << ".crvdata2" end #----------------------------------------------------------------------------- # New. #----------------------------------------------------------------------------- def self.compress(data) Zlib::Deflate.deflate(data) end #----------------------------------------------------------------------------- # New. #----------------------------------------------------------------------------- def self.decompress(data) Zlib::Inflate.inflate(data) end #----------------------------------------------------------------------------- # Overwrite. All save files should be compressed #----------------------------------------------------------------------------- def self.save_game_without_rescue(index) File.open(make_compressed_filename(index), "wb") do |file| $game_system.on_before_save file.write(compress(Marshal.dump(make_save_header))) file.write(compress(Marshal.dump(make_save_contents))) @last_savefile_index = index end return true end def self.load_game_without_rescue(index) begin return load_compressed_game(index) rescue return th_compressed_saves_load_game_without_rescue(index) end end def self.load_header_without_rescue(index) begin return load_compressed_header(index) rescue return th_compressed_saves_load_header_without_rescue(index) end end #----------------------------------------------------------------------------- # New. #----------------------------------------------------------------------------- def self.load_compressed_game(index) File.open(make_compressed_filename(index), "rb") do |file| Marshal.load(file) extract_save_contents(Marshal.load(decompress(file))) reload_map_if_updated @last_savefile_index = index end return true end #----------------------------------------------------------------------------- # New. #----------------------------------------------------------------------------- def self.load_compressed_header(index) File.open(make_compressed_filename(index), "rb") do |file| return Marshal.load(decompress(file.read)) end end end