Are anyone capable to explain usage of this script?
#!/opt/local/bin/ruby
DOFUS_DIR = File.expand_path("~/Documents/Projects/DofusApp/source")
def tabs size = 3
" " * size
end
filepath_rb = ENV["TM_FILEPATH"]
filepath_as = filepath_rb.gsub(File.expand_path('~/Documents/Projects/dofus/includes'), '')
filepath_as = DOFUS_DIR + File.join(File.dirname(filepath_as).downcase, File.basename(filepath_as, File.extname(filepath_as)) + ".as")
raise unless File.exists?(filepath_as)
File.open(filepath_as) do |f|
data = f.read
as_tabs, code = data.scan(/([ \t]+)public function deserializeAs_.+?:void\s*\{\s+(.+?)\s+return.+\1\}/miu).first
new_code = code.lines.
map { |l| l.strip!; tabs + l }.
join("\n").
gsub("this.", "@").
gsub("();", "").
gsub(/var loc\d+:\*=(0|null);\s*/ium, "").
gsub(/super\.deserialize\(arg1\);/ium, "super data\n#{tabs}").
gsub(/com\.ankamagames\.jerakine\.network\.utils\.BooleanByteWrapper\.getFlag\(loc\d+,\s*(\d+)\);/iu, "BBW.get(byte, \\1)").
gsub(/if \([^\(\)]+\) \{\s+throw new Error[^\n]+\s+\}[\s\n\t]*/ium, "").
gsub(/[ \t]+var loc\d+:\*=\s*arg\d+\.readUnsignedShort\s+while.+\s+loc\d+ = arg1.(\w+)\s+(@\w+).+\s+.+\s+\}/u, <<-EOD).
#{tabs}
#{tabs}\\2 = []
#{tabs}data.readUShort.times {
#{tabs} \\2 << data.\\1
#{tabs}}
EOD
gsub(/[ \t]+var loc\d+:\*=\s*arg\d+\.readUnsignedShort\s+while.+\s+\(loc\d+ = new com[^A-Z]+(\w+).+\s+(@\w+).+\s+.+\s+\}/u, <<-EOD).
#{tabs}
#{tabs}\\2 = []
#{tabs}data.readUShort.times {
#{tabs} (obj = \\1.new).deserialize(data)
#{tabs} \\2 << obj
#{tabs}}
EOD
gsub(/[ \t]+var loc\d+:\*=\s*arg\d+\.readUnsignedShort\s+while.+\s*{\s*if \(\(loc\d+ = \w+.(read\w+).+\s*throw new Error.+\s+\}\s.+(@\w+).+\s+\+\+loc\d.+\s.+/iu, <<-EOD).
#{tabs}
#{tabs}\\2 = []
#{tabs}data.readUShort.times {
#{tabs} \\2 << data.\\1
#{tabs}}
EOD
gsub(/[^\n]+var loc\d+:\*=\s*arg\d+\.readUnsignedShort\s+while\s*\([^)]+\)\s*{\s*loc\d+ = \w+\.readUnsignedShort\s\s*\(loc\d+ = [\w\.]+ProtocolTypeManager\.getInstance.+\.deserialize\((.+)\)[^@]+(@\w+)[^}]+}\n/iu, <<-EOD).
#{tabs}
#{tabs}\\2 = []
#{tabs}data.readUShort.times {
#{tabs} type_id = data.readUShort
#{tabs} (obj = Types.get(type_id)).deserialize(\\1)
#{tabs} \\2 << obj
#{tabs}}
#{tabs}
EOD
gsub(/var loc\d:\*=arg1.readUnsignedShort\s+(@\w+) = .+ProtocolTypeManager.getInstance[^A-Z]+(\w+).+\s+\1.+/u, <<-EOD).
#{tabs}
#{tabs}type_id = data.readUShort
#{tabs}(\\1 = Types.get(type_id)).deserialize(data)
#{tabs}
EOD
gsub(/\s+(\S+) = new com.ankamagames[^A-Z]+(\w+)/u, "\n#{tabs}\n#{tabs}\\1 = \\2.new").
gsub(".deserialize(arg1);", ".deserialize(arg1)\n#{tabs}").
gsub("arg1", "data").
strip
print "#{new_code}
#{tabs}
#{tabs}false"
end