225 lines
6.4 KiB
Python
225 lines
6.4 KiB
Python
""" Makes the configs as a massive string """
|
|
|
|
# Used for the conditions in the <double> type
|
|
condDict = {}
|
|
defaultDict = {}
|
|
bindOrAlias = "bind"
|
|
from json import dumps
|
|
from copy import deepcopy
|
|
|
|
def makeCFG(cfg, default=False):
|
|
global bindOrAlias
|
|
global condDict
|
|
global defaultDict
|
|
|
|
bindOrAlias = "bind"
|
|
if default:
|
|
# Write to defaultDict instead of condDict
|
|
condDict = defaultDict
|
|
else:
|
|
condDict = deepcopy(defaultDict)
|
|
|
|
ret = ''
|
|
for key, data in cfg.items():
|
|
isAlias = False
|
|
if "alias" in data:
|
|
isAlias = data.pop("alias")
|
|
if isAlias:
|
|
bindOrAlias = "alias"
|
|
else:
|
|
bindOrAlias = "bind"
|
|
ret += branch(key, data)
|
|
|
|
# Doubles are weird. All of the toggles got put into a dictionary.
|
|
# This takes all of the nested dictionaries and turns them into the right string
|
|
for key, toggles in condDict.items():
|
|
plusToggleStr = ';'.join(toggles["plus_toggles"])
|
|
minusToggleStr = ';'.join(toggles["minus_toggles"])
|
|
ret += f'alias +{key}_toggles "{plusToggleStr}"\n' +\
|
|
f'alias -{key}_toggles "{minusToggleStr}"\n' +\
|
|
f'{bindOrAlias} {key} "+{key}_toggles"\n'
|
|
|
|
del condDict # free deep copy
|
|
|
|
return ret
|
|
|
|
def typeOf(dictIn):
|
|
""" Find the first element common to both lists """
|
|
types = [
|
|
"impulse",
|
|
"hold",
|
|
"toggle",
|
|
"double",
|
|
"repeat"
|
|
]
|
|
for t in types:
|
|
if t in dictIn.keys():
|
|
return t
|
|
|
|
def branch(keyName, bindContent):
|
|
bindType = typeOf(bindContent)
|
|
bindContent = bindContent.pop(bindType)
|
|
|
|
if bindType == "impulse":
|
|
return impulse(keyName, bindContent)
|
|
|
|
elif bindType == "hold":
|
|
if isinstance(bindContent, str):
|
|
return simpleHold(keyName, bindContent)
|
|
else:
|
|
return listHold(keyName, bindContent)
|
|
|
|
elif bindType == "toggle":
|
|
return toggle(keyName, bindContent)
|
|
|
|
elif bindType == "double":
|
|
return double(keyName, bindContent)
|
|
|
|
elif bindType == "repeat":
|
|
return repeat(keyName, bindContent)
|
|
|
|
def impulse(key, instruction):
|
|
global bindOrAlias
|
|
if isinstance(instruction, list):
|
|
instruction = ';'.join(instruction)
|
|
|
|
allInstructions = []
|
|
|
|
for indivCmd in instruction.split(';'):
|
|
allInstructions.append(impulseShortcuts(indivCmd))
|
|
|
|
instruction = ';'.join(allInstructions)
|
|
|
|
return f'{bindOrAlias} {key} "{instruction}"\n'
|
|
|
|
def impulseShortcuts(instruction):
|
|
splitCommand = instruction.split(' ')
|
|
cmd = splitCommand[0]
|
|
shortcuts = {
|
|
"primary": "slot1",
|
|
"secondary": "slot2",
|
|
"melee": "slot3"
|
|
}
|
|
if cmd in shortcuts:
|
|
for sc, expansion in shortcuts.items():
|
|
if cmd == sc:
|
|
splitCommand[0] = expansion
|
|
break
|
|
instruction = ' '.join(splitCommand)
|
|
|
|
restOfCmd = ' '.join(splitCommand[1:])
|
|
if cmd == "voice":
|
|
instruction = voice(restOfCmd)
|
|
|
|
elif cmd == "build" or cmd == "destroy":
|
|
instruction = f"{cmd} " + expandBuildings(restOfCmd)
|
|
|
|
return instruction
|
|
|
|
def voice(keyword):
|
|
keyword = keyword.lower()
|
|
|
|
allLists = [
|
|
["medic", "thanks", "go", "move up", "go left", "go right", "yes", "no", "pass to me"],
|
|
["incoming", "spy", "sentry ahead", "teleporter here", "dispenser here", "sentry here", "activate uber", "uber ready"],
|
|
["help", "battle cry", "cheers", "jeers", "positive", "negative", "nice shot", "good job"]
|
|
]
|
|
|
|
for menu, voiceList in enumerate(allLists):
|
|
for selection, shortcut in enumerate(voiceList):
|
|
if keyword == shortcut:
|
|
return f'voicemenu {menu} {selection}'
|
|
|
|
def expandBuildings(building):
|
|
buildingNums = {
|
|
"dispenser": "0 0",
|
|
"entrance": "1 0",
|
|
"exit": "1 1",
|
|
"sentry": "2 0"
|
|
}
|
|
for shortBuild, num in buildingNums.items():
|
|
if building == shortBuild:
|
|
return num
|
|
|
|
def simpleHold(key, instruction):
|
|
global bindOrAlias
|
|
# This isn't quite right, fix later!
|
|
if instruction[0] == '+' or instruction[0] == '-':
|
|
return f'{bindOrAlias} {key} "{instruction}"\n'
|
|
else:
|
|
return f'{bindOrAlias} {key} "+{instruction}"\n'
|
|
|
|
def listHold(key, options):
|
|
global bindOrAlias
|
|
pressStr = options["press"]
|
|
if isinstance(pressStr, list):
|
|
pressStr = ';'.join(pressStr)
|
|
|
|
releaseStr = options["release"]
|
|
if isinstance(releaseStr, list):
|
|
releaseStr = ';'.join(releaseStr)
|
|
|
|
ret = f'alias +{key}_bind "{pressStr}"\n' +\
|
|
f'alias -{key}_bind "{releaseStr}"\n' +\
|
|
f'{bindOrAlias} {key} "+{key}_bind"\n'
|
|
|
|
return ret
|
|
|
|
def toggle(key, instruction):
|
|
global bindOrAlias
|
|
onStr = f'turn_{key}_on'
|
|
offStr = f'turn_{key}_off'
|
|
togStr = f'toggle_{key}'
|
|
|
|
ret = f'alias {onStr} "+{instruction}; alias {togStr} {offStr}"\n' +\
|
|
f'alias {offStr} "-{instruction}; alias {togStr} {onStr}"\n' +\
|
|
f'alias {togStr} "{onStr}"\n' +\
|
|
f'{bindOrAlias} {key} "{togStr}"\n'
|
|
return ret
|
|
|
|
def double(key, options):
|
|
primaryAction = options["primary"]
|
|
|
|
secAction = options["secondary"]
|
|
|
|
mainStr = f'{key}_main'
|
|
altStr = f'{key}_alt'
|
|
pTogStr = f'+toggle_{key}'
|
|
mTogStr = f'-toggle_{key}'
|
|
|
|
global bindOrAlias
|
|
oldBindOrAlias = bindOrAlias
|
|
bindOrAlias = "alias"
|
|
recursiveCode = branch(mainStr, primaryAction) +\
|
|
branch(altStr, secAction)
|
|
bindOrAlias = oldBindOrAlias
|
|
|
|
ret = recursiveCode +\
|
|
f'alias {pTogStr} "{bindOrAlias} {key} {altStr}"\n' +\
|
|
f'alias {mTogStr} "{bindOrAlias} {key} {mainStr}"\n'+\
|
|
f'{bindOrAlias} {key} "{mainStr}"\n'
|
|
|
|
condName = options["condition"]
|
|
global condDict
|
|
if condName in condDict:
|
|
# If the condition key (like "mouse4") already has toggles,
|
|
# just append another toggle string
|
|
plusToggles = condDict[condName]["plus_toggles"]
|
|
minusToggles = condDict[condName]["minus_toggles"]
|
|
if pTogStr not in plusToggles:
|
|
plusToggles.append(pTogStr)
|
|
minusToggles.append(mTogStr)
|
|
else:
|
|
# If the condition key doesn't already exist, make it
|
|
condDict.update( {
|
|
condName: {
|
|
"plus_toggles": [ pTogStr ],
|
|
"minus_toggles": [ mTogStr ]
|
|
}
|
|
} )
|
|
|
|
return ret
|
|
|
|
def repeat(key, options):
|
|
return f'placeholder for {key} (repeat)\n'
|