Advertisement
Dekita

MNK::FileTest

Sep 2nd, 2014
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. #===============================================================================
  2. module MNK::FileHandle
  3. #===============================================================================
  4. #-----------------------------------------------------------------------------
  5. # ☆ Save File Configuration
  6. #-----------------------------------------------------------------------------
  7. Folder = "MNK/"
  8. Name = "sD13x"
  9. Ext = ".mnk"
  10.  
  11.  
  12. #-----------------------------------------------------------------------------
  13. #
  14. #-----------------------------------------------------------------------------
  15. def self.make_folder(dir_name)
  16. return if Dir.exist?(dir_name)
  17. return Dir.mkdir(dir_name)
  18. end
  19. #-----------------------------------------------------------------------------
  20. #
  21. #-----------------------------------------------------------------------------
  22. def self.filename
  23. "#{Folder}#{Name}#{Ext}"
  24. end
  25. #-----------------------------------------------------------------------------
  26. #
  27. #-----------------------------------------------------------------------------
  28. def self.save_file_exists?
  29. !Dir.glob(filename).empty?
  30. end
  31. #-----------------------------------------------------------------------------
  32. #
  33. #-----------------------------------------------------------------------------
  34. def self.setup
  35. make_game_data
  36. load_game_data if save_file_exists?
  37. end
  38. #-----------------------------------------------------------------------------
  39. #
  40. #-----------------------------------------------------------------------------
  41. def self.make_game_data
  42. $mnk = MNK::DATA.new
  43. make_folder(Folder)
  44. end
  45. #-----------------------------------------------------------------------------
  46. #
  47. #-----------------------------------------------------------------------------
  48. def self.save_game_data
  49. begin
  50. save_game_without_rescue
  51. rescue
  52. delete_save_file
  53. false
  54. end
  55. end
  56. #-----------------------------------------------------------------------------
  57. #
  58. #-----------------------------------------------------------------------------
  59. def self.load_game_data
  60. load_game_without_rescue rescue false
  61. end
  62. #-----------------------------------------------------------------------------
  63. #
  64. #-----------------------------------------------------------------------------
  65. def self.delete_save_file
  66. File.delete(filename) rescue nil
  67. end
  68. #-----------------------------------------------------------------------------
  69. #
  70. #-----------------------------------------------------------------------------
  71. def self.save_game_without_rescue
  72. File.open(filename, "wb") { |file| file << compress($mnk) }
  73. return true
  74. end
  75. #-----------------------------------------------------------------------------
  76. #
  77. #-----------------------------------------------------------------------------
  78. def self.load_game_without_rescue
  79. File.open(filename, "rb") { |file| $mnk = decompress(file) }
  80. return true
  81. end
  82. #-----------------------------------------------------------------------------
  83. #
  84. #-----------------------------------------------------------------------------
  85. def self.compress(data)
  86. Zlib::Deflate.deflate(Marshal.dump(data))
  87. end
  88. #-----------------------------------------------------------------------------
  89. #
  90. #-----------------------------------------------------------------------------
  91. def self.decompress(data)
  92. Marshal.load(Zlib::Inflate.inflate(data.read))
  93. end
  94. #-----------------------------------------------------------------------------
  95. #
  96. #-----------------------------------------------------------------------------
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement