Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- # Iavra Event Tag Reader 1.06
- # -----------------------------------------------------------------------------
- # Description:
- # Offers a generic hook for other scripts to read notetags from the current
- # page of event.
- # -----------------------------------------------------------------------------
- # Prerequisites:
- # None
- # -----------------------------------------------------------------------------
- # How to Use:
- #
- # Register a regex in your script like this:
- #
- # IAVRA::EVENT_TAGS.register(:key, /regex/, :int, :int)
- #
- # This will attempt to capture the first 2 groups from your regex and convert
- # them to integers. Everything less than 2 will be set to nil in the result,
- # while everything more than 2 will be discarded.
- #
- # Get the matches for that regex during runtime like this:
- #
- # IAVRA::EVENT_TAGS.get_all(event_id, :key) # returns all matches.
- # IAVRA::EVENT_TAGS.get(event_id, :key) # returns the first match.
- # IAVRA::EVENT_TAGS.get(event_id, :key, index) # returns the (index)th match.
- #
- # Each match is an array containing your captured groups.
- # -----------------------------------------------------------------------------
- # Terms of Use:
- # Free to use for both commercial and non-commercial games. Please give credit.
- # -----------------------------------------------------------------------------
- # Credits:
- # Iavra
- # -----------------------------------------------------------------------------
- # Changelog:
- # - 1.01: Added support for named capture groups.
- # - 1.02: Renamed get to get_all and added a new get method for direct access.
- # - 1.03: Added an $imported entry for the script.
- # - 1.04: Shortened the hash building.
- # - 1.05: Rebuilt the script. Support for named groups was discarded, since i
- # figured those don't get used much, anyway.
- # - 1.06: Reduced the number of iterations to improve performance.
- #==============================================================================
- ($imported ||= {})[:iavra_event_tags] = true
- #==============================================================================
- # ▼ IAVRA::EVENT_TAGS
- #==============================================================================
- module IAVRA
- module EVENT_TAGS
- #==========================================================================
- # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
- #==========================================================================
- #==========================================================================
- # Procs responsible for converting the captured groups. Nil values will be
- # checked beforehand, so the functions don't need to care about them.
- #==========================================================================
- CAPTURES = {
- :string => lambda {|value| value},
- :symbol => lambda {|value| value.to_sym},
- :int => lambda {|value| value.to_i},
- :float => lambda {|value| value.to_f}
- }
- #==========================================================================
- # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
- #==========================================================================
- #==========================================================================
- # String used to join multi-line comments together.
- #==========================================================================
- LINEBREAK = "\r\n"
- #==========================================================================
- # Instance variable used to store registered regexes.
- #==========================================================================
- class << self
- attr_reader :registered
- end
- @registered = {}
- #==========================================================================
- # Call this in your script to register your regex and its capture groups.
- # Make sure to pick an unused symbol as key.
- #==========================================================================
- def self.register(key, regex, *captures)
- @registered[key] = [regex, captures]
- end
- #==========================================================================
- # Gets all matches for a registered regex on an event.
- #==========================================================================
- def self.get_all(event_id, key)
- $game_map.events[event_id].iavra_event_tags[key] || []
- end
- #==========================================================================
- # Get one match for a registered regex on an event.
- #==========================================================================
- def self.get(event_id, key, index = 0)
- get_all(event_id, key)[index]
- end
- end
- end
- #==============================================================================
- # ▼ Game_Event
- #==============================================================================
- class Game_Event < Game_Character
- attr_reader :iavra_event_tags
- alias :iavra_event_tags_setup_page :setup_page
- #============================================================================
- # Parses the event's notetags, whenever a page becomes active.
- #============================================================================
- def setup_page(*args)
- iavra_event_tags_setup_page(*args)
- iavra_event_tags_parse_notetags
- end
- #============================================================================
- # First collects all comments on the current page and afterwards iterates
- # over them, matching each comment against the registered regexes and storing
- # the captured groups.
- #============================================================================
- def iavra_event_tags_parse_notetags
- comments = (@page ? @page.list : []).reduce([]){|a, c|
- a << c.parameters[0] if [108, 408].include?(c.code); a
- }.join(IAVRA::EVENT_TAGS::LINEBREAK)
- @iavra_event_tags = Hash[IAVRA::EVENT_TAGS.registered.map{|k, d|
- [k, comments.scan(d[0]).map{d[1].map.with_index(1){|k, i|
- v = $~[i]; v ? IAVRA::EVENT_TAGS::CAPTURES[k].call(v) : nil
- }}]
- }]
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement