migrate Steam.Service

This commit is contained in:
Aigio Liu
2021-02-26 18:04:49 +08:00
parent 7fca6001dd
commit 1d92c138f7
20 changed files with 700 additions and 84 deletions
@@ -1,4 +1,5 @@
using System.Application.Services;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Application.Services;
using System.Application.Services.Implementation;
using System.Application.UI;
using System.Application.UI.ViewModels;
@@ -98,5 +99,38 @@ namespace Microsoft.Extensions.DependencyInjection
services.AddSingleton<ISteamService, SteamServiceImpl>();
return services;
}
/// <summary>
/// 尝试添加 Steamworks LocalApi Service
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection TryAddSteamworksLocalApiService(this IServiceCollection services)
{
services.TryAddSingleton<ISteamworksLocalApiService, SteamworksLocalApiServiceImpl>();
return services;
}
/// <summary>
/// 添加 SteamDb WebApi Service
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddSteamDbWebApiService(this IServiceCollection services)
{
services.AddSingleton<ISteamDbWebApiService, SteamDbWebApiServiceImpl>();
return services;
}
/// <summary>
/// 添加 Steamworks WebApi Service
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddSteamworksWebApiService(this IServiceCollection services)
{
services.AddSingleton<ISteamworksWebApiService, SteamworksWebApiServiceImpl>();
return services;
}
}
}
@@ -23,7 +23,7 @@ namespace System.Application.Services
/// </summary>
ILocalizationViewModel[] ViewModels { get; }
public static IList<KeyValuePair<string, string>> Languages;
public static readonly IList<KeyValuePair<string, string>> Languages;
static ILocalizationService()
{
@@ -33,6 +33,8 @@ namespace System.Application.Services
{ "en", "English" },
{ "zh-Hans", "Chinese(Simplified)" },
{ "zh-Hant", "Chinese(Traditional)" },
{ "ja-JP", "Japanese" },
{ "ru", "Russian" },
}.ToList();
}
}
@@ -0,0 +1,26 @@
using System.Application.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace System.Application.Services
{
public interface ISteamDbWebApiService
{
public static ISteamDbWebApiService Instance => DI.Get<ISteamDbWebApiService>();
Task<SteamUser> GetUserInfo(long steamId64);
Task<List<SteamUser>> GetUserInfo(IEnumerable<long> steamId64s);
Task<SteamApp> GetAppInfo(int appId);
}
#if DEBUG
[Obsolete("use ISteamDbWebApiService", true)]
public class SteamDbApiService
{
}
#endif
}
@@ -23,8 +23,21 @@ namespace System.Application.Services
/// <summary>
/// 结束 Steam 进程
/// </summary>
[Obsolete("可能引发,Win32异常,拒绝访问,改为 TryKillSteamProcess false 弹窗提示用户手动关闭 Steam,true 使用 IsRunningSteamProcess 再检测一遍,返回 int(pid) 提示用户去任务管理器中结束进程,null 再进行业务逻辑", true)]
void KillSteamProcess();
/// <summary>
/// 尝试结束 Steam 进程
/// </summary>
/// <returns></returns>
bool TryKillSteamProcess();
/// <summary>
/// Steam 进程是否正在运行,如果正在运行,返回进程PID提示用户去任务管理器中结束进程
/// </summary>
/// <returns></returns>
int? IsRunningSteamProcess();
/// <summary>
/// 启动 Steam
/// </summary>
@@ -86,6 +99,11 @@ namespace System.Application.Services
[Obsolete("use ISteamService", true)]
public class SteamService
{
[Obsolete("use ISteamworksLocalApiService.Instance / ISteamDbWebApiService.Instance / ISteamworksWebApiService.Instance", true)]
public static SteamService Instance => throw new NotImplementedException();
[Obsolete("use ISteamworksLocalApiService.Instance / ISteamDbWebApiService.Instance / ISteamworksWebApiService.Instance", true)]
public TInterface Get<TInterface>() => throw new NotImplementedException();
}
#endif
@@ -1,24 +1,85 @@
using System;
using System.Application.Models;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace System.Application.Services
{
public interface ISteamworksLocalApiService
{
//Client SteamClient { get; }
}
protected const string TAG = "SteamworksLocalApiS";
public static ISteamworksLocalApiService Instance => DI.Get<ISteamworksLocalApiService>();
/// <summary>
/// 当前平台是否支持
/// </summary>
bool IsSupported { get; }
bool Initialize();
bool Initialize(int appid);
long GetSteamId64();
bool OwnsApps(uint appid);
List<SteamApp> OwnsApps(List<SteamApp> apps);
string GetAppData(uint appid, string key);
bool IsSteamChinaLauncher();
bool IsAppInstalled(uint appid);
string GetAppInstallDir(uint appid);
string GetIPCountry();
string GetCurrentGameLanguage();
string GetAvailableGameLanguages();
bool GetStatValue(string name, out int value);
bool GetStatValue(string name, out float value);
bool GetAchievementState(string name, out bool isAchieved);
bool GetAchievementAndUnlockTime(string name, out bool isAchieved, out long unlockTime);
bool GetAchievementAchievedPercent(string name, out float percent);
void AddUserStatsReceivedCallback(Action<IUserStatsReceived> action);
bool RequestCurrentStats();
bool ResetAllStats(bool achievementsToo);
bool SetAchievement(string name, bool state);
bool SetStatValue(string name, int value);
bool SetStatValue(string name, float value);
bool StoreStats();
void RequestGlobalAchievementPercentages();
void RunCallbacks(bool server);
public interface IUserStatsReceived
{
ulong GameId { get; }
int Result { get; }
}
}
#if DEBUG
[Obsolete("use ISteamworksLocalApiService", true)]
public class SteamworksApiService
{
}
#endif
}
}
@@ -0,0 +1,26 @@
using System.Application.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace System.Application.Services
{
public interface ISteamworksWebApiService
{
public static ISteamworksWebApiService Instance => DI.Get<ISteamworksWebApiService>();
Task<string> GetAllSteamAppsString();
Task<List<SteamApp>> GetAllSteamAppList();
Task<SteamUser> GetUserInfo(long steamId64);
}
#if DEBUG
[Obsolete("use ISteamworksWebApiService", true)]
public class SteamworksWebApiService
{
}
#endif
}
@@ -35,6 +35,20 @@ namespace System.Application.Services.Implementation
{
if (response.Content != null)
{
var rspContentClrType = typeof(T);
if (rspContentClrType == typeof(string))
{
return (T)(object)await response.Content.ReadAsStringAsync(cancellationToken);
}
else if (rspContentClrType == typeof(byte[]))
{
return (T)(object)await response.Content.ReadAsByteArrayAsync(cancellationToken);
}
else if (rspContentClrType == typeof(Stream))
{
// 因为上面用了 using 释放,所以不支持流的形式
throw new NotSupportedException();
}
var mime = response.Content.Headers.ContentType?.MediaType ?? Accept;
switch (mime)
{
@@ -0,0 +1,40 @@
using System.Application.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace System.Application.Services.Implementation
{
internal sealed class SteamDbWebApiServiceImpl : ISteamDbWebApiService
{
readonly IHttpService s;
public SteamDbWebApiServiceImpl(IHttpService s)
{
this.s = s;
}
public async Task<SteamUser> GetUserInfo(long steamId64)
{
var requestUri = string.Format(SteamApiUrls.STEAMDB_USERINFO_URL, steamId64);
var rsp = await s.GetAsync<SteamUser>(requestUri);
return rsp ?? new SteamUser() { SteamId64 = steamId64 };
}
public async Task<List<SteamUser>> GetUserInfo(IEnumerable<long> steamId64s)
{
var users = new List<SteamUser>();
foreach (var i in steamId64s)
{
users.Add(await GetUserInfo(i));
}
return users;
}
public async Task<SteamApp> GetAppInfo(int appId)
{
var requestUri = string.Format(SteamApiUrls.STEAMDB_APPINFO_URL, appId);
var rsp = await s.GetAsync<SteamApp>(requestUri);
return rsp ?? new SteamApp() { AppId = (uint)appId };
}
}
}
@@ -53,6 +53,33 @@ namespace System.Application.Services.Implementation
}
}
public bool TryKillSteamProcess()
{
try
{
KillSteamProcess();
return true;
}
catch (Exception e)
{
Log.Error(TAG, e, "KillSteamProcess Fail.");
return false;
}
}
public int? IsRunningSteamProcess()
{
var processes = Process.GetProcesses();
foreach (var p in processes)
{
if (steamProcess.Contains(p.ProcessName, StringComparer.OrdinalIgnoreCase))
{
return p.Id;
}
}
return default;
}
public void StartSteam(string arguments)
{
if (!string.IsNullOrEmpty(SteamProgramPath))
@@ -0,0 +1,90 @@
using System.Application.Models;
using System.Collections.Generic;
using static System.Application.Services.ISteamworksLocalApiService;
namespace System.Application.Services.Implementation
{
internal sealed class SteamworksLocalApiServiceImpl : ISteamworksLocalApiService
{
public bool IsSupported => false;
public bool Initialize() => default;
public bool Initialize(int appid) => default;
public long GetSteamId64() => default;
public bool OwnsApps(uint appid) => default;
public List<SteamApp> OwnsApps(List<SteamApp> apps) => new List<SteamApp>();
public string GetAppData(uint appid, string key) => string.Empty;
public bool IsSteamChinaLauncher() => default;
public bool IsAppInstalled(uint appid) => default;
public string GetAppInstallDir(uint appid) => string.Empty;
public string GetIPCountry() => string.Empty;
public string GetCurrentGameLanguage() => string.Empty;
public string GetAvailableGameLanguages() => string.Empty;
public bool GetStatValue(string name, out int value)
{
value = default;
return default;
}
public bool GetStatValue(string name, out float value)
{
value = default;
return default;
}
public bool GetAchievementState(string name, out bool isAchieved)
{
isAchieved = default;
return default;
}
public bool GetAchievementAndUnlockTime(string name, out bool isAchieved, out long unlockTime)
{
isAchieved = default;
unlockTime = default;
return default;
}
public bool GetAchievementAchievedPercent(string name, out float percent)
{
percent = default;
return default;
}
public void AddUserStatsReceivedCallback(Action<IUserStatsReceived> action)
{
}
public bool RequestCurrentStats() => default;
public bool ResetAllStats(bool achievementsToo) => default;
public bool SetAchievement(string name, bool state) => default;
public bool SetStatValue(string name, int value) => default;
public bool SetStatValue(string name, float value) => default;
public bool StoreStats() => default;
public void RequestGlobalAchievementPercentages()
{
}
public void RunCallbacks(bool server)
{
}
}
}
@@ -0,0 +1,35 @@
using System.Application.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace System.Application.Services.Implementation
{
internal sealed class SteamworksWebApiServiceImpl : ISteamworksWebApiService
{
readonly IHttpService s;
public SteamworksWebApiServiceImpl(IHttpService s)
{
this.s = s;
}
public async Task<string> GetAllSteamAppsString()
{
var rsp = await s.GetAsync<string>(SteamApiUrls.STEAMAPP_LIST_URL);
return rsp ?? string.Empty;
}
public async Task<List<SteamApp>> GetAllSteamAppList()
{
var rsp = await s.GetAsync<SteamApps>(SteamApiUrls.STEAMAPP_LIST_URL);
return rsp?.AppList?.Apps ?? new List<SteamApp>();
}
public async Task<SteamUser> GetUserInfo(long steamId64)
{
var requestUri = string.Format(SteamApiUrls.STEAM_USERINFO_XML_URL, steamId64);
var rsp = await s.GetAsync<SteamUser>(requestUri);
return rsp ?? new SteamUser() { SteamId64 = steamId64 };
}
}
}
@@ -15,10 +15,12 @@ namespace System.Application.UI.ViewModels
/// </summary>
public static bool IsInDesignMode { get; set; }
protected static TViewModel GetViewModel<TViewModel>() where TViewModel : ViewModelBase, new()
{
if (IsInDesignMode) return new TViewModel();
return DI.Get<TViewModel>();
}
//protected static TViewModel GetViewModel<TViewModel>() where TViewModel : ViewModelBase, new()
//{
// if (IsInDesignMode) return new TViewModel();
// return DI.Get<TViewModel>();
//}
protected static TViewModel GetViewModel<TViewModel>() where TViewModel : ViewModelBase => DI.Get<TViewModel>();
}
}