Skip to content

Commit

Permalink
Merge pull request #70 from tinyhoot/0.10-fixes
Browse files Browse the repository at this point in the history
0.10 fixes
  • Loading branch information
tinyhoot authored Jan 27, 2023
2 parents 3519fec + 455a53a commit 06ef409
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion SubnauticaRandomiser/Initialiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion SubnauticaRandomiser/Logic/AuroraLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 22 additions & 6 deletions SubnauticaRandomiser/Logic/CoreLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -94,6 +94,11 @@ private void Awake()
_spoilerLog = gameObject.EnsureComponent<SpoilerLog>();
}

private void OnDestroy()
{
_harmony?.UnpatchSelf();
}

private void EnableModules()
{
if (!_Config.sSpawnPoint.Equals("Vanilla"))
Expand Down Expand Up @@ -194,12 +199,14 @@ private IEnumerator RandomiseMainEntities(List<LogicEntity> 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)
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -281,7 +292,6 @@ internal void ApplyAllChanges()
/// Get the next entity to be randomised, prioritising essential or elective ones.
/// </summary>
/// <returns>The next entity.</returns>
[NotNull]
private LogicEntity ChooseNextEntity(List<LogicEntity> notRandomised)
{
// Make sure the list of absolutely essential entities is exhausted first.
Expand All @@ -299,6 +309,12 @@ private LogicEntity ChooseNextEntity(List<LogicEntity> 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));
Expand All @@ -311,13 +327,13 @@ private LogicEntity ChooseNextEntity(List<LogicEntity> notRandomised)
/// </summary>
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));
}

/// <summary>
Expand Down
14 changes: 12 additions & 2 deletions SubnauticaRandomiser/Patches/LanguagePatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
[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;
}
}

/// <summary>
/// For people with stupidly fast computers. STOP LOADING EVERYTHING SO FAST
/// </summary>
[HarmonyPostfix]
[HarmonyPatch(typeof(Player), nameof(Player.Start))]
public static void PatchAccessCodeEntriesWithAHammer()
{
PatchAccessCodeEntries();
}
}
}

0 comments on commit 06ef409

Please sign in to comment.