Check if file exists in encrypted game?

My minimap seems to bug on this line of code:

@has_image = File.exist?("Graphics/#{FOLDER}/#{filename}.#{FILE_EXT}")It always returns false when the game data is compressed and encrypted. Obviously there is no Graphics/Minimap/whatever.png file anymore.. how do I solve that? I tried moving the code to the Cache but that doesn't work either.

http://forums.rpgmakerweb.com/index.php?/topic/13801-napoleons-minimap/?p=155370

This still seems to work (as it should according to the documentation):

#===============================================================================# Cache#===============================================================================module Cache #-------------------------------------------------------------------------- # * Get Minimap Graphic #-------------------------------------------------------------------------- def self.minimap(filename) load_bitmap("Graphics/#{Nap::Minimap::FOLDER}/", filename) endendThe official documentation:

Encrypted ArchivesEncrypted archives make it difficult for others to analyze and/or modify the game contents. Normally, all data and graphic files (but not audio and font files) are stored in Game.rgss3a. You can create an encrypted archive by checking the [Create encrypted archive] box when compressing the game data.The files within the encrypted archive can be accessed as though they were in the game folder by using the following methods from the built-in game library:load_data, Bitmap.new, Graphics.transitionWhen there is an encrypted archive in the game folder, the script data (normally Data\Scripts.rvdata) defined in the Scripts line of Game.ini will always be read from the archive. This is a limitation that prevents files within the archive from being read by an external script.Due to its nature, the encrypted archive's internal format has not, and will not, be released to the public. Please refrain from analyzing it.Uh... so there is no File.exists support here? I simply have to use Bitmap.new(filename) and then catch it if it crashes? I hope there is a better solution.
load_data works because they read from their archive, which still preserves the directory structure.

Ruby's File module doesn't know anything about the archive unless they modified them, but I don't think they did.

Also there is no binary nor text reading support from custom config files and such when the game is encrypted? Lot's of RGSS3 scripts will probably bug when the game data is encrypted then.
The easiest way is to make sure you serialize those files before putting them in your data folder. That way, load_data will Marshal.load them properly.

You can write your scripts so that they will automatically serialize any of these custom files during playtest or something.

There are alternative (hacks) to load them such as

Code:
# Load files from non-RM filesclass << Marshal  alias_method(:___load, :load)  def load(port, proc = nil)    ___load(port, proc)  rescue TypeError    if port.kind_of?(File)      port.rewind       port.read    else      port    end  endend unless Marshal.respond_to?(:___load)
Which gets around the requirement that they be stored in Marshal serialized format.
I do not want to load them into memory, I just want to check if they exist (which hopefully costs less performance than partially/fully loading a file from disk).

But I may have other scripts that do require regular text to be loaded from an encrypted game. Ah nevermind I just noticed that those script sare created dynamically on the run and thus those files are not encrypted (created when user runs the game) and therefor by pure luck my other scripts seem to work haha.

This hack, does allow me to read non-serialized files like xml files or plain text files? That would be awesome and really handy to know for scripters!

Code:
# Load files from non-RM filesclass << Marshal  alias_method(:___load, :load)  def load(port, proc = nil)    ___load(port, proc)  rescue TypeError    if port.kind_of?(File)      port.rewind       port.read    else      port    end  endend unless Marshal.respond_to?(:___load)
I do not want to load them into memory, I just want to check if they exist (which hopefully costs less performance than partially/fully loading a file from disk).
I am not aware of any solutions for checking whether a file exists in the archive. Maybe someone else might know.

This hack, does allow me to read non-serialized files like xml files or plain text files? That would be awesome and really handy to know for scripters!
Yes, though it returns the data as a String (raw byte string, no encoding or anything) so you can't really treat it as a File object. Which shouldn't be an issue though if you are working with very simple input (like a plain text file or just binary data). However if you're dealing with XML or any other serialization that might be an issue for your libraries...


I tried to StringIO the output (http://stackoverflow.com/questions/2094291/ruby-convert-string-to-file), but StringIO does not exist in RGSS so that isn't an option.
Thanks for these nice answers. I will just proceed using a begin-rescue-end for now, as I take it that there is no RM File.exists?() alternative for encrypted archives. But someday I might need that hack as well.

Yap they stripped out many useful libraries in 'RM Ruby'...
You can alternatively build a map during test play which records which files exists in.

This file you can then load from the encrypted archive and check for file existance.

In terms of treating a string as an IO made this small script a while ago.

Spoiler
class StringBuffer < IO   MODE_STRING = 1   MODE_FILE_MARSHAL = 2   MODE_FILE_BINMODE = 3   ##   # Initialization of the String Buffer   # The mode can be MODE_STRING, MODE_FILE_MARSHAL or MODE_FILE_BINMODE   #   def initialize(data = nil, mode = MODE_FILE_MARSHAL, compressed = true)     if data.nil?       @data = ''     else       # Check which type the data is       case mode       when MODE_STRING         @data = data       when MODE_FILE_MARSHAL         begin           @data = load_data(data)         rescue           @data = ''         end       when MODE_FILE_BINMODE         if FileTest.exists?(data)           f = File.open(data, 'rb')           f.binmode           @data = f.read           f.close         end       end       # Decompress the data if it is compressed       if compressed         @data = Zlib::Inflate.inflate(@data)       end     end   end    ##   # Save to file   # The mode can be MODE_FILE_MARSHAL or MODE_FILE_BINMODE   #   def save(filename, mode = MODE_FILE_MARSHAL, compress = true)     # Check whether the data should be compressed     if compress       data = Zlib:😀eflate.deflate(@data)     else       data = @data     end     # Check whether to save in binary or marshal mode     case mode     when MODE_FILE_MARSHAL       save_data(data, filename)     when MODE_FILE_BINMODE       f = File.open(filename, 'w+')       f.binmode       f.print data       f.close     end   end   ##   # Get data   #   def data     return @data   end    ##   # Binary mode   #   def binmode     # Do nothing   end    ##   # Write data   #   def write(data)     @data += data   end    ##   # Get char   #   def getc     return @data.slice!(0)   end    ##   # Read an amount of bytes   #   def read(amount)     return @data.slice!(0...amount)   endend

*hugs*

Original thread: https://forums.rpgmakerweb.com/threads/check-if-file-exists-in-encrypted-game.15756/