【Ruby脚本】 class Object def getLiteral if (self.is_a?(String)) str = self.clone str.removeBeginning("@") str.addQuotes return str end end end
#============================================================================== # ■ String+ #------------------------------------------------------------------------------ # 处理字符串的附加方法类。 #==============================================================================
class String def addSlashes 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?(Hash) @file.write("#{scrStart} = {};\n") if (head != "this") keys = obj.keys for keyName in keys exportObj(obj[keyName], "#{scrStart}[#{keyName}]") 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 # is an object with attributes to be exported 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. #==============================================================================
exporter = Exporter.new("MapInfos") for keyName in exporter.data.keys # 删除地图名以#开头的地图 exporter.data.delete(keyName) if exporter.data[keyName].name.beginsWith("#") end exporter.export()
|