Converting map feature tdf's to lua - How would you do it?

Converting map feature tdf's to lua - How would you do it?

Various things about Spring that do not fit in any of the other forums listed below, including forum rules.

Moderator: Moderators

Post Reply
User avatar
SirArtturi
Posts: 1164
Joined: 23 Jan 2008, 18:29

Converting map feature tdf's to lua - How would you do it?

Post by SirArtturi »

Hello,

I was about to convert tdf's of my tree feature set to lua format, but I find it too laborous to do it by hand and I really don't have time and motivation to start writing some code for it...

How would you do it?
Hell, If you'd do it, it be more than greatful and give you the credits for this particular task of this project...
User avatar
Beherith
Posts: 5145
Joined: 26 Oct 2007, 16:21

Re: Converting map feature tdf's to lua - How would you do it?

Post by Beherith »

Im working on a python script to do this, but some things still arent too clear.
User avatar
Beherith
Posts: 5145
Joined: 26 Oct 2007, 16:21

Re: Converting map feature tdf's to lua - How would you do it?

Post by Beherith »

How is the case sensitivity in lua defs and in tdfs?
It seems that tdf is case insensitive, and lua needs proper case.
How do I find the proper cases for the lua keys?
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Converting map feature tdf's to lua - How would you do it?

Post by Forboding Angel »

From what I understand, lua ends up being case insensitive due to some sort of parser or something it gets run through to convert all the upper case to lower case. That may only apply to unitdefs though.
User avatar
Beherith
Posts: 5145
Joined: 26 Oct 2007, 16:21

Re: Converting map feature tdf's to lua - How would you do it?

Post by Beherith »

Then here it is.
Needs python 2.6 installed, just copy it, and it will make the .lua files next to each .tdf recursively in all subdirs.

Code: Select all

import os
import re
import string
import time



dbg=0 # set to 0 if its too verbose. set to 1 for tons of debugging output
delnonobjs=0 # set to 1 to not create lua files for tdfs with missing s3o or 3do files.

#float paramters, values of these keys will be attempted to be parsed as FLOAT
fp= ["damage","energy","metal","height","reclaimtime",'hitdensity']

#boolean paramters, values of these keys will be attempted to be parsed as BOOL
bp=['blocking','reclaimable','autoreclaimable','upright','destructible','indestructible','flammable','noselect','geothermal','shadtrans','animtrans','animating','nodrawundergrey']

#string params:
sp=['customparams','world','description','object','category','name','collisionvolumetype','featurereclamate','seqnamereclamate','featuredead','seqname','filename','seqnamedie']

#integer params
ip=['footprintx','footprintz']

#vectors of 3 floats:
f3p=['collisionvolumeoffsets','collisionvolumescales']

rootdir='I:\\maps\\features' # specify a dir other than the current dir here. NOTE: remember that \ in a path should be \\ for python not to parse it as escape chars, example: 'I:\\maps\\features'
objdir= '' # dir to search for object files, optional
if rootdir=='':
	rootdir=os.getcwd()
print 'cwd:'
print os.getcwd()
nfeatures=0
nskipped=0

