Advertisement
TheSixth

File Checker / Changer

Aug 26th, 2016
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 11.54 KB | None | 0 0
  1. #===============================================================================
  2. # * [ACE] File Checker / Changer
  3. #===============================================================================
  4. # * Made by: Sixth (www.rpgmakervxace.net, www.forums.rpgmakerweb.com)
  5. # * Version: 1.0
  6. # * Updated: 26/08/2016
  7. # * Requires: ------
  8. #-------------------------------------------------------------------------------
  9. # * < Change Log >
  10. #-------------------------------------------------------------------------------
  11. # * Version 1.0 (26/08/2015)
  12. #   - Initial release.
  13. #-------------------------------------------------------------------------------
  14. # * < Description >
  15. #-------------------------------------------------------------------------------
  16. # * This script will let you make files that you can use to store, check and
  17. #   change your in-game data.
  18. # * The files can be outside or inside your game's folder. If it is inside the
  19. #   game's folder, make sure that it is NOT in an encrypted folder!
  20. # * Add new data or remove existing ones from your files during gameplay too!
  21. # * Currently limited to one-liner data, so don't use line breaks!
  22. #   This will change soon!
  23. #-------------------------------------------------------------------------------
  24. # * < Script Calls >
  25. #-------------------------------------------------------------------------------
  26. # * To check a data from a file, you can use this script call:
  27. #
  28. #     FileOpener.check_it("data_key","filename")
  29. #
  30. #   The "data_key" must be a valid key from the 'Contents' settings found in
  31. #   this script.
  32. #
  33. #   The "filename" must be the name od the file WITH the extension of the file!
  34. #   So, it can be "Settings.ini" or "Secrets.txt", and so on.
  35. #   The system won't complain that you haven't used an extension, but if you
  36. #   did that, your users might not be able to edit these files (although, I
  37. #   never tested this, so I can't know for sure).
  38. #
  39. #   This would return the value of the data checked IF the file got the data!
  40. #   The returned data is always a string in that case!
  41. #   But if the data is NOT in the file, it returns nil!
  42. #  
  43. #   So, you can use this in your condition checks using the "Script" option.
  44. #
  45. #   Examples:
  46. #
  47. #     FileOpener.check_it("TheData2","GameSecrets.ini")
  48. #   This returns whatever value the "TheData2" line in your file has IF it got
  49. #   a line with that key! If the file got no such lines, it returns nil!
  50. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  51. # * To set a new value for a data in your file, use this script call:
  52. #
  53. #     FileOpener.change_it("data_key","value","filename")
  54. #
  55. #   All of the arguments are the same as before, with the addition of the
  56. #   "value" argument, but it should be obvious what it does.
  57. #   That will be the new value of the data you changed.
  58. #   Again, it must be a string!
  59. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  60. # * To add a new value for a data in your file, use this script call:
  61. #
  62. #     FileOpener.add_data("data_key","value","filename")
  63. #
  64. #   Identical to the previous script call, but this one won't change anything,
  65. #   instead, it will add completely new data (new line) in your file.
  66. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  67. # * To completely remove a data from your file, use this script call:
  68. #
  69. #     FileOpener.remove_data("data_key","filename")
  70. #
  71. #   This completely removes an existing data IF it is found in the file.
  72. #-------------------------------------------------------------------------------
  73. # * < Installation >
  74. #-------------------------------------------------------------------------------
  75. # * In the script editor, place this script below Materials but above Main!
  76. #-------------------------------------------------------------------------------
  77. # * < Compatibility Info >
  78. #-------------------------------------------------------------------------------
  79. # * No known incompatibilities.
  80. #-------------------------------------------------------------------------------
  81. # * < Known Issues >
  82. #-------------------------------------------------------------------------------
  83. # * No known issues.
  84. #-------------------------------------------------------------------------------
  85. # * < Terms of Use >
  86. #-------------------------------------------------------------------------------
  87. # * Free to use for whatever purposes you want.
  88. # * Credit me (Sixth) in your game, pretty please! :P
  89. # * Posting modified versions of this script is allowed as long as you notice me
  90. #   about it with a link to it!
  91. #===============================================================================
  92. $imported = {} if $imported.nil?
  93. $imported["SixthFileChecker"] = true
  94. #===============================================================================
  95. # Settings:
  96. #===============================================================================
  97. module FileOpener
  98.   #--------------------------------------------------------------------------  
  99.   # Debug Setting:
  100.   #--------------------------------------------------------------------------    
  101.   # Just a debug setting.
  102.   # If this is true, every time you request a file made with this script, it
  103.   # will be re-initialized (default settings loaded).
  104.   # Note that this will break the script calls for changing the data in your
  105.   # files!
  106.   #--------------------------------------------------------------------------    
  107.   Reload = false
  108.  
  109.   #--------------------------------------------------------------------------  
  110.   # Environmental Path Settings:
  111.   #--------------------------------------------------------------------------    
  112.   # The environmental path of the folder for your file.
  113.   # This path can differ from PC to PC, and that is why it should use the
  114.   # 'ENV' module to get the correct path to the desired folder(s).
  115.   # In any case, you can edit the text inside the [brackets] to get different
  116.   # folder paths. You can use these values:
  117.   #
  118.   # "AppData" = Will get: "C\\Users\\*Username*\\AppData\\Roaming"
  119.   # "LOCALAPPDATA" = Will get: "C\\Users\\*Username*\\AppData\\Local"
  120.   # "ProgramFiles" = Will get: "C\\ProgramFiles (x86)"
  121.   # "CommonProgramFiles" = Will get: "C\\ProgramFiles (x86)\\Common Files"
  122.   #
  123.   # Again, these folder paths might differ, especially on different OS.
  124.   # The examples I gave are for a Windows 7 PC with the windows installed on
  125.   # the C partition.
  126.   #
  127.   # Also, some of these might require your game to be "Run as Administrator"!
  128.   #
  129.   # If you don't want to use these environmental folder paths, you can use a
  130.   # simple string to set a static path to your folder.
  131.   # In this case, the root of the path will be your project's folder, so if you
  132.   # just enter a simple empty string, it will be in your project's folder.
  133.   #--------------------------------------------------------------------------    
  134.   DPath = ENV["LOCALAPPDATA"]
  135.  
  136.   #--------------------------------------------------------------------------  
  137.   # Static Path Settings:
  138.   #--------------------------------------------------------------------------    
  139.   # This is the static sub-folder path to the actual file. These are the
  140.   # non-environmental parts of your folder path to your file.
  141.   # Each string you add into the array will be a sub-folder created, nested in
  142.   # each other.
  143.   # The sample settings will place the used files to the following folder:
  144.   # "C\\Users\\*Username*\\AppData\\Local\\SubFolder1\\SubFolder2\\"
  145.   #--------------------------------------------------------------------------    
  146.   SPath = ["SubFolder1","SubFolder2"]
  147.    
  148.   #--------------------------------------------------------------------------  
  149.   # Default Content Settings:
  150.   #--------------------------------------------------------------------------    
  151.   # The starting content of your file.
  152.   # This is how your file will appear the first time it's accessed.
  153.   # Unless you set the 'Reload' setting to true, this will only be loaded once
  154.   # in your game and never again!
  155.   # Of course, different windows users might get different folder paths, which
  156.   # means they will get a different file in return, so for each user, this will
  157.   # be initialized but only once!
  158.   #
  159.   # Format:
  160.   #
  161.   #   "filename" => {
  162.   #     "key1" => "value1",
  163.   #     "key2" => "value2",
  164.   #     ...
  165.   #   },
  166.   #
  167.   # The "filename" must be the file's name which will get the assigned content
  168.   # loaded. It must contain the file's extension!
  169.   # This will be used in the script calls to fetch the data from the correct
  170.   # files!
  171.   #
  172.   # The "key" and "value" pair are the actual data which will be stored in
  173.   # your file. Both of them must be strings!
  174.   # You will use the keys set up here to get a data from a file!
  175.   #--------------------------------------------------------------------------    
  176.   Contents = {
  177.     "GameSecrets1.ini" => {
  178.       "Event1" => "This will be the value of Event1!",
  179.       "Event2" => "This will be the value of Event2!",
  180.       "Thing5" => "Just something else for Thing5...",
  181.     },
  182.     "GameSecrets2.rvdata2" => {
  183.       "Event1" => "This will be the value of Event1!",
  184.       "Event2" => "This will be the value of Event2!",
  185.       "Thing5" => "Just something else for Thing5...",
  186.     },
  187.     # Add more settings here!
  188.   }
  189.  
  190. #===============================================================================
  191. # End of settings! O.o
  192. #===============================================================================
  193.  
  194.   def self.init(file,name)
  195.     File.open(file,"w+") do |f|
  196.       Contents[name].each {|key,value| f.write(key + " => " + value + "\n") }
  197.     end
  198.   end
  199.      
  200.   def self.check_it(data,name)
  201.     file = DPath + "\\"
  202.     SPath.each do |path|
  203.       file += path + "\\"
  204.       Dir.mkdir(file) if !FileTest.exist?(file)
  205.     end
  206.     file += name
  207.     self.init(file,name) if !File.exist?(file) || Reload
  208.     File.open(file,"r+") do |f|
  209.       f.each_line {|line| return $1 if line =~ /#{data} => (.*)/i }
  210.     end
  211.   end
  212.  
  213.   def self.change_it(data,value,name)
  214.     file = DPath + "\\"
  215.     SPath.each do |path|
  216.       file += path + "\\"
  217.       Dir.mkdir(file) if !FileTest.exist?(file)
  218.     end
  219.     file += name
  220.     self.init(file,name) if !File.exist?(file) || Reload
  221.     temp_file = ""
  222.     File.open(file,"r+") do |f|
  223.       f.each_line do |line|
  224.         if line =~ /#{data} => (.*)/i
  225.           txt = line.clone.sub($1,value)
  226.           temp_file += txt
  227.         else
  228.           temp_file += line
  229.         end
  230.       end
  231.     end
  232.     File.open(file,"w+") do |f|
  233.       f.write(temp_file)
  234.     end
  235.   end
  236.    
  237.   def self.add_data(data,value,name)
  238.     file = DPath + "\\"
  239.     SPath.each do |path|
  240.       file += path + "\\"
  241.       Dir.mkdir(file) if !FileTest.exist?(file)
  242.     end
  243.     file += name
  244.     self.init(file,name) if !File.exist?(file) || Reload
  245.     File.open(file,"a+") do |f|
  246.       f.write(data + " => " + value + "\n")
  247.     end
  248.   end
  249.  
  250.   def self.remove_data(data,name)
  251.     file = DPath + "\\"
  252.     SPath.each do |path|
  253.       file += path + "\\"
  254.       Dir.mkdir(file) if !FileTest.exist?(file)
  255.     end
  256.     file += name
  257.     self.init(file,name) if !File.exist?(file) || Reload
  258.     temp_file = ""
  259.     File.open(file,"r+") do |f|
  260.       f.each_line do |line|
  261.         next if line =~ /#{data} => (.*)/i
  262.         temp_file += line
  263.       end
  264.     end
  265.     File.open(file,"w+") do |f|
  266.       f.write(temp_file)
  267.     end
  268.   end
  269.  
  270. end
  271. #==============================================================================
  272. # !!END OF SCRIPT - OHH, NOES!!
  273. #==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement