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 _logger, HashUtil _hashUtil, TimeUtil _timeUtil, RandomUtil _randomUtil, SaveServer _saveServer, DatabaseService _databaseService, LocalisationService _localisationService, ConfigServer _configServer, Watermark _watermark ) { protected CoreConfig _coreConfig = _configServer.GetConfig(); /// /// Returns a simple string of Pong! /// /// public string Ping() { return "Pong!"; } /// /// Returns all available profile types and descriptions for creation. /// - This is also localised. /// /// public Dictionary Types() { var result = new Dictionary(); 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; } /// /// Checks if login details were correct. /// /// /// public bool Login(LoginRequestData info) { var sessionId = GetSessionId(info); return sessionId is not null; } /// /// Register a new profile. /// /// /// 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; } /// /// Make a password change. /// /// /// public bool PasswordChange(ChangeRequestData info) { var sessionId = GetSessionId(info); if (sessionId is null) return false; _saveServer.GetProfile(sessionId).ProfileInfo!.Password = info.Password; return true; } /// /// Remove profile from server. /// /// /// public bool Remove(LoginRequestData info) { var sessionId = GetSessionId(info); return sessionId is not null && _saveServer.RemoveProfile(sessionId); } /// /// Gets the Servers SPT Version. /// - "4.0.0" /// /// public string SptVersion() { return _watermark.GetVersionTag(); } /// /// Gets the compatible EFT Version. /// - "0.14.9.31124" /// /// public string EftVersion() { return _coreConfig.CompatibleTarkovVersion; } /// /// Gets the Servers loaded mods. /// /// public Dictionary LoadedMods() { return new Dictionary(); } /// /// Creates the account from provided details. /// /// /// 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; } }