LittleNyima / honkai-starrail-toolkit

《崩坏:星穹铁道》工具箱,支持抽卡分析、帧率解锁等功能
https://pypi.org/project/starrail-toolkit/
GNU General Public License v3.0
125 stars 4 forks source link

关于autodet.py的一点修改意见 #10

Closed bio-punk closed 7 months ago

bio-punk commented 7 months ago

get_cache_path函数,米哈游偷偷改了cache文件的存储路径规则,改为按照版本号生成,建议改成

def get_cache_path(game_install_path):
    from functools import cmp_to_key
    def cmp(ver_a: str, ver_b: str):
        ver_a = ver_a.split('.')
        ver_b = ver_b.split('.')
        ver_a = [int(a) for a in ver_a]
        ver_b = [int(b) for b in ver_b]
        length = min(len(ver_a), len(ver_b))
        for i in range(length):
            if ver_a[i] != ver_b[i]:
                return ver_b[i] - ver_a[i]
        return len(ver_b) - len(ver_a)
    logger.info('Getting gacha query cache path')
    cache_path = os.path.join(
        game_install_path,
        'StarRail_Data', 'webCaches'
    )
    maybe_dirs = os.listdir(cache_path)
    # 排序,要版本号大的在前
    maybe_dirs = sorted(maybe_dirs, key=cmp_to_key(cmp))
    cache_path = os.path.join(
        cache_path,
        maybe_dirs[0],
        'Cache',
        'Cache_Data',
        'data_2'
    )

    if not os.path.exists(cache_path):
        error_msg = (
            f'Cache path {cache_path} is not existing. Please visit '
            'the gacha querying page before exporting gacha data.'
        )
        logger.error(error_msg)
    return cache_path

关于windows上无法打开被占用文件的问题,建议改成robocopy来复制,get_api_from_cache建议改成

def get_api_from_cache(cache_path):
    def get_api_form_cache_windows(cache_path):
        tmpdir_path='{}\\tmp'.format(os.path.abspath('.'))
        try:
            if os.path.exists(tmpdir_path) and os.path.isfile(tmpdir_path):
                tmpdir_path='{}{:0>4d}'.format(tmpdir_path, int(time.time()) % 10000)
                os.mkdir(tmpdir_path)
            if not os.path.exists(tmpdir_path):
                os.mkdir(tmpdir_path)
        except BaseException as E:
            logger.error('TODO20240413:{}'.format(E))
        else:
            cmd = 'robocopy "{}" "{}" data_2'.format(os.path.dirname(cache_path), tmpdir_path)
            res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            res.wait(5)
            assert 0==res.returncode, res.stderr.read()
            cache_path='{}\\data_2'.format(tmpdir_path)
            logger.info('Creating tmp file ok')
            return cache_path

    logger.info('Getting api URL from cache')

    import os, time, subprocess
    if os.name.startswith('nt'):
        cache_path = get_api_form_cache_windows(cache_path)
    with open(cache_path, 'rb') as f_cache:
        cache = f_cache.read()
    parts = cache.split(b'1/0/')
    parts = [part.split(b'\x00')[0].decode(errors='ignore') for part in parts]
    parts = map(get_url_from_text, parts)
    parts = list(filter(bool, parts))
    if not parts:
        error_msg = (
            'API URL is not found in cache. Please visit the gacha querying '
            'page before exporting gacha data.'
        )
        logger.error(error_msg)
        return ''
    return get_latest_url(parts)

在python3.9,崩铁2.22,windows10上测试了以上修改是ok的,希望版本能够更新一下,谢谢,懒得提merge请求了

LittleNyima commented 7 months ago

感谢反馈,我最近会再更新一版

LittleNyima commented 7 months ago

已经参考进行了修改并发布新版本