newton_miku
文章11
标签17
分类6
修复Steam快捷方式图标显示错误

修复Steam快捷方式图标显示错误

Steam下载的游戏自动创建的快捷图标有些时候会抽风,变成一张白纸,其实就是图标没有下载成功,手动下载一个就好了。但是,如果只是几个还好,一旦多起来了就很烦。所以编写了以下python脚本实现自动检测与下载(仅支持已经下载且Steam库中能识别到的游戏)

初版脚本

from steam.client import SteamClient
import requests,vdf
import os,winreg
# ***************************************************************
#                 Steam图标修复
#		本工具用于修复Steam图标空白问题
#               @author:newton_miku
#
#	Github:newton_miku	https://github.com/newton-miku
#	BiliBili:鑫说数境	https://space.bilibili.com/24915794
#		My Blog:	https://blog.ddxnb.cn
# ***************************************************************

def read_reg(ep, p = r"", k = ''):
    try:
        key = winreg.OpenKeyEx(ep, p)
        value = winreg.QueryValueEx(key,k)
        if key:
            winreg.CloseKey(key)
        return value[0]
    except Exception as e:
        return None
    return None

print("本脚本由newton_miku制作")
print("Powered by newton_miku")
print("Github:newton_miku	https://github.com/newton-miku")
print("B站:鑫说数境		https://space.bilibili.com/24915794")
print("博客地址:https://blog.ddxnb.cn")
print()
#版权信息

Path1=str(read_reg(ep=winreg.HKEY_LOCAL_MACHINE,p=r"SOFTWARE\Wow6432Node\Valve\Steam",k = 'InstallPath'))
Path2 = str(read_reg(ep = winreg.HKEY_LOCAL_MACHINE, p = r"SOFTWARE\Valve\Steam", k = 'InstallPath'))
pathCtrl=0
Path = ""
if not os.path.exists(Path1):
    Path1 = ""
    pathCtrl += 1
if not os.path.exists(Path2):
    Path2 = ""
    pathCtrl += 10
if pathCtrl == 0 or pathCtrl == 10:
    Path = Path1
elif pathCtrl == 1:
    Path = Path2
else:
    print("无法获取到Steam路径")
    input('请按任意键退出')
    exit()
print("成功获取到Steam路径:"+Path)
if not os.path.exists(Path + '\config\libraryfolders.vdf'):
    print("无法获取到Steam库相关文件,请检查"+Path+'\config\libraryfolders.vdf 文件是否存在')
    input('请按任意键退出')
    exit()
vdfText = vdf.parse(open(Path + '\config\libraryfolders.vdf'))
print("即将连接到Steam数据库")
client = SteamClient()
client.anonymous_login()
assert client.logged_on
print("成功连接到Steam数据库")
#print(vdfText)
for i in vdfText['libraryfolders']:
    for id in vdfText['libraryfolders'][str(i)]['apps']:
        appid = int(id)
        if(appid != 228980):
            apps_info = client.get_product_info(apps=[appid, ])
            #print(apps_info['apps'][appid]['common'])
            pic_name = apps_info['apps'][appid]['common']['clienticon'] #设置文件夹的名字
            url = "http://cdn.cloudflare.steamstatic.com/steamcommunity/public/images/apps/"+str(appid) + "/" + str(pic_name) + ".ico"#拼接网址
            print(url)
            r = requests.get(url)#下载图片
            # 写入图片
            with open(str(Path + "\steam\games\/" + pic_name)+".ico", "wb") as f:
                f.write(r.content)
                f.close()
input('请按任意键退出')

该版脚本所用的Steamclient包安装需要使用

pip install -u steam[client]

使用pyinstaller打包后的脚本曾出现过WebAPI boostrap failed: (‘Connection aborted.’, FileNotFoundError(2, ‘No such file or directory’))报错,暂时不知道如何解决。且使用steamclient包后,打包的程序大小达到了16M,故对该脚本优化后有了下面的版本

优化版脚本

import requests,vdf
import os,winreg
# ***************************************************************
#               Steam图标修复——优化版
#			本工具用于修复Steam图标空白问题
#               @author:newton_miku
#
#	Github:newton_miku	https://github.com/newton-miku
#	BiliBili:鑫说数境	https://space.bilibili.com/24915794
#		My Blog:	https://blog.ddxnb.cn
# ***************************************************************

def read_reg(ep, p = r"", k = ''):
    try:
        key = winreg.OpenKeyEx(ep, p)
        value = winreg.QueryValueEx(key,k)
        if key:
            winreg.CloseKey(key)
        return value[0]
    except Exception as e:
        return None
    return None
    
