diff --git a/SubnauticaRandomiser/Initialiser.cs b/SubnauticaRandomiser/Initialiser.cs index f05d60d..935ef7c 100644 --- a/SubnauticaRandomiser/Initialiser.cs +++ b/SubnauticaRandomiser/Initialiser.cs @@ -20,7 +20,7 @@ internal class Initialiser : BaseUnityPlugin { public const string GUID = "com.github.tinyhoot.SubnauticaRandomiser"; public const string NAME = "Subnautica Randomiser"; - public const string VERSION = "0.10.0"; + public const string VERSION = "0.10.1"; // Files and structure. internal static string _ModDirectory; diff --git a/SubnauticaRandomiser/Logic/AuroraLogic.cs b/SubnauticaRandomiser/Logic/AuroraLogic.cs index e55b1e0..8f1e9bb 100644 --- a/SubnauticaRandomiser/Logic/AuroraLogic.cs +++ b/SubnauticaRandomiser/Logic/AuroraLogic.cs @@ -74,7 +74,7 @@ public void RandomiseDoorCodes() // Keypads only have numbers 1-9, zeroes cannot be entered at all. while (code.Contains("0")) { - code = _random.Next(1111, 9999).ToString().PadLeft(4, '3'); + code = _random.Next(1111, 9999).ToString(); } _log.Debug($"[AR] Assigning accessCode {code} to {classId}"); keyCodes.Add(classId, code); diff --git a/SubnauticaRandomiser/Logic/CoreLogic.cs b/SubnauticaRandomiser/Logic/CoreLogic.cs index 1ddbecd..457ffc0 100644 --- a/SubnauticaRandomiser/Logic/CoreLogic.cs +++ b/SubnauticaRandomiser/Logic/CoreLogic.cs @@ -5,7 +5,6 @@ using System.Linq; using System.Threading.Tasks; using HarmonyLib; -using JetBrains.Annotations; using SubnauticaRandomiser.Handlers; using SubnauticaRandomiser.Interfaces; using SubnauticaRandomiser.Logic.Recipes; @@ -32,6 +31,7 @@ internal class CoreLogic : MonoBehaviour public EntityHandler EntityHandler { get; private set; } public IRandomHandler Random { get; private set; } + private Harmony _harmony; private ProgressionManager _manager; private SpoilerLog _spoilerLog; @@ -94,6 +94,11 @@ private void Awake() _spoilerLog = gameObject.EnsureComponent(); } + private void OnDestroy() + { + _harmony?.UnpatchSelf(); + } + private void EnableModules() { if (!_Config.sSpawnPoint.Equals("Vanilla")) @@ -194,12 +199,14 @@ private IEnumerator RandomiseMainEntities(List notRandomised) yield return null; if (circuitbreaker > 3000) { - _Log.InGameMessage("[Core] Failed to randomise items: stuck in infinite loop!"); + _Log.InGameMessage("[Core] Failed to randomise entities: stuck in infinite loop!"); _Log.Fatal("[Core] Encountered infinite loop, aborting!"); throw new TimeoutException("Encountered infinite loop while randomising!"); } LogicEntity nextEntity = ChooseNextEntity(notRandomised); + if (nextEntity is null) + continue; // Try to get a handler for this type of entity. ILogicModule handler = _handlingModules.GetOrDefault(nextEntity.EntityType, null); if (handler is null) @@ -242,7 +249,11 @@ public void AddPrerequisitesAsPriority(LogicEntity entity) { LogicEntity prereq = EntityHandler.GetEntity(techType); if (!HasRandomised(prereq)) + { _priorityEntities.Insert(0, prereq); + // Ensure that the prerequisites' requirements are also fulfilled. + AddPrerequisitesAsPriority(prereq); + } } } @@ -281,7 +292,6 @@ internal void ApplyAllChanges() /// Get the next entity to be randomised, prioritising essential or elective ones. /// /// The next entity. - [NotNull] private LogicEntity ChooseNextEntity(List notRandomised) { // Make sure the list of absolutely essential entities is exhausted first. @@ -299,6 +309,12 @@ private LogicEntity ChooseNextEntity(List notRandomised) _priorityEntities.RemoveAt(0); } next ??= Random.Choice(notRandomised); + while (HasRandomised(next)) + { + _Log.Debug($"[Core] Found duplicate entity in main loop, removing: {next}"); + notRandomised.Remove(next); + next = Random.Choice(notRandomised); + } // Invoke the associated event. EntityChosen?.Invoke(this, new EntityEventArgs(next)); @@ -311,13 +327,13 @@ private LogicEntity ChooseNextEntity(List notRandomised) /// private void EnableHarmony() { - Harmony harmony = new Harmony(Initialiser.GUID); + _harmony = new Harmony(Initialiser.GUID); foreach (ILogicModule module in _modules) { - module.SetupHarmonyPatches(harmony); + module.SetupHarmonyPatches(_harmony); } // Always apply bugfixes. - harmony.PatchAll(typeof(VanillaBugfixes)); + _harmony.PatchAll(typeof(VanillaBugfixes)); } /// diff --git a/SubnauticaRandomiser/Patches/LanguagePatcher.cs b/SubnauticaRandomiser/Patches/LanguagePatcher.cs index 4290c57..f81a7e2 100644 --- a/SubnauticaRandomiser/Patches/LanguagePatcher.cs +++ b/SubnauticaRandomiser/Patches/LanguagePatcher.cs @@ -11,16 +11,26 @@ internal class LanguagePatcher /// Edit the description of the PDA logs on door access codes to reflect the codes they were randomised to. /// [HarmonyPostfix] - [HarmonyPatch(typeof(Language), nameof(Language.Start))] + [HarmonyPatch(typeof(Language), nameof(Language.SetCurrentLanguage))] public static void PatchAccessCodeEntries() { foreach (var kv in CoreLogic._Serializer.DoorKeyCodes) { string descId = AuroraLogic.KeypadPrefabClassIds[kv.Key]; string originalDesc = Language.main.Get(descId); - string newDesc = Regex.Replace(originalDesc, "[0-9]{4}", kv.Value); + string newDesc = Regex.Replace(originalDesc, "[0-9]{4}", kv.Value, RegexOptions.CultureInvariant); Language.main.strings[descId] = newDesc; } } + + /// + /// For people with stupidly fast computers. STOP LOADING EVERYTHING SO FAST + /// + [HarmonyPostfix] + [HarmonyPatch(typeof(Player), nameof(Player.Start))] + public static void PatchAccessCodeEntriesWithAHammer() + { + PatchAccessCodeEntries(); + } } } \ No newline at end of file