Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Need help with Trickster's "File_LoadRxdata"

Hello.
I have MACL 1.5 and I'm trying to read some data from a .txt file using Trickster's File_LoadRxdata. I'm a bit confused, I don't clearly understand how to use it. I'd like to make this work the same way as Trickster do.

Code:
id x
name string
variable 12345
ect.


If someone can help me, especially Trickster:p, it would be appreciated. A little tutorial would be nice! ^_^

Thanks in advance!

-Dargor
 
And here is the version I have for MACL 2.0
Code:
module Trickster
  class File_LoadRxdata < File
    #-------------------------------------------------------------------------
    # * Public Instance Variables
    #-------------------------------------------------------------------------
    attr_reader :line
    #-------------------------------------------------------------------------
    # * Name:      Format Readline
    #   Info:      Gets Next Line 
    #   Author:    Trickster
    #   Call Info: No Arguments
    #   Returns:   An Array of information that is on the next line
    #-------------------------------------------------------------------------
    def format_readline
      # Get Line
      data = self.readline
      # Make a Copy
      @line = data.dup
      # Split it
      data = data.split
      # Return Data
      return data
    end
    #-------------------------------------------------------------------------
    # * Name:      Requirements
    #   Info:      Required Data Needed to be loaded
    #   Author:    Trickster
    #   Call Info: Four Arguments
    #              Array Data, Data Loaded
    #              Array Required, Data Required to be loaded
    #              Array Properties, Names to be loaded
    #              Boolean Elements, True if Indexes False for Parameters
    #-------------------------------------------------------------------------
    def requirements(data, required, properties, elements)
      required.each do |i|
        if (i < properties.size and (data[i].nil? and elements) or
           (data.is_a?(Hash) and data[properties[i]].nil? and not elements))
          Kernel.print("Error Loading Data \n#{properties[i]} is not defined")
          exit
        end
      end
    end
    #-------------------------------------------------------------------------
    # * Name:      Incorrect Name Error
    #   Info:      Generates Incorrect Defining Name Message
    #   Author:    Trickster
    #   Call Info: No Arguments
    #-------------------------------------------------------------------------
    def error_incorrect_name
      Kernel.print("Error at Line #{lineno}\nIncorrect parameter\nLine: #{line}")
      exit
    end
    #-------------------------------------------------------------------------
    # * Name:      Array Error
    #   Info:      Generates Missing ] for Array Message
    #   Author:    Trickster
    #   Call Info: One Argument, Integer Extra, Line Number
    #-------------------------------------------------------------------------
    def error_array(extra)
      Kernel.print("Error at Line #{extra}\nMissing ']' for Array\nLine: #{line}")
      exit
    end
    #-------------------------------------------------------------------------
    # * Name:      Hash Error (Syntax Error)
    #   Info:      Generates Missing } for Hash Message
    #   Author:    Trickster
    #   Call Info: One Argument, Integer Extra, Line Number
    #-------------------------------------------------------------------------
    def error_hash(extra)
      Kernel.print("Error at Line #{extra}\nMissing '}' for Hash\nLine: #{line}")
      exit
    end
    #-------------------------------------------------------------------------
    # * Name:      Syntax Error
    #   Info:      Generates Syntax Error Message
    #   Author:    Trickster
    #   Call Info: One Argument Integer Extra, Line number
    #-------------------------------------------------------------------------
    def error_syntax(extra)
      Kernel.print("Error at Line #{extra}\nSyntax Error\nLine: #{line}")
      exit
    end
    #-------------------------------------------------------------------------
    # * Name:      Range Error 
    #   Info:      Generates Error Message
    #   Author:    Trickster
    #   Call Info: One Argument Integer Extra, Line number
    #-------------------------------------------------------------------------
    def error_range(extra)
      Kernel.print("Error at Line #{extra}\nError with Range\nLine: #{line}")
      exit
    end
    #-------------------------------------------------------------------------
    #   Name:      Regexp Fail Error 
    #   Info:      Generates Error Message
    #   Author:    Trickster
    #   Call Info: One Argument Integer Extra, Line number
    #-------------------------------------------------------------------------
    def error_regexp_fail(extra)
      Kernel.print("Error at Line #{extra}\nRegexp Failed\nLine: #{line}")
      exit
    end
    #-------------------------------------------------------------------------
    # * Name:      Load Next Line
    #   Info:      Reads Next Line of Data and returns false if End of File,
    #              true if Empty or Commented else the Data
    #   Author:    Trickster
    #   Call Info: No Arguments
    #-------------------------------------------------------------------------
    def load_next_line
      # Return False if End of File
      return false if self.eof?
      # Get Data
      data = self.format_readline
      # Set Comment Flag if text is =begin
      @comment = true if data[0] == '=begin'
      # If text is =end
      if data[0] == '=end'
        # Unset Comment Flag
        @comment = false 
        # True
        return true
      end
      # Return true if comment or nothing
      return true if data == [] or data[0].include?('#') or @comment
      # Return Data
      return data
    end
    #-------------------------------------------------------------------------
    # * Name:      Test Type
    #   Info:      Tests Data Type and returns sata in the correct format
    #   Author:    Trickster
    #   Call Info: Three to Four Arguments
    #              String data, String to be check
    #              String Line, Line where data was extracted
    #              Integer Lineno, Line number
    #              Integer Multi, Multi Lines for error checking
    #   Comments:  Doesn't check for all data types, it will detect Nil, Array,
    #              Hash, Strings, and Integers. I suggest you use the proper
    #              Conversion method to ensure you get the data type you want
    #-------------------------------------------------------------------------
    def test_type(data, line, lineno, multi = 0)
      # Make a copy of the data
      temp = data.dup
      # Set extra text if an error is found
      extra = (multi > 0) ? "s #{lineno}-#{lineno+multi}" : " #{lineno}"
      # If a nil reference
      if test_nilclass?(temp)
        # Set Data to Nil
        data = nil
      # If True
      elsif test_trueclass?(temp)
        # Set to True
        data = true
      # If False
      elsif test_falseclass?(temp)
        # Set to false
        data = false
      # If an array
      elsif test_array?(temp)
        # Error Array if it doesn't include ]
        self.error_array(extra) unless temp.include?(']')
        # Evaluate Data
        data = eval_data(data, extra)
      # If a hash
      elsif test_hash?(temp)
        # Error Hash if it doesn't include }
        self.error_hash(extra) unless temp.include?('}')
        # Evaluate Data
        data = eval_data(data, extra)
      # If a number (Integer)
      elsif test_integer?(temp)
        # Convert to Integer
        data = data.to_i
      # If a number (Float)
      elsif test_float?(temp)
        # Convert to Float
        data = data.to_f
      # If a Range
      elsif test_range?(temp)
        begin
          # Evaluate Data
          data = eval_data(data, extra)
        rescue
          # Print Range Error
          self.error_range(extra)
        end
      # If a Regexp
      elsif test_regexp?(temp)
        begin
          # Evaluate Data
          data = eval_data(data, extra)
        rescue RegexpError
          # If Regexp Error then print Error
          self.error_regexp_fail
        end
      # If a Symbol
      elsif test_symbol?(temp)
        # Evaluate Data
        data = eval_data(data, extra)
      # If an Instance of a Class
      elsif test_class?(temp)
        # Evaluate Data
        data = eval_data(data, extra)
      # Elsif a string
      elsif test_string?(temp)
        # Just Delete First and Last Index
        data = data[1...(data.size-1)]
      end
      # Return Data
      return data
    end
    #-------------------------------------------------------------------------
    # * Name:      Test NilClass
    #   Info:      Tests if Data is nil
    #   Author:    Trickster
    #   Call Info: String data - data to check
    #-------------------------------------------------------------------------
    def test_nilclass?(data)
      return data == 'nil'
    end
    #-------------------------------------------------------------------------
    # * Name:      Test TrueClass
    #   Info:      Tests if Data is true
    #   Author:    Trickster 
    #   Call Info: String data - data to Check
    #-------------------------------------------------------------------------
    def test_trueclass?(data)
      return data == 'true'
    end
    #-------------------------------------------------------------------------
    # * Name:      Test FalseClass
    #   Info:      Tests if Data is false
    #   Author:    Trickster 
    #   Call Info: String data - data to Check
    #-------------------------------------------------------------------------
    def test_falseclass?(data)
      return data == 'false'
    end
    #-------------------------------------------------------------------------
    # * Name:      Test Array
    #   Info:      Tests if Data is an array (starts with [)
    #   Author:    Trickster 
    #   Call Info: String data - data to Check
    #-------------------------------------------------------------------------
    def test_array?(data)
      return data[0, 1] == '['
    end
    #-------------------------------------------------------------------------
    # * Name:      Test Hash
    #   Info:      Tests if Data is a hash (starts with { and has =>)
    #   Author:    Trickster 
    #   Call Info: String data - data to Check
    #-------------------------------------------------------------------------
    def test_hash?(data)
      return data[0, 1] == '{' && data.include?('=>') && data
    end
    #-------------------------------------------------------------------------
    # * Name:      Test Integer
    #   Info:      Tests if Data is an integer (if to_i.to_s == data)
    #   Author:    Trickster 
    #   Call Info: String data - data to Check
    #-------------------------------------------------------------------------
    def test_integer?(data)
      return data.to_i.to_s == data
    end
    #-------------------------------------------------------------------------
    # * Name:      Test Float
    #   Info:      Tests if Data is a float (if to_f.to_s == data)
    #   Author:    Trickster 
    #   Call Info: String data - data to Check
    #-------------------------------------------------------------------------
    def test_float?(data)
      return data.to_f.to_s == data
    end
    #-------------------------------------------------------------------------
    # * Name:      Test Range
    #   Info:      Tests if Data is a range (includes .. or ... 
    #              and first and last are integers and when .. or ... is removed
    #              is an integer)
    #   Author:    Trickster 
    #   Call Info: String data - data to Check
    #-------------------------------------------------------------------------
    def test_range?(data)
      # Create Copy
      copy = data.dup
      # Return false if no .. or ...
      return false unless copy.include?('..') or copy.include?('...')
      # Substitute .. or ... from data
      copy.sub!(/\.{2,3}/, '')
      # Return false if leftovers is not a int
      return false if not test_integer?(copy)
      # Return first and last characters are integers
      return test_integer?(data[0,1]) && test_integer?(data[-1,1])
    end
    #-------------------------------------------------------------------------
    # * Name:      Test Regexp
    #   Info:      Tests if Data is a regexp (first and last are /)
    #   Author:    Trickster 
    #   Call Info: String data - data to Check
    #-------------------------------------------------------------------------
    def test_regexp?(data)
      return data[0, 1] == '/' && data[-1, 1] == '/'
    end
    #-------------------------------------------------------------------------
    # * Name:      Test Symbol
    #   Info:      Tests if Data is a symbol (first is :)
    #   Author:    Trickster 
    #   Call Info: String data - data to Check
    #-------------------------------------------------------------------------
    def test_symbol?(data)
      return data[0, 1] == ':'
    end
    #-------------------------------------------------------------------------
    # * Name:      Test Class
    #   Info:      Tests if Data is a class (has .new)
    #   Author:    Trickster 
    #   Call Info: String data - data to Check
    #-------------------------------------------------------------------------
    def test_class?(data)
      return data.include?('.new')
    end
    #-------------------------------------------------------------------------
    # * Name:      Test String
    #   Info:      Tests if Data is a string (first and last are ")
    #   Author:    Trickster 
    #   Call Info: String data - data to Check
    #-------------------------------------------------------------------------
    def test_string?(data)
      return data[0] == 34 && data[-1] == 34
    end
    #-------------------------------------------------------------------------
    # * Name:      Eval Data
    #   Info:      calls eval on data and catches Syntax Errors
    #   Author:    Trickster 
    #   Call Info: String data, extra - data to Check, error info
    #-------------------------------------------------------------------------
    def eval_data(data, extra = '')
      begin
        # Evaluate Data
        data = eval(data)
      rescue SyntaxError
        # If Syntax Error then print Error
        self.error_syntax(extra)
      end
    end
  end
  #-------------------------------------------------------------------------
  # * Name:      Load Data From Text file
  #   Info:      Loads data from a text file
  #   Author:    Trickster
  #   Call Info: Two to Five Arguments
  #              String File_name, file name to load from
  #              Array Properties, Defining Values
  #              Array Required, An Array of Indexes that must be loaded
  #                defaults to index 0
  #              Array Multilined, An Array of Indexes that read from multilines
  #                defaults to nothing, note: for strings only
  #              Integer Elements, If 0 Elements are arrays (Default)
  #                                If 1 Elements are hashes Key = id
  #                                Else Elements are hashes Key = parameter
  #   Returns:   an array with the data from text file
  #-------------------------------------------------------------------------
  def self.load_data_from_txt(file_name, properties, required = [0],
                              multi_lined = [], elements = 0)
    # Initialize local variables
    final_data = [] 
    data_txt = elements == 0 ? [] : {}
    index = 1
    # Load File
    file = Trickster::File_LoadRxdata.open(file_name)
    # Loop until End of File (EOF)
    until file.eof?
      # Get line data
      data = file.format_readline
      # Get Line
      line = file.line
      # Set Comment Flag if text is =begin
      @comment = true if data[0] == '=begin'
      # If text is =end
      if data[0] == '=end'
        # Unset Comment Flag
        @comment = false 
        # Next
        next
      end
      # Next if no data or commented line
      next if data == [] or data[0].include?('#') or @comment
      # Get id from the properties
      id = properties.index(data[0])
      # If a new id
      if id == 0 and not data_txt.empty?
        # Check requirements and return error if not fulfilled
        file.requirements(data_txt, required, properties, [0,1].include?(elements))
        # Finished reading a piece of data
        final_data[index] = data_txt.dup
        # Increase index and reset data
        index += 1
        data_txt.clear
      elsif id == nil
        # Incorrent Defining name error message
        file.error_incorrect_name
      end
      # Remove defining name and join together
      data.delete_at(0)
      data = data.join(' ')
      # Get line number
      lineno = file.lineno
      # Start multi line information checking
      multi = 1
      if multi_lined.include?(id)
        # Load next line
        next_line = file.load_next_line
        # Get first data
        first = next_line.is_a?(Array) ? next_line[0] : ""
        # Reset flag
        flag = false
        # While an invalid property and the file is not eof? or data loaded
        while (properties.index(first) == nil and (next_line != false or next_line.is_a?(Array)))
          # While was excuted once
          flag = true
          position = file.pos
          # add to data if an array
          if next_line.is_a?(Array)
            # Get property data
            first = next_line[0]
            if properties.index(first) == nil
              data += ' ' + next_line.join(' ')
            end
          end
          # Load next line and reset first
          next_line = file.load_next_line
          first = ""
          # increase multi line count
          multi += 1
          # break if file.eof? continue if line was a comment
          break if next_line == false
          next if next_line == true
          first = next_line[0]
        end
        if flag
          file.pos = position
        end
        if next_line.is_a?(Array)
          if properties.index(first) == nil
            data += next_line.join(' ')
          end
        end
      end      
      data = file.test_type(data, line, lineno, multi)
      data_txt[id] = data if [0,1].include?(elements)
      data_txt[properties[id]] = data if not [0,1].include?(elements)
    end
    # Reached End of File Get last data if any
    if not data_txt.empty?
      # Check requirements and return error if not fulfilled
      file.requirements(data_txt, required, properties, [0,1].include?(elements))
      # Finished reading a piece of data
      final_data[index] = data_txt.dup
      # Increase index and reset data
      index += 1
      data_txt.clear
    end
    # return all data compacted
    return final_data.compact
  end
