94 lines
3.4 KiB
C#
94 lines
3.4 KiB
C#
|
using System;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Threading;
|
|||
|
using Cysharp.Threading.Tasks;
|
|||
|
using UnityEngine;
|
|||
|
using Steamworks;
|
|||
|
using Steamworks.Data;
|
|||
|
|
|||
|
namespace BITKit.Steamwork
|
|||
|
{
|
|||
|
public class SteamService : MonoBehaviour,ISteamService
|
|||
|
{
|
|||
|
internal static SteamService Singleton;
|
|||
|
[Header(Constant.Header.Settings)]
|
|||
|
[SerializeField]private uint appId=480;
|
|||
|
|
|||
|
[Header(Constant.Header.Settings)]
|
|||
|
[SerializeField] private TextAsset allowCharacters;
|
|||
|
|
|||
|
[Header(Constant.Header.Settings)]
|
|||
|
[SerializeField] private Optional<UnityInventoryItemDef[]> overrideInventoryItemDefs;
|
|||
|
|
|||
|
//接口实现
|
|||
|
public ulong SteamId => SteamClient.SteamId;
|
|||
|
public string Name =>SteamClient.IsValid ? SteamClient.Name.Where(x=>allowCharacters.text.Contains(x)).Aggregate("",(current, c) => current+c) : Environment.UserDomainName;
|
|||
|
public bool IsInitialized=>SteamClient.IsValid&&SteamClient.IsLoggedOn;
|
|||
|
public async UniTask<Texture2D> GetAvatarAsync(CancellationToken token)
|
|||
|
{
|
|||
|
var avatar =await SteamFriends.GetLargeAvatarAsync(SteamClient.SteamId);
|
|||
|
var texture = new Texture2D(0, 0);
|
|||
|
return avatar.Value.Covert();
|
|||
|
}
|
|||
|
|
|||
|
public async UniTask<ISteamInventoryItemDef[]> GetInventoryItemDefsAsync(CancellationToken token)
|
|||
|
{
|
|||
|
if(overrideInventoryItemDefs.Allow)return overrideInventoryItemDefs.Value;
|
|||
|
var items = await SteamInventory.GetAllItemsAsync();
|
|||
|
if(items.HasValue is false)return Array.Empty<ISteamInventoryItemDef>();
|
|||
|
return items.Value.GetItems().Select(x => new SteamInventoryItemDef()
|
|||
|
{
|
|||
|
Id = x.Id,
|
|||
|
DefId = x.DefId
|
|||
|
}).Cast<ISteamInventoryItemDef>().ToArray();
|
|||
|
}
|
|||
|
|
|||
|
public int Id => (int)SteamClient.SteamId.AccountId;
|
|||
|
|
|||
|
private bool initialized;
|
|||
|
|
|||
|
private void Awake()
|
|||
|
{
|
|||
|
Singleton = this;
|
|||
|
}
|
|||
|
|
|||
|
private void Start()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (SteamClient.IsValid is false)
|
|||
|
{
|
|||
|
SteamClient.Init(appId);
|
|||
|
}
|
|||
|
initialized = true;
|
|||
|
BIT4Log.Log<SteamService>("已初始化Steam");
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
BIT4Log.Warning<SteamService>("Steam初始化失败,请验证客户端是否运行");
|
|||
|
Debug.Log(e);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void OnDestroy()
|
|||
|
{
|
|||
|
if (initialized)
|
|||
|
SteamClient.Shutdown();
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
[Serializable]
|
|||
|
public class SteamServiceSingleton:ISteamService
|
|||
|
{
|
|||
|
private ISteamService _steamServiceImplementation=>SteamService.Singleton;
|
|||
|
public int Id => _steamServiceImplementation.Id;
|
|||
|
public ulong SteamId => _steamServiceImplementation.SteamId;
|
|||
|
public string Name => _steamServiceImplementation.Name;
|
|||
|
public bool IsInitialized=>_steamServiceImplementation.IsInitialized;
|
|||
|
public UniTask<Texture2D> GetAvatarAsync(CancellationToken token)=>_steamServiceImplementation.GetAvatarAsync(token);
|
|||
|
public UniTask<ISteamInventoryItemDef[]> GetInventoryItemDefsAsync(CancellationToken token)=>_steamServiceImplementation.GetInventoryItemDefsAsync(token);
|
|||
|
}
|
|||
|
}
|