Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # encoding: utf-8
- #===============================================================================
- # ■ メモ欄取得スクリプト for RGSS3
- #-------------------------------------------------------------------------------
- # Ru/むっくRu
- #-------------------------------------------------------------------------------
- # メモ欄の指定フォーマット行を取得します
- #-------------------------------------------------------------------------------
- # 【更新履歴】
- #===============================================================================
- module Torigoya
- module Utils
- module CSVLine
- # CSVっぽい1行の文字列を分解する
- # @param [String] line CSVっぽい文字列
- # @return [Array]
- def self.parse(line)
- columns = []
- line = line.split(//)
- while line.size > 0
- case c = line.shift
- when /\s/
- next
- when '"', "'"
- area = line.slice!(0, search_char(line, c))
- columns << area.join('')
- line.slice!(0, search_char(line, ',') || line.size)
- line.delete_at(0)
- else
- area = line.slice!(0, (search_char(line, ',') || line.size))
- columns << "#{c}#{area.join('')}"
- line.delete_at(0)
- end
- end
- columns.each { |column| column.strip!; column.gsub!(/\\(?=.)/, '') }
- end
- # 文字配列の指定文字のindexを取得する(エスケープ文字を考慮する)
- # @param [String] chars 文字配列
- # @param [String] target_char 検索する文字
- # @return [Integer] インデックス値。指定文字がない場合はnil
- def self.search_char(chars, target_char)
- chars.find_index.with_index do |c, i|
- return i if c == target_char && !(i > 0 && chars[i - 1] == '\\')
- end
- nil
- end
- end
- end
- module Note
- # メモ欄の命令行の先頭に付ける文字列
- # この文字列が先頭にない行は処理対象にしない
- PREFIX = '[t]'
- # メモ欄取得メソッドを実装するmodule
- module Readable
- # メモ欄から必要な情報を抽出したBookインスタンスを取得
- # @return [Book]
- def torigoya_note
- @torigoya_note ||= ::Torigoya::Note::Book.parse(self.note)
- end
- end
- # メモ欄解析結果を内包するクラス
- class Book
- include Enumerable
- # メモ欄の内容を解析しBookインスタンスを返す
- # @param [String] note メモ欄の中身
- # @return [Book] 解析結果
- def self.parse(note)
- tips = note.split(/\r?\n/).select { |n| n.index(PREFIX) == 0 }.map do |line|
- Tip.parse(line.sub(PREFIX, ''))
- end
- self.new(tips)
- end
- # 初期化
- # @param [Hash] collection
- def initialize(tips)
- @tips = tips
- @collection = {}
- tips.each do |tip|
- @collection[tip.name] ||= []
- @collection[tip.name] << tip
- end
- end
- attr_reader :tips
- # 指定名のTip配列の取得
- # @param [String] name 取得するTipの名前
- # @return [Array]
- def [](name)
- @collection[name.to_s] || []
- end
- def each
- @collection.each { |k, v| yield(k, v) }
- end
- end
- # メモ欄の項目名に表記ゆれを許容するための辞書
- class AliasDictionary
- include Enumerable
- def initialize
- @data = {}
- end
- # 指定キーのエイリアス先を取得
- # @return [String] エイリアス先の名前、未定義の場合はnilを返す
- def [](key)
- @data[key.to_s]
- end
- # 指定キーのエイリアスを登録
- # @param [String] key エイリアス元の名前
- # @param [String] alias_name エイリアス先の名前
- # @return [void]
- def []=(key, alias_name)
- @data[key.to_s] = alias_name.to_s
- end
- def each
- @data.each { |k, v| yield(k, v) }
- end
- end
- # メモ欄の各行ごとの解析済みデータ
- class Tip
- # エイリアス辞書の取得
- # @return [AliasDictionary]
- def self.alias_dictionary
- @alias_dictionary ||= AliasDictionary.new
- end
- # note欄の行を元にTipを作成
- # @param [String] str noteの行(PREFIXは含まない)
- # @return [Tip]
- def self.parse(str)
- case str
- when /\A\s*([^:]+):\s*(.+)\s*\z/
- key = $1.to_s
- text = $2.to_s
- values = Torigoya::Utils::CSVLine.parse(text).map do |n|
- case n
- when /\A\d+\.\d+\z/; n.to_f
- when /\A\d+\z/; n.to_i
- else; n
- end
- end
- else
- key = str
- text = ''
- values = []
- end
- self.new(self.alias_dictionary[key] || key, text, values)
- end
- # 初期化
- # @param [String] name 名前
- # @param [String] text データ(文字列)
- # @param [Array] values データ(配列)
- def initialize(name, text, values = [])
- @name = name
- @text = text
- @values = values
- end
- attr_reader :name
- attr_reader :text
- attr_reader :values
- end
- end
- end
- # メモ欄を持つクラスにinclude
- class RPG::BaseItem
- include Torigoya::Note::Readable
- end
- class RPG::Tileset
- include Torigoya::Note::Readable
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement