def test_get_by_pid(self): # given data = {"default_pid": 456, "configs": [{"pid": 123}, {"pid": 456}]} target = AppConfigsSchema() app_configs = target.load(data) # when for data_config in data["configs"]: actual = app_configs[data_config["pid"]] # then self.assertEqual(actual.pid, data_config["pid"])
def test_loads_minimal(self): # given data = {"default_pid": 456, "configs": [{"pid": 123}, {"pid": 456}]} target = AppConfigsSchema() # when app_configs = target.load(data) # then self.assertIsInstance(app_configs, AppConfigs) self.assertEqual(len(app_configs.configs), 2) for i in range(2): self.assertIsInstance(app_configs.configs[i], AppConfig) self.assertEqual(app_configs.configs[i].pid, data["configs"][i]["pid"])
def test_loads_maximal(self): # given data = { "default_pid": 456, "configs": [{ "pid": 123, "mana_memory_address": "1231", "speed_memory_address": "12312", "soul_points_memory_address": "123123", "hp_memory_address": "1231231", "magic_shield_memory_address": "12312312", "max_mana_address": "123123123", "max_hp_address": "1231231231" }, { "pid": 456, "mana_memory_address": "4564", "speed_memory_address": "45645", "soul_points_memory_address": "456456", "hp_memory_address": "4564564", "magic_shield_memory_address": "45645645", "max_mana_address": "456456456", "max_hp_address": "4564564564" }] } target = AppConfigsSchema() # when app_configs = target.load(data) # then self.assertIsInstance(app_configs, AppConfigs) self.assertEqual(len(app_configs.configs), 2) for i in range(2): self.assertIsInstance(app_configs.configs[i], AppConfig) self.assertEqual(app_configs.configs[i].pid, data["configs"][i]["pid"]) self.assertEqual(app_configs.configs[i].mana_memory_address, data["configs"][i]["mana_memory_address"]) self.assertEqual(app_configs.configs[i].speed_memory_address, data["configs"][i]["speed_memory_address"]) self.assertEqual(app_configs.configs[i].soul_points_memory_address, data["configs"][i]["soul_points_memory_address"]) self.assertEqual(app_configs.configs[i].hp_memory_address, data["configs"][i]["hp_memory_address"]) self.assertEqual( app_configs.configs[i].magic_shield_memory_address, data["configs"][i]["magic_shield_memory_address"]) self.assertEqual(app_configs.configs[i].max_mana_address, data["configs"][i]["max_mana_address"]) self.assertEqual(app_configs.configs[i].max_hp_address, data["configs"][i]["max_hp_address"])
def main(cliwin, pid, app_config_path: str, char_configs_path: str, enable_mana: bool, enable_hp: bool, enable_magic_shield: bool, enable_speed: bool, only_monitor: bool): if pid is None or pid == "": raise Exception("PID is required, you may use psgrep -a -l bin/Tibia " "to find the process id") app_configs_schema = AppConfigsSchema() app_configs = app_configs_schema.loadf(app_config_path) app_config = app_configs[str(pid)] tibia_wid = get_tibia_wid(pid) stats_logger = StatsLogger() def print_async(msg): stats_logger.log_action(2, msg) view_renderer = ViewRenderer(cliwin) cmd_processor = CommandProcessor(tibia_wid, stats_logger, only_monitor) hotkeys_config = HotkeysConfigSchema().loadf( os.path.join(char_configs_path, 'hotkeys_config.json')) char_configs = list(load_configs(char_configs_path)) if len(char_configs) == 0: print(f"No .charconfig files found in {char_configs_path}", file=sys.stderr) sys.exit(1) client = ClientInterface(hotkeys_config, logger=stats_logger, cmd_processor=cmd_processor) char_keeper = CharKeeper(client, char_configs[0], char_configs[0].battle_configs[0], hotkeys_config) char_reader = CharReader(MemoryReader(pid, print_async)) eq_reader = EquipmentReader() loot_macro = LootMacro(client, hotkeys_config) tibia_terminator = TibiaTerminator(tibia_wid, char_keeper, char_reader, eq_reader, app_config, char_configs, cliwin, loot_macro, stats_logger, view_renderer, cmd_processor, enable_mana=enable_mana, enable_hp=enable_hp, enable_magic_shield=enable_magic_shield, enable_speed=enable_speed, only_monitor=only_monitor) tibia_terminator.monitor_char()
def main(args: Namespace): set_debug_level(args.debug_level) if args.debug_level > 1: print(args) if not args.pid: raise Exception("PID is required, you may use psgrep -a -l bin/Tibia " "to find the process id") app_configs_schema = AppConfigsSchema() app_configs = app_configs_schema.loadf(args.app_config_path) app_config = app_configs[str(args.pid)] if not app_configs[str(args.pid)]: raise Exception( f"App config for PID: {args.pid} not configured. Available" f" PIDs: {[c.pid for c in app_configs.configs]}") hotkeys_config = HotkeysConfigSchema().loadf( os.path.join(args.char_configs_path, "hotkeys_config.json")) char_configs = list(load_configs(args.char_configs_path)) if len(char_configs) == 0: raise Exception( f"No .charconfig files found in {args.char_configs_path}") tibia_window_spec_schema = TibiaWindowSpecSchema() tibia_window_spec = tibia_window_spec_schema.loadf( args.tibia_window_config_path) curses.wrapper( curses_main, args.pid, app_config, char_configs, tibia_window_spec, hotkeys_config, enable_mana=not args.no_mana, enable_hp=not args.no_hp, enable_magic_shield=not args.no_magic_shield, enable_speed=not args.no_speed, only_monitor=args.only_monitor, x_offset=args.x_offset, )
def test_cleans_memory_address(self): # given data = { "default_pid": 456, "configs": [{ "pid": 123, "mana_memory_address": "0x1231", "speed_memory_address": "0x12312", "soul_points_memory_address": "0x123123", "hp_memory_address": "0x1231231", "magic_shield_memory_address": "0xabcdef", "max_mana_address": "0x0123456789abcdef", "max_hp_address": "0x0" }] } target = AppConfigsSchema() # when app_configs = target.load(data) # then self.assertIsInstance(app_configs.configs[0], AppConfig) self.assertEqual(app_configs.configs[0].pid, data["configs"][0]["pid"]) self.assertEqual(app_configs.configs[0].mana_memory_address, data["configs"][0]["mana_memory_address"][2:]) self.assertEqual(app_configs.configs[0].speed_memory_address, data["configs"][0]["speed_memory_address"][2:]) self.assertEqual(app_configs.configs[0].soul_points_memory_address, data["configs"][0]["soul_points_memory_address"][2:]) self.assertEqual(app_configs.configs[0].hp_memory_address, data["configs"][0]["hp_memory_address"][2:]) self.assertEqual(app_configs.configs[0].magic_shield_memory_address, data["configs"][0]["magic_shield_memory_address"][2:]) self.assertEqual(app_configs.configs[0].max_mana_address, data["configs"][0]["max_mana_address"][2:]) self.assertEqual(app_configs.configs[0].max_hp_address, data["configs"][0]["max_hp_address"][2:])
reader.init_magic_shield_address() if soul_points_address is not None: reader.init_soul_points_address(int(soul_points_address, 16)) if magic_shield_address is not None: reader.init_magic_shield_address(int(magic_shield_address, 16)) if max_hp_address is not None: reader.init_max_hp_address(int(max_hp_address, 16)) if max_mana_address is not None: reader.init_max_mana_address(int(max_mana_address, 16)) stats = reader.get_stats().get() print(f"HP={stats['hp']};MANA={stats['mana']};SPEED={stats['speed']};" f"SOUL_POINTS={stats['soul_points']};" f"MAGIC_SHIELD={stats['magic_shield']};" f"MAX_MANA={reader.get_max_mana()};MAX_HP={reader.get_max_hp()}") if __name__ == "__main__": args = parser.parse_args() pid = args.pid or MEM_CONFIG['default_pid'] app_config_schema = AppConfigsSchema() configs = app_config_schema.loadf(args.app_config_path) config = configs[pid] main(pid, args.mana_address or config.mana_memory_address, args.hp_address or config.hp_memory_address, args.magic_shield_address or config.magic_shield_memory_address, args.speed_address or config.speed_memory_address, args.soul_points_address or config.soul_points_memory_address, args.max_hp_address or config.max_hp_address, args.max_mana_address or config.max_mana_address, args.verbose)
def read_app_configs(app_config_file: str) -> AppConfigs: return AppConfigsSchema().loadf(app_config_file)
def write_app_config(app_configs: AppConfigs, app_config_file: str) -> None: with open(app_config_file, mode="w", encoding="utf-8") as fp: json.dump(AppConfigsSchema().dump(app_configs), fp, indent=4, sort_keys=True)