Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. # encoding: utf-8
  2. #===============================================================================
  3. # ■ メモ欄取得スクリプト for RGSS3
  4. #-------------------------------------------------------------------------------
  5. # Ru/むっくRu
  6. #-------------------------------------------------------------------------------
  7. # メモ欄の指定フォーマット行を取得します
  8. #-------------------------------------------------------------------------------
  9. # 【更新履歴】
  10. #===============================================================================
  11.  
  12. module Torigoya
  13. module Utils
  14. module CSVLine
  15. # CSVっぽい1行の文字列を分解する
  16. # @param [String] line CSVっぽい文字列
  17. # @return [Array]
  18. def self.parse(line)
  19. columns = []
  20. line = line.split(//)
  21. while line.size > 0
  22. case c = line.shift
  23. when /\s/
  24. next
  25. when '"', "'"
  26. area = line.slice!(0, search_char(line, c))
  27. columns << area.join('')
  28. line.slice!(0, search_char(line, ',') || line.size)
  29. line.delete_at(0)
  30. else
  31. area = line.slice!(0, (search_char(line, ',') || line.size))
  32. columns << "#{c}#{area.join('')}"
  33. line.delete_at(0)
  34. end
  35. end
  36. columns.each { |column| column.strip!; column.gsub!(/\\(?=.)/, '') }
  37. end
  38.  
  39. # 文字配列の指定文字のindexを取得する(エスケープ文字を考慮する)
  40. # @param [String] chars 文字配列
  41. # @param [String] target_char 検索する文字
  42. # @return [Integer] インデックス値。指定文字がない場合はnil
  43. def self.search_char(chars, target_char)
  44. chars.find_index.with_index do |c, i|
  45. return i if c == target_char && !(i > 0 && chars[i - 1] == '\\')
  46. end
  47. nil
  48. end
  49. end
  50. end
  51.  
  52. module Note
  53. # メモ欄の命令行の先頭に付ける文字列
  54. # この文字列が先頭にない行は処理対象にしない
  55. PREFIX = '[t]'
  56.  
  57. # メモ欄取得メソッドを実装するmodule
  58. module Readable
  59. # メモ欄から必要な情報を抽出したBookインスタンスを取得
  60. # @return [Book]
  61. def torigoya_note
  62. @torigoya_note ||= ::Torigoya::Note::Book.parse(self.note)
  63. end
  64. end
  65.  
  66. # メモ欄解析結果を内包するクラス
  67. class Book
  68. include Enumerable
  69.  
  70. # メモ欄の内容を解析しBookインスタンスを返す
  71. # @param [String] note メモ欄の中身
  72. # @return [Book] 解析結果
  73. def self.parse(note)
  74. tips = note.split(/\r?\n/).select { |n| n.index(PREFIX) == 0 }.map do |line|
  75. Tip.parse(line.sub(PREFIX, ''))
  76. end
  77. self.new(tips)
  78. end
  79.  
  80. # 初期化
  81. # @param [Hash] collection
  82. def initialize(tips)
  83. @tips = tips
  84. @collection = {}
  85. tips.each do |tip|
  86. @collection[tip.name] ||= []
  87. @collection[tip.name] << tip
  88. end
  89. end
  90. attr_reader :tips
  91.  
  92. # 指定名のTip配列の取得
  93. # @param [String] name 取得するTipの名前
  94. # @return [Array]
  95. def [](name)
  96. @collection[name.to_s] || []
  97. end
  98.  
  99. def each
  100. @collection.each { |k, v| yield(k, v) }
  101. end
  102. end
  103.  
  104. # メモ欄の項目名に表記ゆれを許容するための辞書
  105. class AliasDictionary
  106. include Enumerable
  107.  
  108. def initialize
  109. @data = {}
  110. end
  111.  
  112. # 指定キーのエイリアス先を取得
  113. # @return [String] エイリアス先の名前、未定義の場合はnilを返す
  114. def [](key)
  115. @data[key.to_s]
  116. end
  117.  
  118. # 指定キーのエイリアスを登録
  119. # @param [String] key エイリアス元の名前
  120. # @param [String] alias_name エイリアス先の名前
  121. # @return [void]
  122. def []=(key, alias_name)
  123. @data[key.to_s] = alias_name.to_s
  124. end
  125.  
  126. def each
  127. @data.each { |k, v| yield(k, v) }
  128. end
  129. end
  130.  
  131. # メモ欄の各行ごとの解析済みデータ
  132. class Tip
  133. # エイリアス辞書の取得
  134. # @return [AliasDictionary]
  135. def self.alias_dictionary
  136. @alias_dictionary ||= AliasDictionary.new
  137. end
  138.  
  139. # note欄の行を元にTipを作成
  140. # @param [String] str noteの行(PREFIXは含まない)
  141. # @return [Tip]
  142. def self.parse(str)
  143. case str
  144. when /\A\s*([^:]+):\s*(.+)\s*\z/
  145. key = $1.to_s
  146. text = $2.to_s
  147. values = Torigoya::Utils::CSVLine.parse(text).map do |n|
  148. case n
  149. when /\A\d+\.\d+\z/; n.to_f
  150. when /\A\d+\z/; n.to_i
  151. else; n
  152. end
  153. end
  154. else
  155. key = str
  156. text = ''
  157. values = []
  158. end
  159.  
  160. self.new(self.alias_dictionary[key] || key, text, values)
  161. end
  162.  
  163. # 初期化
  164. # @param [String] name 名前
  165. # @param [String] text データ(文字列)
  166. # @param [Array] values データ(配列)
  167. def initialize(name, text, values = [])
  168. @name = name
  169. @text = text
  170. @values = values
  171. end
  172. attr_reader :name
  173. attr_reader :text
  174. attr_reader :values
  175. end
  176. end
  177. end
  178.  
  179. # メモ欄を持つクラスにinclude
  180. class RPG::BaseItem
  181. include Torigoya::Note::Readable
  182. end
  183. class RPG::Tileset
  184. include Torigoya::Note::Readable
  185. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement