import ctypes
import glob
import os
from steamworks import STEAMWORKS
from steamworks.exceptions import SteamNotRunningException
class SteamClass:
def __init__(self):
# Specify the directory containing the DLLs (use current working directory as an example)
dll_directory = os.getcwd()
# Add and load all DLLs from the specified directory
loaded_dlls = self.add_and_load_all_dlls(dll_directory)
self.steamworks = STEAMWORKS()
# - SteamNotLoadedException : STEAMWORKS class has not yet been loaded
# - SteamNotRunningException : Steam is not running
# - SteamConnectionException : The API connection to Steam could not be established
# - GenericSteamException : A generic exception occured (retry when encountering this)
try:
self.steamworks.initialize()
except SteamNotRunningException:
print(SteamNotRunningException)
exit()
except OSError:
print(OSError)
exit()
self.login()
# Function to add all DLLs in a given directory to the DLL search path and load them
def add_and_load_all_dlls(self, directory):
# Add the specified directory to the DLL search path
os.add_dll_directory(directory)
# Get all DLL files in the specified directory
dll_files = glob.glob(os.path.join(directory, '*.dll'))
loaded_dlls = {}
for dll in dll_files:
try:
# Attempt to load the DLL
loaded_dlls[dll] = ctypes.CDLL(dll)
print(f"{os.path.basename(dll)} was successfully loaded.")
except OSError as e:
print(f"Failed to load {os.path.basename(dll)}: {e}")
return loaded_dlls
def login(self):
"""
Execute two basic calls from the SteamUsers interface to retrieve SteamID from logged in user and Steam profile level
"""
my_steam64 = self.steamworks.Users.GetSteamID()
my_steam_level = self.steamworks.Users.GetPlayerSteamLevel()
print(f'Logged on as {my_steam64}, level: {my_steam_level}')
def store_data(self):
# Store the updated stats to Steam
store_success = self.steamworks.UserStats.StoreStats()
if store_success:
print("Data stored successfully.")
else:
print("Failed to store data.")
pass
def get_stat(self, STAT_ID):
if (self.steamworks.UserStats.RequestCurrentStats() == True):
print('Stats successfully retrieved!')
else:
print('Failed to get stats. Shutting down.')
exit(0)
stat = self.steamworks.UserStats.GetStatInt(STAT_ID)
return stat
def set_stat(self, STAT_ID, num): # STAT_CURRENT_STAGE, 1
if (self.steamworks.UserStats.RequestCurrentStats() == True):
print('Stats successfully retrieved!')
else:
print('Failed to get stats. Shutting down.')
exit(0)
stat = self.steamworks.UserStats.GetStatInt(STAT_ID)
stat += num
self.steamworks.UserStats.SetStat('STAT_CURRENT_STAGE', stat)
self.store_data()
pass
# Function to unlock an achievement
def unlock_achievement(self, achievement_id):
# Check if the achievement is already unlocked
ach_status = self.steamworks.UserStats.GetAchievement(achievement_id)
if ach_status:
print(f"Achievement '{achievement_id}' is already unlocked.")
else:
# Unlock the achievement
success = self.steamworks.UserStats.SetAchievement(achievement_id)
if success:
# Store the updated stats to Steam
store_success = self.steamworks.UserStats.StoreStats()
if store_success:
print(f"Achievement '{achievement_id}' unlocked and stored successfully.")
else:
print(f"Failed to store achievement '{achievement_id}'.")
else:
print(f"Failed to set achievement '{achievement_id}'.")
steam = SteamClass()
stages_complete = steam.get_stat('STAT_STAGES_COMPLETE')
stages_complete += 10
steam.set_stat('STAT_STAGES_COMPLETE', stages_complete)
stages_complete = steam.get_stat('STAT_STAGES_COMPLETE')
print(f'STAGES COMPLETE: {stages_complete}')
ACH_STAGE_1 = steam.steamworks.UserStats.GetAchievement('ACH_STAGE_1')
steam.unlock_achievement('ACH_STAGE_1')
print(ACH_STAGE_1)
print(steam.unlock_achievement('ACH_STAGE_1'))
我的输出如下 [这不是我的 SteamID]:
SteamworksPy64.dll was successfully loaded.
steam_api64.dll was successfully loaded.
Setting breakpad minidump AppID = 480
SteamInternal_SetMinidumpSteamID: Caching Steam ID: 1234123123123123 [API loaded no]
Logged on as 1234123123123123 , level: 2
Stats successfully retrieved!
Stats successfully retrieved!
Data stored successfully.
Stats successfully retrieved!
STAGES COMPLETE: 0
Failed to set achievement 'ACH_STAGE_1'.
False
Failed to set achievement 'ACH_STAGE_1'.
None
那么我如何使用 SteamworksPy 正确检索数据(这不是我的 Steam ID)或者我是否错误地检索存储的数据?我使用的是 Windows64 版本的 SteamworksAPI。另外,似乎说 API 未加载,那么这是为什么呢?
看起来已经成功地初始化了 Steamworks 并连接到 Steam,但出现了几个问题,导致无法正确检索和存储统计数据以及成就。让我们来分解一下问题,并找出解决方案:
-
Steam ID 不正确: 看到的是一个占位符 Steam ID(1234123123123123),这表明 Steamworks 没有正确地检索到的 Steam ID。这可能是由于以下几个原因造成的:
- Steam 未运行或未连接: 确保 Steam 正在运行,并且已登录到的 Steam 帐户。
-
应用程序 ID 不正确:
确认使用的应用程序 ID 正确,并且与在 Steamworks 中注册的游戏相匹配。可以在代码中通过打印
steamworks.Apps.GetAppID()
来检查当前使用的应用程序 ID。 -
初始化顺序:
确保在调用任何其他 Steamworks 函数之前调用了
steamworks.initialize()
。
-
统计数据未更新: 正在设置和获取统计数据,但值似乎没有在 Steam 服务器上持久化。这可能是因为:
-
StoreStats()
调用失败: 确保steamworks.UserStats.StoreStats()
调用返回True
,表示统计数据已成功存储到 Steam 服务器。 - 回调未处理: Steamworks 使用回调来异步处理请求。确保已正确设置回调,并在统计数据存储成功时收到通知。
-
-
成就未解锁:
unlock_achievement
函数似乎没有正确地解锁成就。请检查以下几点:- 成就 ID 正确性: 确保使用的成就 ID 与在 Steamworks 中定义的相匹配。
-
成就 API 初始化:
确保在调用任何成就相关函数之前调用了
steamworks.initialize()
。 -
成就状态检查:
使用
GetAchievement()
函数检查成就是否已解锁。
-
"API loaded no" 消息: 此消息表明 SteamworksPy 无法完全初始化 Steam API。这可能是由以下原因造成的:
-
DLL 文件缺失或位置错误:
确保
steam_api64.dll
文件位于的应用程序可以找到的地方,例如与的可执行文件相同的目录,或者在系统 PATH 环境变量中指定的目录中。 - Steam 客户端版本不兼容: 尝试更新的 Steam 客户端到最新版本。
- 防病毒软件干扰: 某些防病毒软件可能会错误地将 Steamworks 文件识别为恶意软件,并阻止它们加载。尝试将 Steamworks 文件添加到的防病毒软件的白名单中。
-
DLL 文件缺失或位置错误:
确保
以下是一些可以尝试的解决方法:
- 检查 SteamworksPy 安装: 确保已正确安装 SteamworksPy,并且所有依赖项都已满足。
- 查看 Steamworks 文档和示例: Steamworks 文档 ( https://partner.steamgames.com/doc/sdk/api ) 提供了有关 API 的详细信息,而 Steamworks 示例项目可以帮助了解如何正确使用 API。
- 调试的代码: 使用打印语句或调试器来跟踪代码的执行流程,并识别可能出现问题的位置。
如果问题仍然存在,请提供更多信息,例如: - 的操作系统和 Steam 客户端版本 - 使用的 SteamworksPy 版本 - 遇到的任何错误消息的完整内容 - 的完整代码(如果可能)
这将有助于我们更准确地诊断问题并提供更有效的帮助。
标签:python,steam,steamworks-api From: 78636799