end

Compared to the one found in MACL 1.5 this version checks for all literals and converts them to that type (like if you type 1.5 it will be loaded as 1.5, or if you typed Color.new(255, 255, 255) it will be loaded as a color object) it also can load negative values (something the older version could not do), but enough on that.

The Actual call to this is
actor_data = Trickster.load_data_from_txt(filename, properties, required, multilined, return_type)

filename is the filename to load from

properties is an array of keys that you type in from the text file, for example, in your code above to get that to be loaded you will have to send in an array with this ['id', 'name', 'variable'] (personally it is easier to use %w( ) in this case since it is an array of just one word strings so %w( id name variable ) but whatever is easiest for you)

required is an array of indexes (from the properties sent) which are required to be loaded the default for this is the first property that is sent so the default is [0]

multilined is an array of indexes (from the properties sent) which can be allow to span multiple lines (line for long lines of text to be loaded) If I remember correctly this should only work for strings, but I may have changed that forgot

return_type is the type of object to be returned use 0 for an array (the data loaded will be an array of arrays, use 1 for a hash but the inner values of the hash are arrays (ex {1 => ['x', 'string', 12345']}), and use 2 (or any value other than 0 or 1) and you will get a hash with the inner values being hashes (ex {1 => {'id' => 'x', 'name' => 'string', 'variable' => 12345'}}) the last is the most efficient imo

Now, if you are loading something from the Data Folder, when the project is encrypted this method will fail, I'll tell you more about this if you ask (or you can just look in my scripts to see what I do to prevent the File Not Found error when encrypting)
 
And here is another update to that

Code:
=begin
============================================================================== 
 ** Loading Data From Text Files V4.0
------------------------------------------------------------------------------
Description:
------------
  This Utility module loads data from text files, the text file loaded must
  be setup like so.
  
something     some_value
parameter     value
parameter     value
parameter     value

  Also has support for comments and block comments within the text file, 
  commented data will be ignored within the file. This also parses the values
  loaded into the data type it is supposed to be, for example if a value was
  50 then it will be loaded as 50 and not "50". This effect works on Strings,
  Integers, Floats, Ranges, Arrays, Hashes, Regexp, Symbols, TrueClass, 
  FalseClass, NilClass, and Classes (by calling the new operator example
  Color.new(0, 0, 0) will be loaded as a color object). Also includes error
  checking and will catch Syntax Errors, missing ] for Array, missing } for
  Hash, Range errors, and Regexp Errors and will also point to the line and
  the data that is on the line if an unexcepted error is found within the file.
  
