Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #===============================================================================
- # * [ACE] File Checker / Changer
- #===============================================================================
- # * Made by: Sixth (www.rpgmakervxace.net, www.forums.rpgmakerweb.com)
- # * Version: 1.0
- # * Updated: 26/08/2016
- # * Requires: ------
- #-------------------------------------------------------------------------------
- # * < Change Log >
- #-------------------------------------------------------------------------------
- # * Version 1.0 (26/08/2015)
- # - Initial release.
- #-------------------------------------------------------------------------------
- # * < Description >
- #-------------------------------------------------------------------------------
- # * This script will let you make files that you can use to store, check and
- # change your in-game data.
- # * The files can be outside or inside your game's folder. If it is inside the
- # game's folder, make sure that it is NOT in an encrypted folder!
- # * Add new data or remove existing ones from your files during gameplay too!
- # * Currently limited to one-liner data, so don't use line breaks!
- # This will change soon!
- #-------------------------------------------------------------------------------
- # * < Script Calls >
- #-------------------------------------------------------------------------------
- # * To check a data from a file, you can use this script call:
- #
- # FileOpener.check_it("data_key","filename")
- #
- # The "data_key" must be a valid key from the 'Contents' settings found in
- # this script.
- #
- # The "filename" must be the name od the file WITH the extension of the file!
- # So, it can be "Settings.ini" or "Secrets.txt", and so on.
- # The system won't complain that you haven't used an extension, but if you
- # did that, your users might not be able to edit these files (although, I
- # never tested this, so I can't know for sure).
- #
- # This would return the value of the data checked IF the file got the data!
- # The returned data is always a string in that case!
- # But if the data is NOT in the file, it returns nil!
- #
- # So, you can use this in your condition checks using the "Script" option.
- #
- # Examples:
- #
- # FileOpener.check_it("TheData2","GameSecrets.ini")
- # This returns whatever value the "TheData2" line in your file has IF it got
- # a line with that key! If the file got no such lines, it returns nil!
- #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # * To set a new value for a data in your file, use this script call:
- #
- # FileOpener.change_it("data_key","value","filename")
- #
- # All of the arguments are the same as before, with the addition of the
- # "value" argument, but it should be obvious what it does.
- # That will be the new value of the data you changed.
- # Again, it must be a string!
- #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # * To add a new value for a data in your file, use this script call:
- #
- # FileOpener.add_data("data_key","value","filename")
- #
- # Identical to the previous script call, but this one won't change anything,
- # instead, it will add completely new data (new line) in your file.
- #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # * To completely remove a data from your file, use this script call:
- #
- # FileOpener.remove_data("data_key","filename")
- #
- # This completely removes an existing data IF it is found in the file.
- #-------------------------------------------------------------------------------
- # * < Installation >
- #-------------------------------------------------------------------------------
- # * In the script editor, place this script below Materials but above Main!
- #-------------------------------------------------------------------------------
- # * < Compatibility Info >
- #-------------------------------------------------------------------------------
- # * No known incompatibilities.
- #-------------------------------------------------------------------------------
- # * < Known Issues >
- #-------------------------------------------------------------------------------
- # * No known issues.
- #-------------------------------------------------------------------------------
- # * < Terms of Use >
- #-------------------------------------------------------------------------------
- # * Free to use for whatever purposes you want.
- # * Credit me (Sixth) in your game, pretty please! :P
- # * Posting modified versions of this script is allowed as long as you notice me
- # about it with a link to it!
- #===============================================================================
- $imported = {} if $imported.nil?
- $imported["SixthFileChecker"] = true
- #===============================================================================
- # Settings:
- #===============================================================================
- module FileOpener
- #--------------------------------------------------------------------------
- # Debug Setting:
- #--------------------------------------------------------------------------
- # Just a debug setting.
- # If this is true, every time you request a file made with this script, it
- # will be re-initialized (default settings loaded).
- # Note that this will break the script calls for changing the data in your
- # files!
- #--------------------------------------------------------------------------
- Reload = false
- #--------------------------------------------------------------------------
- # Environmental Path Settings:
- #--------------------------------------------------------------------------
- # The environmental path of the folder for your file.
- # This path can differ from PC to PC, and that is why it should use the
- # 'ENV' module to get the correct path to the desired folder(s).
- # In any case, you can edit the text inside the [brackets] to get different
- # folder paths. You can use these values:
- #
- # "AppData" = Will get: "C\\Users\\*Username*\\AppData\\Roaming"
- # "LOCALAPPDATA" = Will get: "C\\Users\\*Username*\\AppData\\Local"
- # "ProgramFiles" = Will get: "C\\ProgramFiles (x86)"
- # "CommonProgramFiles" = Will get: "C\\ProgramFiles (x86)\\Common Files"
- #
- # Again, these folder paths might differ, especially on different OS.
- # The examples I gave are for a Windows 7 PC with the windows installed on
- # the C partition.
- #
- # Also, some of these might require your game to be "Run as Administrator"!
- #
- # If you don't want to use these environmental folder paths, you can use a
- # simple string to set a static path to your folder.
- # In this case, the root of the path will be your project's folder, so if you
- # just enter a simple empty string, it will be in your project's folder.
- #--------------------------------------------------------------------------
- DPath = ENV["LOCALAPPDATA"]
- #--------------------------------------------------------------------------
- # Static Path Settings:
- #--------------------------------------------------------------------------
- # This is the static sub-folder path to the actual file. These are the
- # non-environmental parts of your folder path to your file.
- # Each string you add into the array will be a sub-folder created, nested in
- # each other.
- # The sample settings will place the used files to the following folder:
- # "C\\Users\\*Username*\\AppData\\Local\\SubFolder1\\SubFolder2\\"
- #--------------------------------------------------------------------------
- SPath = ["SubFolder1","SubFolder2"]
- #--------------------------------------------------------------------------
- # Default Content Settings:
- #--------------------------------------------------------------------------
- # The starting content of your file.
- # This is how your file will appear the first time it's accessed.
- # Unless you set the 'Reload' setting to true, this will only be loaded once
- # in your game and never again!
- # Of course, different windows users might get different folder paths, which
- # means they will get a different file in return, so for each user, this will
- # be initialized but only once!
- #
- # Format:
- #
- # "filename" => {
- # "key1" => "value1",
- # "key2" => "value2",
- # ...
- # },
- #
- # The "filename" must be the file's name which will get the assigned content
- # loaded. It must contain the file's extension!
- # This will be used in the script calls to fetch the data from the correct
- # files!
- #
- # The "key" and "value" pair are the actual data which will be stored in
- # your file. Both of them must be strings!
- # You will use the keys set up here to get a data from a file!
- #--------------------------------------------------------------------------
- Contents = {
- "GameSecrets1.ini" => {
- "Event1" => "This will be the value of Event1!",
- "Event2" => "This will be the value of Event2!",
- "Thing5" => "Just something else for Thing5...",
- },
- "GameSecrets2.rvdata2" => {
- "Event1" => "This will be the value of Event1!",
- "Event2" => "This will be the value of Event2!",
- "Thing5" => "Just something else for Thing5...",
- },
- # Add more settings here!
- }
- #===============================================================================
- # End of settings! O.o
- #===============================================================================
- def self.init(file,name)
- File.open(file,"w+") do |f|
- Contents[name].each {|key,value| f.write(key + " => " + value + "\n") }
- end
- end
- def self.check_it(data,name)
- file = DPath + "\\"
- SPath.each do |path|
- file += path + "\\"
- Dir.mkdir(file) if !FileTest.exist?(file)
- end
- file += name
- self.init(file,name) if !File.exist?(file) || Reload
- File.open(file,"r+") do |f|
- f.each_line {|line| return $1 if line =~ /#{data} => (.*)/i }
- end
- end
- def self.change_it(data,value,name)
- file = DPath + "\\"
- SPath.each do |path|
- file += path + "\\"
- Dir.mkdir(file) if !FileTest.exist?(file)
- end
- file += name
- self.init(file,name) if !File.exist?(file) || Reload
- temp_file = ""
- File.open(file,"r+") do |f|
- f.each_line do |line|
- if line =~ /#{data} => (.*)/i
- txt = line.clone.sub($1,value)
- temp_file += txt
- else
- temp_file += line
- end
- end
- end
- File.open(file,"w+") do |f|
- f.write(temp_file)
- end
- end
- def self.add_data(data,value,name)
- file = DPath + "\\"
- SPath.each do |path|
- file += path + "\\"
- Dir.mkdir(file) if !FileTest.exist?(file)
- end
- file += name
- self.init(file,name) if !File.exist?(file) || Reload
- File.open(file,"a+") do |f|
- f.write(data + " => " + value + "\n")
- end
- end
- def self.remove_data(data,name)
- file = DPath + "\\"
- SPath.each do |path|
- file += path + "\\"
- Dir.mkdir(file) if !FileTest.exist?(file)
- end
- file += name
- self.init(file,name) if !File.exist?(file) || Reload
- temp_file = ""
- File.open(file,"r+") do |f|
- f.each_line do |line|
- next if line =~ /#{data} => (.*)/i
- temp_file += line
- end
- end
- File.open(file,"w+") do |f|
- f.write(temp_file)
- end
- end
- end
- #==============================================================================
- # !!END OF SCRIPT - OHH, NOES!!
- #==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement