class Object def getLiteral if (self.is_a?(String)) str = self.clone str.removeBeginning("@") str.addQuotes return str end end end class String def addSlashes self.gsub!(/(["'\\])/) {"\\" + $&} end def addQuotes self.addSlashes self.insert(0, "\"") self.concat("\"") end def beginsWith(str) strlen = str.length if self.length < strlen return false end if self[0, strlen] == str return true else return false end end def removeBeginning(str) self[0, str.length] = '' if self.beginsWith(str) end def trim return if (self.empty?) while self[0, 1] == " " or self[0, 1] == "\t" self[0, 1] = "" end while self[-1, 1] == " " self[-1, 1] = "" end end end
#============================================================================== # ■ Exporter #------------------------------------------------------------------------------ # 处理RMXP数据导出的类。 #==============================================================================
class Exporter #-------------------------------------------------------------------------- # ● 常量 #-------------------------------------------------------------------------- AS_FOLDER = "FlashProject/RXData/" #-------------------------------------------------------------------------- # ● 定义实例变量 #-------------------------------------------------------------------------- attr_accessor :data attr_reader :rxdataFile attr_reader :as3File attr_reader :clsName attr_reader :file #-------------------------------------------------------------------------- # ● 初始化对像 # rxdataFile : rxdata文件名 #-------------------------------------------------------------------------- def initialize(rxdataFileName) @clsName = rxdataFileName @clsName = "MapInfo" if (rxdataFileName == "MapInfos") @rxdataFile = "data/" + rxdataFileName + ".rxdata" @as3File = AS_FOLDER + @clsName + ".as" @data = load_data(@rxdataFile) @variables = [] end #-------------------------------------------------------------------------- # ● 导出到AS文件 #-------------------------------------------------------------------------- def export @file = File.open(@as3File, "w") @file.write("//这些代码是系统自动生成的,请不要修改这些代码\n") @file.write("package RXData {\n") @file.write("\tpublic dynamic class #{@clsName} {\n") @file.write("\t\tpublic function #{@clsName}() {\n") exportObj(@data, "\t\t\tthis") @file.write("\t\t}\n\t}\n}") @file.close() end #-------------------------------------------------------------------------- # ● 导出数组或对象 #-------------------------------------------------------------------------- def exportObj(obj, scrStart) head = scrStart.clone head.trim if obj.is_a?(NilClass) return elsif obj.is_a?(Hash) @file.write("#{scrStart} = {};\n") if (head != "this") for keyName in obj.keys exportObj(obj[keyName], "#{scrStart}[#{keyName}]") end elsif obj.is_a?(Array) @file.write("#{scrStart} = [];\n") if (head != "this") i = 0 for value in obj exportObj(value, "#{scrStart}[#{i}]") i += 1 end elsif obj.is_a?(Numeric) value = obj.to_s @file.write("#{scrStart} = #{value};\n") elsif obj.is_a?(String) obj.addQuotes @file.write("#{scrStart} = #{obj};\n") elsif obj.is_a?(TrueClass) or obj.is_a?(FalseClass) value = obj.to_s @file.write("#{scrStart} = #{value};\n") else # 导出对象的全部属性 type = obj.type @file.write("#{scrStart} = {}; // #{type}\n") if (head != "this") vars = obj.instance_variables for varName in vars propName = varName.clone propName.removeBeginning("@") exportObj(obj.instance_variable_get(varName), "#{scrStart}.#{propName}") end end end end #============================================================================== # ** Main #------------------------------------------------------------------------------ # This script is written by Octopus. #==============================================================================
#-------------------------------------------------------------------------- # ● 打开所有rxdata文件 #-------------------------------------------------------------------------- files = Dir.glob("Data/*.rxdata") exporters = {} for file in files file[/^Data\//] = ''; file[/\.rxdata$/] = ''; exporters[file] = Exporter.new(file) end
#-------------------------------------------------------------------------- # ● 生成 ActionScript3 代码文件 #-------------------------------------------------------------------------- for exporter in exporters exporter[1].export() end
exit
|