165 lines
5.2 KiB
C#
165 lines
5.2 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Logic.CrashSight;
|
|
#if STEAM_CHANNEL || STEAMWORKS_NET
|
|
using Steamworks;
|
|
#endif
|
|
using TH1_Logic.Config;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
namespace TH1_Logic.Oss
|
|
{
|
|
[Serializable]
|
|
public class StsCredentials
|
|
{
|
|
public string accessKeyId;
|
|
public string accessKeySecret;
|
|
public string securityToken;
|
|
public string steamAppId;
|
|
public string endpoint;
|
|
public string bucket;
|
|
public string objectKey;
|
|
public string policy;
|
|
public string signature;
|
|
public int expiresIn;
|
|
}
|
|
|
|
[Serializable]
|
|
public class SteamAuthWarmupResponse
|
|
{
|
|
public bool success;
|
|
public bool cached;
|
|
public string steamId;
|
|
public string steamAppId;
|
|
public string version;
|
|
public int expiresIn;
|
|
}
|
|
|
|
public class StsTokenService
|
|
{
|
|
private readonly string _functionUrl;
|
|
|
|
public StsTokenService(string functionUrl)
|
|
{
|
|
_functionUrl = functionUrl;
|
|
}
|
|
|
|
public async Task<StsCredentials> RequestStsTokenAsync(string steamId, string authTicket, string type = "ossdata",
|
|
string versionOverride = null)
|
|
{
|
|
var requestBody = JsonUtility.ToJson(new StsRequest
|
|
{
|
|
steamId = steamId,
|
|
steamAppId = GetSteamAppId(),
|
|
authTicket = authTicket,
|
|
version = GetRequestVersion(versionOverride),
|
|
type = type,
|
|
});
|
|
var bodyBytes = Encoding.UTF8.GetBytes(requestBody);
|
|
|
|
using var request = new UnityWebRequest(_functionUrl, "POST");
|
|
request.uploadHandler = new UploadHandlerRaw(bodyBytes);
|
|
request.downloadHandler = new DownloadHandlerBuffer();
|
|
request.SetRequestHeader("Content-Type", "application/json");
|
|
request.timeout = 30;
|
|
|
|
var operation = request.SendWebRequest();
|
|
while (!operation.isDone)
|
|
{
|
|
await Task.Yield();
|
|
}
|
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
{
|
|
LogSystem.LogError($"STS request failed: {request.error}, Response: {request.downloadHandler.text}");
|
|
throw new Exception($"STS request failed: {request.error}");
|
|
}
|
|
|
|
return JsonUtility.FromJson<StsCredentials>(request.downloadHandler.text);
|
|
}
|
|
|
|
public async Task<SteamAuthWarmupResponse> WarmupSteamAuthAsync(string steamId, string authTicket,
|
|
string versionOverride = null)
|
|
{
|
|
var requestBody = JsonUtility.ToJson(new StsRequest
|
|
{
|
|
action = "steamauth",
|
|
steamId = steamId,
|
|
steamAppId = GetSteamAppId(),
|
|
authTicket = authTicket,
|
|
version = GetRequestVersion(versionOverride),
|
|
type = "",
|
|
});
|
|
var bodyBytes = Encoding.UTF8.GetBytes(requestBody);
|
|
|
|
using var request = new UnityWebRequest(_functionUrl, "POST");
|
|
request.uploadHandler = new UploadHandlerRaw(bodyBytes);
|
|
request.downloadHandler = new DownloadHandlerBuffer();
|
|
request.SetRequestHeader("Content-Type", "application/json");
|
|
request.timeout = 30;
|
|
|
|
var operation = request.SendWebRequest();
|
|
while (!operation.isDone)
|
|
{
|
|
await Task.Yield();
|
|
}
|
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
{
|
|
LogSystem.LogWarning($"Steam auth warmup failed: {request.error}, Response: {request.downloadHandler.text}");
|
|
throw new Exception($"Steam auth warmup failed: {request.error}");
|
|
}
|
|
|
|
return JsonUtility.FromJson<SteamAuthWarmupResponse>(request.downloadHandler.text);
|
|
}
|
|
|
|
[Serializable]
|
|
private class StsRequest
|
|
{
|
|
public string action;
|
|
public string steamId;
|
|
public string steamAppId;
|
|
public string authTicket;
|
|
public string version;
|
|
public string type;
|
|
}
|
|
|
|
private static string GetRequestVersion(string versionOverride)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(versionOverride))
|
|
return versionOverride.Trim();
|
|
|
|
try
|
|
{
|
|
if (ConfigManager.Instance.VersionCfg == null)
|
|
ConfigManager.Instance.VersionCfg = TH1Resource.ResourceLoader.Load<Logic.Config.VersionConfig>("Export/VersionConfig");
|
|
|
|
return ConfigManager.Instance.VersionCfg?.CurVersionInfo?.Version ?? Application.version;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return Application.version;
|
|
}
|
|
}
|
|
|
|
private static string GetSteamAppId()
|
|
{
|
|
#if STEAM_CHANNEL || STEAMWORKS_NET
|
|
try
|
|
{
|
|
var appId = SteamUtils.GetAppID().m_AppId;
|
|
return appId == 0 ? string.Empty : appId.ToString();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
#else
|
|
return string.Empty;
|
|
#endif
|
|
}
|
|
}
|
|
}
|