150 lines
6.1 KiB
C#
150 lines
6.1 KiB
C#
#if UNITY_STANDALONE && STEAMWORKS_NET
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using Steamworks;
|
|
|
|
namespace BaseGames.Platform
|
|
{
|
|
/// <summary>
|
|
/// 基于 Steamworks.NET 的平台服务实现(仅 UNITY_STANDALONE + STEAMWORKS_NET 时编译)。
|
|
/// </summary>
|
|
public class SteamPlatformService : IPlatformService
|
|
{
|
|
public bool IsInitialized { get; private set; }
|
|
public bool IsCloudAvailable => IsInitialized && SteamRemoteStorage.IsCloudEnabledForApp();
|
|
|
|
// ── 初始化 / 生命周期 ──────────────────────────────────────────────────
|
|
public async Task<bool> InitializeAsync()
|
|
{
|
|
try
|
|
{
|
|
if (!SteamAPI.Init())
|
|
{
|
|
Debug.LogWarning("[SteamPlatform] SteamAPI.Init() 失败,请确认 Steam 客户端已运行。");
|
|
IsInitialized = false;
|
|
return false;
|
|
}
|
|
IsInitialized = true;
|
|
Debug.Log("[SteamPlatform] Steam 初始化成功。");
|
|
return true;
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Debug.LogError($"[SteamPlatform] 初始化异常: {ex.Message}");
|
|
IsInitialized = false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void RunCallbacks()
|
|
{
|
|
if (IsInitialized) SteamAPI.RunCallbacks();
|
|
}
|
|
|
|
public void Shutdown()
|
|
{
|
|
if (IsInitialized) SteamAPI.Shutdown();
|
|
}
|
|
|
|
// ── 成就 ──────────────────────────────────────────────────────────────
|
|
public void UnlockAchievement(string achievementId)
|
|
{
|
|
if (!IsInitialized) return;
|
|
SteamUserStats.SetAchievement(achievementId);
|
|
SteamUserStats.StoreStats();
|
|
}
|
|
|
|
public void ClearAchievement(string achievementId)
|
|
{
|
|
if (!IsInitialized) return;
|
|
SteamUserStats.ClearAchievement(achievementId);
|
|
SteamUserStats.StoreStats();
|
|
}
|
|
|
|
public async Task<bool> IsAchievementUnlocked(string achievementId)
|
|
{
|
|
if (!IsInitialized) return false;
|
|
SteamUserStats.GetAchievement(achievementId, out bool achieved);
|
|
return achieved;
|
|
}
|
|
|
|
// ── 统计数据 ──────────────────────────────────────────────────────────
|
|
public void SetStat(string statId, int value)
|
|
{
|
|
if (!IsInitialized) return;
|
|
SteamUserStats.SetStat(statId, value);
|
|
SteamUserStats.StoreStats();
|
|
}
|
|
|
|
public void IncrementStat(string statId, int increment = 1)
|
|
{
|
|
if (!IsInitialized) return;
|
|
SteamUserStats.GetStat(statId, out int current);
|
|
SteamUserStats.SetStat(statId, current + increment);
|
|
SteamUserStats.StoreStats();
|
|
}
|
|
|
|
public int GetStat(string statId)
|
|
{
|
|
if (!IsInitialized) return 0;
|
|
SteamUserStats.GetStat(statId, out int value);
|
|
return value;
|
|
}
|
|
|
|
// ── 云存档 ────────────────────────────────────────────────────────────
|
|
public async Task<bool> CloudSaveAsync(string fileName, byte[] data)
|
|
{
|
|
if (!IsCloudAvailable) return false;
|
|
return await Task.Run(() =>
|
|
SteamRemoteStorage.FileWrite(fileName, data, data.Length));
|
|
}
|
|
|
|
public async Task<byte[]> CloudLoadAsync(string fileName)
|
|
{
|
|
if (!IsCloudAvailable || !SteamRemoteStorage.FileExists(fileName))
|
|
return null;
|
|
int size = SteamRemoteStorage.GetFileSize(fileName);
|
|
var buf = new byte[size];
|
|
await Task.Run(() => SteamRemoteStorage.FileRead(fileName, buf, size));
|
|
return buf;
|
|
}
|
|
|
|
// ── Rich Presence ─────────────────────────────────────────────────────
|
|
public void SetRichPresence(string key, string value)
|
|
{
|
|
if (IsInitialized) SteamFriends.SetRichPresence(key, value);
|
|
}
|
|
|
|
public void ClearRichPresence()
|
|
{
|
|
if (IsInitialized) SteamFriends.ClearRichPresence();
|
|
}
|
|
|
|
// ── 排行榜 ────────────────────────────────────────────────────────────
|
|
public void SubmitLeaderboardScore(string boardId, long score)
|
|
{
|
|
// Steamworks 排行榜提交为异步,此处为 fire-and-forget 简化实现
|
|
Debug.Log($"[SteamPlatform] SubmitLeaderboardScore: {boardId} = {score}");
|
|
}
|
|
|
|
public Task<LeaderboardEntry[]> GetLeaderboardEntries(string boardId, int maxCount)
|
|
{
|
|
return Task.FromResult(System.Array.Empty<LeaderboardEntry>());
|
|
}
|
|
|
|
// ── DLC ───────────────────────────────────────────────────────────────
|
|
public bool IsDLCOwned(string dlcId)
|
|
{
|
|
if (!IsInitialized || !uint.TryParse(dlcId, out uint appId)) return false;
|
|
return SteamApps.BIsDlcInstalled((AppId_t)appId);
|
|
}
|
|
|
|
// ── 覆盖层 ────────────────────────────────────────────────────────────
|
|
public void ShowOverlay(string dialog)
|
|
{
|
|
if (IsInitialized) SteamFriends.ActivateGameOverlay(dialog);
|
|
}
|
|
}
|
|
}
|
|
#endif
|