From b8411b3553febe4fee4a7c2e7108d777ef3d84d4 Mon Sep 17 00:00:00 2001 From: CWX Date: Sat, 11 Jan 2025 23:30:13 +0000 Subject: [PATCH] Add routers, Fix tarkovVersioning, format --- Core/Routers/SaveLoad/HealthSaveLoadRouter.cs | 47 +++++++++++++++++++ Core/Routers/SaveLoad/InraidSaveLoadRouter.cs | 22 +++++++++ .../SaveLoad/InsuranceSaveLoadRouter.cs | 22 +++++++++ .../Routers/SaveLoad/ProfileSaveLoadRouter.cs | 22 +++++++++ Core/Servers/SaveServer.cs | 30 +++++++----- Core/Utils/Watermark.cs | 2 +- 6 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 Core/Routers/SaveLoad/HealthSaveLoadRouter.cs create mode 100644 Core/Routers/SaveLoad/InraidSaveLoadRouter.cs create mode 100644 Core/Routers/SaveLoad/InsuranceSaveLoadRouter.cs create mode 100644 Core/Routers/SaveLoad/ProfileSaveLoadRouter.cs diff --git a/Core/Routers/SaveLoad/HealthSaveLoadRouter.cs b/Core/Routers/SaveLoad/HealthSaveLoadRouter.cs new file mode 100644 index 00000000..843fe0ae --- /dev/null +++ b/Core/Routers/SaveLoad/HealthSaveLoadRouter.cs @@ -0,0 +1,47 @@ +using Core.Annotations; +using Core.DI; +using Core.Models.Eft.Profile; + +namespace Core.Routers.SaveLoad; + +[Injectable] +public class HealthSaveLoadRouter : SaveLoadRouter +{ + protected override List GetHandledRoutes() + { + return new List() { new HandledRoute("spt-health", false) }; + } + + public override SptProfile HandleLoad(SptProfile profile) + { + if (profile?.VitalityData == null) + profile.VitalityData = new() { Health = null, Effects = null }; + + profile.VitalityData.Health = new() + { + Hydration = 0, + Energy = 0, + Temperature = 0, + Head = 0, + Chest = 0, + Stomach = 0, + LeftArm = 0, + RightArm = 0, + LeftLeg = 0, + RightLeg = 0 + }; + + profile.VitalityData.Effects = new() + { + Head = new(), + Chest = new(), + Stomach = new(), + LeftArm = new(), + RightArm = new(), + LeftLeg = new(), + RightLeg = new() + }; + + return profile; + } +} diff --git a/Core/Routers/SaveLoad/InraidSaveLoadRouter.cs b/Core/Routers/SaveLoad/InraidSaveLoadRouter.cs new file mode 100644 index 00000000..b288e1f8 --- /dev/null +++ b/Core/Routers/SaveLoad/InraidSaveLoadRouter.cs @@ -0,0 +1,22 @@ +using Core.Annotations; +using Core.DI; +using Core.Models.Eft.Profile; + +namespace Core.Routers.SaveLoad; + +[Injectable] +public class InraidSaveLoadRouter : SaveLoadRouter +{ + protected override List GetHandledRoutes() + { + return new List() { new HandledRoute("spt-inraid", false) }; + } + + public override SptProfile HandleLoad(SptProfile profile) + { + if (profile.InraidData == null) + profile.InraidData = new Inraid { Location = "none", Character = "none" }; + + return profile; + } +} diff --git a/Core/Routers/SaveLoad/InsuranceSaveLoadRouter.cs b/Core/Routers/SaveLoad/InsuranceSaveLoadRouter.cs new file mode 100644 index 00000000..4b871c9c --- /dev/null +++ b/Core/Routers/SaveLoad/InsuranceSaveLoadRouter.cs @@ -0,0 +1,22 @@ +using Core.Annotations; +using Core.DI; +using Core.Models.Eft.Profile; + +namespace Core.Routers.SaveLoad; + +[Injectable] +public class InsuranceSaveLoadRouter : SaveLoadRouter +{ + protected override List GetHandledRoutes() + { + return new List() { new HandledRoute("spt-insurance", false) }; + } + + public override SptProfile HandleLoad(SptProfile profile) + { + if (profile.InsuranceList == null) + profile.InsuranceList = new List(); + + return profile; + } +} diff --git a/Core/Routers/SaveLoad/ProfileSaveLoadRouter.cs b/Core/Routers/SaveLoad/ProfileSaveLoadRouter.cs new file mode 100644 index 00000000..66458599 --- /dev/null +++ b/Core/Routers/SaveLoad/ProfileSaveLoadRouter.cs @@ -0,0 +1,22 @@ +using Core.Annotations; +using Core.DI; +using Core.Models.Eft.Profile; + +namespace Core.Routers.SaveLoad; + +[Injectable] +public class ProfileSaveLoadRouter : SaveLoadRouter +{ + protected override List GetHandledRoutes() + { + return new List() { new HandledRoute("spt-profile", false) }; + } + + public override SptProfile HandleLoad(SptProfile profile) + { + if (profile.CharacterData == null) + profile.CharacterData = new Characters { PmcData = new(), ScavData = new() }; + + return profile; + } +} diff --git a/Core/Servers/SaveServer.cs b/Core/Servers/SaveServer.cs index fa7db3c2..62f4a171 100644 --- a/Core/Servers/SaveServer.cs +++ b/Core/Servers/SaveServer.cs @@ -85,9 +85,11 @@ public class SaveServer // load profiles var stopwatch = Stopwatch.StartNew(); - foreach (var file in files) { + foreach (var file in files) + { LoadProfile(_vfs.StripExtension(file)); } + stopwatch.Stop(); _logger.Debug($"{files.Count()} Profiles took: {stopwatch.ElapsedMilliseconds}ms to load."); } @@ -99,7 +101,8 @@ public class SaveServer { // Save every profile var totalTime = 0L; - foreach ( var sessionID in profiles) { + foreach (var sessionID in profiles) + { totalTime += SaveProfile(sessionID.Key); } @@ -139,7 +142,7 @@ public class SaveServer * @returns Dictionary of ISptProfile */ public Dictionary GetProfiles() => profiles; - + /** * Delete a profile by id * @param sessionID Id of profile to remove @@ -171,7 +174,7 @@ public class SaveServer new SptProfile() { ProfileInfo = profileInfo, - CharacterData = new Characters() { PmcData = new PmcData(), ScavData = new PmcData() } + CharacterData = new Characters() { PmcData = new(), ScavData = new() } }); } @@ -191,8 +194,8 @@ public class SaveServer */ public void LoadProfile(string sessionID) { - var filename = $"{sessionID}.json"; - var filePath = $"{profileFilepath}{filename}"; + var filename = $"{sessionID}.json"; + var filePath = $"{profileFilepath}{filename}"; if (_vfs.FileExists(filePath)) { // File found, store in profiles[] @@ -200,7 +203,8 @@ public class SaveServer } // Run callbacks - foreach (var callback in _saveLoadRouters) { + foreach (var callback in _saveLoadRouters) // HealthSaveLoadRouter, InraidSaveLoadRouter, InsuranceSaveLoadRouter, ProfileSaveLoadRouter. THESE SHOULD EXIST IN HERE + { profiles[sessionID] = callback.HandleLoad(GetProfile(sessionID)); } } @@ -216,9 +220,9 @@ public class SaveServer var filePath = $"{profileFilepath}{sessionID}.json"; // Run pre-save callbacks before we save into json - foreach ( var callback in onBeforeSaveCallbacks) + foreach (var callback in onBeforeSaveCallbacks) { - var previous = profiles[sessionID]; + var previous = profiles[sessionID]; try { profiles[sessionID] = onBeforeSaveCallbacks[callback.Key](profiles[sessionID]); @@ -231,9 +235,10 @@ public class SaveServer } var start = Stopwatch.StartNew(); - var jsonProfile = _jsonUtil.Serialize(profiles[sessionID], !_configServer.GetConfig(ConfigTypes.CORE).Features.CompressProfile); - var fmd5 = _hashUtil.GenerateMd5ForData(jsonProfile); - if (!saveMd5.TryGetValue(sessionID, out var currentMd5) || currentMd5 != fmd5) { + var jsonProfile = _jsonUtil.Serialize(profiles[sessionID], !_configServer.GetConfig(ConfigTypes.CORE).Features.CompressProfile); + var fmd5 = _hashUtil.GenerateMd5ForData(jsonProfile); + if (!saveMd5.TryGetValue(sessionID, out var currentMd5) || currentMd5 != fmd5) + { saveMd5[sessionID] = fmd5; // save profile to disk _vfs.WriteFile(filePath, jsonProfile); @@ -256,6 +261,7 @@ public class SaveServer profiles.Remove(sessionID); _vfs.DeleteFile(file); } + return !_vfs.FileExists(file); } } diff --git a/Core/Utils/Watermark.cs b/Core/Utils/Watermark.cs index 882303ed..2622a1b1 100644 --- a/Core/Utils/Watermark.cs +++ b/Core/Utils/Watermark.cs @@ -110,7 +110,7 @@ public class Watermark { var versionTag = /*ProgramStatics.DEBUG&*/ $"{sptVersion} - {_localisationService.GetText("bleeding_edge_build")}"; if (withEftVersion) { - var tarkovVersion = sptConfig.CompatibleTarkovVersion.Split(".").First(); + var tarkovVersion = sptConfig.CompatibleTarkovVersion.Split(".").Last(); return $"{versionTag} ({tarkovVersion})"; }