zlib and launcher callbacks

This commit is contained in:
Alex
2025-01-10 15:30:54 +00:00
parent 966562bc08
commit f61183570a
6 changed files with 245 additions and 115 deletions
+23 -14
View File
@@ -20,71 +20,80 @@ public class LauncherCallbacks
SaveServer saveServer,
Watermark watermark)
{
_httpResponseUtil = httpResponse;
_launcherController = launcherController;
_saveServer = saveServer;
_watermark = watermark;
}
public string Connect()
{
throw new NotImplementedException();
return _httpResponseUtil.NoBody(_launcherController.Connect());
}
public string Login(string url, LoginRequestData info, string sessionID)
{
throw new NotImplementedException();
var output = _launcherController.Login(info);
return output ?? "FAILED";
}
public string Register(string url, RegisterData info, string sessionID)
{
throw new NotImplementedException();
var output = _launcherController.Register(info);
return string.IsNullOrEmpty(output) ? "FAILED" : "OK";
}
public string Get(string url, LoginRequestData info, string sessionID)
{
throw new NotImplementedException();
var output = _launcherController.Find(_launcherController.Login(info));
return _httpResponseUtil.NoBody(output);
}
public string ChangeUsername(string url, ChangeRequestData info, string sessionID)
{
throw new NotImplementedException();
var output = _launcherController.ChangeUsername(info);
return string.IsNullOrEmpty(output) ? "FAILED" : "OK";
}
public string ChangePassword(string url, ChangeRequestData info, string sessionID)
{
throw new NotImplementedException();
var output = _launcherController.ChangePassword(info);
return string.IsNullOrEmpty(output) ? "FAILED" : "OK";
}
public string Wipe(string url, RegisterData info, string sessionID)
{
throw new NotImplementedException();
var output = _launcherController.Wipe(info);
return string.IsNullOrEmpty(output) ? "FAILED" : "OK";
}
public string GetServerVersion()
{
throw new NotImplementedException();
return _httpResponseUtil.NoBody(_watermark.GetVersionTag());
}
public string Ping(string url, EmptyRequestData info, string sessionID)
{
throw new NotImplementedException();
return _httpResponseUtil.NoBody("pong!");
}
public string RemoveProfile(string url, RemoveProfileData info, string sessionID)
{
throw new NotImplementedException();
return _httpResponseUtil.NoBody(_saveServer.RemoveProfile(sessionID));
}
public string GetCompatibleTarkovVersion()
{
throw new NotImplementedException();
return _httpResponseUtil.NoBody(_launcherController.GetCompatibleTarkovVersion());
}
public string GetLoadedServerMods()
{
throw new NotImplementedException();
return _httpResponseUtil.NoBody(_launcherController.GetLoadedServerMods());
}
public string GetServerModsProfileUsed(string url, EmptyRequestData info, string sessionID)
{
throw new NotImplementedException();
return _httpResponseUtil.NoBody(_launcherController.GetServerModsProfileUsed(sessionID));
}
}
+182 -90
View File
@@ -1,149 +1,241 @@
using System.Text.Json.Serialization;
using Core.Annotations;
using Core.Helpers;
using Core.Models.Eft.Common.Tables;
using Core.Models.Eft.Launcher;
using Core.Models.Eft.Profile;
using Core.Models.Enums;
using Core.Models.Spt.Config;
using Core.Models.Spt.Mod;
using Core.Servers;
using Core.Services;
using Core.Utils;
using Core.Utils.Extensions;
using ILogger = Core.Models.Utils.ILogger;
using Info = Core.Models.Eft.Profile.Info;
namespace Core.Controllers;
[Injectable]
public class LauncherController
{
/// <summary>
///
/// </summary>
/// <returns></returns>
protected CoreConfig _coreConfig;
protected ILogger _logger;
protected HashUtil _hashUtil;
protected TimeUtil _timeUtil;
protected RandomUtil _randomUtil;
protected SaveServer _saveServer;
protected HttpServerHelper _httpServerHelper;
protected ProfileHelper _profileHelper;
protected DatabaseService _databaseService;
protected LocalisationService _localisationService;
public LauncherController(
Models.Utils.ILogger logger,
HashUtil hashUtil,
TimeUtil timeUtil,
RandomUtil randomUtil,
SaveServer saveServer,
HttpServerHelper httpServerHelper,
ProfileHelper profileHelper,
DatabaseService databaseService,
LocalisationService localisationService,
// TODO => @inject("PreSptModLoader") protected preSptModLoader: PreSptModLoader,
ConfigServer configServer
) {
_logger = logger;
_hashUtil = hashUtil;
_timeUtil = timeUtil;
_randomUtil = randomUtil;
_saveServer = saveServer;
_httpServerHelper = httpServerHelper;
_profileHelper = profileHelper;
_databaseService = databaseService;
_localisationService = localisationService;
_coreConfig = configServer.GetConfig<CoreConfig>(ConfigTypes.CORE);
}
public ConnectResponse Connect()
{
throw new NotImplementedException();
// Get all possible profile types + filter out any that are blacklisted
var profiles = typeof(ProfileTemplates).GetProperties()
.Select(p => p.GetJsonName())
.Where(profileName => !_coreConfig.Features.CreateNewProfileTypesBlacklist.Contains(profileName))
.ToList();
return new ConnectResponse(){
BackendUrl = _httpServerHelper.GetBackendUrl(),
Name = _coreConfig.ServerName,
Editions = profiles,
ProfileDescriptions = GetProfileDescriptions(),
};
}
/// <summary>
/// Get descriptive text for each of the profile edtions a player can choose, keyed by profile.json profile type
/// e.g. "Edge Of Darkness"
/// </summary>
/// <returns>Dictionary of profile types with related descriptive text</returns>
private Dictionary<string, string> GetProfileDescriptions()
/**
* Get descriptive text for each of the profile edtions a player can choose, keyed by profile.json profile type e.g. "Edge Of Darkness"
* @returns Dictionary of profile types with related descriptive text
*/
protected Dictionary<string, string> GetProfileDescriptions()
{
throw new NotImplementedException();
var result = new Dictionary<string, string>();
var dbProfiles = _databaseService.GetProfiles();
foreach (var templatesProperty in typeof(ProfileTemplates).GetProperties())
{
var propertyValue = templatesProperty.GetValue(dbProfiles);
if (propertyValue == null) {
_logger.Warning(_localisationService.GetText("launcher-missing_property", templatesProperty));
continue;
}
var casterPropertyValue = propertyValue as ProfileSides;
result[templatesProperty.GetJsonName()] = _localisationService.GetText(casterPropertyValue.DescriptionLocaleKey);
}
return result;
}
/// <summary>
///
/// </summary>
/// <param name="sessionId"></param>
/// <returns></returns>
public Info Find(string sessionId)
public Info? Find(string sessionId)
{
throw new NotImplementedException();
return _saveServer.GetProfiles().TryGetValue(sessionId, out var profile) ? profile.ProfileInfo : null;
}
/// <summary>
///
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public string Login(LoginRequestData info)
{
throw new NotImplementedException();
foreach (var sessionID in _saveServer.GetProfiles()) {
var account = _saveServer.GetProfile(sessionID.Key).ProfileInfo;
if (info.Username == account.UserName) {
return sessionID.Key;
}
}
return "";
}
/// <summary>
///
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public string Register(RegisterData info)
{
throw new NotImplementedException();
foreach (var sessionID in _saveServer.GetProfiles()) {
if (info.Username == _saveServer.GetProfile(sessionID.Key).ProfileInfo.UserName) {
return "";
}
}
return CreateAccount(info);
}
/// <summary>
///
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
private string CreateAccount(RegisterData info)
protected string CreateAccount(RegisterData info)
{
throw new NotImplementedException();
var profileId = GenerateProfileId();
var scavId = GenerateProfileId();
var newProfileDetails = new Info(){
ProfileId = profileId,
ScavengerId = scavId,
Aid = _hashUtil.GenerateAccountId(),
UserName = info.Username,
Password = info.Password,
IsWiped = true,
Edition = info.Edition,
};
_saveServer.CreateProfile(newProfileDetails);
_saveServer.LoadProfile(profileId);
_saveServer.SaveProfile(profileId);
return profileId;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private string GenerateProfileId()
protected string GenerateProfileId()
{
throw new NotImplementedException();
var timestamp = _timeUtil.GetTimeStamp();
return FormatID(timestamp, timestamp * _randomUtil.GetInt(1, 1000000));
}
/// <summary>
///
/// </summary>
/// <param name="timeStamp"></param>
/// <param name="counter"></param>
/// <returns></returns>
private string FormatId(
long timeStamp,
int counter)
protected string FormatID(long timeStamp, long counter)
{
throw new NotImplementedException();
var timeStampStr = Convert.ToString(timeStamp, 16).PadLeft(8, '0');
var counterStr = Convert.ToString(counter, 16).PadLeft(16, '0');
return timeStampStr.ToLower() + counterStr.ToLower();
}
/// <summary>
///
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public string ChangeUsername(ChangeRequestData info)
{
throw new NotImplementedException();
var sessionID = Login(info);
if (!string.IsNullOrEmpty(sessionID)) {
_saveServer.GetProfile(sessionID).ProfileInfo.UserName = info.Change;
}
return sessionID;
}
/// <summary>
///
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public string ChangePassword(ChangeRequestData info)
{
throw new NotImplementedException();
var sessionID = Login(info);
if (!string.IsNullOrEmpty(sessionID)) {
_saveServer.GetProfile(sessionID).ProfileInfo.Password = info.Change;
}
return sessionID;
}
/// <summary>
/// Handle launcher requesting profile be wiped
/// </summary>
/// <param name="info">RegisterData</param>
/// <returns>Session id</returns>
public string Wipe(RegisterData info)
/**
* Handle launcher requesting profile be wiped
* @param info IRegisterData
* @returns Session id
*/
public string? Wipe(RegisterData info)
{
throw new NotImplementedException();
if (!_coreConfig.AllowProfileWipe) {
return null;
}
var sessionID = Login(info);
if (!string.IsNullOrEmpty(sessionID)) {
var profileInfo = _saveServer.GetProfile(sessionID).ProfileInfo;
profileInfo.Edition = info.Edition;
profileInfo.IsWiped = true;
}
return sessionID;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public string GetCompatibleTarkovVersion()
{
throw new NotImplementedException();
return _coreConfig.CompatibleTarkovVersion;
}
/// <summary>
/// Get the mods the server has currently loaded
/// </summary>
/// <returns>Dictionary of mod name and mod details</returns>
public Dictionary<string, PackageJsonData> GetLoadedServerMods() // TODO: We no longer use a package.json
/**
* Get the mods the server has currently loaded
* @returns Dictionary of mod name and mod details
*/
public Dictionary<string, PackageJsonData> GetLoadedServerMods()
{
throw new NotImplementedException();
return new Dictionary<string, PackageJsonData>();
// TODO => return this.preSptModLoader.getImportedModDetails();
}
/// <summary>
/// Get the mods a profile has ever loaded into game with
/// </summary>
/// <param name="sessionId">Player id</param>
/// <returns>List of mod details</returns>
/**
* Get the mods a profile has ever loaded into game with
* @param sessionId Player id
* @returns Array of mod details
*/
public List<ModDetails> GetServerModsProfileUsed(string sessionId)
{
throw new NotImplementedException();
var profile = _profileHelper.GetFullProfile(sessionId);
/* TODO => modding
if (profile?.spt?.mods) {
return this.preSptModLoader.GetProfileModsGroupedByModName(profile?.spt?.mods);
}
*/
return [];
}
}
+3 -1
View File
@@ -1,10 +1,12 @@
using Core.Models.Eft.Common;
using Core.Annotations;
using Core.Models.Eft.Common;
using Core.Models.Eft.Common.Tables;
using Core.Models.Eft.Profile;
using Core.Models.Enums;
namespace Core.Helpers;
[Injectable]
public class ProfileHelper
{
/// <summary>
+11 -3
View File
@@ -106,6 +106,7 @@ public class SptHttpListener : IHttpListener
if (IsDebugRequest(req)) {
// Send only raw response without transformation
SendJson(resp, output, sessionID);
Console.WriteLine($"Response: {output}");
// TODO: this.logRequest(req, output);
return;
}
@@ -118,7 +119,7 @@ public class SptHttpListener : IHttpListener
// No serializer can handle the request (majority of requests dont), zlib the output and send response back
SendZlibJson(resp, output, sessionID);
}
Console.WriteLine($"Response: {output}");
// TODO: this.LogRequest(req, output);
}
@@ -181,8 +182,15 @@ public class SptHttpListener : IHttpListener
public void SendZlibJson(HttpResponse resp, string? output, string sessionID)
{
if (!string.IsNullOrEmpty(output))
new ZLibStream(resp.Body, CompressionLevel.SmallestSize, false).WriteAsync(Encoding.UTF8.GetBytes(output)).AsTask().Wait();
using (var ms = new MemoryStream())
{
using (var deflateStream = new ZLibStream(ms, CompressionLevel.SmallestSize))
{
deflateStream.WriteAsync(Encoding.UTF8.GetBytes(output)).AsTask().Wait();
}
var bytes = ms.ToArray();
resp.Body.WriteAsync(bytes, 0, bytes.Length).Wait();
}
resp.StartAsync().Wait();
resp.CompleteAsync().Wait();
}
@@ -0,0 +1,15 @@
using System.Reflection;
using System.Text.Json.Serialization;
namespace Core.Utils.Extensions;
public static class MemberInfoExtensions
{
public static string GetJsonName(this MemberInfo memberInfo)
{
return Attribute.IsDefined(memberInfo, typeof(JsonPropertyNameAttribute))
? (Attribute.GetCustomAttribute(memberInfo, typeof(JsonPropertyNameAttribute)) as JsonPropertyNameAttribute).Name
: memberInfo.Name;
}
}
+11 -7
View File
@@ -10,8 +10,9 @@ namespace Core.Utils;
[Injectable(InjectionType.Singleton)]
public class JsonUtil
{
private readonly JsonSerializerOptions jsonSerializerOptions = new()
private static readonly JsonSerializerOptions jsonSerializerOptionsNoIndent = new()
{
WriteIndented = false,
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
Converters =
{
@@ -23,27 +24,30 @@ public class JsonUtil
new EftEnumConverter<ProfileChangeEventType>()
}
};
private static readonly JsonSerializerOptions jsonSerializerOptionsIndented = new(jsonSerializerOptionsNoIndent)
{
WriteIndented = true
};
public T? Deserialize<T>(string? json)
{
return string.IsNullOrEmpty(json) ? default : JsonSerializer.Deserialize<T>(json, jsonSerializerOptions);
return string.IsNullOrEmpty(json) ? default : JsonSerializer.Deserialize<T>(json, jsonSerializerOptionsNoIndent);
}
public object? Deserialize(string? json, Type type)
{
return string.IsNullOrEmpty(json) ? null : JsonSerializer.Deserialize(json, type, jsonSerializerOptions);
return string.IsNullOrEmpty(json) ? null : JsonSerializer.Deserialize(json, type, jsonSerializerOptionsNoIndent);
}
public string? Serialize<T>(T? obj, bool indented = false)
{
jsonSerializerOptions.WriteIndented = indented;
return obj == null ? null : JsonSerializer.Serialize(obj, jsonSerializerOptions);
return obj == null ? null : JsonSerializer.Serialize(obj, indented ? jsonSerializerOptionsIndented : jsonSerializerOptionsNoIndent);
}
public string? Serialize(object? obj, Type type, bool indented = false)
{
jsonSerializerOptions.WriteIndented = indented;
return obj == null ? null : JsonSerializer.Serialize(obj, type, jsonSerializerOptions);
return obj == null ? null : JsonSerializer.Serialize(obj, type, indented ? jsonSerializerOptionsIndented : jsonSerializerOptionsNoIndent);
}
}