tooncher 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import yaml
  5. import tooncher
  6. import tooncher.controls
  7. def run(username, config_path, engine_path=None, validate_ssl_certs=True,
  8. cpu_limit_percent=None, enable_extended_keyboard_controls=False,
  9. extended_keyboard_control_toggle_keysym_name=None,
  10. engine_window_name=None):
  11. if os.path.exists(config_path):
  12. with open(config_path) as f:
  13. config = yaml.safe_load(f.read())
  14. else:
  15. config = {}
  16. if engine_path is None:
  17. if 'engine_path' in config:
  18. engine_path = config['engine_path']
  19. else:
  20. raise Exception('missing path to toontown engine')
  21. accounts = config['accounts'] if 'accounts' in config else []
  22. for account in accounts:
  23. if account['username'] == username:
  24. tooncher.launch(
  25. engine_path=engine_path,
  26. username=account['username'],
  27. password=account['password'],
  28. validate_ssl_certs=validate_ssl_certs,
  29. cpu_limit_percent=cpu_limit_percent,
  30. enable_extended_keyboard_controls=enable_extended_keyboard_controls,
  31. extended_keyboard_control_toggle_keysym_name=extended_keyboard_control_toggle_keysym_name,
  32. engine_window_name=engine_window_name,
  33. )
  34. def _init_argparser():
  35. import argparse
  36. argparser = argparse.ArgumentParser(description=None)
  37. argparser.add_argument('username')
  38. argparser.add_argument(
  39. '--config',
  40. '-c',
  41. metavar='path',
  42. dest='config_path',
  43. help='path to config file (default: %(default)s)',
  44. default=os.path.join(os.path.expanduser('~'), '.tooncher'),
  45. )
  46. argparser.add_argument(
  47. '--engine-path',
  48. '-e',
  49. metavar='path',
  50. dest='engine_path',
  51. default=tooncher.TOONTOWN_ENGINE_DEFAULT_PATH,
  52. help='\n'.join([
  53. 'path to toontown engine.',
  54. 'this overrides the one specified in config file',
  55. '(default: %(default)s)',
  56. ]),
  57. )
  58. argparser.add_argument(
  59. '--no-ssl-cert-validation',
  60. '-k',
  61. dest='validate_ssl_certs',
  62. help='do not validate ssl certificates',
  63. action='store_false',
  64. )
  65. argparser.add_argument(
  66. '--cpu-limit',
  67. dest='cpu_limit_percent',
  68. type=int,
  69. default=None,
  70. help='maximally allowed cpu usage in percent'
  71. + ' (requires cpulimit command, default: %(default)s)',
  72. )
  73. argparser.add_argument(
  74. '--extended-controls',
  75. dest='enable_extended_keyboard_controls',
  76. action='store_true',
  77. help='enable extended keyboard controls'
  78. + ', e.g. walk with WASD'
  79. + ' (requires xlib for python, default: %(default)s)',
  80. )
  81. argparser.add_argument(
  82. '--extended-controls-toggle',
  83. metavar='KEYSYM_NAME',
  84. dest='extended_keyboard_control_toggle_keysym_name',
  85. default=tooncher.controls.EXTENDED_CONTROLS_DEFAULT_TOGGLE_KEYSYM_NAME,
  86. help='key to turn extended keyboard controls on / off.'
  87. + ' any keysym name may be used'
  88. + ' (see XStringToKeysym & X11/keysymdef.h, '
  89. + ' default: %(default)s)',
  90. )
  91. argparser.add_argument(
  92. '--change-window-name',
  93. metavar='ENGINE_WINDOW_NAME',
  94. dest='engine_window_name',
  95. default=None,
  96. help='Change window name of engine after launch.'
  97. + ' This requires Extended Controls to be enabled.'
  98. + ' (default: no change)',
  99. )
  100. return argparser
  101. def main(argv):
  102. argparser = _init_argparser()
  103. args = argparser.parse_args(argv)
  104. run(**vars(args))
  105. return 0
  106. if __name__ == "__main__":
  107. sys.exit(main(sys.argv[1:]))