for subdir, dirs, files in os.walk(rootdir):
	
    for file in files:
		if file.find(".tdf")>1:
			if dbg==1:
				print "Opening file: %s" % file
				print subdir
				
			f=open(subdir+'\\'+file, 'r')
			lines=f.readlines()
			f.close()
			luafilename=''
			objname=''
			lf=''
			foundobj=0
			for line in lines:
				comment=''
				if line.find('//')>-1:
					tmp=line.partition('//')
					if dbg==1:
						print 'Found a comment in line: %s' % line
					comment=tmp[2]
					line=tmp[0]
				 
				if re.search("\[.*\]",line): #then we got the first line!
					objname=line.partition('[')[2]
					objname=objname.rpartition(']')[0]
					if objname != '':
						if dbg==1:
							print 'objname= %s'% objname
						
						if objname.find('-')>-1 or objname.find('+')>-1 :
							print "Warning: object name %s contains disallowed characters (-, +), replacing with _" %objname
							objname=objname.replace('-','_')
							objname=objname.replace('+','_')
						luafilename='%s.lua' % objname
						lf=open(subdir+'\\'+luafilename,'w')
						lf.write('local objectname= \"%s\" \n' %objname)
						lf.write('local featureDef	=	{\n')
						lf.write('\tname\t\t\t= \"%s\",\n' %objname)
						foundobj=0;
				elif line.find('{')>-1:
					if dbg==1:
						print 'found { char in line: %s' % line
				elif line.find('}')>-1:
					if dbg==1:
						print 'Ending file on line: %s' % line
					lf.write('} \n')
					lf.write('return lowerkeys({[objectname] = featureDef}) \n')
					
					lf.close()
					if foundobj ==0 and delnonobjs ==1:
						print 'Didnt find OBJ in %s, deleting' %luafilename
						lf=open(subdir+'\\'+luafilename,'w')
						lf.write(' ')
						lf.close
						nskipped+=1
					else:
						nfeatures+=1
				elif line.find('=')>-1:
					parts=line.partition('=');
					if parts[0]=='' or parts[2]=='':
						print 'Error parsing line : %s ' % line						
					key=parts[0].lstrip()
					key=key.rstrip()
					key=string.lower(key)
					value=parts[2]
					if value.find(';')<0:
						print 'Error, no terminating ; found in tdf file at line: %s   ->Skipping line' % line
						break
					else:
						value=value.rstrip().rstrip(';').lstrip().rstrip()
						
					if key in fp:
						lf.write('\t%s\t\t\t\t=%s,\n' % (key,value))
					elif key in bp:
						i=0
						try:
							i=int(value)
						except ValueError:
							if i=='true':
								i=1
							else:
								i=0
							
						if i==1:
							lf.write('\t%s\t\t\t\t=true,\n' % (key))
						elif i==0:
							lf.write('\t%s\t\t\t\t=false,\n' % (key))
						else :
							print 'Error parsing tdf on a boolean value: %s   -> Value not written!' %line
					elif key in sp:
						if key=='name':
							if value != objname:
								print 'Warning: object name (%s) doesnt match parameter name= %s' %(value,objname)
						if key=='object' : 
							if string.lower(str(value)).find('.s3o')>-1 or  string.lower(str(value)).find('.3do')>-1:
								if delnonobjs ==1:
									if objdir !='':
										for subdir2, dirs2, files2 in os.walk(objdir):
											for file2 in files2:
												if string.lower(str(file2))==string.lower(str(value)):
													
													foundobj=1
													print '%s found in %s' %(str(value),objdir)
												
												#else:
													#print '%s != %s' %(str(value),str(file2))
										if foundobj==0:
											print 'Error: %s not found in %s, discarding' %(str(value),objdir)
									else:
										foundobj=1
											
								else:
									foundobj=1
							else:
								print '%s is not s3o or 3do' %str(value)
								
							
						lf.write('\t%s\t\t\t\t=\"%s\",\n' % (key,value))
					elif key in ip:
						lf.write('\t%s\t\t\t\t=%s,\n' % (key,value))
					elif key in f3p:
						tmp=string.split(value,' ')
						if len(tmp)>3 or len(tmp)<3:
							print 'Error splitting string into 3 floats on: %s' % line
						else:
							lf.write('\t%s\t\t\t\t={%s, %s, %s},\n' % (key,tmp[0],tmp[1],tmp[2]))
					else:
						print 'Warning: Unknown key in %s   ->still attempting to parse and convert it' % line
						v='';
						try:
							v=str(float(value))
						except ValueError:
							v=value
						
						lf.write('\t%s\t\t\t\t=\"%s\",\n' % (key,v))
				if comment != '':
					try:
						lf.write('-- %s' % comment)
					except ValueError:
						print 'Warning, attempting to write %s comment to a closed file' %comment
					except AttributeError:
						print 'Warning, attempt to write comment to nonexisting file! in line: %s, file= %s' %(line,file)
						
print '%i Feature defs created, %i skipped' %(nfeatures,nskipped)
s = raw_input('Done, press ENTER to exit')
time.sleep(5)
User avatar
Beherith
Posts: 5145
Joined: 26 Oct 2007, 16:21

Re: Converting map feature tdf's to lua - How would you do it?

Post by Beherith »

Bugfix update (could crash on some obscenely badly formed tdf files):

Edit2:
Even uglier bug got fixed now...

Edit3:
Should be the last fix...

edit: more stuff
Attachments
tdf2lua.7z
(2.17 KiB) Downloaded 17 times
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Converting map feature tdf's to lua - How would you do it?

Post by Forboding Angel »

Doesn't recognize "indestructible"
User avatar
Beherith
Posts: 5145
Joined: 26 Oct 2007, 16:21

Re: Converting map feature tdf's to lua - How would you do it?

Post by Beherith »

Cause thats a freaking typo. Indestructible
Oh looool, its not, I spent years typing indestructable. :)
You can change 1 char in it to make it recognize it.
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Converting map feature tdf's to lua - How would you do it?

Post by Forboding Angel »

Still, prolly better release a new version to include the proper spelling of it.

This tool is really great behe, thanks :-)
User avatar
Beherith
Posts: 5145
Joined: 26 Oct 2007, 16:21

Re: Converting map feature tdf's to lua - How would you do it?

Post by Beherith »

released new, more options: can ignore files with no corresponding .s3o files

Note: it will convert unrecognized params, and will attempt to parse them as floats, if that fails, they get written as strings.
Post Reply

Return to “General Discussion”