Method List:
------------
  Trickster.load_data_from_txt
  
Classes:
--------
  Class File_LoadRxdata
==============================================================================
=end
module Trickster
  class File_LoadRxdata < File
    #-------------------------------------------------------------------------
    # * Public Instance Variables
    #-------------------------------------------------------------------------
    attr_reader :line
    #-------------------------------------------------------------------------
    # * Name      : Format Readline
    #   Info      : Gets Next Line returns An Array of information on next line
    #   Author    : Trickster
    #   Call Info : No Arguments
    #-------------------------------------------------------------------------
    def format_readline
      # Get Line
      data = self.readline
      # Make a Copy
      @line = data.dup
      # Split it
      data = data.split
      # Return Data
      return data
    end
    #-------------------------------------------------------------------------
    # * Name      : Requirements
    #   Info      : Required Data Needed to be loaded
    #   Author    : Trickster
    #   Call Info : Four Arguments
    #               Array Data, Data Loaded
    #               Array Required, Data Required to be loaded
    #               Array Properties, Names to be loaded
    #               Boolean Elements, True if Indexes False for Parameters
    #-------------------------------------------------------------------------
    def requirements(data, required, properties, elements)
      required.each do |i|
        if (i < properties.size and (data[i].nil? and elements) or
           (data.is_a?(Hash) and data[properties[i]].nil? and not elements))
          Kernel.print("Error Loading Data \n#{properties[i]} is not defined")
          exit
        end
      end
    end
    #-------------------------------------------------------------------------
    # * Name      : Incorrect Name Error
    #   Info      : Generates Incorrect Defining Name Message
    #   Author    : Trickster
    #   Call Info : No Arguments
    #-------------------------------------------------------------------------
    def error_incorrect_name
      Kernel.print("Error at Line #{lineno}\nIncorrect parameter\nLine: #{line}")
      exit
    end
    #-------------------------------------------------------------------------
    # * Name      : Array Error
    #   Info      : Generates Missing ] for Array Message
    #   Author    : Trickster
    #   Call Info : One Argument, Integer Extra, Line Number
    #-------------------------------------------------------------------------
    def error_array(extra)
      Kernel.print("Error at Line #{extra}\nMissing ']' for Array\nLine: #{line}")
      exit
    end
    #-------------------------------------------------------------------------
    # * Name      : Hash Error (Syntax Error)
    #   Info      : Generates Missing } for Hash Message
    #   Author    : Trickster
    #   Call Info : One Argument, Integer Extra, Line Number
    #-------------------------------------------------------------------------
    def error_hash(extra)
      Kernel.print("Error at Line #{extra}\nMissing '}' for Hash\nLine: #{line}")
      exit
    end
    #-------------------------------------------------------------------------
    # * Name      : Syntax Error
    #   Info      : Generates Syntax Error Message
    #   Author    : Trickster
    #   Call Info : One Argument Integer Extra, Line number
    #-------------------------------------------------------------------------
    def error_syntax(extra)
      Kernel.print("Error at Line #{extra}\nSyntax Error\nLine: #{line}")
      exit
    end
    #-------------------------------------------------------------------------
    # * Name      : Range Error 
    #   Info      : Generates Error Message
    #   Author    : Trickster
    #   Call Info : One Argument Integer Extra, Line number
    #-------------------------------------------------------------------------
    def error_range(extra)
      Kernel.print("Error at Line #{extra}\nError with Range\nLine: #{line}")
      exit
    end
    #-------------------------------------------------------------------------
    #   Name      : Regexp Fail Error 
    #   Info      : Generates Error Message
    #   Author    : Trickster
    #   Call Info : One Argument Integer Extra, Line number
    #-------------------------------------------------------------------------
    def error_regexp_fail(extra)
      Kernel.print("Error at Line #{extra}\nRegexp Failed\nLine: #{line}")
      exit
    end
    #-------------------------------------------------------------------------
    # * Name      : Load Next Line
    #   Info      : Reads Next Line of Data and returns false if End of File,
    #               true if Empty or Commented else the Data
    #   Author    : Trickster
    #   Call Info : No Arguments
    #-------------------------------------------------------------------------
    def load_next_line
      # Return False if End of File
      return false if self.eof?
      # Get Data
      data = self.format_readline
      # Set Comment Flag if text is =begin
      @comment = true if data[0] == '=begin'
      # If text is =end
      if data[0] == '=end'
        # Unset Comment Flag
        @comment = false 
        # True
        return true
      end
      # Return true if comment or nothing
      return true if data == [] or data[0].include?('#') or @comment
      # Return Data
      return data
    end
    #-------------------------------------------------------------------------
    # * Name      : Test Type
    #   Info      : Tests Data Type and returns sata in the correct format
    #   Author    : Trickster
    #   Call Info : Three to Four Arguments
    #               String data, String to be check
    #               String Line, Line where data was extracted
    #               Integer Lineno, Line number
    #               Integer Multi, Multi Lines for error checking
    #   Comment   : Checks for almost all data types
    #-------------------------------------------------------------------------
    def test_type(data, line, lineno, multi = 0)
      # Make a copy of the data
      temp = data.dup
      # Set extra text if an error is found
      extra = (multi > 0) ? "s #{lineno}-#{lineno+multi}" : " #{lineno}"
      # If a nil reference
      if test_nilclass?(temp)
        # Set Data to Nil
        data = nil
      # If True
      elsif test_trueclass?(temp)
        # Set to True
        data = true
      # If False
      elsif test_falseclass?(temp)
        # Set to false
        data = false
      # If an array
      elsif test_array?(temp)
        # Error Array if it doesn't include ]
        self.error_array(extra) unless temp.include?(']')
        # Evaluate Data
        data = eval_data(data, extra)
      # If a hash
      elsif test_hash?(temp)
        # Error Hash if it doesn't include }
        self.error_hash(extra) unless temp.include?('}')
        # Evaluate Data
        data = eval_data(data, extra)
      # If a number (Integer)
      elsif test_integer?(temp)
        # Convert to Integer
        data = data.to_i
      # If a number (Float)
      elsif test_float?(temp)
        # Convert to Float
        data = data.to_f
      # If a Range
      elsif test_range?(temp)
        begin
          # Evaluate Data
          data = eval_data(data, extra)
        rescue
          # Print Range Error
          self.error_range(extra)
        end
      # If a Regexp
      elsif test_regexp?(temp)
        begin
          # Evaluate Data
          data = eval_data(data, extra)
        rescue RegexpError
          # If Regexp Error then print Error
          self.error_regexp_fail
        end
      # If a Symbol
      elsif test_symbol?(temp)
        # Evaluate Data
        data = eval_data(data, extra)
      # If an Instance of a Class
      elsif test_class?(temp)
        # Evaluate Data
        data = eval_data(data, extra)
      # Elsif a string
      elsif test_string?(temp)
        # Just Delete First and Last Index
        data = data[1...(data.size-1)]
      end
      # Return Data
      return data
    end
    #-------------------------------------------------------------------------
    # * Name      : Test NilClass
    #   Info      : Tests if Data is nil
    #   Author    : Trickster
    #   Call Info : String data - data to check
    #-------------------------------------------------------------------------
    def test_nilclass?(data)
      return data == 'nil'
    end
    #-------------------------------------------------------------------------
    # * Name      : Test TrueClass
    #   Info      : Tests if Data is true
    #   Author    : Trickster 
    #   Call Info : String data - data to Check
    #-------------------------------------------------------------------------
    def test_trueclass?(data)
      return data == 'true'
    end
    #-------------------------------------------------------------------------
    # * Name      : Test FalseClass
    #   Info      : Tests if Data is false
    #   Author    : Trickster 
    #   Call Info : String data - data to Check
    #-------------------------------------------------------------------------
    def test_falseclass?(data)
      return data == 'false'
    end
    #-------------------------------------------------------------------------
    # * Name      : Test Array
    #   Info      : Tests if Data is an array (starts with [)
    #   Author    : Trickster 
    #   Call Info : String data - data to Check
    #-------------------------------------------------------------------------
    def test_array?(data)
      return data[0, 1] == '['
    end
    #-------------------------------------------------------------------------
    # * Name      : Test Hash
    #   Info      : Tests if Data is a hash (starts with { and has =>)
    #   Author    : Trickster 
    #   Call Info : String data - data to Check
    #-------------------------------------------------------------------------
    def test_hash?(data)
      return data[0, 1] == '{' && data.include?('=>') && data
    end
    #-------------------------------------------------------------------------
    # * Name      : Test Integer
    #   Info      : Tests if Data is an integer (if to_i.to_s == data)
    #   Author    : Trickster 
    #   Call Info : String data - data to Check
    #-------------------------------------------------------------------------
    def test_integer?(data)
      return data.to_i.to_s == data
    end
    #-------------------------------------------------------------------------
    # * Name      : Test Float
    #   Info      : Tests if Data is a float (if to_f.to_s == data)
    #   Author    : Trickster 
    #   Call Info : String data - data to Check
    #-------------------------------------------------------------------------
    def test_float?(data)
      return data.to_f.to_s == data
    end
    #-------------------------------------------------------------------------
    # * Name      : Test Range
    #   Info      : Tests if Data is a range (includes .. or ... 
    #               and first and last are integers and when .. or ... is removed
    #               is an integer)
    #   Author    : Trickster 
    #   Call Info : String data - data to Check
    #-------------------------------------------------------------------------
    def test_range?(data)
      # Create Copy
      copy = data.dup
      # Return false if no .. or ...
      return false unless copy.include?('..') or copy.include?('...')
      # Substitute .. or ... from data
      copy.sub!(/\.{2,3}/, '')
      # Return false if leftovers is not a int
      return false if not test_integer?(copy)
      # Return first and last characters are integers
      return test_integer?(data[0,1]) && test_integer?(data[-1,1])
    end
    #-------------------------------------------------------------------------
    # * Name      : Test Regexp
    #   Info      : Tests if Data is a regexp (first and last are /)
    #   Author    : Trickster 
    #   Call Info : String data - data to Check
    #-------------------------------------------------------------------------
    def test_regexp?(data)
      return data[0, 1] == '/' && data[-1, 1] == '/'
    end
    #-------------------------------------------------------------------------
    # * Name      : Test Symbol
    #   Info      : Tests if Data is a symbol (first is :)
    #   Author    : Trickster 
    #   Call Info : String data - data to Check
    #-------------------------------------------------------------------------
    def test_symbol?(data)
      return data[0, 1] == ':'
    end
    #-------------------------------------------------------------------------
    # * Name      : Test Class
    #   Info      : Tests if Data is a class (has .new)
    #   Author    : Trickster 
    #   Call Info : String data - data to Check
    #-------------------------------------------------------------------------
    def test_class?(data)
      return data.include?('.new')
    end
    #-------------------------------------------------------------------------
    # * Name      : Test String
    #   Info      : Tests if Data is a string (first and last are ")
    #   Author    : Trickster 
    #   Call Info : String data - data to Check
    #-------------------------------------------------------------------------
    def test_string?(data)
      return data[0] == 34 && data[-1] == 34
    end
    #-------------------------------------------------------------------------
    # * Name      : Eval Data
    #   Info      : calls eval on data and catches Syntax Errors
    #   Author    : Trickster 
    #   Call Info : String data, extra - data to Check, error info
    #-------------------------------------------------------------------------
    def eval_data(data, extra = '')
      begin
        # Evaluate Data
        data = eval(data)
      rescue SyntaxError
        # If Syntax Error then print Error
        self.error_syntax(extra)
      end
    end
  end
  #-------------------------------------------------------------------------
  # * Name      : Load Data From Text file
  #   Info      : Loads data from a text file
  #               returns an array/hash with the data from text file
  #   Author    : Trickster
  #   Call Info : Two to Five Arguments
  #               String File_name, file name to load from
  #               Array Properties, Defining Values
  #               Array Required, An Array of Indexes that must be loaded
  #                 defaults to index 0
  #               Array Multilined, An Array of Indexes that read from multilines
  #                 defaults to nothing, note: for strings only
  #               Integer Elements, If 0 Elements are arrays (Default)
  #                                 If 1 Elements are hashes Key = id
  #                                 Else Elements are hashes Key = parameter
  #-------------------------------------------------------------------------
  def self.load_data_from_txt(file_name, properties, required = [0],
                              multi_lined = [], elements = 0)
    # Initialize local variables
    final_data = [] 
    data_txt = elements == 0 ? [] : {}
    index = 1
    # Load File
    file = Trickster::File_LoadRxdata.open(file_name)
    # Loop until End of File (EOF)
    until file.eof?
      # Get line data
      data = file.format_readline
      # Get Line
      line = file.line
      # Set Comment Flag if text is =begin
      @comment = true if data[0] == '=begin'
      # If text is =end
      if data[0] == '=end'
        # Unset Comment Flag
        @comment = false 
        # Next
        next
      end
      # Next if no data or commented line
      next if data == [] or data[0].include?('#') or @comment
      # Get id from the properties
      id = properties.index(data[0])
      # If a new id
      if id == 0 and not data_txt.empty?
        # Check requirements and return error if not fulfilled
        file.requirements(data_txt, required, properties, [0,1].include?(elements))
        # Finished reading a piece of data
        final_data[index] = data_txt.dup
        # Increase index and reset data
        index += 1
        data_txt.clear
      elsif id == nil
        # Incorrent Defining name error message
        file.error_incorrect_name
      end
      # Remove defining name and join together
      data.delete_at(0)
      data = data.join(' ')
      # Get line number
      lineno = file.lineno
      # Start multi line information checking
      multi = 1
      if multi_lined.include?(id)
        # Load next line
        next_line = file.load_next_line
        # Get first data
        first = next_line.is_a?(Array) ? next_line[0] : ""
        # Reset flag
        flag = false
        # While an invalid property and the file is not eof? or data loaded
        while (properties.index(first) == nil and (next_line != false or next_line.is_a?(Array)))
          # While was excuted once
          flag = true
          position = file.pos
          # add to data if an array
          if next_line.is_a?(Array)
            # Get property data
            first = next_line[0]
            if properties.index(first) == nil
              data += ' ' + next_line.join(' ')
            end
          end
          # Load next line and reset first
          next_line = file.load_next_line
          first = ""
          # increase multi line count
          multi += 1
          # break if file.eof? continue if line was a comment
          break if next_line == false
          next if next_line == true
          first = next_line[0]
        end
        if flag
          file.pos = position
        end
        if next_line.is_a?(Array)
          if properties.index(first) == nil
            data += next_line.join(' ')
          end
        end
      end      
      data = file.test_type(data, line, lineno, multi)
      data_txt[id] = data if [0,1].include?(elements)
      data_txt[properties[id]] = data if not [0,1].include?(elements)
    end
    # Reached End of File Get last data if any
    if not data_txt.empty?
      # Check requirements and return error if not fulfilled
      file.requirements(data_txt, required, properties, [0,1].include?(elements))
      # Finished reading a piece of data
      final_data[index] = data_txt.dup
      # Increase index and reset data
      index += 1
      data_txt.clear
    end
    # Close File
    file.close
    # return all data compacted
    return final_data.compact
  end
end
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top