Unstable 1.1.14.0

This commit is contained in:
Markus Isberg
2023-10-02 16:43:54 +03:00
parent 94f5a93a0c
commit cf8f0de659
606 changed files with 21906 additions and 11456 deletions
@@ -1,12 +1,20 @@
#nullable enable
using Barotrauma.Steam;
using RestSharp;
using System;
using System.Net;
using System.Threading.Tasks;
namespace Barotrauma
{
static partial class GameAnalyticsManager
{
/// <summary>
/// The protocol used to communicate with the remote consent server may change.
/// This number tells the server which version the game is using so we can implement backwards-compatibility.
/// </summary>
private const string RemoteRequestVersion = "2";
public enum Consent
{
/// <summary>
@@ -42,13 +50,14 @@ namespace Barotrauma
private static bool consentTextAvailable
=> TextManager.ContainsTag("statisticsconsentheader")
&& TextManager.ContainsTag("statisticsconsenttext");
private const string consentServerUrl = "https://barotraumagame.com/baromaster/";
private const string consentServerFile = "consentserver.php";
private readonly static string consentServerUrl = "https://barotraumagame.com/baromaster/";
private readonly static string consentServerFile = "consentserver.php";
private static string GetAuthTicket()
private static async Task<string> GetAuthTicket()
{
Steamworks.AuthTicket authTicket = SteamManager.GetAuthSessionTicket();
var ticketOption = await SteamManager.GetAuthTicketForGameAnalyticsConsent();
if (!ticketOption.TryUnwrap(out var authTicket) || authTicket.Data is null) { return ""; }
//convert byte array to hex
return BitConverter.ToString(authTicket.Data).Replace("-", "");
}
@@ -59,23 +68,27 @@ namespace Barotrauma
/// the database or the user accepting via the privacy policy
/// prompt should enable it.
/// </summary>
public static void SetConsent(Consent consent)
public static void SetConsent(Consent consent, Action? onAnswerSent = null)
{
if (consent == Consent.Yes)
{
throw new Exception(
"Cannot call SetConsent with value Consent.Yes, must only be set to this value via consent prompt");
}
SetConsentInternal(consent);
SetConsentInternal(consent, onAnswerSent);
}
/// <summary>
/// Implementation of the bulk of SetConsent.
/// DO NOT CALL THIS UNLESS NEEDED.
/// </summary>
private static void SetConsentInternal(Consent consent)
private static void SetConsentInternal(Consent consent, Action? onAnswerSent)
{
if (UserConsented == consent) { return; }
if (UserConsented == consent)
{
onAnswerSent?.Invoke();
return;
}
if (consent == Consent.Ask)
{
@@ -94,42 +107,72 @@ namespace Barotrauma
ShutDown();
}
TaskPool.Add(
"GameAnalyticsConsent.SendAnswerToRemoteDatabase",
SendAnswerToRemoteDatabase(consent),
t =>
{
onAnswerSent?.Invoke();
if (!t.TryGetResult(out bool success) || !success) { return; }
UserConsented = consent;
if (consent == Consent.Yes)
{
Init();
}
});
}
/// <summary>
/// Try to send the user's response to the remote consent server.
/// Returns true upon success, false otherwise.
/// </summary>
private static async Task<bool> SendAnswerToRemoteDatabase(Consent consent)
{
string authTicketStr;
try
{
authTicketStr = GetAuthTicket();
authTicketStr = await GetAuthTicket();
}
catch (Exception e)
{
DebugConsole.ThrowError("Error in GameAnalyticsManager.SetConsent. Could not get a Steam authentication ticket.", e);
return;
return false;
}
RestClient client = null;
if (string.IsNullOrEmpty(authTicketStr))
{
DebugConsole.ThrowError("Error in GameAnalyticsManager.SetContent. Steam authentication ticket was empty.");
return false;
}
IRestResponse response;
try
{
client = new RestClient(consentServerUrl);
var client = new RestClient(consentServerUrl);
var request = new RestRequest(consentServerFile, Method.GET);
request.AddParameter("authticket", authTicketStr);
request.AddParameter("action", "setconsent");
request.AddParameter("consent", consent == Consent.Yes ? 1 : 0);
request.AddParameter("request_version", RemoteRequestVersion);
response = await client.ExecuteAsync(request, Method.GET);
}
catch (Exception e)
{
DebugConsole.ThrowError("Error while connecting to consent server", e);
return false;
}
if (client == null) { return; }
var request = new RestRequest(consentServerFile, Method.GET);
request.AddParameter("authticket", authTicketStr);
request.AddParameter("action", "setconsent");
request.AddParameter("consent", consent == Consent.Yes ? 1 : 0);
if (!CheckResponse(response)) { return false; }
var response = client.Execute(request, Method.GET);
if (CheckResponse(response))
if (!string.IsNullOrWhiteSpace(response.Content))
{
UserConsented = consent;
if (consent == Consent.Yes)
{
Init();
}
DebugConsole.ThrowError($"Error in GameAnalyticsManager.SetContent. Consent server reported an error: {response.Content.Trim()}");
return false;
}
return true;
}
static partial void CreateConsentPrompt();
@@ -146,12 +189,6 @@ namespace Barotrauma
return;
}
static void error(string reason, Exception exception)
{
DebugConsole.ThrowError($"Error in GameAnalyticsManager.GetConsent: {reason}", exception);
SetConsent(Consent.Error);
}
if (!SteamManager.IsInitialized)
{
DebugConsole.AddWarning("Error in GameAnalyticsManager.GetConsent: Could not get a Steam authentication ticket (not connected to Steam).");
@@ -159,15 +196,33 @@ namespace Barotrauma
return;
}
TaskPool.Add(
"GameAnalyticsConsent.RequestAnswerFromRemoteDatabase",
RequestAnswerFromRemoteDatabase(),
t =>
{
if (!t.TryGetResult(out Consent consent)) { return; }
SetConsentInternal(consent, onAnswerSent: null);
});
}
private static async Task<Consent> RequestAnswerFromRemoteDatabase()
{
static void error(string reason, Exception exception)
{
DebugConsole.ThrowError($"Error in GameAnalyticsManager.GetConsent: {reason}", exception);
SetConsent(Consent.Error);
}
string authTicketStr;
try
{
authTicketStr = GetAuthTicket();
authTicketStr = await GetAuthTicket();
}
catch (Exception e)
{
error("Could not get a Steam authentication ticket.", e);
return;
return Consent.Error;
}
RestClient client;
@@ -178,37 +233,36 @@ namespace Barotrauma
catch (Exception e)
{
error("Error while connecting to consent server.", e);
return;
return Consent.Error;
}
var request = new RestRequest(consentServerFile, Method.GET);
request.AddParameter("authticket", authTicketStr);
request.AddParameter("action", "getconsent");
request.AddParameter("request_version", RemoteRequestVersion);
TaskPool.Add($"{nameof(GameAnalyticsManager)}.{nameof(InitIfConsented)}", client.ExecuteAsync(request), (t) =>
IRestResponse response;
try
{
if (t.Exception != null)
{
error("Error executing the request to the consent server.", t.Exception.InnerException);
return;
}
response = await client.ExecuteAsync(request);
}
catch (Exception e)
{
error("Error executing the request to the consent server.", e.GetInnermost());
return Consent.Error;
}
if (!t.TryGetResult(out IRestResponse response)) { return; }
if (!CheckResponse(response))
{
SetConsent(Consent.Error);
}
else if (string.IsNullOrEmpty(response.Content))
{
SetConsent(Consent.Ask);
}
else
{
SetConsentInternal(response.Content[0] == '1'
? Consent.Yes
: Consent.No);
}
});
if (!CheckResponse(response))
{
return Consent.Error;
}
if (string.IsNullOrEmpty(response.Content))
{
return Consent.Ask;
}
return response.Content[0] == '1'
? Consent.Yes
: Consent.No;
}
private static bool CheckResponse(IRestResponse response)
@@ -218,24 +272,24 @@ namespace Barotrauma
DebugConsole.ThrowError(TextManager.GetWithVariable("MasterServerErrorException", "[error]", response.ErrorException.ToString()));
return false;
}
else if (response.StatusCode != HttpStatusCode.OK)
if (response.StatusCode == HttpStatusCode.OK) { return true; }
switch (response.StatusCode)
{
switch (response.StatusCode)
{
case HttpStatusCode.NotFound:
DebugConsole.ThrowError(TextManager.GetWithVariable("MasterServerError404", "[masterserverurl]", consentServerUrl));
break;
case HttpStatusCode.ServiceUnavailable:
DebugConsole.ThrowError(TextManager.Get("MasterServerErrorUnavailable"));
break;
default:
DebugConsole.ThrowError(TextManager.GetWithVariables("MasterServerErrorDefault",
("[statuscode]", response.StatusCode.ToString()),
("[statusdescription]", response.StatusDescription)));
break;
}
case HttpStatusCode.NotFound:
DebugConsole.ThrowError(TextManager.GetWithVariable("MasterServerError404", "[masterserverurl]", consentServerUrl));
break;
case HttpStatusCode.ServiceUnavailable:
DebugConsole.ThrowError(TextManager.Get("MasterServerErrorUnavailable"));
break;
default:
DebugConsole.ThrowError(TextManager.GetWithVariables("MasterServerErrorDefault",
("[statuscode]", response.StatusCode.ToString()),
("[statusdescription]", response.StatusDescription)));
break;
}
return response.StatusCode == HttpStatusCode.OK;
return false;
}
}
}