104 lines
3.9 KiB
C#
104 lines
3.9 KiB
C#
using System;
|
|
using System.Text;
|
|
using Logic.CrashSight;
|
|
using TH1_Logic.Config;
|
|
using UnityEngine;
|
|
|
|
namespace TH1_Logic.Questionnaire
|
|
{
|
|
[Serializable]
|
|
public class QuestionnaireUploadPayload
|
|
{
|
|
public string schema = "th1.questionnaire-answer.v1";
|
|
public string responseId;
|
|
public string questionnaireId;
|
|
public long submittedAtUnix;
|
|
public string submittedAtUtc;
|
|
public string createdAtUtc;
|
|
public string createdAtLocal;
|
|
public string timezone;
|
|
public string steamId;
|
|
public string version;
|
|
public string unityVersion;
|
|
public string platform;
|
|
public string crashSightDeviceId;
|
|
public string deviceModel;
|
|
public string deviceName;
|
|
public string operatingSystem;
|
|
public string processorType;
|
|
public int processorCount;
|
|
public int systemMemorySizeMb;
|
|
public string graphicsDeviceName;
|
|
public int graphicsMemorySizeMb;
|
|
public QuestionnaireAnswerSheet answerSheet;
|
|
}
|
|
|
|
public static class QuestionnaireUploadService
|
|
{
|
|
public const int MaxQuestionnaireUploadBytes = 512 * 1024;
|
|
|
|
private static readonly UTF8Encoding Utf8NoBom = new UTF8Encoding(false);
|
|
|
|
public static byte[] BuildPayloadBytes(string steamId, QuestionnaireAnswerSheet sheet, string version)
|
|
{
|
|
if (sheet == null) return Array.Empty<byte>();
|
|
|
|
var payload = new QuestionnaireUploadPayload
|
|
{
|
|
responseId = string.IsNullOrEmpty(sheet.ResponseId) ? Guid.NewGuid().ToString("N") : sheet.ResponseId,
|
|
questionnaireId = sheet.QuestionnaireId ?? string.Empty,
|
|
submittedAtUnix = sheet.SubmittedAtUnix,
|
|
submittedAtUtc = sheet.SubmittedAtUtc ?? string.Empty,
|
|
createdAtUtc = DateTime.UtcNow.ToString("O"),
|
|
createdAtLocal = DateTime.Now.ToString("O"),
|
|
timezone = GetLocalTimezone(),
|
|
steamId = steamId ?? string.Empty,
|
|
version = string.IsNullOrWhiteSpace(version) ? GetCurrentVersion() : version.Trim(),
|
|
unityVersion = Application.unityVersion,
|
|
platform = Application.platform.ToString(),
|
|
crashSightDeviceId = CrashSightManager.GetCrashSightDeviceId(),
|
|
deviceModel = SystemInfo.deviceModel,
|
|
deviceName = SystemInfo.deviceName,
|
|
operatingSystem = SystemInfo.operatingSystem,
|
|
processorType = SystemInfo.processorType,
|
|
processorCount = SystemInfo.processorCount,
|
|
systemMemorySizeMb = SystemInfo.systemMemorySize,
|
|
graphicsDeviceName = SystemInfo.graphicsDeviceName,
|
|
graphicsMemorySizeMb = SystemInfo.graphicsMemorySize,
|
|
answerSheet = sheet
|
|
};
|
|
|
|
return Utf8NoBom.GetBytes(JsonUtility.ToJson(payload, true));
|
|
}
|
|
|
|
public static string GetCurrentVersion()
|
|
{
|
|
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 GetLocalTimezone()
|
|
{
|
|
try
|
|
{
|
|
var offset = DateTimeOffset.Now.Offset;
|
|
var sign = offset < TimeSpan.Zero ? "-" : "+";
|
|
return $"{TimeZoneInfo.Local.Id} (UTC{sign}{offset.Duration():hh\\:mm})";
|
|
}
|
|
catch
|
|
{
|
|
return DateTimeOffset.Now.Offset.ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|