|
本帖最后由 209154 于 2025-7-13 08:20 编辑
update_hosts_cached.vdf 文件是 Steam/蒸汽平台客户端用于获取更新文件的服务器列表缓存。
如果没有该文件,Steam/蒸汽平台客户端将会通过默认服务器(目前为“cdn.steamstatic.com”)更新客户端文件。
这意味着用户在首次安装并运行 Steam/蒸汽平台客户端时,仅能通过默认服务器更新客户端文件。
而生成 update_hosts_cached.vdf 文件后,用户可以通过连接效果更好的本区域服务器来获取更新文件。
PowerShell 脚本,点击显示 - # 定义 API 端点
- $urls = @(
- "https://api.steamchina.com/IContentServerDirectoryService/GetClientUpdateHosts/v1/?format=json",
- "https://api.steampowered.com/IContentServerDirectoryService/GetClientUpdateHosts/v1/?format=json"
- )
- # 尝试获取 API 数据
- $response = $null
- foreach ($url in $urls) {
- try {
- Write-Host "尝试从 $url 获取数据..."
- $response = Invoke-RestMethod -Uri $url -ErrorAction Stop
- if ($response) {
- Write-Host "成功从 $url 获取数据" -ForegroundColor Green
- break
- }
- } catch {
- Write-Host "尝试 $url 失败: $($_.Exception.Message)" -ForegroundColor Yellow
- }
- }
- if (-not $response) {
- Write-Host "所有 API 端点均不可用" -ForegroundColor Red
- exit 1
- }
- # 提取并转换数据
- $hosts_kv = $response.response.hosts_kv -replace '"hosts"', '"Hosts"'
- $valid_until = $response.response.valid_until_time
- $ip_country = $response.response.ip_country
- # 构建完整 VDF 内容
- $vdfContent = @"
- ""
- {
- "ServerList"
- {
- $($hosts_kv)
- }
- "ClientInfo"
- {
- "valid_until" "$valid_until"
- "ip_country_code" "$ip_country"
- }
- }
- "@
- # 确定保存路径
- $outputPath = "update_hosts_cached.vdf"
- $steamPath = $null
- if ($env:OS -like "Windows*") {
- # 1. 尝试注册表路径
- try {
- $steamPath = (Get-ItemProperty -Path "HKCU:\Software\Valve\Steam" -Name "SteamPath" -ErrorAction Stop).SteamPath
- Write-Host "检测到注册表 Steam 路径: $steamPath" -ForegroundColor Cyan
- $outputPath = Join-Path -Path $steamPath -ChildPath "update_hosts_cached.vdf"
- } catch {
- Write-Host "注册表读取失败: $($_.Exception.Message)" -ForegroundColor Yellow
- }
-
- # 2. 如果注册表路径不存在,尝试标准 Steam 路径
- if (-not $steamPath) {
- $standardPaths = @(
- "C:\Program Files (x86)\Steam",
- "C:\Program Files\Steam"
- )
-
- foreach ($path in $standardPaths) {
- if (Test-Path -Path $path) {
- $steamPath = $path
- Write-Host "检测到标准 Steam 路径: $steamPath" -ForegroundColor Cyan
- $outputPath = Join-Path -Path $steamPath -ChildPath "update_hosts_cached.vdf"
- break
- }
- }
- }
-
- # 3. 如果标准路径不存在,尝试蒸汽平台路径
- if (-not $steamPath) {
- $steamChinaPaths = @(
- "C:\Program Files (x86)\SteamChina",
- "C:\Program Files\SteamChina"
- )
-
- foreach ($path in $steamChinaPaths) {
- if (Test-Path -Path $path) {
- $steamPath = $path
- Write-Host "检测到蒸汽平台路径: $steamPath" -ForegroundColor Cyan
- $outputPath = Join-Path -Path $steamPath -ChildPath "update_hosts_cached.vdf"
- break
- }
- }
- }
-
- # 4. 如果所有路径都不存在,使用当前目录
- if (-not $steamPath) {
- Write-Host "未检测到 Steam 安装路径,使用当前目录保存文件" -ForegroundColor Yellow
- }
- } else {
- Write-Host "非 Windows 系统,使用当前目录保存文件" -ForegroundColor Cyan
- }
- # 保存文件
- try {
- $vdfContent | Out-File -FilePath $outputPath -Encoding UTF8
- Write-Host "文件已保存到: $outputPath" -ForegroundColor Green
- } catch {
- Write-Host "文件保存失败: $($_.Exception.Message)" -ForegroundColor Red
- exit 1
- }
- # 转换有效期时间戳
- try {
- $epoch = [DateTime]::new(1970, 1, 1, 0, 0, 0, 0, [DateTimeKind]::Utc)
- $expireDate = $epoch.AddSeconds($valid_until).ToLocalTime().ToString('yyyy-MM-dd HH:mm:ss')
- } catch {
- $expireDate = "转换失败 ($valid_until)"
- }
- # 输出结果
- Write-Host ("=" * 50) -ForegroundColor Green
- Write-Host "Steam 客户端更新服务器配置已成功更新!" -ForegroundColor Green
- Write-Host "配置文件已保存至: $outputPath" -ForegroundColor Cyan
- Write-Host "有效期至: $expireDate" -ForegroundColor Cyan
- Write-Host "当前区域代码: $ip_country" -ForegroundColor Cyan
- Write-Host ("=" * 50) -ForegroundColor Green
- Write-Host "该脚本由清靈語 & DeepSeek 创建,通过 PowerShell 驱动" -ForegroundColor Magenta
- Write-Host "请合法合规地使用脚本!" -ForegroundColor Magenta
- Write-Host ("=" * 50) -ForegroundColor Green
复制代码
新建一个文本文件,将以上代码复制粘贴进文本文件后保存文件,并将文件重命名为“update_hosts_cache.ps1”。
Python 脚本,点击显示 - #!/usr/bin/env python3
- import json
- import os
- import sys
- import requests
- import datetime
- import platform
- # 定义 API 端点
- URLS = [
- "https://api.steamchina.com/IContentServerDirectoryService/GetClientUpdateHosts/v1/?format=json",
- "https://api.steampowered.com/IContentServerDirectoryService/GetClientUpdateHosts/v1/?format=json"
- ]
- def get_steam_path():
- """获取 Steam 安装路径 (Windows 系统)"""
- steam_path = None
-
- if platform.system() == "Windows":
- # 1. 尝试注册表路径
- try:
- import winreg
- with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Valve\Steam") as key:
- steam_path = winreg.QueryValueEx(key, "SteamPath")[0]
- print(f"检测到注册表 Steam 路径: {steam_path}")
- except Exception as e:
- print(f"注册表读取失败: {e}")
-
- # 2. 如果注册表路径不存在,尝试标准 Steam 路径
- if not steam_path:
- standard_paths = [
- r"C:\Program Files (x86)\Steam",
- r"C:\Program Files\Steam"
- ]
-
- for path in standard_paths:
- if os.path.exists(path):
- steam_path = path
- print(f"检测到标准 Steam 路径: {steam_path}")
- break
-
- # 3. 如果标准路径不存在,尝试蒸汽平台路径
- if not steam_path:
- steam_china_paths = [
- r"C:\Program Files (x86)\SteamChina",
- r"C:\Program Files\SteamChina"
- ]
-
- for path in steam_china_paths:
- if os.path.exists(path):
- steam_path = path
- print(f"检测到蒸汽平台路径: {steam_path}")
- break
-
- return steam_path
- def main():
- # 尝试获取 API 数据
- response_data = None
- for url in URLS:
- try:
- print(f"尝试从 {url} 获取数据...")
- response = requests.get(url, timeout=10)
- response.raise_for_status()
- response_data = response.json()
- print(f"成功从 {url} 获取数据")
- break
- except requests.RequestException as e:
- print(f"尝试 {url} 失败: {e}")
-
- if not response_data:
- print("所有 API 端点均不可用")
- sys.exit(1)
-
- # 提取并转换数据
- try:
- resp = response_data["response"]
- hosts_kv = resp["hosts_kv"].replace('"hosts"', '"Hosts"')
- valid_until = resp["valid_until_time"]
- ip_country = resp["ip_country"]
- except KeyError as e:
- print(f"JSON 解析错误: 缺少关键字段 {e}")
- sys.exit(1)
-
- # 构建完整 VDF 内容
- vdf_content = f'''""
- {{
- "ServerList"
- {{
- {hosts_kv}
- }}
- "ClientInfo"
- {{
- "valid_until" "{valid_until}"
- "ip_country_code" "{ip_country}"
- }}
- }}'''
-
- # 确定保存路径
- output_path = "update_hosts_cached.vdf"
- steam_path = get_steam_path()
- if steam_path:
- output_path = os.path.join(steam_path, "update_hosts_cached.vdf")
- print(f"使用 Steam 路径: {output_path}")
- else:
- print("使用当前目录保存文件")
-
- # 保存文件
- try:
- with open(output_path, "w", encoding="utf-8") as f:
- f.write(vdf_content)
- print(f"文件已保存到: {output_path}")
- except Exception as e:
- print(f"文件保存失败: {e}")
- sys.exit(1)
-
- # 转换时间戳
- try:
- valid_time = datetime.datetime.fromtimestamp(int(valid_until)).strftime('%Y-%m-%d %H:%M:%S')
- except:
- valid_time = valid_until
-
- # 输出结果
- print("=" * 50)
- print("Steam 客户端更新服务器配置已成功更新!")
- print("=" * 50)
- print(f"配置文件已保存至: {output_path}")
- print(f"有效期至: {valid_time}")
- print(f"当前区域代码: {ip_country}")
- print("=" * 50)
- print("该脚本由清靈語 & DeepSeek 创建,通过 Python 驱动")
- print("请合法合规地使用脚本!")
- print("=" * 50)
- if __name__ == "__main__":
- main()
复制代码
新建一个文本文件,将以上代码复制粘贴进文本文件后保存文件,并将文件重命名为“update_hosts_cache.py”。
PowerShell 脚本需安装 PowerShell 7+,Python 脚本仅在“Python 3.13.5 (tags/v3.13.5:6cb20a2, Jun 11 2025, 16:15:46) [MSC v.1943 64 bit (AMD64)] on win32”平台测试通过。
Windows 系统推荐通过 Windows 终端(Windows 10 需自行安装)运行 PowerShell。
右键 PowerShell、Python 脚本所在目录,选择“在终端中打开”。
输入以下命令并执行。
PowerShell 脚本
Python 脚本
- python3 update_hosts_cache.py
复制代码
即可生成 update_hosts_cached.vdf 文件。
在 Windows 系统中,脚本会通过注册表及默认安装路径查找 Steam。如未找到 Steam,或是其他系统,则默认保存在当前目录,请自行将文件移动至 Steam 安装目录。
脚本完全由 DeepSeek 生成,不保证可用性,欢迎各位大佬优化脚本或根据思路创建其他程序。
虽然蒸汽平台客户端的默认服务器即为中国内地区域服务器,但由于更新服务器的域名时常发生变化,而安装程序中客户端主程序所包含的默认服务器无法及时更新,因此同样需要 update_hosts_cached.vdf 文件。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
×
1、转载或引用本网站内容,必须注明本文网址:https://keylol.com/t1008269-1-1。如发文者注明禁止转载,则请勿转载
2、对于不当转载或引用本网站内容而引起的民事纷争、行政处理或其他损失,本网站不承担责任
3、对不遵守本声明或其他违法、恶意使用本网站内容者,本网站保留追究其法律责任的权利
4、所有帖子仅代表作者本人意见,不代表本社区立场
|