From e4a80f1f42aa166f48096b45233a366543414569 Mon Sep 17 00:00:00 2001 From: Archangel Date: Sun, 17 Aug 2025 17:57:26 +0200 Subject: [PATCH] Make main database properties immutable --- .../Models/Eft/Common/Globals.cs | 10 +- .../Models/Eft/Common/Tables/Trader.cs | 8 +- .../Models/Spt/Bots/Bots.cs | 6 +- .../Models/Spt/Hideout/Hideout.cs | 12 +- .../Models/Spt/Server/DatabaseTables.cs | 20 +-- .../Models/Spt/Server/LocaleBase.cs | 6 +- .../Models/Spt/Server/Locations.cs | 40 ++--- .../Models/Spt/Server/SettingsBase.cs | 2 +- .../Models/Spt/Templates/Templates.cs | 28 ++-- .../Services/DatabaseService.cs | 144 +++--------------- 10 files changed, 86 insertions(+), 190 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Models/Eft/Common/Globals.cs b/Libraries/SPTarkov.Server.Core/Models/Eft/Common/Globals.cs index 4c58b747..df79acb6 100644 --- a/Libraries/SPTarkov.Server.Core/Models/Eft/Common/Globals.cs +++ b/Libraries/SPTarkov.Server.Core/Models/Eft/Common/Globals.cs @@ -12,19 +12,19 @@ public record Globals public Dictionary ExtensionData { get; init; } = []; [JsonPropertyName("config")] - public Config Configuration { get; set; } + public required Config Configuration { get; init; } [JsonPropertyName("LocationInfection")] - public LocationInfection LocationInfection { get; set; } + public required LocationInfection LocationInfection { get; init; } [JsonPropertyName("bot_presets")] - public IEnumerable BotPresets { get; set; } + public required IEnumerable BotPresets { get; init; } [JsonPropertyName("BotWeaponScatterings")] - public IEnumerable BotWeaponScatterings { get; set; } + public required IEnumerable BotWeaponScatterings { get; init; } [JsonPropertyName("ItemPresets")] - public Dictionary ItemPresets { get; set; } + public required Dictionary ItemPresets { get; init; } } public record PlayerSettings diff --git a/Libraries/SPTarkov.Server.Core/Models/Eft/Common/Tables/Trader.cs b/Libraries/SPTarkov.Server.Core/Models/Eft/Common/Tables/Trader.cs index 57cc0a59..5b9f95c2 100644 --- a/Libraries/SPTarkov.Server.Core/Models/Eft/Common/Tables/Trader.cs +++ b/Libraries/SPTarkov.Server.Core/Models/Eft/Common/Tables/Trader.cs @@ -12,16 +12,16 @@ public record Trader public Dictionary ExtensionData { get; init; } = []; [JsonPropertyName("assort")] - public TraderAssort Assort { get; set; } + public required TraderAssort Assort { get; set; } [JsonPropertyName("base")] - public TraderBase Base { get; set; } + public required TraderBase Base { get; init; } [JsonPropertyName("dialogue")] - public Dictionary?> Dialogue { get; set; } + public required Dictionary?> Dialogue { get; init; } [JsonPropertyName("questassort")] - public Dictionary>? QuestAssort { get; set; } + public required Dictionary> QuestAssort { get; init; } [JsonPropertyName("suits")] public List? Suits { get; set; } diff --git a/Libraries/SPTarkov.Server.Core/Models/Spt/Bots/Bots.cs b/Libraries/SPTarkov.Server.Core/Models/Spt/Bots/Bots.cs index 69a32dd9..4a4f635c 100644 --- a/Libraries/SPTarkov.Server.Core/Models/Spt/Bots/Bots.cs +++ b/Libraries/SPTarkov.Server.Core/Models/Spt/Bots/Bots.cs @@ -9,11 +9,11 @@ public record Bots public Dictionary ExtensionData { get; init; } = []; [JsonPropertyName("types")] - public Dictionary Types { get; set; } + public required Dictionary Types { get; init; } [JsonPropertyName("base")] - public BotBase Base { get; set; } + public required BotBase Base { get; init; } [JsonPropertyName("core")] - public Dictionary Core { get; set; } + public required Dictionary Core { get; init; } } diff --git a/Libraries/SPTarkov.Server.Core/Models/Spt/Hideout/Hideout.cs b/Libraries/SPTarkov.Server.Core/Models/Spt/Hideout/Hideout.cs index 96dd0a9d..968aec45 100644 --- a/Libraries/SPTarkov.Server.Core/Models/Spt/Hideout/Hideout.cs +++ b/Libraries/SPTarkov.Server.Core/Models/Spt/Hideout/Hideout.cs @@ -9,20 +9,20 @@ public record Hideout public Dictionary ExtensionData { get; init; } = []; [JsonPropertyName("areas")] - public List Areas { get; set; } + public required List Areas { get; init; } [JsonPropertyName("customAreas")] - public List? CustomAreas { get; set; } + public required List? CustomAreas { get; init; } [JsonPropertyName("customisation")] - public HideoutCustomisation Customisation { get; set; } + public required HideoutCustomisation Customisation { get; init; } [JsonPropertyName("production")] - public HideoutProductionData Production { get; set; } + public required HideoutProductionData Production { get; init; } [JsonPropertyName("settings")] - public HideoutSettingsBase Settings { get; set; } + public required HideoutSettingsBase Settings { get; init; } [JsonPropertyName("qte")] - public List Qte { get; set; } + public required List Qte { get; init; } } diff --git a/Libraries/SPTarkov.Server.Core/Models/Spt/Server/DatabaseTables.cs b/Libraries/SPTarkov.Server.Core/Models/Spt/Server/DatabaseTables.cs index 6cf10914..26fefc3f 100644 --- a/Libraries/SPTarkov.Server.Core/Models/Spt/Server/DatabaseTables.cs +++ b/Libraries/SPTarkov.Server.Core/Models/Spt/Server/DatabaseTables.cs @@ -10,23 +10,23 @@ public record DatabaseTables [JsonExtensionData] public Dictionary ExtensionData { get; init; } = []; - public required Bots.Bots Bots { get; set; } + public required Bots.Bots Bots { get; init; } - public required Hideout.Hideout Hideout { get; set; } + public required Hideout.Hideout Hideout { get; init; } - public required LocaleBase Locales { get; set; } + public required LocaleBase Locales { get; init; } - public required Locations Locations { get; set; } + public required Locations Locations { get; init; } - public required Match Match { get; set; } + public required Match Match { get; init; } - public required Templates.Templates Templates { get; set; } + public required Templates.Templates Templates { get; init; } - public required Dictionary Traders { get; set; } + public required Dictionary Traders { get; init; } - public required Globals Globals { get; set; } + public required Globals Globals { get; init; } - public required ServerBase Server { get; set; } + public required ServerBase Server { get; init; } - public required SettingsBase Settings { get; set; } + public required SettingsBase Settings { get; init; } } diff --git a/Libraries/SPTarkov.Server.Core/Models/Spt/Server/LocaleBase.cs b/Libraries/SPTarkov.Server.Core/Models/Spt/Server/LocaleBase.cs index 9f29a1e2..8666e0b9 100644 --- a/Libraries/SPTarkov.Server.Core/Models/Spt/Server/LocaleBase.cs +++ b/Libraries/SPTarkov.Server.Core/Models/Spt/Server/LocaleBase.cs @@ -13,11 +13,11 @@ public record LocaleBase /// THIS IS LAZY LOADED AND YOUR CHANGES WILL NOT BE SAVED /// [JsonPropertyName("global")] - public required Dictionary>> Global { get; set; } + public required Dictionary>> Global { get; init; } [JsonPropertyName("menu")] - public required Dictionary> Menu { get; set; } + public required Dictionary> Menu { get; init; } [JsonPropertyName("languages")] - public required Dictionary Languages { get; set; } + public required Dictionary Languages { get; init; } } diff --git a/Libraries/SPTarkov.Server.Core/Models/Spt/Server/Locations.cs b/Libraries/SPTarkov.Server.Core/Models/Spt/Server/Locations.cs index d4ea59a0..d9781277 100644 --- a/Libraries/SPTarkov.Server.Core/Models/Spt/Server/Locations.cs +++ b/Libraries/SPTarkov.Server.Core/Models/Spt/Server/Locations.cs @@ -58,67 +58,67 @@ public record Locations private Dictionary? _locationDictionaryCache; [JsonPropertyName("bigmap")] - public Eft.Common.Location Bigmap { get; set; } + public required Eft.Common.Location Bigmap { get; init; } [JsonPropertyName("develop")] - public Eft.Common.Location? Develop { get; set; } + public required Eft.Common.Location? Develop { get; init; } [JsonPropertyName("factory4_day")] - public Eft.Common.Location Factory4Day { get; set; } + public required Eft.Common.Location Factory4Day { get; init; } [JsonPropertyName("factory4_night")] - public Eft.Common.Location Factory4Night { get; set; } + public required Eft.Common.Location Factory4Night { get; init; } [JsonPropertyName("hideout")] - public Eft.Common.Location? Hideout { get; set; } + public required Eft.Common.Location? Hideout { get; init; } [JsonPropertyName("interchange")] - public Eft.Common.Location Interchange { get; set; } + public required Eft.Common.Location Interchange { get; init; } [JsonPropertyName("laboratory")] - public Eft.Common.Location Laboratory { get; set; } + public required Eft.Common.Location Laboratory { get; init; } [JsonPropertyName("lighthouse")] - public Eft.Common.Location Lighthouse { get; set; } + public required Eft.Common.Location Lighthouse { get; init; } [JsonPropertyName("privatearea")] - public Eft.Common.Location? PrivateArea { get; set; } + public required Eft.Common.Location? PrivateArea { get; init; } [JsonPropertyName("rezervbase")] - public Eft.Common.Location RezervBase { get; set; } + public required Eft.Common.Location RezervBase { get; init; } [JsonPropertyName("shoreline")] - public Eft.Common.Location Shoreline { get; set; } + public required Eft.Common.Location Shoreline { get; init; } [JsonPropertyName("suburbs")] - public Eft.Common.Location? Suburbs { get; set; } + public required Eft.Common.Location? Suburbs { get; init; } [JsonPropertyName("tarkovstreets")] - public Eft.Common.Location TarkovStreets { get; set; } + public required Eft.Common.Location TarkovStreets { get; init; } [JsonPropertyName("labyrinth")] - public Eft.Common.Location Labyrinth { get; set; } + public required Eft.Common.Location Labyrinth { get; init; } [JsonPropertyName("terminal")] - public Eft.Common.Location? Terminal { get; set; } + public required Eft.Common.Location? Terminal { get; init; } [JsonPropertyName("town")] - public Eft.Common.Location? Town { get; set; } + public required Eft.Common.Location? Town { get; init; } [JsonPropertyName("woods")] - public Eft.Common.Location Woods { get; set; } + public required Eft.Common.Location Woods { get; init; } [JsonPropertyName("sandbox")] - public Eft.Common.Location Sandbox { get; set; } + public required Eft.Common.Location Sandbox { get; init; } [JsonPropertyName("sandbox_high")] - public Eft.Common.Location SandboxHigh { get; set; } + public required Eft.Common.Location SandboxHigh { get; init; } /// /// Holds a mapping of the linkages between locations on the UI /// [JsonPropertyName("base")] - public LocationsBase? Base { get; set; } + public required LocationsBase Base { get; init; } /// /// Get map locations as a dictionary, keyed by its name e.g. Factory4Day diff --git a/Libraries/SPTarkov.Server.Core/Models/Spt/Server/SettingsBase.cs b/Libraries/SPTarkov.Server.Core/Models/Spt/Server/SettingsBase.cs index 1a96d6d9..e4352f2b 100644 --- a/Libraries/SPTarkov.Server.Core/Models/Spt/Server/SettingsBase.cs +++ b/Libraries/SPTarkov.Server.Core/Models/Spt/Server/SettingsBase.cs @@ -9,7 +9,7 @@ public record SettingsBase public Dictionary ExtensionData { get; init; } = []; [JsonPropertyName("config")] - public Config Configuration { get; set; } + public required Config Configuration { get; init; } } public record Config diff --git a/Libraries/SPTarkov.Server.Core/Models/Spt/Templates/Templates.cs b/Libraries/SPTarkov.Server.Core/Models/Spt/Templates/Templates.cs index 207e8357..0989aa40 100644 --- a/Libraries/SPTarkov.Server.Core/Models/Spt/Templates/Templates.cs +++ b/Libraries/SPTarkov.Server.Core/Models/Spt/Templates/Templates.cs @@ -11,62 +11,62 @@ public record Templates public Dictionary ExtensionData { get; init; } = []; [JsonPropertyName("character")] - public List Character { get; set; } + public required List Character { get; init; } [JsonPropertyName("customisationStorage")] - public List CustomisationStorage { get; set; } + public required List CustomisationStorage { get; init; } [JsonPropertyName("items")] - public Dictionary Items { get; set; } + public required Dictionary Items { get; init; } [JsonPropertyName("prestige")] - public Prestige? Prestige { get; set; } + public required Prestige Prestige { get; init; } [JsonPropertyName("quests")] - public Dictionary Quests { get; set; } + public required Dictionary Quests { get; init; } [JsonPropertyName("repeatableQuests")] - public RepeatableQuestDatabase RepeatableQuests { get; set; } + public required RepeatableQuestDatabase RepeatableQuests { get; init; } [JsonPropertyName("handbook")] - public HandbookBase Handbook { get; set; } + public required HandbookBase Handbook { get; init; } [JsonPropertyName("customization")] - public Dictionary Customization { get; set; } + public required Dictionary Customization { get; init; } /// /// The profile templates listed in the launcher on profile creation, split by account type (e.g. Standard) then side (e.g. bear/usec) /// [JsonPropertyName("profiles")] - public Dictionary Profiles { get; set; } + public required Dictionary Profiles { get; init; } /// /// Flea prices of items - gathered from online flea market dump /// [JsonPropertyName("prices")] - public Dictionary Prices { get; set; } + public required Dictionary Prices { get; init; } /// /// Default equipment loadouts that show on main inventory screen /// [JsonPropertyName("defaultEquipmentPresets")] - public List DefaultEquipmentPresets { get; set; } + public required List DefaultEquipmentPresets { get; init; } /// /// Achievements /// [JsonPropertyName("achievements")] - public List Achievements { get; set; } + public required List Achievements { get; init; } /// /// Achievements /// [JsonPropertyName("customAchievements")] - public List CustomAchievements { get; set; } + public required List CustomAchievements { get; init; } /// /// Location services data /// [JsonPropertyName("locationServices")] - public LocationServices LocationServices { get; set; } + public required LocationServices LocationServices { get; init; } } diff --git a/Libraries/SPTarkov.Server.Core/Services/DatabaseService.cs b/Libraries/SPTarkov.Server.Core/Services/DatabaseService.cs index 2dc1165a..524cf877 100644 --- a/Libraries/SPTarkov.Server.Core/Services/DatabaseService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/DatabaseService.cs @@ -37,56 +37,31 @@ public class DatabaseService( /// assets/database/bots/ public Bots GetBots() { - if (databaseServer.GetTables().Bots == null) - { - throw new Exception(serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/bots")); - } - - return databaseServer.GetTables().Bots!; + return databaseServer.GetTables().Bots; } /// assets/database/globals.json public Globals GetGlobals() { - if (databaseServer.GetTables().Globals == null) - { - throw new Exception(serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/globals.json")); - } - - return databaseServer.GetTables().Globals!; + return databaseServer.GetTables().Globals; } /// assets/database/hideout/ public Hideout GetHideout() { - if (databaseServer.GetTables().Hideout == null) - { - throw new Exception(serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/hideout")); - } - - return databaseServer.GetTables().Hideout!; + return databaseServer.GetTables().Hideout; } /// assets/database/locales/ public LocaleBase GetLocales() { - if (databaseServer.GetTables().Locales == null) - { - throw new Exception(serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/locales")); - } - - return databaseServer.GetTables().Locales!; + return databaseServer.GetTables().Locales; } /// assets/database/locations public Locations GetLocations() { - if (databaseServer.GetTables().Locations == null) - { - throw new Exception(serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/locations")); - } - - return databaseServer.GetTables().Locations!; + return databaseServer.GetTables().Locations; } /// @@ -96,7 +71,7 @@ public class DatabaseService( /// assets/database/locations/ public Location? GetLocation(string locationId) { - var desiredLocation = GetLocations()?.GetByJsonProp(locationId.ToLowerInvariant()); + var desiredLocation = GetLocations().GetByJsonProp(locationId.ToLowerInvariant()); if (desiredLocation == null) { logger.Error(serverLocalisationService.GetText("database-no_location_found_with_id", locationId)); @@ -110,158 +85,79 @@ public class DatabaseService( /// assets/database/match/ public Match GetMatch() { - if (databaseServer.GetTables().Match == null) - { - throw new Exception(serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/locales")); - } - - return databaseServer.GetTables().Match!; + return databaseServer.GetTables().Match; } /// assets/database/server.json public ServerBase GetServer() { - if (databaseServer.GetTables().Server == null) - { - throw new Exception(serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/server.json")); - } - - return databaseServer.GetTables().Server!; + return databaseServer.GetTables().Server; } /// assets/database/settings.json public SettingsBase GetSettings() { - if (databaseServer.GetTables().Settings == null) - { - throw new Exception(serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/settings.json")); - } - - return databaseServer.GetTables().Settings!; + return databaseServer.GetTables().Settings; } /// assets/database/templates/ public Templates GetTemplates() { - if (databaseServer.GetTables().Templates == null) - { - throw new Exception(serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/templates")); - } - - return databaseServer.GetTables().Templates!; + return databaseServer.GetTables().Templates; } /// assets/database/templates/achievements.json public List GetAchievements() { - if (databaseServer.GetTables().Templates?.Achievements == null) - { - throw new Exception( - serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/templates/achievements.json") - ); - } - - return databaseServer.GetTables().Templates?.Achievements!; + return databaseServer.GetTables().Templates.Achievements; } /// assets/database/templates/customAchievements.json public List GetCustomAchievements() { - if (databaseServer.GetTables().Templates?.Achievements == null) - { - throw new Exception( - serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/templates/customAchievements.json") - ); - } - - return databaseServer.GetTables().Templates?.CustomAchievements!; + return databaseServer.GetTables().Templates.CustomAchievements; } /// assets/database/templates/customisation.json - public Dictionary GetCustomization() + public Dictionary GetCustomization() { - if (databaseServer.GetTables().Templates?.Customization == null) - { - throw new Exception( - serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/templates/customization.json") - ); - } - - return databaseServer.GetTables().Templates?.Customization!; + return databaseServer.GetTables().Templates.Customization; } /// assets/database/templates/handbook.json public HandbookBase GetHandbook() { - if (databaseServer.GetTables().Templates?.Handbook == null) - { - throw new Exception( - serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/templates/handbook.json") - ); - } - - return databaseServer.GetTables().Templates?.Handbook!; + return databaseServer.GetTables().Templates.Handbook; } /// assets/database/templates/items.json public Dictionary GetItems() { - if (databaseServer.GetTables().Templates?.Items == null) - { - throw new Exception(serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/templates/items.json")); - } - - return databaseServer.GetTables().Templates?.Items!; + return databaseServer.GetTables().Templates.Items; } /// assets/database/templates/prices.json public Dictionary GetPrices() { - if (databaseServer.GetTables().Templates?.Prices == null) - { - throw new Exception( - serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/templates/prices.json") - ); - } - - return databaseServer.GetTables().Templates?.Prices!; + return databaseServer.GetTables().Templates.Prices; } /// assets/database/templates/profiles.json public Dictionary GetProfileTemplates() { - if (databaseServer.GetTables().Templates?.Profiles == null) - { - throw new Exception( - serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/templates/profiles.json") - ); - } - - return databaseServer.GetTables().Templates?.Profiles!; + return databaseServer.GetTables().Templates.Profiles; } /// assets/database/templates/quests.json public Dictionary GetQuests() { - if (databaseServer.GetTables().Templates?.Quests == null) - { - throw new Exception( - serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/templates/quests.json") - ); - } - - return databaseServer.GetTables().Templates?.Quests!; + return databaseServer.GetTables().Templates.Quests; } /// assets/database/traders/ public Dictionary GetTraders() { - if (databaseServer.GetTables().Traders == null) - { - throw new Exception(serverLocalisationService.GetText("database-data_at_path_missing", "assets/database/traders")); - } - - return databaseServer.GetTables().Traders!; + return databaseServer.GetTables().Traders; } ///