print("本脚本由newton_miku制作")
print("Powered by newton_miku")
print("Github:newton_miku	https://github.com/newton-miku")
print("B站:鑫说数境		https://space.bilibili.com/24915794")
print("博客地址:https://blog.ddxnb.cn")
print()
#版权信息

Path1=str(read_reg(ep=winreg.HKEY_LOCAL_MACHINE,p=r"SOFTWARE\Wow6432Node\Valve\Steam",k = 'InstallPath'))
Path2 = str(read_reg(ep = winreg.HKEY_LOCAL_MACHINE, p = r"SOFTWARE\Valve\Steam", k = 'InstallPath'))
pathCtrl=0
Path = ""
if not os.path.exists(Path1):
    Path1 = ""
    pathCtrl += 1
if not os.path.exists(Path2):
    Path2 = ""
    pathCtrl += 10
if pathCtrl == 0 or pathCtrl == 10:
    Path = Path1
elif pathCtrl == 1:
    Path = Path2
else:
    print("无法获取到Steam路径")
    input('请按任意键退出')
    exit()
print("成功获取到Steam路径:"+Path)
if not os.path.exists(Path + '\config\libraryfolders.vdf'):
    print("无法获取到Steam库相关文件,请检查"+Path+'\config\libraryfolders.vdf 文件是否存在')
    input('请按任意键退出')
    exit()
vdfText = vdf.parse(open(Path + '\config\libraryfolders.vdf'))
#print(vdfText)
for i in vdfText['libraryfolders']:
    for id in vdfText['libraryfolders'][str(i)]['apps']:
        appid = int(id)
        if(appid != 228980):
            app_info_url = "http://api.steamcmd.net/v1/info/"+str(appid)
            apps_info_data = requests.get(app_info_url)
            times = 0
            while apps_info_data.status_code!=200 or times<4:
                apps_info_data = requests.get(app_info_url)
                times += 1
            if apps_info_data.status_code!=200:
                print("无法获取到appid:"+str(appid)+"的图标名(已重试3次),跳过当前应用")
                time.sleep(0.3)#增加等待时间,避免频繁请求导致的拒绝
                continue
            apps_info = apps_info_data.json()
            common = apps_info['data'][str(appid)]['common']
            pic_name = common['clienticon'] #设置文件夹的名字
            url = "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/"+str(appid) + "/" + str(pic_name) + ".ico"#拼接网址
            
            if 'name_localized' in common:
                if 'schinese' in common['name_localized']:
                    app_name = common['name_localized']['schinese']
                elif 'tchinese' in common['name_localized']['tchinese']:
                    app_name = common['name_localized']['tchinese']
                else:
                    app_name = common['name']
            else:
                app_name = common['name']
                
            if os.path.exists(str(Path + "\steam\games\/" + pic_name)+".ico"):
                print(app_name+" 的图标已存在")
                time.sleep(0.3)#增加等待时间,避免频繁请求导致的拒绝
                continue
            print("正在下载 "+app_name+" 的图标")
            r = requests.get(url)#下载图片
            # 写入图片
            with open(str(Path + "\steam\games\/" + pic_name)+".ico", "wb") as f:
                f.write(r.content)
                f.close()
input('请按任意键退出')

注:代码中的cdn地址可用Akamai,也可用CloudFlare的

该版脚本使用了SteamCMD的API来获取相应app的图标名称,打包后的体积更小

使用前请注意

该脚本已经添加了对于Steam路径的识别,使用的是读取相关注册表,如果注册表中没有那么可能会报错

相较于网上能搜索到的其他解决办法,例如什么手动改图标,或者是steam验证修复游戏,这个脚本能更简单更直接地修复相关错误

打包版本下载地址

蓝奏云下载链接:https://ddxnb.lanzoul.com/imuA40r55ged

密码:e6t4

Githubhttps://github.com/newton-miku/SteamIconFixer

参考资料

steam上下载游戏图标变成空白怎么办? - 知乎

SteamCMD API

本文作者:newton_miku
本文链接:http://blog.ddxnb.cn/2023/03/25/%E4%BF%AE%E5%A4%8DSteam%E5%BF%AB%E6%8D%B7%E6%96%B9%E5%BC%8F%E5%9B%BE%E6%A0%87%E6%98%BE%E7%A4%BA%E9%94%99%E8%AF%AF/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可
本文阅读量:
×