Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #===============================================================================
- module MNK::FileHandle
- #===============================================================================
- #-----------------------------------------------------------------------------
- # ☆ Save File Configuration
- #-----------------------------------------------------------------------------
- Folder = "MNK/"
- Name = "sD13x"
- Ext = ".mnk"
- #-----------------------------------------------------------------------------
- #
- #-----------------------------------------------------------------------------
- def self.make_folder(dir_name)
- return if Dir.exist?(dir_name)
- return Dir.mkdir(dir_name)
- end
- #-----------------------------------------------------------------------------
- #
- #-----------------------------------------------------------------------------
- def self.filename
- "#{Folder}#{Name}#{Ext}"
- end
- #-----------------------------------------------------------------------------
- #
- #-----------------------------------------------------------------------------
- def self.save_file_exists?
- !Dir.glob(filename).empty?
- end
- #-----------------------------------------------------------------------------
- #
- #-----------------------------------------------------------------------------
- def self.setup
- make_game_data
- load_game_data if save_file_exists?
- end
- #-----------------------------------------------------------------------------
- #
- #-----------------------------------------------------------------------------
- def self.make_game_data
- $mnk = MNK::DATA.new
- make_folder(Folder)
- end
- #-----------------------------------------------------------------------------
- #
- #-----------------------------------------------------------------------------
- def self.save_game_data
- begin
- save_game_without_rescue
- rescue
- delete_save_file
- false
- end
- end
- #-----------------------------------------------------------------------------
- #
- #-----------------------------------------------------------------------------
- def self.load_game_data
- load_game_without_rescue rescue false
- end
- #-----------------------------------------------------------------------------
- #
- #-----------------------------------------------------------------------------
- def self.delete_save_file
- File.delete(filename) rescue nil
- end
- #-----------------------------------------------------------------------------
- #
- #-----------------------------------------------------------------------------
- def self.save_game_without_rescue
- File.open(filename, "wb") { |file| file << compress($mnk) }
- return true
- end
- #-----------------------------------------------------------------------------
- #
- #-----------------------------------------------------------------------------
- def self.load_game_without_rescue
- File.open(filename, "rb") { |file| $mnk = decompress(file) }
- return true
- end
- #-----------------------------------------------------------------------------
- #
- #-----------------------------------------------------------------------------
- def self.compress(data)
- Zlib::Deflate.deflate(Marshal.dump(data))
- end
- #-----------------------------------------------------------------------------
- #
- #-----------------------------------------------------------------------------
- def self.decompress(data)
- Marshal.load(Zlib::Inflate.inflate(data.read))
- end
- #-----------------------------------------------------------------------------
- #
- #-----------------------------------------------------------------------------
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement