236 lines
7.7 KiB
Python
236 lines
7.7 KiB
Python
"""Verify all the things that could go wrong."""
|
|
def verifyConfig(cfg: dict) -> (dict, dict):
|
|
verifiedConfig = {}
|
|
|
|
# Do defaults first
|
|
errors = {}
|
|
|
|
defaults = None
|
|
|
|
if "default" in cfg:
|
|
defaults = cfg.pop("default")
|
|
errMessages = []
|
|
for key, data in defaults.items():
|
|
isAlias = False
|
|
if "alias" in data:
|
|
isAlias = data["alias"]
|
|
if not isinstance(isAlias, bool):
|
|
errMessages.append(f'"alias" field in "{key}" makes no sense: "{isAlias}"')
|
|
errMessages.extend(validBind(key, data, alias = isAlias) )
|
|
if len(errMessages) > 0:
|
|
errors.update( {"default": errMessages} )
|
|
|
|
classList = [
|
|
"scout",
|
|
"soldier",
|
|
"pyro",
|
|
("demo","demoman"),
|
|
("engi","engineer"),
|
|
("heavy","heavyweapons"),
|
|
"medic",
|
|
"sniper",
|
|
"spy"
|
|
]
|
|
|
|
for cclass in classList:
|
|
classCFG = None
|
|
className = cclass
|
|
if isinstance(cclass, str) and cclass in cfg:
|
|
classCFG = cfg.pop(cclass)
|
|
elif isinstance(cclass, tuple):
|
|
for tupClass in cclass:
|
|
if tupClass in cfg:
|
|
classCFG = cfg.pop(tupClass)
|
|
className = cclass[0]
|
|
break
|
|
if classCFG is None:
|
|
# Invalid class, this gets caught later.
|
|
# It may be less efficient this way, but
|
|
# it makes for more descriptive error messages
|
|
continue
|
|
errMessages = []
|
|
for key, data in classCFG.items():
|
|
isAlias = False
|
|
if "alias" in data:
|
|
isAlias = data["alias"]
|
|
if not isinstance(isAlias, bool):
|
|
errMessages.append(f'"alias" field in "{key}" makes no sense: "{isAlias}"')
|
|
errMessages.extend( validBind(key, data, alias = isAlias) )
|
|
if len(errMessages) > 0:
|
|
errors.update( {className: errMessages} )
|
|
verifiedConfig.update({className: classCFG})
|
|
|
|
# Turn list into only strings by expanding tuples
|
|
for i, clss in enumerate(classList):
|
|
if isinstance(clss, tuple):
|
|
classList.insert(i+1, clss[1])
|
|
classList.insert(i+1, clss[0])
|
|
classList.pop(i)
|
|
|
|
globalErrors = []
|
|
for remainingClass in cfg:
|
|
if remainingClass not in classList:
|
|
globalErrors.append(f'"{remainingClass}" is not a valid class')
|
|
else:
|
|
otherName = findTwin(remainingClass)
|
|
globalErrors.append(f'Conflicting names for section: "{remainingClass}" and "{otherName}"')
|
|
|
|
if len(globalErrors) > 0:
|
|
errors.update( {"file": globalErrors} )
|
|
|
|
if len(errors) > 0:
|
|
verifiedConfig.update({"errors": errors})
|
|
|
|
return verifiedConfig, defaults
|
|
|
|
def validBind(key, data, alias = False) -> list:
|
|
"""Check for valid key and valid binding"""
|
|
ret = []
|
|
if (not alias and not validKey(key)):
|
|
ret.append(f'Invalid key "{key}"')
|
|
|
|
# The values passed to validBindType get mutilated, so a copy must be made
|
|
dataCopy = data.copy()
|
|
dataCopy, errMsgs = validBindType(key, dataCopy)
|
|
if len(errMsgs) > 0:
|
|
for msg in errMsgs:
|
|
ret.append(msg)
|
|
|
|
extras = dataCopy.keys()
|
|
if len(extras) > 0:
|
|
extrasString = "\n ".join(extras)
|
|
ret.append(f'Unused fields in "{key}":\n {extrasString}')
|
|
|
|
return ret
|
|
|
|
validKeyList = [
|
|
'\'', '=', ',', '-', '[', '\\', ']', '`', '.', '/',
|
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
|
|
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
|
|
'u', 'v', 'w', 'x', 'y', 'z',
|
|
'mouse1', 'mouse2', 'mouse3', 'mouse4', 'mouse5',
|
|
'shift', 'capslock', 'ctrl', 'semicolon', 'space', 'enter',
|
|
'backspace',
|
|
'scrolllock', 'numlock',
|
|
'ins', 'home', 'pgup',
|
|
'del', 'end', 'pgdn'
|
|
]
|
|
|
|
def validKey(key):
|
|
"""determines if the key is a valid key"""
|
|
key = str(key).lower()
|
|
return key in validKeyList
|
|
|
|
def validBindType(key, data: dict):
|
|
"""
|
|
Checks if `key` has a valid bind type,
|
|
like 'double' or 'hold'
|
|
"""
|
|
validType = False
|
|
types = [
|
|
"impulse",
|
|
"hold",
|
|
"toggle",
|
|
"double",
|
|
"repeat"
|
|
]
|
|
|
|
errMsgs = []
|
|
for potentialType in data.keys():
|
|
if potentialType in types:
|
|
validType = True
|
|
data, errMsgs = removeRelaventFields(data, potentialType)
|
|
break
|
|
|
|
if not validType:
|
|
errMsgs.append(f'Key "{key}" has no known bind type')
|
|
|
|
return data, errMsgs
|
|
|
|
def removeRelaventFields(data, bindType):
|
|
errMsgs = []
|
|
if "alias" in data: data.pop("alias")
|
|
# These types are simple, just the bind type and argument
|
|
if bindType in ["impulse", "toggle"]:
|
|
data.pop(bindType)
|
|
|
|
elif bindType == "hold":
|
|
content = data.pop("hold")
|
|
if isinstance(content, dict):
|
|
if "press" not in content:
|
|
errMsgs.append("If hold is not a single argument, it requires a `press` argument")
|
|
elif "release" not in content:
|
|
errMsgs.append("If hold is not a single argument, it requires a `release` argument")
|
|
elif not isinstance(content, str):
|
|
errMsgs.append(f"Hold must be either single action or press and release")
|
|
|
|
elif bindType == "double":
|
|
content = data.pop("double")
|
|
if "primary" not in content:
|
|
errMsgs.append("Double requires primary action")
|
|
else:
|
|
# Nasty bit of recursion to validate the action.
|
|
# It takes advantage of `alias = True` not verifying the key,
|
|
# but it isn't an alias, I'm just lazy.
|
|
errMessages = validBind("primary", content["primary"], alias = True)
|
|
if len(errMessages) > 0:
|
|
errMsgs += errMessages
|
|
|
|
if "secondary" not in content:
|
|
errMsgs.append("Double requires secondary action")
|
|
else:
|
|
# Same logic as above
|
|
errMessages = validBind("secondary", content["secondary"], alias = True)
|
|
if len(errMessages) > 0:
|
|
errMsgs += errMessages
|
|
|
|
if "condition" not in content:
|
|
errMsgs.append("Double requires condition to toggle")
|
|
else:
|
|
# Validate the toggler
|
|
key = content["condition"]
|
|
if not validKey(key):
|
|
errMsgs.append(f'Invalid condition to toggle "{key}"')
|
|
|
|
elif bindType == "repeat":
|
|
content = data.pop("repeat")
|
|
|
|
unit = "s"
|
|
if "unit" in content:
|
|
# Set unit if provided
|
|
unitStr = str(content["unit"]).lower()
|
|
if unitStr in ["t", "ticks"]:
|
|
unit = "t"
|
|
elif unitStr not in ["s", "seconds"]:
|
|
# If not seconds or ticks, error
|
|
errMsgs.append(f"Invalid interval unit {unitStr}")
|
|
|
|
if "interval" not in content:
|
|
errMsgs.append("Repeat requires interval")
|
|
else:
|
|
intervalStr = content["interval"]
|
|
if unit == "s":
|
|
interval = float(intervalStr)
|
|
else:
|
|
interval = int(intervalStr)
|
|
if interval <= 0:
|
|
errMsgs.append("Repeat interval must be positive")
|
|
elif interval <= 200/6:
|
|
errMsgs.append(f"Repeat interval must be greater than 1 tick (approx. {200/6:.3f}s)")
|
|
|
|
return data, errMsgs
|
|
|
|
def findTwin(className):
|
|
classDict = {
|
|
"demo": "demoman",
|
|
"engi": "engineer",
|
|
"heavy": "heavyweapons"
|
|
}
|
|
for className1, className2 in classDict.items():
|
|
if className == className1:
|
|
return className2
|
|
elif className == className2:
|
|
return className1
|
|
|
|
return None |