From 44fb32814b5e1c9f786a34412be7356055d80173 Mon Sep 17 00:00:00 2001 From: Wiz Date: Wed, 21 Aug 2024 17:30:09 +0100 Subject: [PATCH] Changed job licenses to properly work with flags. This also limits them to 21 loaded at any given time. --- CL.Common/LicenseEnums.cs | 7 +- CL.Game/LicenseManager.cs | 70 ++++++++++++++++--- .../Inspector/CustomLicenseContainerEditor.cs | 4 +- 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/CL.Common/LicenseEnums.cs b/CL.Common/LicenseEnums.cs index 2252e8b..a156283 100644 --- a/CL.Common/LicenseEnums.cs +++ b/CL.Common/LicenseEnums.cs @@ -1,8 +1,13 @@ -namespace CL.Common +using UnityEngine; + +namespace CL.Common { public enum LicenseType { + [Tooltip("General licenses are used for locomotives, generic utility like manual service or concurrent jobs, and most mod functionality")] General, + [Tooltip("Job licenses are the ones that show up on job papers, like job type, hazmat and military cargo, or train length\n" + + "Please be aware there's a limit of 21 JOB LICENSES that can be active at a given time")] Job } diff --git a/CL.Game/LicenseManager.cs b/CL.Game/LicenseManager.cs index 4e1b49c..fad7918 100644 --- a/CL.Game/LicenseManager.cs +++ b/CL.Game/LicenseManager.cs @@ -27,6 +27,7 @@ internal class LicenseManager private static GeneralLicenseType_v2? s_de2; private static JobLicenseType_v2? s_hazmat1; + private static List s_availableJobIds = GetAvailableJobLicenseIds(); public static GeneralLicenseType_v2 LicenseDE2 => s_de2 != null ? s_de2 : s_de2 = GeneralLicenseType.DE2.ToV2(); public static JobLicenseType_v2 LicenseHazmat1 => s_hazmat1 != null ? s_hazmat1 : s_hazmat1 = JobLicenses.Hazmat1.ToV2(); @@ -36,7 +37,7 @@ public static void LoadLicenses(UnityModManager.ModEntry mod) List newGeneralLicenses = new List(); List newJobLicenses = new List(); - // Find the 'cargo.json' files. + // Find the 'license.json' files. foreach (string jsonPath in Directory.EnumerateFiles(mod.Path, Constants.LicenseFile, SearchOption.AllDirectories)) { CustomLicense? l; @@ -175,6 +176,13 @@ private static bool TryLoadJobLicense(string jsonPath, CustomLicense l, out Cust return false; } + if (s_availableJobIds.Count <= 0) + { + CLMod.Error($"Cannot load job license '{l.Identifier}', maximum loaded job licenses reached!"); + v2 = null!; + return false; + } + v2 = l.ToJobV2(); v2.icon = TryLoadIcon(directory); @@ -319,22 +327,24 @@ public static void ApplyJobMappings() { CLMod.Log("Applying job mappings..."); - int highest = Constants.DefaultLicenseValue; - - // Get the highest ID, to start counting from there. - foreach (var item in JobMapping) - { - highest = Mathf.Max(highest, (int)item.Value); - } + s_availableJobIds = GetAvailableJobLicenseIds(); + s_availableJobIds.RemoveAll(x => JobMapping.ContainsValue(x)); AddedJobValues = new HashSet(); int newTypes = 0; foreach (var (_, v2) in AddedJobLicenses) { + if (s_availableJobIds.Count <= 0) + { + CLMod.Error("Reached maximum number of job licenses in save file, aborting mapping of new ones."); + break; + } + if (!JobMapping.TryGetValue(v2.id, out int type)) { - type = ++highest; + type = s_availableJobIds[0]; + s_availableJobIds.RemoveAt(0); JobMapping.Add(v2.id, type); newTypes++; } @@ -345,7 +355,47 @@ public static void ApplyJobMappings() // Recalculate caches with the new values. Globals.G.Types.RecalculateCaches(); - CLMod.Log($"Mappings applied: {AddedJobValues.Count}/{JobMapping.Count} (new: {newTypes}), highest value is {highest}"); + CLMod.Log($"Mappings applied: {AddedJobValues.Count}/{JobMapping.Count} (new: {newTypes})"); } + + // Mod limits job licenses to 21 due to the way they work in DV. + public static List GetAvailableJobLicenseIds() => new List + { + // Basic: 0 + // Hazmat 1: 1 + // Hazmat 2: 2 + // Hazmat 3: 4 + // Military 1: 8 + // Military 2: 16 + // Military 3: 32 + // Passenger Jobs (mod compatibility): 64 + 128, + 256, + // Freight Haul: 512 + // Shunting: 1024 + // Logistical Haul: 2048 + 4096, + 8192, + // Train Length 1: 16384 + // Train Length 2: 32768 + 65536, + // Lazyness ensues: + 1 << 17, + 1 << 18, + 1 << 19, + 1 << 20, + 1 << 21, + 1 << 22, + 1 << 23, + 1 << 24, + 1 << 25, + 1 << 26, + 1 << 27, + 1 << 28, + 1 << 29, + 1 << 30, + 1 << 31, + 1 << 32 + }; } } diff --git a/CL.Unity/Inspector/CustomLicenseContainerEditor.cs b/CL.Unity/Inspector/CustomLicenseContainerEditor.cs index 16a0d07..afdef04 100644 --- a/CL.Unity/Inspector/CustomLicenseContainerEditor.cs +++ b/CL.Unity/Inspector/CustomLicenseContainerEditor.cs @@ -138,7 +138,9 @@ private static void Autofill(CustomLicense license, FillType fillType) if (!string.IsNullOrEmpty(description.Value)) { - Debug.Log("Please fill out the fields marked [LIKE THIS] in the description of the license!"); + EditorUtility.DisplayDialog("Attention", + "Please fill out the fields marked [LIKE THIS] in the description of the license!", + "I will thanks"); } } }