use loaders for keybindings and charmaps which allow them to import templates

This commit is contained in:
troido 2019-01-18 13:08:05 +01:00
parent 28a622a388
commit e912bae21a
3 changed files with 52 additions and 5 deletions

View File

@ -0,0 +1,44 @@
import os
from .paths import keybindingsPath
import json
standardKeyFiles = {
"default": os.path.join(keybindingsPath, "keybindings.json"),
"azerty": os.path.join(keybindingsPath, "azerty.json")
}
def loadKeybindings(name):
fname = None
if name in standardKeyFiles:
fname = standardKeyFiles[name])
else:
fname = name
with open(fname) as f:
data = json.load(f)
bindings = {}
for template in data.get(templates, []):
if template.partition(os.sep)[0] in {".", ".."}:
template = os.path.relpath(template, fname)
bindings.update(loadKeybindings(template))
bindings.update(data["actions"])
return (bindings, data["help"])
def loadCharmap(name):
fname = None
if name in standardKeyFiles:
fname = standardKeyFiles[name])
else:
fname = name
with open(fname) as f:
data = json.load(f)
bindings = {}
for template in data.get(templates, []):
if template.partition(os.sep)[0] in {".", ".."}:
template = os.path.relpath(template, fname)
bindings.update(loadKeybindings(template))
bindings.update(data["actions"])
return (bindings, data["help"])

View File

@ -8,11 +8,8 @@ import os
import os.path
from .start import main as clientmain
from .paths import keybindingsPath, charmapPath
thisPath = os.path.dirname(__file__)
farmsPath = os.path.join(thisPath, "..")
charMapPath = os.path.join(farmsPath, "charmaps")
keybindingsPath = os.path.join(farmsPath, "keybindings")
standardCharFiles = [name[:-5] for name in os.listdir(charMapPath) if name[-5:] == ".json"]
standardKeyFiles = [name[:-5] for name in os.listdir(keybindingsPath) if name[-5:] == ".json"]
@ -35,7 +32,7 @@ def main(argv=None):
parser.add_argument('-n', '--name', help='Your player name (must be unique!). Defaults to username on inet sockets and tildename on (unix socket (including abstract)', default=None)
parser.add_argument("-a", "--address", help="The address of the socket. When the socket type is 'abstract' this is just a name. When it is 'unix' this is a filename. When it is 'inet' is should be in the format 'address:port', eg 'localhost:8080'. Defaults depends on the socket type")
parser.add_argument("-s", "--socket", help="the socket type. 'unix' is unix domain sockets, 'abstract' is abstract unix domain sockets and 'inet' is inet sockets. ", choices=["abstract", "unix", "inet"], default="abstract")
parser.add_argument('-k', '--keybindings', help='The file with the keybinding configuration. This file is a JSON file.', default="keybindings")
parser.add_argument('-k', '--keybindings', help='The file with the keybinding configuration. This file is a JSON file.', default="default")
parser.add_argument('-c', '--characters', help='The file with the character mappings for the graphics. If it is either of these names: {} it will be loaded from the charmaps directory.'.format(standardCharFiles), default="default")
parser.add_argument('-o', '--logfile', help='All game messages will be written to this file.'.format(standardCharFiles), default=None)

View File

@ -0,0 +1,6 @@
clientPath = os.path.dirname(__file__)
farmsPath = os.path.join(clientPath, "..")
charMapPath = os.path.join(farmsPath, "charmaps")
keybindingsPath = os.path.join(farmsPath, "keybindings")