Fandom

Foldit Wiki

Lua Export and Import with Python

1,465pages on
this wiki
Add New Page
Comments0 Share

create a folder 'Recipes' in your Foldit one

put the script in a file named recipe.py (or whatever)

edit the rl (recipe list) array with the script IDs you find in your all.macro file

script_names can be anything, they're only used to call the right ID (and create the .lua file to work in). this script extracts your recipe from the all.macro file then sets it back once you're done

hope this helps

calling the scriptEdit

cd-command to your 'Recipes' folder then type


python recipe.py extract script_name

python recipe.py replace anotherscript_name

names must be the ones you set on the rl array


script codeEdit


import sys,re

type=sys.argv[1]
file=sys.argv[2]
mfpath="../all.macro" # all.macro file path. OS dependent, watch out
prescript='"script\\" : \\"' # file marker placed before script
postscript='\\"\\n \\"script_version\\"' # file marker placed after script

# escape patterns
ep = [
	['\\\\n','\n'],
	['\\\\t','\t'],
	['\\\\\\,',','],
	['\\\\\\#','#'],
	['\\\\\\"','"']
]

# recipe list array. always in the form ['ID', 'name'] separated by commas
rl = [
	['script_ID1', 'script_name'],
	['script_ID2', 'another_script_name'],
	['script_ID3', 'wow_lots_of_recipes'],
	['script_ID4', 'aliens_oh_my'],
	['script_ID5', 'short_name'],
	['script_ID6', 'shorter'],
	['script_ID7', 's']
]

def main():
	index=get_line()
	if index is False: return
	if type=="extract": extract_script(index)
	elif type=="replace": replace_script(index)

def get_line():
	for i in range(len(rl)):
		if file==rl[i][1]:
			return rl[i][0]
	print('Key not found. Stopping.')
	return False

def find_script(text):
	temp=text.split(prescript,1)
	pre=temp[0]
	text=temp[1]
	temp=text.split(postscript,1)
	text=temp[0]
	post=temp[1]
	return [pre, text, post]

def extract_script(index):
	mf=open(mfpath) # macro file
	df=open(file+'_decoded.lua','w') #decoded file
	text=""
	for line in mf.readlines():
		if line.find(index) != -1:
			text=line
			break
	text=find_script(text)[1]
	for i in range(len(ep)):
		text=text.replace(ep[i][0],ep[i][1])
	df.write(text)
	df.close()

def replace_script(index):
	text=open(file+'_decoded.lua').read()
	for i in range(len(ep)):
		text=text.replace(ep[i][1],ep[i][0])

	mf=open(mfpath, 'r') # macro file
	oldtext=mf.read()
	mf.close()

	# save file
	mf=open(mfpath+'.pygen_bak', 'w') # macro file
	mf.write(oldtext)
	mf.close()

	# now work on file
	result=re.findall('\n.*'+index+'.*\n',oldtext)
	line=result[0][1:-1]
	newline=find_script(line)
	newline=newline[0]+prescript+text+postscript+newline[2]
	text=oldtext.replace(line, newline)
	mf=open(mfpath, 'w')
	mf.write(text)
	mf.close()

main()

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.