Пример #1
0
 def main(args: Namespace) -> None:
     logging.basicConfig(level=args.log_level)
     hotkeys_config = HotkeysConfigSchema().loadf(args.hotkeys_config_file)
     with ScreenReader(int(get_tibia_wid(args.tibia_pid))) as screen_reader:
         with OcrNumberReader(screen_reader, PyTessBaseAPI()) as ocr_reader:
             finder = AppConfigMemoryAddressFinder(
                 tibia_pid=args.tibia_pid,
                 memory_address_finder=MemoryAddressFinder(
                     ocr_reader, args.tibia_pid),
                 hotkeys_config=hotkeys_config,
                 mana_rect=Rect(
                     args.mana_xy[0],
                     args.mana_xy[1],
                     args.mana_wh[0],
                     args.mana_wh[1],
                 ),
                 speed_rect=Rect(
                     args.speed_xy[0],
                     args.speed_xy[1],
                     args.speed_wh[0],
                     args.speed_wh[1],
                 ),
             )
             app_config = finder.build_app_config_entry()
             print(
                 json.dumps(AppConfigSchema().dump(app_config),
                            indent=4,
                            sort_keys=True))
Пример #2
0
 def main(args: Namespace) -> None:
     logging.basicConfig(level=args.log_level)
     with ScreenReader(int(get_tibia_wid(args.tibia_pid))) as screen_reader:
         with OcrNumberReader(screen_reader, PyTessBaseAPI()) as ocr_reader:
             f = MemoryAddressFinder(ocr_reader, args.tibia_pid)
             addresses, _ = f.find_address(
                 args.update_keys,
                 Rect(args.x, args.y, args.width, args.height),
                 c_int,
             )
             for address in addresses:
                 value = f.read_memory(address, c_int)
                 print(f"Address: {hex(address)}")
                 print(f"Value: {value}")
Пример #3
0
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()
Пример #4
0
    def main(args: Namespace):
        tibia_wid = get_tibia_wid(args.tibia_pid)
        with ScreenReader(int(tibia_wid)) as screen_reader:
            with OcrNumberReader(screen_reader, PyTessBaseAPI()) as ocr_reader:
                times = []
                text = None
                rect = Rect(
                    x=args.coords[0],
                    y=args.coords[1],
                    width=args.width,
                    height=args.height,
                )
                # warm-up
                logger.info("Performing warm-up")
                for _ in range(10):
                    text = ocr_reader.read_number(rect)

                logger.info("Reading %s samples", args.samples)
                for _ in range(args.samples):
                    start = time.time()
                    text = ocr_reader.read_number(rect)
                    end = time.time()
                    times.append(end - start)

                if args.show_image:
                    (_, image) = ocr_reader.read_number(rect, True)
                    image.show()

                times.sort()
                avg_ms = sum(times) * 1000 / len(times)
                max_ms = times[-1] * 1000
                min_ms = times[0] * 1000
                median_ms = percentile(times, 0.5)
                p99_ms = percentile(times, 0.99)
                p95_ms = percentile(times, 0.95)
                p75_ms = percentile(times, 0.75)
                p25_ms = percentile(times, 0.25)
                p90_ms = percentile(times, 0.90)
                p01_ms = percentile(times, 0.01)
                p05_ms = percentile(times, 0.05)
                p10_ms = percentile(times, 0.10)

                print(f"samples: {args.samples}")
                print(f"avg: {avg_ms:.1f} median: {median_ms:.1f}")
                print(f"max: {max_ms:.1f} min: {min_ms:.1f}")
                print(f"p99: {p99_ms:.1f} p95: {p95_ms:.1f} p90: {p90_ms:.1f}")
                print(f"p75: {p75_ms:.1f} p50: {median_ms:.1f} p25: {p25_ms:.1f}")
                print(f"p10: {p10_ms:.1f} p05: {p05_ms:.1f} p01: {p01_ms:.1f}")
                print(f"Read text: {text}")
def print_item_spec(
    tibia_pid: int,
    tibia_window_config_path: str,
    equipment_type: str,
    colored_output: bool,
    ring_name: Optional[str],
    amulet_name: Optional[str],
) -> None:
    tibia_wid = int(get_tibia_wid(tibia_pid, 0))
    schema = TibiaWindowSpecSchema()
    tibia_window_spec = schema.loadf(tibia_window_config_path)
    repository_spec = generate_repository_spec(tibia_wid, tibia_window_spec,
                                               equipment_type, ring_name,
                                               amulet_name)
    print_repository_spec(repository_spec, tibia_window_config_path,
                          colored_output)
def main(tibia_pid,
         credentials_user,
         credentials_path,
         only_check=False,
         login=False,
         max_wait=120):
    """Main entry point of the program."""
    tibia_wid = get_tibia_wid(tibia_pid)
    if only_check:
        handle_check(tibia_wid)
    if login:
        if credentials_path is None:
            raise Exception(("A path to a credentials json file is required.\n"
                             "See example: example.credentials.json"))
        if credentials_user is None:
            raise Exception("We require a user profile to login.")

        credential = load_credential(credentials_user, credentials_path)
        handle_login(tibia_wid, credential, max_wait)
