在AS3里用Lua的性能太低了, 即使每条配置数据只要1毫秒, 但随便一套任务或状态就有几千条数据, 加载的进度条就要跑开好几秒。 实在不能耐,只好把配置数据改用XML写,再转到AS3保存,忍痛牺牲Lua的脚本功能(自动生成配置表和相关小功能用不了了)。
另外补充一句, Altova XMLSpy这个XML编辑工具相当好用。 特别是其网格模式下的自动合并相关参成列表的功能, 做得相当漂亮。
public function XML2OBJ( _xml:XML ):Object { var ret = new Object(); //设置属性 var pars:XMLList = _xml.attributes(); for each( var par:XML in pars ) { ret[par.name().toString()]= par.toString(); } //设置集合 var table:XMLList = _xml.children(); if( table.length() > 0 ) { for each( var item:XML in table ) { var sub_ret:Object = XML2OBJ(item); var tmp:Object = ret[ item.name() ]; if( null == tmp ) { ret[ item.name() ] = sub_ret; } else if(tmp is Array) { (tmp as Array).push( sub_ret ); } else { ret[ item.name() ] = new Array(tmp,sub_ret); } } } return ret; }
/** * xml转obj */ public function XML2OBJ( _xml:XML ):Object { var ret:Object = new Object(); //设置属性 var pars:XMLList = _xml.attributes(); for each( var par:XML in pars ) { par.prettyPrinting=false; var val:String = par.toString(); val = val.split("\r\n").join("\r"); //flash会因为换行符的定义不同导致出现两次换行 if( "\"" == val.charAt() ) { ret[par.name().toString()]= val.substr(1,val.length-2); } else if( "" != val && val is String) { ret[par.name().toString()]= Number( val ); } else { ret[par.name().toString()]= val; } } //设置集合 var table:XMLList = _xml.children(); if( table.length() > 0 ) { for each( var item:XML in table ) { var name:String = item.name(); var item_type:String = name.substr(0,4); var sub_ret:Object = XML2OBJ(item); var tmp:Object = ret[ name ]; if( null == tmp && null == sub_ret.TAB_ID && "ARY_" != item_type ) { ret[ name ] = sub_ret; } else { //调整原结构 if( null == tmp ) { ret[ name ] = new Array(); } else if( !(tmp is Array) ) { ret[ name ] = new Array(tmp); } //添加新数 if( null == sub_ret.TAB_ID ) { ret[ name ].push( sub_ret ); } else { ret[ name ][sub_ret.TAB_ID] = sub_ret; } } } } return ret; }
/** * * 替换字符串中特殊字符 * */ public function txt2xml( _txt:String ):String { if (_txt == null) { return ""; } var txt:String = _txt; var reg:RegExp = null; reg = /&/g; txt = txt.replace(reg,"&"); reg = /</g; txt = txt.replace(reg,"<"); reg = />/g; txt = txt.replace(reg,">"); reg = /\"/g; txt = txt.replace(reg,"""); return txt; } /** * * 还原字符串中特殊字符 * */ public function xml2txt( _txt:String ):String { if (_txt == null) { return ""; } var txt:String = _txt; var reg:RegExp = null; reg = /</g; txt = txt.replace("<","<"); reg = />/g; txt = txt.replace(">",">"); reg = /"/g; txt = txt.replace(""","\""); reg = /&/g; txt = txt.replace("&","&"); return txt; }