V2 launcher API

This commit is contained in:
CWX
2025-01-19 14:29:59 +00:00
parent ebddcb5a89
commit bb8d56c8b0
13 changed files with 476 additions and 0 deletions
+112
View File
@@ -0,0 +1,112 @@
using Core.Annotations;
using Core.Controllers;
using Core.Models.Eft.Launcher;
using Core.Models.Spt.Launcher;
using Core.Utils;
namespace Core.Callbacks;
[Injectable]
public class LauncherV2Callbacks(
HttpResponseUtil _httpResponseUtil,
LauncherV2Controller _launcherV2Controller,
ProfileController _profileController
)
{
public string Ping()
{
return _httpResponseUtil.NoBody(new LauncherV2PingResponse
{
Response = _launcherV2Controller.Ping()
}
);
}
public string Types()
{
return _httpResponseUtil.NoBody(new LauncherV2TypesResponse
{
Response = _launcherV2Controller.Types()
});
}
public string Login(LoginRequestData info)
{
return _httpResponseUtil.NoBody(new LauncherV2LoginResponse
{
Response = _launcherV2Controller.Login(info)
}
);
}
public string Register(RegisterData info)
{
return _httpResponseUtil.NoBody(new LauncherV2RegisterResponse
{
Response = _launcherV2Controller.Register(info),
Profiles = _profileController.GetMiniProfiles()
}
);
}
public string PasswordChange(ChangeRequestData info)
{
return _httpResponseUtil.NoBody(new LauncherV2PasswordChangeResponse
{
Response = _launcherV2Controller.PasswordChange(info),
Profiles = _profileController.GetMiniProfiles()
}
);
}
public string Remove(LoginRequestData info)
{
return _httpResponseUtil.NoBody(new LauncherV2RemoveResponse
{
Response = _launcherV2Controller.Remove(info),
Profiles = _profileController.GetMiniProfiles()
}
);
}
public string CompatibleVersion()
{
return _httpResponseUtil.NoBody(new LauncherV2VersionResponse
{
Response = new LauncherV2CompatibleVersion
{
SptVersion = _launcherV2Controller.SptVersion(),
EftVersion = _launcherV2Controller.EftVersion()
}
}
);
}
public string Mods()
{
return _httpResponseUtil.NoBody(new LauncherV2ModsResponse
{
Response = _launcherV2Controller.LoadedMods()
}
);
}
public string Profiles()
{
return _httpResponseUtil.NoBody(new LauncherV2ProfilesResponse
{
Response = _profileController.GetMiniProfiles()
}
);
}
public string Profile()
{
throw new NotImplementedException();
}
public string ProfileMods()
{
throw new NotImplementedException();
}
}
+208
View File
@@ -0,0 +1,208 @@
using Core.Annotations;
using Core.Helpers;
using Core.Models.Eft.Common.Tables;
using Core.Models.Eft.Launcher;
using Core.Models.Spt.Config;
using Core.Models.Spt.Mod;
using Core.Models.Utils;
using Core.Servers;
using Core.Services;
using Core.Utils;
using Core.Utils.Extensions;
using Info = Core.Models.Eft.Profile.Info;
namespace Core.Controllers;
[Injectable]
public class LauncherV2Controller(
ISptLogger<LauncherV2Controller> _logger,
HashUtil _hashUtil,
TimeUtil _timeUtil,
RandomUtil _randomUtil,
SaveServer _saveServer,
DatabaseService _databaseService,
LocalisationService _localisationService,
ConfigServer _configServer,
Watermark _watermark
)
{
protected CoreConfig _coreConfig = _configServer.GetConfig<CoreConfig>();
/// <summary>
/// Returns a simple string of Pong!
/// </summary>
/// <returns></returns>
public string Ping()
{
return "Pong!";
}
/// <summary>
/// Returns all available profile types and descriptions for creation.
/// - This is also localised.
/// </summary>
/// <returns></returns>
public Dictionary<string, string> Types()
{
var result = new Dictionary<string, string>();
var dbProfiles = _databaseService.GetProfiles();
foreach (var templatesProperty in typeof(ProfileTemplates).GetProperties().Where(p => p.CanWrite))
{
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>
/// Checks if login details were correct.
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public bool Login(LoginRequestData info)
{
var sessionId = GetSessionId(info);
return sessionId is not null;
}
/// <summary>
/// Register a new profile.
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public bool Register(RegisterData info)
{
foreach (var session in _saveServer.GetProfiles())
{
if (info.Username == _saveServer.GetProfile(session.Key).ProfileInfo!.Username)
{
return false;
}
}
CreateAccount(info);
return true;
}
/// <summary>
/// Make a password change.
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public bool PasswordChange(ChangeRequestData info)
{
var sessionId = GetSessionId(info);
if (sessionId is null)
return false;
_saveServer.GetProfile(sessionId).ProfileInfo!.Password = info.Password;
return true;
}
/// <summary>
/// Remove profile from server.
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public bool Remove(LoginRequestData info)
{
var sessionId = GetSessionId(info);
return sessionId is not null && _saveServer.RemoveProfile(sessionId);
}
/// <summary>
/// Gets the Servers SPT Version.
/// - "4.0.0"
/// </summary>
/// <returns></returns>
public string SptVersion()
{
return _watermark.GetVersionTag();
}
/// <summary>
/// Gets the compatible EFT Version.
/// - "0.14.9.31124"
/// </summary>
/// <returns></returns>
public string EftVersion()
{
return _coreConfig.CompatibleTarkovVersion;
}
/// <summary>
/// Gets the Servers loaded mods.
/// </summary>
/// <returns></returns>
public Dictionary<string, PackageJsonData> LoadedMods()
{
return new Dictionary<string, PackageJsonData>();
}
/// <summary>
/// Creates the account from provided details.
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
protected string CreateAccount(RegisterData info)
{
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;
}
protected string GenerateProfileId()
{
var timestamp = _timeUtil.GetTimeStamp();
return FormatID(timestamp, timestamp * _randomUtil.GetInt(1, 1000000));
}
protected string FormatID(long timeStamp, long counter)
{
var timeStampStr = Convert.ToString(timeStamp, 16).PadLeft(8, '0');
var counterStr = Convert.ToString(counter, 16).PadLeft(16, '0');
return timeStampStr.ToLower() + counterStr.ToLower();
}
protected string? GetSessionId(LoginRequestData info)
{
foreach (var profile in _saveServer.GetProfiles())
{
if (info.Username == profile.Value.ProfileInfo!.Username
&& info.Password == profile.Value.ProfileInfo.Password)
return profile.Key;
}
return null;
}
}
@@ -0,0 +1,9 @@
using Core.Models.Utils;
namespace Core.Models.Spt.Launcher;
public class LauncherV2CompatibleVersion : IRequestData
{
public required string SptVersion { get; set; }
public required string EftVersion { get; set; }
}
@@ -0,0 +1,8 @@
using Core.Models.Utils;
namespace Core.Models.Spt.Launcher;
public class LauncherV2LoginResponse : IRequestData
{
public required bool Response { get; set; }
}
@@ -0,0 +1,9 @@
using Core.Models.Spt.Mod;
using Core.Models.Utils;
namespace Core.Models.Spt.Launcher;
public class LauncherV2ModsResponse : IRequestData
{
public required Dictionary<string, PackageJsonData> Response { get; set; }
}
@@ -0,0 +1,10 @@
using Core.Models.Eft.Launcher;
using Core.Models.Utils;
namespace Core.Models.Spt.Launcher;
public class LauncherV2PasswordChangeResponse : IRequestData
{
public required bool Response { get; set; }
public required List<MiniProfile> Profiles { get; set; }
}
@@ -0,0 +1,8 @@
using Core.Models.Utils;
namespace Core.Models.Spt.Launcher;
public class LauncherV2PingResponse : IRequestData
{
public required string Response { get; set; }
}
@@ -0,0 +1,9 @@
using Core.Models.Eft.Launcher;
using Core.Models.Utils;
namespace Core.Models.Spt.Launcher;
public class LauncherV2ProfilesResponse : IRequestData
{
public required List<MiniProfile> Response { get; set; }
}
@@ -0,0 +1,10 @@
using Core.Models.Eft.Launcher;
using Core.Models.Utils;
namespace Core.Models.Spt.Launcher;
public class LauncherV2RegisterResponse : IRequestData
{
public required bool Response { get; set; }
public required List<MiniProfile> Profiles { get; set; }
}
@@ -0,0 +1,10 @@
using Core.Models.Eft.Launcher;
using Core.Models.Utils;
namespace Core.Models.Spt.Launcher;
public class LauncherV2RemoveResponse : IRequestData
{
public required bool Response { get; set; }
public required List<MiniProfile> Profiles { get; set; }
}
@@ -0,0 +1,8 @@
using Core.Models.Utils;
namespace Core.Models.Spt.Launcher;
public class LauncherV2TypesResponse : IRequestData
{
public required Dictionary<string, string> Response { get; set; }
}
@@ -0,0 +1,8 @@
using Core.Models.Utils;
namespace Core.Models.Spt.Launcher;
public record LauncherV2VersionResponse : IRequestData
{
public required LauncherV2CompatibleVersion Response { get; set; }
}
@@ -0,0 +1,67 @@
using Core.Annotations;
using Core.Callbacks;
using Core.DI;
using Core.Models.Eft.Launcher;
using Core.Utils;
namespace Core.Routers.Static;
[Injectable(InjectableTypeOverride = typeof(StaticRouter))]
public class LauncherV2StaticRouter : StaticRouter
{
public LauncherV2StaticRouter(LauncherV2Callbacks launcherV2Callbacks, JsonUtil jsonUtil) : base(
jsonUtil,
[
new RouteAction(
"/launcher/v2/ping",
(url, _, sessionID, _) => launcherV2Callbacks.Ping()
),
new RouteAction(
"/launcher/v2/types",
(url, _, sessionID, _) => launcherV2Callbacks.Types()
),
new RouteAction(
"/launcher/v2/Login",
(url, info, sessionID, _) => launcherV2Callbacks.Login(info as LoginRequestData),
typeof(LoginRequestData)
),
new RouteAction(
"/launcher/v2/Register",
(url, info, sessionID, _) => launcherV2Callbacks.Register(info as RegisterData),
typeof(RegisterData)
),
new RouteAction(
"/launcher/v2/passwordChange",
(url, info, sessionID, _) => launcherV2Callbacks.PasswordChange(info as ChangeRequestData),
typeof(ChangeRequestData)
),
new RouteAction(
"/launcher/v2/Remove",
(url, info, sessionID, _) => launcherV2Callbacks.Remove(info as LoginRequestData),
typeof(LoginRequestData)
),
new RouteAction(
"/launcher/v2/version",
(url, _, sessionID, _) => launcherV2Callbacks.CompatibleVersion()
),
new RouteAction(
"/launcher/v2/mods",
(url, _, sessionID, _) => launcherV2Callbacks.Mods()
),
new RouteAction(
"/launcher/v2/profiles",
(url, _, sessionID, _) => launcherV2Callbacks.Profiles()
),
new RouteAction(
"/launcher/v2/profile",
(url, _, sessionID, _) => launcherV2Callbacks.Profile()
),
new RouteAction(
"/launcher/v2/profileMods",
(url, _, sessionID, _) => launcherV2Callbacks.ProfileMods()
),
]
)
{
}
}