Код: Выделить всё
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'))
Код: Выделить всё
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
Подробнее здесь: https://stackoverflow.com/questions/786 ... rieve-data