Advertisement
MakerSystems

RPG Maker VX Ace - Save File Doctor

May 28th, 2015
1,562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.24 KB | None | 0 0
  1. %Q(
  2. ╔════╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═════╗
  3. ║ ╔══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╗ ║
  4. ╠─╣ Save File Doctor. ╠─╣
  5. ╠─╣ by RPG Maker Source. ╠─╣
  6. ╠─╣ www.rpgmakersource.com ╠─╣
  7. ║ ╚══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╝ ║
  8. ╠════╩═╤═╩═╤═╩═╤═╩═╤═╩═╤═╩═╤═╩═╤═╩═╤═╩═╤═╩═╤═╩═╤═╩═╤═╩═╤═╩═╤═╩═╤═╩═╤═╩═╤═╩═════╣
  9. ║ ┌────┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─────┐ ║
  10. ╠─┤ Version 1.1.0 28/05/15 DD/MM/YY ├─╣
  11. ║ └────┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─────┘ ║
  12. ╠══════╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══════╣
  13. ║ ║
  14. ║ This work is protected by the following license: ║
  15. ║ ╔══════════════════════════════════════════════════════════════════╗ ║
  16. ║ │ │ ║
  17. ║ │ Copyright © 2014 Maker Systems. │ ║
  18. ║ │ │ ║
  19. ║ │ This software is provided 'as-is', without any kind of │ ║
  20. ║ │ warranty. Under no circumstances will the author be held │ ║
  21. ║ │ liable for any damages arising from the use of this software. │ ║
  22. ║ │ │ ║
  23. ║ │ Permission is granted to anyone to use this software on their │ ║
  24. ║ │ free or commercial games made with a legal copy of RPG Maker │ ║
  25. ║ │ VX Ace, as long as Maker Systems - RPG Maker Source is │ ║
  26. ║ │ credited within the game. │ ║
  27. ║ │ │ ║
  28. ║ │ Selling this code or any portions of it 'as-is' or as part of │ ║
  29. ║ │ another code, is not allowed. │ ║
  30. ║ │ │ ║
  31. ║ │ The original header, which includes this copyright notice, │ ║
  32. ║ │ must not be edited or removed from any verbatim copy of the │ ║
  33. ║ │ sotware nor from any edited version. │ ║
  34. ║ │ │ ║
  35. ║ ╚══════════════════════════════════════════════════════════════════╝ ║
  36. ║ ║
  37. ║ ║
  38. ╠══════════════════════════════════════════════════════════════════════════════╣
  39. ║ 1. VERSION HISTORY. ▼ ║
  40. ╠══════════════════════════════════════════════════════════════════════════════╣
  41. ║ ║
  42. ║ • Version 1.0.0, 27/12/14 - (DD/MM/YY). ║
  43. ║ ║
  44. ║ • Version 1.1.0, 28/05/15 - (DD/MM/YY). ║
  45. ║ ║
  46. ╠══════════════════════════════════════════════════════════════════════════════╣
  47. ╠══════════════════════════════════════════════════════════════════════════════╣
  48. ║ 2. USER MANUAL. ▼ ║
  49. ╠══════════════════════════════════════════════════════════════════════════════╣
  50. ║ ║
  51. ║ ┌──────────────────────────────────────────────────────────────────────────┐ ║
  52. ║ │ ■ Introduction. │ ║
  53. ║ └┬┬┬┬──────────────────────────────────────────────────────────────────┬┬┬┬┘ ║
  54. ║ ║
  55. ║ Hello there! This script is "plug and play", you can simply insert it ║
  56. ║ into your project and it will perform flawlessly. ║
  57. ║ ║
  58. ║ Save File Doctor aims to fix the most common problem encountered when ║
  59. ║ loading a save file after adding a new script to your project. ║
  60. ║ ║
  61. ║ The problem is usually caused by the lack of initialization for variables ║
  62. ║ used in the recently created script. When loading an old save file, some ║
  63. ║ things were already initialized and the new script will assume that's also ║
  64. ║ the case for its own variables. ║
  65. ║ ║
  66. ║ It is also useful for DLC or patching systems, since it would take care ║
  67. ║ of said problem in case you add scripts that way. ║
  68. ║ ║
  69. ║ We hope you enjoy it. ║
  70. ║ ║
  71. ║ Thanks for choosing our products. ║
  72. ║ ║
  73. ╠══════════════════════════════════════════════════════════════════════════════╣
  74. ╠══════════════════════════════════════════════════════════════════════════════╣
  75. ║ 3. NOTES. ▼ ║
  76. ╠══════════════════════════════════════════════════════════════════════════════╣
  77. ║ ║
  78. ║ Have fun and enjoy! ║
  79. ║ ║
  80. ╠══════════════════════════════════════════════════════════════════════════════╣
  81. ╠══════════════════════════════════════════════════════════════════════════════╣
  82. ║ 4. CONTACT. ▼ ║
  83. ╠══════════════════════════════════════════════════════════════════════════════╣
  84. ║ ║
  85. ║ Need support? Post in our forums! ║
  86. ║ ║
  87. ║ forums.rpgmakersource.com ║
  88. ║ ║
  89. ║ Keep in touch with us and be the first to know about new releases: ║
  90. ║ ║
  91. ║ www.rpgmakersource.com ║
  92. ║ www.facebook.com/RPGMakerSource ║
  93. ║ www.twitter.com/RPGMakerSource ║
  94. ║ www.youtube.com/user/RPGMakerSource ║
  95. ║ ║
  96. ║ Get involved! Have an idea for a system? Let us know. ║
  97. ║ ║
  98. ║ Spread the word and help us reach more people so we can continue creating ║
  99. ║ awesome resources for you! ║
  100. ║ ║
  101. ╚══════════════════════════════════════════════════════════════════════════════╝)
  102.  
  103. #==============================================================================
  104. # ** MakerSystems
  105. #------------------------------------------------------------------------------
  106. # Module for our systems.
  107. #==============================================================================
  108.  
  109. module MakerSystems
  110.  
  111. #============================================================================
  112. # ** SaveFileDoctor
  113. #----------------------------------------------------------------------------
  114. # Ph.D in game development and family care.
  115. #============================================================================
  116.  
  117. module SaveFileDoctor
  118.  
  119. #----------------------------------------------------------------------
  120. # * Custom Case for Game_Actors. [NEW]
  121. #----------------------------------------------------------------------
  122. def self.custom_case_for_game_actors(patient, current)
  123. # Get data array that contains each Actor.
  124. patient_data = patient.instance_variable_get('@data')
  125. # Go through each Actor.
  126. patient_data.each do |actor|
  127. # Next if actor is nil. Needed because how Game_Actors handles [].
  128. next unless actor
  129. # Get current actor from current Game_Actors using patient's id.
  130. current_actor = current[actor.instance_variable_get('@actor_id')]
  131. # Fixes each of patient's Game_Actor objects.
  132. fix_object(actor, current_actor)
  133. # Healing process for each instance variable of this actor.
  134. child_health(object_data(actor), object_data(current_actor))
  135. end
  136. end
  137. #----------------------------------------------------------------------
  138. # * Heal. [NEW]
  139. #----------------------------------------------------------------------
  140. def self.heal(contents)
  141. # Will keep track of repaired objects to avoid a stack overflow.
  142. @ms_save_file_doctor_ids = []
  143. # Goes through each object in contents hash.
  144. contents.each_value do |patient|
  145. # Updated version of this Object.
  146. current = patient.class.new
  147. # Fixes this (loaded) Object with the help of its updated version.
  148. fix_object(patient, current)
  149. # Starts the healing process for each instance variable.
  150. child_health(object_data(patient), object_data(current))
  151. end
  152. # No need to keep the objects ids in memory.
  153. @ms_save_file_doctor_ids = nil
  154. end
  155. #----------------------------------------------------------------------
  156. # * Enumerable Child Processing. [NEW]
  157. #----------------------------------------------------------------------
  158. def self.enum_child(patient, current)
  159. # Iterates loaded version and updated version as a sequence.
  160. patient.zip(current).each do |p_value, c_value|
  161. # If Enumerable, there might be instanceable Objects inside it.
  162. enum_child(p_value.to_a, c_value.to_a) if Enumerable === p_value
  163. # Ignore if it can't be instanced.
  164. next unless p_value.class.respond_to?(:new)
  165. # Stop if this Object was already healed to avoid a stack overflow.
  166. return if @ms_save_file_doctor_ids.include?(p_value.__id__)
  167. # Saves current Object id.
  168. @ms_save_file_doctor_ids << p_value.__id__
  169. # Fixes this Object with the help of its updated version.
  170. fix_object(p_value, c_value)
  171. # Healing process for each instance variable of this Object.
  172. child_health(object_data(p_value), object_data(c_value))
  173. end
  174. end
  175. #----------------------------------------------------------------------
  176. # * Object Data. [NEW]
  177. #----------------------------------------------------------------------
  178. def self.object_data(object)
  179. # Returns a hash with each instance variable name and value.
  180. Hash[
  181. object.instance_variables.map do |name|
  182. [name, object.instance_variable_get(name)]
  183. end
  184. ]
  185. end
  186. #----------------------------------------------------------------------
  187. # * Fix Object. [NEW]
  188. #----------------------------------------------------------------------
  189. def self.fix_object(patient, current)
  190. # Name of the method that should especially handle this object.
  191. special = "custom_case_for_#{patient.class.name.downcase}"
  192. # Attempts special handling.
  193. send(special, patient, current) if respond_to?(special)
  194. # Adds new data to loaded Object's data target.
  195. data = object_data(current).merge!(object_data(patient))
  196. # Uses the correct data target to set each instance variable.
  197. data.each { |name, value| patient.instance_variable_set(name, value) }
  198. end
  199. #----------------------------------------------------------------------
  200. # * Child Health. [NEW]
  201. #----------------------------------------------------------------------
  202. def self.child_health(patient, current)
  203. # Goes through each instance variable.
  204. patient.each do |name, value|
  205. # If Enumerable, there might be instanceable Objects inside it.
  206. enum_child(value.to_a, current[name].to_a) if Enumerable === value
  207. # Ignore if it can't be instanced.
  208. next unless value.class.respond_to?(:new)
  209. # Stop if this Object was already healed to avoid a stack overflow.
  210. return if @ms_save_file_doctor_ids.include?(value.__id__)
  211. # Saves current Object id.
  212. @ms_save_file_doctor_ids << value.__id__
  213. # Fixes this Object with the help of its updated version.
  214. fix_object(value, current[name])
  215. # Healing process for each instance variable of this Object.
  216. child_health(object_data(value), object_data(current[name]))
  217. end
  218. end
  219.  
  220. end
  221.  
  222. end
  223.  
  224. #==============================================================================
  225. # ** DataManager
  226. #------------------------------------------------------------------------------
  227. # This module manages the database and game objects. Almost all of the
  228. # global variables used by the game are initialized by this module.
  229. #==============================================================================
  230.  
  231. module DataManager
  232.  
  233. class << self
  234.  
  235. #------------------------------------------------------------------------
  236. # * Alias Extract Save Contents. [NEW]
  237. #------------------------------------------------------------------------
  238. alias_method(:ms_save_file_doctor_original_extract_save_contents,
  239. :extract_save_contents)
  240. #------------------------------------------------------------------------
  241. # * Extract Save Contents. [MOD]
  242. #------------------------------------------------------------------------
  243. def extract_save_contents(contents)
  244. # Healthcare. Object repairing is handled at a local level.
  245. MakerSystems::SaveFileDoctor.heal(contents)
  246. # Proceeds with the original method, using repaired contents.
  247. ms_save_file_doctor_original_extract_save_contents(contents)
  248. end
  249.  
  250. end
  251.  
  252. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement