271 lines
7.8 KiB
Python
271 lines
7.8 KiB
Python
""" Makes the configs as a massive string """
|
|
|
|
# Used for the conditions in the <double> type
|
|
condDict = {}
|
|
defaultDict = {}
|
|
bindOrAlias = "bind"
|
|
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
|
|
if default or condDict != defaultDict:
|
|
# ==, and by extension !=, does in fact check
|
|
# for dictionary equality in keys and values
|
|
for key, toggles in condDict.items():
|
|
onCondPress = ';'.join(toggles["change_keys"])
|
|
onCondRelease = ';'.join(toggles["restore_keys"])
|
|
ret += f'alias +{key}_toggles "{onCondPress}"\n' +\
|
|
f'alias -{key}_toggles "{onCondRelease}"\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):
|
|
""" Using the provided keyName and content, call the correct function """
|
|
|
|
"""
|
|
Terser syntax, ex.
|
|
impulse e:
|
|
<content>
|
|
instead of
|
|
e:
|
|
impulse:
|
|
<content>
|
|
"""
|
|
splitKey = keyName.split(' ', 1)
|
|
if len(splitKey) > 1:
|
|
keyName = splitKey[1]
|
|
bindContent = {splitKey[0]: 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, dict):
|
|
instruction = instruction["command"]
|
|
|
|
if not isinstance(instruction, list):
|
|
instruction = instruction.split(';')
|
|
|
|
instuction = impulseShortcuts(instruction)
|
|
instruction = ';'.join(instruction)
|
|
|
|
return f'{bindOrAlias} {key} "{instruction}"\n'
|
|
|
|
def impulseShortcuts(instList):
|
|
for i, instruction in enumerate(instList):
|
|
splitCmd = instruction.split(' ')
|
|
cmd = splitCmd[0]
|
|
restOfCmd = ' '.join(splitCmd[1:])
|
|
shortcuts = {
|
|
"primary": "slot1",
|
|
"secondary": "slot2",
|
|
"melee": "slot3"
|
|
}
|
|
if cmd in shortcuts:
|
|
cmd = shortcuts[cmd]
|
|
|
|
if cmd == "voice":
|
|
cmd = "voicemenu"
|
|
restOfCmd = voice(restOfCmd)
|
|
|
|
elif cmd == "build" or cmd == "destroy":
|
|
restOfCmd = expandBuildings(restOfCmd)
|
|
|
|
elif cmd == "load_itempreset" and restOfCmd.isalpha():
|
|
restOfCmd = restOfCmd.lower()
|
|
restOfCmd = ['a','b','c','d'].index(restOfCmd)
|
|
|
|
if restOfCmd != "":
|
|
cmd += ' ' + restOfCmd
|
|
instList[i] = cmd
|
|
|
|
return instList
|
|
|
|
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'{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
|
|
|
|
oldBindOrAlias = bindOrAlias
|
|
bindOrAlias = 'alias'
|
|
ret = impulse(f'+{key}_press', options["press"]) +\
|
|
impulse(f'-{key}_release', options["release"])
|
|
|
|
bindOrAlias = oldBindOrAlias
|
|
|
|
ret += f'{bindOrAlias} {key} "+{key}_press"\n'
|
|
|
|
return ret
|
|
|
|
def toggle(key, instruction):
|
|
global bindOrAlias
|
|
onStr = f'turn_{key}_on'
|
|
offStr = f'turn_{key}_off'
|
|
togStr = f'toggle_{key}'
|
|
if instruction[0] == '+':
|
|
instruction = instruction[1:]
|
|
|
|
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'
|
|
pShiftStr = f'+shift_{key}'
|
|
mShiftStr = f'-shift_{key}'
|
|
|
|
global bindOrAlias
|
|
oldBindOrAlias = bindOrAlias
|
|
bindOrAlias = "alias"
|
|
recursiveCode = branch(mainStr, primaryAction) +\
|
|
branch(altStr, secAction)
|
|
bindOrAlias = oldBindOrAlias
|
|
|
|
ret = recursiveCode +\
|
|
f'alias {pShiftStr} "{bindOrAlias} {key} {altStr}"\n' +\
|
|
f'alias {mShiftStr} "{bindOrAlias} {key} {mainStr}"\n'+\
|
|
f'{bindOrAlias} {key} "{mainStr}"\n'
|
|
|
|
isToggle = ("toggle" in options and options.pop("toggle") == True)
|
|
if isToggle:
|
|
toggleStr = toggle(key, pShiftStr)
|
|
|
|
condName = options["condition"]
|
|
global condDict
|
|
if condName in condDict:
|
|
# If the condition key (like "mouse4") already has toggles,
|
|
# just append another toggle string
|
|
changes = condDict[condName]["change_keys"]
|
|
restores = condDict[condName]["restore_keys"]
|
|
|
|
if isToggle:
|
|
# "toggle: true" specified, add the toggle string
|
|
if toggleStr not in changes:
|
|
changes.append(toggleStr)
|
|
if pShiftStr in changes:
|
|
# If key already has normal shift, remove it
|
|
changes.remove(pShiftStr)
|
|
changes.remove(mShiftStr)
|
|
|
|
elif pShiftStr not in changes:
|
|
# not toggle, not already in changes
|
|
changes.append(pShiftStr)
|
|
restores.append(mShiftStr)
|
|
else:
|
|
# If the condition key doesn't already exist, make it
|
|
if isToggle:
|
|
condDict.update( {
|
|
condName: {
|
|
"change_keys": [ toggleStr ],
|
|
"restore_keys": [ ]
|
|
}
|
|
} )
|
|
else:
|
|
condDict.update( {
|
|
condName: {
|
|
"change_keys": [ pShiftStr ],
|
|
"restore_keys": [ mShiftStr ]
|
|
}
|
|
} )
|
|
|
|
return ret
|
|
|
|
def repeat(key, options):
|
|
return f'placeholder for {key} (repeat)\n'
|