Guest User

Mod2HTML

a guest
Oct 18th, 2011
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.86 KB | None | 0 0
  1. import os
  2. import sys
  3. only=''
  4. if len(sys.argv)>1:
  5.     only=sys.argv[1]
  6.  
  7. unitdir='\\units\\'
  8.  
  9. def getkvlua(line): #get key, value pair from a line
  10.     k=line.partition('=')[0]
  11.     v=line.partition('=')[2]
  12.     k=k.strip().lower()
  13.     v=v.strip().strip(',').strip()
  14.     if '\"' in v:
  15.         v=v.strip('\"')
  16.     if '\'' in v:
  17.         v=v.strip('\'')
  18.     return (k,v)
  19.  
  20. def getkvtdf(line): #get key, value pair from a line
  21.     k=line.partition('=')[0]
  22.     v=line.partition('=')[2]
  23.     k=k.strip()#.lower()
  24.     v=v.strip().strip(';').strip()
  25.     if '\"' in v:
  26.         v=v.strip('\"')
  27.     return (k,v)
  28.        
  29.  
  30. def parselua (fln, l,d): #parse a lua table
  31.     main={}
  32.     while(l<len(fln)):# and '}' not in fln[l]):
  33.         k,v=getkvlua(fln[l])
  34.         # print '   '*d,k,v
  35.         l+=1
  36.         if '}' in k or '}' in v:
  37.             break
  38.         if v=='{':
  39.             a,b=parselua(fln,l,d+1)
  40.             main[k]=a
  41.             l=b
  42.         else:
  43.             main[k]=v
  44.     return main,l
  45.  
  46. def parsetdf (fln, l,d): #parse a lua table
  47.     main={}
  48.     while(l<len(fln)):# and '}' not in fln[l]):
  49.         k,v=getkv(fln[l])
  50.         print ' '*d,k,v
  51.         l+=1
  52.         if '[' in k or ']' in k:
  53.            
  54.             break
  55.         if '[' in k:
  56.             l+=1
  57.             a,b=parselua(fln,l,d+1)
  58.             main[k]=a
  59.             l=b
  60.         else:
  61.             main[k]=v
  62.     return main,l
  63.  
  64. def td(file,entry,params=''):
  65.     file.write('<td '+params+'>'+entry+'</td>')
  66.    
  67. def srow(file,params=''):
  68.     file.write('<tr '+params+'>')
  69.  
  70. def erow(file):
  71.     file.write('</tr>\n')
  72. def starthtml(file,t):
  73.     file.write('<html>\n<title>'+t+'</title>\n<body>\n')
  74. def endhtml(file):
  75.     file.write('</body></html>\n')
  76. def link(file,url,text):
  77.     file.write('<a href='+url+'>'+text+'</a>')
  78. def tdlink(file,url,text):
  79.     file.write('<td><a href='+url+'>'+text+'</a></td>')
  80. def br(file):
  81.     file.write('<br/>\n')
  82. def unitimg(file,unitname):
  83.     link(file,unitname+'.html','<img title=\"rape\" src=\"'+unitname+'.png\" />')
  84.  
  85. def tdunitimg(file,unitname,tooltip=''):
  86.     tdlink(file,unitname+'.html','<img title=\"'+tooltip+'\" src=\"'+unitname+'.png\" />')
  87. # read modinfo
  88. f=open('modinfo.lua','r')
  89. fln=f.readlines()
  90. f.close()
  91. modinfo,z=parselua(fln,1,0)
  92.  
  93. #read explosions:
  94. explosions={}
  95. for filename in os.listdir(os.getcwd()+'/weapons/'):
  96.     f=open('weapons/'+filename,'r')
  97.     fln=f.readlines()
  98.     f.close()
  99.     if '.lua' in filename:
  100.         weapon,z=parselua(fln,2,0)
  101.     else:
  102.         weapon,z=parselua(fln,2,0)
  103.     explosions[filename.partition('.')[0]]=weapon
  104.        
  105. # for filename in os.listdir(os.getcwd()+'/unitpics/'):
  106.     # if '.dds' in filename:
  107.         # cmd='convert unitpics/'+filename+' '+filename.partition('.')[0]+'.png'
  108.         # print cmd
  109.         # os.system(cmd)
  110.        
  111.        
  112. #read units:
  113. units={}
  114. for filename in os.listdir(os.getcwd()+'/units/'):
  115.     if only in filename:
  116.         f=open('units/'+filename,'r')
  117.         fln=f.readlines()
  118.         f.close()
  119.         if '.lua' in filename:
  120.             unit,z=parselua(fln,2,0)
  121.         else:
  122.             unit,z=parselua(fln,2,0)
  123.         units[filename.partition('.')[0]]=unit
  124.  
  125.  
  126. # f=open('armcom.lua','r')
  127. # fln=f.readlines()
  128. # f.close()
  129. # unitinfo,z=parselua(fln,2,0)
  130. # unitinfo=unitinfo.sort()
  131. #print unitinfo
  132. # for key in sorted(unitinfo.iterkeys()):
  133.     # print key, unitinfo[key]
  134.  
  135. def makeheader(outf):
  136.     starthtml(outf, modinfo['name']+' '+modinfo['version'])
  137.     outf.write('<br/><h1>'+modinfo['name']+' '+modinfo['version']+'</h1>\n')
  138.     br(outf)
  139.     link(outf,'index.html','Index')
  140.     outf.write('    ')
  141.     link(outf,'arm.html','Arm')
  142.     outf.write('|')
  143.     link(outf,'core.html','Core')
  144.     br(outf)
  145.  
  146. #make main page:
  147. def makelist(fname,filter):
  148.     outf=open(fname,'w')
  149.     makeheader(outf)
  150.     outf.write('<table border=\"0\">')
  151.     srow(outf)
  152.    
  153.     outf.write('<td><table border=\"0\">')
  154.     srow(outf)
  155.     cnt=0
  156.     for key in sorted (units.iterkeys()):
  157.         if filter in key:
  158.             cnt+=1
  159.     td(outf,str(cnt)+' units in list')
  160.     erow(outf)
  161.  
  162.     srow(outf, 'bgcolor=#aaaaff')
  163.     td(outf,'Name')
  164.     td(outf,'Unitname')
  165.     td(outf,'Description')
  166.     erow(outf)
  167.     i=0
  168.     for key in sorted (units.iterkeys()):
  169.         if filter not in key:
  170.             continue
  171.         i+=1
  172.         if i%2==0:
  173.             p='bgcolor=#ddddff'
  174.         else:
  175.             p=''
  176.         srow(outf,p)
  177.         tdlink(outf,key+'.html',key)
  178.         td(outf,units[key]['name'])
  179.         td(outf,units[key]['description'])
  180.         erow(outf)
  181.     outf.write('</table></td><td valign=\"top\"><table border=\"0\"><tr><td>')
  182.     #plants
  183.     outf.write('Plants:</td></tr>')
  184.    
  185.     for key in sorted (units.iterkeys()):
  186.         if filter not in key:
  187.             continue
  188.         if 'builder' in units[key] and 'acceleration' in units[key] and units[key]['builder']=='true' and units[key]['acceleration']=='0':
  189.             outf.write('<tr>')
  190.             tdunitimg(outf,key,key+' - '+units[key]['name'])
  191.             outf.write('</tr>')
  192.     outf.write('</table>')
  193.     outf.write('</td>')
  194.    
  195.  
  196.     outf.write('<td valign=\"top\">')
  197.     outf.write('<table border=\"0\"><tr><td>Builders:</td></tr>')
  198.    
  199.     for key in sorted (units.iterkeys()):
  200.         if filter not in key:
  201.             continue
  202.         if 'buildoptions' in units[key] and 'acceleration' in units[key] and units[key]['builder']=='true' and units[key]['acceleration']!='0':
  203.             outf.write('<tr>')
  204.             tdunitimg(outf,key,key+' - '+units[key]['name'])
  205.             outf.write('</tr>')
  206.     outf.write('</table>')
  207.     outf.write('</td>')
  208.     outf.write('</td>')
  209.     erow(outf)
  210.     outf.write('</table>')
  211.     outf.write('</table>')
  212.     endhtml(outf)
  213.  
  214. makelist('index.html','')
  215. makelist('arm.html','arm')
  216. makelist('core.html','cor')
  217. def writel(outf,mainl,id,header):
  218.     mainl.sort()
  219.     i=0
  220.     outf.write('<tr><th colspan=\"2\">'+header+'</th><tr>')
  221.     for l in mainl:
  222.         if l not in units[id]:
  223.             continue
  224.         i+=1
  225.         if i%2==1:
  226.             srow(outf)
  227.         else:
  228.             srow(outf,'bgcolor=#ddddff')
  229.         td(outf,l)
  230.         td(outf,units[id][l])
  231.         erow(outf)
  232. def bold(str):
  233.     return '<b>'+str+'</b>'
  234. def hr(outf):
  235.     outf.write('<tr><td colspan=\"2\"><hr/></td></tr>\n')
  236. def writeweapon(outf,k,v):
  237.     hr(outf)
  238.     srow(outf,'bgcolor=#aaaaff')
  239.     td(outf,'Weapon name')
  240.     td(outf,k)
  241.     erow(outf)
  242.     i=0
  243.     for k2 in sorted(v.iterkeys()):
  244.         if type(v[k2]).__name__!='dict':
  245.             i+=1
  246.             if i%2==1:
  247.                 srow(outf)
  248.             else:
  249.                 srow(outf,'bgcolor=#ddddff')
  250.             td(outf,k2)
  251.             td(outf,v[k2])
  252.             erow(outf)
  253.         else:
  254.             srow(outf)
  255.             td(outf,k2)
  256.             outf.write('<td><table border=\"0\" cellspacing="0">') ## TABLE 5 START
  257.             srow(outf,'bgcolor=#aaaaff')
  258.             td(outf,'Target')
  259.             td(outf,'Damage')
  260.             td(outf,'DPS')
  261.             erow(outf)
  262.             for k3, v3 in v[k2].iteritems():
  263.                 srow(outf,'bgcolor=#ffaaaa')
  264.                 td(outf,k3)
  265.                 td(outf,v3)
  266.                 if 'reloadtime' in v:
  267.                     td(outf,'%.2f'%(float(v3)/float(v['reloadtime'])))
  268.                 else:
  269.                     td(outf,'-')
  270.                 erow(outf)
  271.            
  272.             outf.write('</table>')#TABLE 5 END
  273.             erow(outf)
  274.  
  275. def unitpage(id):
  276.     outf=open(id+'.html','w')
  277.     makeheader(outf)
  278.     outf.write('<table border=\"0\">')## TABLE 1 START
  279.     srow(outf, 'bgcolor=#aaaaff')
  280.     td(outf,'<h3>'+bold(units[id]['name'])+' - '+units[id]['description']+'</h3>\n','colspan=\"3\"')
  281.     erow(outf)
  282.     srow(outf)
  283.     outf.write('<td valign=\"top\"><table border=\"0\" cellspacing="0">') ## TABLE 2 START
  284.     srow(outf)
  285.     tdunitimg(outf,id,id+' - '+units[id]['name'])
  286.     erow(outf)
  287.     srow(outf)
  288.     td(outf,id)
  289.     erow(outf)
  290.     if 'buildoptions' in units[id]:
  291.         srow(outf)
  292.         td(outf,bold('Can Build:'))
  293.         erow(outf)
  294.         for k,v in units[id]['buildoptions'].iteritems():
  295.             srow(outf)
  296.             tdunitimg(outf,v,v+' - '+units[v]['name'])
  297.             erow(outf)
  298.     srow(outf)
  299.     td(outf,bold('Built by:'))
  300.     erow(outf)
  301.     for key in sorted (units.iterkeys()):
  302.         if 'buildoptions' in units[key] and id in units[key]['buildoptions'].viewvalues():
  303.             srow(outf)
  304.             tdunitimg(outf,key,key+' - '+units[key]['name'])
  305.             erow(outf)
  306.     outf.write('</table></td><td align=\"left\" valign=\"top\"><table border=\"0\" cellspacing="0">') ## TABLE 2 end TABLE 3 START
  307.    
  308.     mainl=['commander','maxdamage','buildcostenergy','buildcostmetal','buildtime','maxvelocity','category']
  309.     writel(outf,mainl,id,'Basic settings:')
  310.  
  311.     hr(outf)
  312.     detectionl=['sightdistance','radardistance','sonardistance','cloakcost','cloakcostmoving','mincloakdistance','seismicsignature','radardistancejam','stealth']
  313.     writel(outf,detectionl,id,'Detection settings:')
  314.     hr(outf)
  315.    
  316.     pathl=['acceleration','brakerate','footprintx','footprintz','maxslope','maxvelocity','maxwaterdepth','movementclass','turnrate','canmove']
  317.     writel(outf,pathl,id,'Pathfinding and movement related:')
  318.     hr(outf)
  319.    
  320.    
  321.     conl=['builder','workertime','cancapture','builddistance','metalmake','energymake','energyuse','activatewhenbuilt','onoffable','extractsmetal','metalstorage']
  322.     writel(outf,conl,id,'Construction settings:')
  323.     hr(outf)   
  324.     otherlist=[]
  325.     for k in sorted(units[id].iterkeys()):
  326.         if k not in mainl and k not in detectionl and k not in pathl and k not in conl and type(units[id][k]).__name__!='dict':
  327.             otherlist.append(k)
  328.            
  329.     writel(outf,otherlist,id,'Other settings:')
  330.     hr(outf)   
  331.    
  332.     outf.write('</table>')#TABLE 3 END
  333.     outf.write('</td>')
  334.    
  335.     outf.write('<td align=\"left\" valign=\"top\"><table border=\"0\" cellspacing="0">') ## TABLE 4 START
  336.     outf.write('<tr><th colspan=\"2\">'+'Weapon Definitions'+'</th><tr>')
  337.     if 'weapondefs' in units[id]:
  338.         for k,v in units[id]['weapondefs'].iteritems():
  339.             writeweapon(outf,k,v)
  340.    
  341.     if 'explodeas' in units[id]:
  342.         writeweapon(outf,units[id]['explodeas'],explosions[units[id]['explodeas'].lower()])
  343.     if 'selfdestructas' in units[id]:
  344.         writeweapon(outf,units[id]['selfdestructas'],explosions[units[id]['selfdestructas'].lower()])
  345.     outf.write('</table>')#TABLE 4 END
  346.     outf.write('</td>')
  347.     erow(outf)
  348.     outf.write('</table>')#TABLE 1 END
  349.    
  350.    
  351.     endhtml(outf)
  352.  
  353.            
  354. for u in units.iterkeys():
  355.     unitpage(u)
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370. #-------------------------------------------|
  371. #BA $Version                                |
  372. #-------------------------------------------|
  373. #Index | sides arm/core                     |
  374. #-------------------------------------------|
  375. #Unitname                                   |
  376. #-------------------------------------------|
  377. #unitpic    |Main info:     |WEAPONS        |
  378. #unitname   |health         |1 pew dps      |
  379. #-----------|metal          |---------------|
  380. #Can build: |energy         |weapon1allinfo-|
  381. #1          |buildtime      |               |
  382. #2          |cost           |               |
  383. #3          |---------------|               |
  384. #4          |allelse        |               |
  385. #Built by:  |...            |               |
  386. #           |               |               |
  387. #--------------------------------------------
  388.  
Advertisement
Add Comment
Please, Sign In to add comment