def find_memory_addresses(args: Namespace, app_configs: AppConfigs) -> AppConfigs:
    hotkeys_config = HotkeysConfigSchema().loadf(args.hotkeys_config)
    tibia_window_config = TibiaWindowSpecSchema().loadf(args.tibia_window_config)
    with ScreenReader(tibia_wid=int(get_tibia_wid(args.pid))) as screen_reader:
        with OcrNumberReader(
            screen_reader=screen_reader,
            ocr_api=PyTessBaseAPI(),
        ) as ocr_reader:
            app_config_memory_address_finder = AppConfigMemoryAddressFinder(
                tibia_pid=args.pid,
                memory_address_finder=MemoryAddressFinder(
                    tibia_pid=args.pid, ocr_reader=ocr_reader
                ),
                hotkeys_config=hotkeys_config,
                mana_rect=tibia_window_config.stats_fields.mana_field,
                speed_rect=tibia_window_config.stats_fields.speed_field,
            )
            built_config = app_config_memory_address_finder.build_app_config_entry()
            new_configs = list(
                config if config.pid != args.pid else built_config
                for config in app_configs.configs
            )
            return app_configs._replace(configs=new_configs)
Пример #8
0
    def find_address(
        self,
        update_keys: List[str],
        text_field_rectangle: Rect,
        ctype_ctor: Callable = c_int,
        *,
        initial_address_space: Optional[List[int]] = None,
        stop_gap_matches: int = 1,
        verbatim: bool = True,
        only_search_heap: bool = True,
    ) -> Tuple[List[int], Rect]:
        def mem_region_filter(mem_region: MemRegion) -> bool:
            if only_search_heap:
                return (mem_region.region_type is MemRegionType.HEAP
                        or "[heap]" in mem_region.filename)
            return True

        with ReadOnlyProcess(self.tibia_pid) as proc:
            prev_value = None
            tibia_wid = int(get_tibia_wid(self.tibia_pid))
            value, _, __ = self.read_ocr_value(text_field_rectangle,
                                               prev_value)
            if initial_address_space is None:
                # never search all of the memory with verbatim False
                addresses = proc.search_all_memory(
                    ctype_ctor(value),
                    writeable_only=True,
                    verbatim=True,
                    mem_region_filter=mem_region_filter)
            else:
                use_verbatim = verbatim or len(addresses) > 250
                addresses = proc.search_addresses(addresses,
                                                  ctype_ctor(value),
                                                  verbatim=use_verbatim)

            logger.info("OCR Scan of %s: %s", text_field_rectangle, value)
            logger.info("Found %s matching.", len(addresses))

            for i, update_key in enumerate(update_keys):
                if len(addresses) <= stop_gap_matches:
                    return (addresses, text_field_rectangle)

                if i > 0:
                    time.sleep(0.75)  # Wait for next key press
                logger.info("Sending key %s to window %s", update_key,
                            tibia_wid)
                send_key(tibia_wid, update_key)
                time.sleep(0.25)  # Wait for the memory to update
                new_value, should_discard_value, new_rect = self.read_ocr_value(
                    text_field_rectangle, value)
                # look for the value *immediately*
                if should_discard_value:
                    logger.warning(
                        "OCR had poor results, ignoring value read (%s).",
                        new_value)
                else:
                    prev_value = value
                    value = new_value
                    text_field_rectangle = new_rect
                    logger.info("Searching %s addresses.", len(addresses))
                    addresses = proc.search_addresses(addresses,
                                                      ctype_ctor(value))
                    logger.info("OCR Scan of %s: %s", text_field_rectangle,
                                value)
                    logger.info("Found %s matching.", len(addresses))
            return (addresses, text_field_rectangle)
Пример #9
0
def curses_main(
    cliwin,
    pid,
    app_config: AppConfig,
    char_configs: List[CharConfig],
    tibia_window_spec: TibiaWindowSpec,
    hotkeys_config: HotkeysConfig,
    enable_mana: bool,
    enable_hp: bool,
    enable_magic_shield: bool,
    enable_speed: bool,
    only_monitor: bool,
    x_offset: int = 0,
):
    tibia_wid = get_tibia_wid(pid)
    window_geometry = get_window_geometry(tibia_wid)
    x_offset = x_offset or window_geometry.x
    stats_logger = StatsLogger()

    def print_async(obj: Any) -> None:
        stats_logger.log_action(2, str(obj))

    view_renderer = ViewRenderer(cliwin)
    cmd_processor = CommandProcessor(tibia_wid, stats_logger, only_monitor)
    xdotool_proc = XdotoolProcess()
    xdotool_proc.start()
    try:
        client = ClientInterface(
            hotkeys_config,
            logger=stats_logger,
            cmd_processor=cmd_processor,
            keystroke_sender=XdotoolKeystrokeSender(xdotool_proc, tibia_wid),
        )
        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(tibia_wid=int(tibia_wid),
                                    tibia_window_spec=tibia_window_spec)
        loot_macro = LootMacro(client, hotkeys_config, x_offset)
        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()
    finally:
        xdotool_proc.stop()