From 41756041d48eace67f26d9acb88be629b4405ebf Mon Sep 17 00:00:00 2001 From: Jesse Date: Mon, 21 Jul 2025 21:03:06 +0200 Subject: [PATCH] More migrations (#499) * Make abstract virtual * Handle nullability on List * Move migrations into their own folders, add new migration for minor 3.10 versions --- .../Migration/AbstractProfileMigration.cs | 6 +- .../Migrations/{ => 3.11}/HideoutSeed.cs | 7 ++- .../Migrations/3.11/ThreeTenMinorFixes.cs | 62 +++++++++++++++++++ .../{ => 3.11}/ThreeTenToThreeEleven.cs | 0 .../Migrations/{ => 4.0}/TheVoices.cs | 4 +- .../{ => 4.0}/ThreeElevenToFourZero.cs | 4 +- .../Services/ProfileMigratorService.cs | 5 ++ 7 files changed, 79 insertions(+), 9 deletions(-) rename Libraries/SPTarkov.Server.Core/Migration/Migrations/{ => 3.11}/HideoutSeed.cs (85%) create mode 100644 Libraries/SPTarkov.Server.Core/Migration/Migrations/3.11/ThreeTenMinorFixes.cs rename Libraries/SPTarkov.Server.Core/Migration/Migrations/{ => 3.11}/ThreeTenToThreeEleven.cs (100%) rename Libraries/SPTarkov.Server.Core/Migration/Migrations/{ => 4.0}/TheVoices.cs (96%) rename Libraries/SPTarkov.Server.Core/Migration/Migrations/{ => 4.0}/ThreeElevenToFourZero.cs (96%) diff --git a/Libraries/SPTarkov.Server.Core/Migration/AbstractProfileMigration.cs b/Libraries/SPTarkov.Server.Core/Migration/AbstractProfileMigration.cs index e7229628..e548a6f6 100644 --- a/Libraries/SPTarkov.Server.Core/Migration/AbstractProfileMigration.cs +++ b/Libraries/SPTarkov.Server.Core/Migration/AbstractProfileMigration.cs @@ -15,7 +15,11 @@ namespace SPTarkov.Server.Core.Migration JsonObject profile, IEnumerable previouslyRanMigrations ); - public abstract JsonObject? Migrate(JsonObject profile); + + public virtual JsonObject? Migrate(JsonObject profile) + { + return profile; + } public virtual bool PostMigrate(SptProfile profile) { diff --git a/Libraries/SPTarkov.Server.Core/Migration/Migrations/HideoutSeed.cs b/Libraries/SPTarkov.Server.Core/Migration/Migrations/3.11/HideoutSeed.cs similarity index 85% rename from Libraries/SPTarkov.Server.Core/Migration/Migrations/HideoutSeed.cs rename to Libraries/SPTarkov.Server.Core/Migration/Migrations/3.11/HideoutSeed.cs index ffa2bb72..0678b647 100644 --- a/Libraries/SPTarkov.Server.Core/Migration/Migrations/HideoutSeed.cs +++ b/Libraries/SPTarkov.Server.Core/Migration/Migrations/3.11/HideoutSeed.cs @@ -38,14 +38,15 @@ namespace SPTarkov.Server.Core.Migration.Migrations { var profileVersion = GetProfileVersion(profile); var fromRange = Range.Parse(FromVersion); - var versionMatches = fromRange.IsSatisfied(profileVersion); + var profileVersionMatches = fromRange.IsSatisfied(profileVersion); var seedNode = profile["characters"]?["pmc"]?["Hideout"]?["Seed"]; + // Check if the seed still has it's numeric value, this is not valid anymore var seedIsNumeric = seedNode is JsonValue seedValue && seedValue.TryGetValue(out _); - return versionMatches && seedIsNumeric; + return profileVersionMatches && seedIsNumeric; } public override JsonObject? Migrate(JsonObject profile) @@ -54,7 +55,7 @@ namespace SPTarkov.Server.Core.Migration.Migrations RandomNumberGenerator.GetBytes(16) ); - return profile; + return base.Migrate(profile); } } } diff --git a/Libraries/SPTarkov.Server.Core/Migration/Migrations/3.11/ThreeTenMinorFixes.cs b/Libraries/SPTarkov.Server.Core/Migration/Migrations/3.11/ThreeTenMinorFixes.cs new file mode 100644 index 00000000..432dddc8 --- /dev/null +++ b/Libraries/SPTarkov.Server.Core/Migration/Migrations/3.11/ThreeTenMinorFixes.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Nodes; +using System.Threading.Tasks; +using SPTarkov.DI.Annotations; +using SPTarkov.Server.Core.Models.Eft.Profile; + +namespace SPTarkov.Server.Core.Migration.Migrations +{ + /// + /// In the minor versions of 3.10 or somewhere in between these properties were added, it's possible that a profile has not updated + /// To these thus never having received them, re-add them here. + /// + [Injectable] + public class ThreeTenMinorFixes : AbstractProfileMigration + { + public override string FromVersion + { + get { return "~3.10"; } + } + + public override string ToVersion + { + get { return "3.11"; } + } + + public override string MigrationName + { + get { return "ThreeTenMinorFixes-SPTSharp"; } + } + + public override IEnumerable PrerequisiteMigrations + { + get { return [typeof(HideoutSeed)]; } + } + + public override bool CanMigrate( + JsonObject profile, + IEnumerable previouslyRanMigrations + ) + { + var cultistRewardsMissing = profile["spt"]?["cultistRewards"] == null; + var friendProfileIdsMissing = profile["friends"] == null; + + return cultistRewardsMissing || friendProfileIdsMissing; + } + + public override bool PostMigrate(SptProfile profile) + { + if (profile.SptData!.CultistRewards == null) + { + profile.SptData.CultistRewards = []; + } + + profile.FriendProfileIds ??= []; + + return base.PostMigrate(profile); + } + } +} diff --git a/Libraries/SPTarkov.Server.Core/Migration/Migrations/ThreeTenToThreeEleven.cs b/Libraries/SPTarkov.Server.Core/Migration/Migrations/3.11/ThreeTenToThreeEleven.cs similarity index 100% rename from Libraries/SPTarkov.Server.Core/Migration/Migrations/ThreeTenToThreeEleven.cs rename to Libraries/SPTarkov.Server.Core/Migration/Migrations/3.11/ThreeTenToThreeEleven.cs diff --git a/Libraries/SPTarkov.Server.Core/Migration/Migrations/TheVoices.cs b/Libraries/SPTarkov.Server.Core/Migration/Migrations/4.0/TheVoices.cs similarity index 96% rename from Libraries/SPTarkov.Server.Core/Migration/Migrations/TheVoices.cs rename to Libraries/SPTarkov.Server.Core/Migration/Migrations/4.0/TheVoices.cs index efb96d92..0bd694bf 100644 --- a/Libraries/SPTarkov.Server.Core/Migration/Migrations/TheVoices.cs +++ b/Libraries/SPTarkov.Server.Core/Migration/Migrations/4.0/TheVoices.cs @@ -30,7 +30,7 @@ namespace SPTarkov.Server.Core.Migration.Migrations public override IEnumerable PrerequisiteMigrations { - // Requires ThreeTenToThreeEleven on legacy profiles, due to that changing customization for the first time + // Requires ThreeTenToThreeEleven on legacy profiles, due to that adding the customization object for the first time get { return [typeof(ThreeTenToThreeEleven)]; } } @@ -59,7 +59,7 @@ namespace SPTarkov.Server.Core.Migration.Migrations HandleScavVoice(profile); } - return profile; + return base.Migrate(profile); } private void HandlePmcVoice(JsonObject profileObject) diff --git a/Libraries/SPTarkov.Server.Core/Migration/Migrations/ThreeElevenToFourZero.cs b/Libraries/SPTarkov.Server.Core/Migration/Migrations/4.0/ThreeElevenToFourZero.cs similarity index 96% rename from Libraries/SPTarkov.Server.Core/Migration/Migrations/ThreeElevenToFourZero.cs rename to Libraries/SPTarkov.Server.Core/Migration/Migrations/4.0/ThreeElevenToFourZero.cs index 86f157c1..9e7deb27 100644 --- a/Libraries/SPTarkov.Server.Core/Migration/Migrations/ThreeElevenToFourZero.cs +++ b/Libraries/SPTarkov.Server.Core/Migration/Migrations/4.0/ThreeElevenToFourZero.cs @@ -65,9 +65,7 @@ namespace SPTarkov.Server.Core.Migration.Migrations } } - //Todo: TaskConditionCounters CounterCrafting? - - return profile; + return base.Migrate(profile); } public override bool PostMigrate(SptProfile profile) diff --git a/Libraries/SPTarkov.Server.Core/Services/ProfileMigratorService.cs b/Libraries/SPTarkov.Server.Core/Services/ProfileMigratorService.cs index 4eb9bf69..3e1f14f2 100644 --- a/Libraries/SPTarkov.Server.Core/Services/ProfileMigratorService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/ProfileMigratorService.cs @@ -77,6 +77,11 @@ namespace SPTarkov.Server.Core.Services $"{profileId} successfully ran profile migration: {ranMigration.MigrationName}" ); + if (sptReadyProfile.SptData!.Migrations is null) + { + sptReadyProfile.SptData.Migrations = []; + } + sptReadyProfile.SptData.Migrations.Add( ranMigration.MigrationName, timeUtil.GetTimeStamp()