Skip to content

Commit

Permalink
Correct set analysis tech entry behavior breaking change (#504)
Browse files Browse the repository at this point in the history
* Correct functionalities change to SetAnalysisTechEntry
* Change Lists to HashSets to prevent useless duplicates being added.
* Correct Timing issue that would remove the items AnalysisTech if the item had a CompoundTech.
* Reimplement lost functionality removed by my failure to properly review ALL aspects of the ScannerGadget PR.
* Change require analysistech causing fail to auto create one instead like before.

Fix: Corrected removals timing
Doc: Added more notes to describe the why things are where they are.
Doc: Added notes on methods that modify instead of replace even if the name is horrible and makes you think they should replace.
  • Loading branch information
MrPurple6411 authored Dec 17, 2023
1 parent 6816548 commit 718fb56
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 59 deletions.
12 changes: 6 additions & 6 deletions Nautilus/Assets/Gadgets/ScanningGadget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,25 +381,25 @@ protected internal override void Build()
PDAHandler.AddEncyclopediaEntry(EncyclopediaEntryData);
}

if (AnalysisTech is { })
{
KnownTechHandler.SetAnalysisTechEntry(AnalysisTech);
}

if (CompoundTechsForUnlock is { Count: > 0 })
{
KnownTechHandler.RemoveAllCurrentAnalysisTechEntry(prefab.Info.TechType);
KnownTechHandler.SetCompoundUnlock(prefab.Info.TechType, CompoundTechsForUnlock);
}

if (AnalysisTech is { })
{
KnownTechHandler.SetAnalysisTechEntry(AnalysisTech);
}

if (ScannerEntryData is { })
{
PDAHandler.AddCustomScannerEntry(ScannerEntryData);
}

if (RequiredForUnlock != TechType.None)
{
KnownTechHandler.AddRequirementForUnlock(prefab.Info.TechType, RequiredForUnlock);
KnownTechHandler.SetAnalysisTechEntry(RequiredForUnlock, new TechType[] { prefab.Info.TechType });
}

if (CompoundTechsForUnlock is { Count: > 0 } || RequiredForUnlock != TechType.None)
Expand Down
59 changes: 46 additions & 13 deletions Nautilus/Handlers/KnownTechHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ private static void Reinitialize()
public static void UnlockOnStart(TechType techType)
{
KnownTechPatcher.UnlockedAtStart.Add(techType);

bool removed = false;
var removalList = new List<string>();
KnownTechPatcher.DefaultRemovalTechs.ForEach((x) =>
{
if (x.Value.Remove(techType))
{
removed = true;
InternalLogger.Debug($"Removed {techType.AsString()} from {x.Key}'s DefaultRemovalTechs");
if (x.Value.Count > 0 )
{
removalList.Add(x.Key);
}
}
});

foreach (string key in removalList)
KnownTechPatcher.DefaultRemovalTechs.Remove(key);

if (removed)
InternalLogger.Debug($"Set {techType.AsString()} to Unlock On Start ");
Reinitialize();
}

Expand All @@ -48,7 +69,7 @@ public static void AddRequirementForUnlock(TechType blueprint, TechType requirem
public static void SetHardLocked(TechType techType)
{
KnownTechPatcher.HardLocked.Add(techType);
KnownTechPatcher.UnlockedAtStart.Remove(techType);
RemoveDefaultUnlock(techType);
Reinitialize();
}

Expand Down Expand Up @@ -140,24 +161,25 @@ internal static void RemoveAnalysisSpecific(TechType targetTechType, List<TechTy
{
foreach (TechType techType in techTypes)
{
if (KnownTechPatcher.RemoveFromSpecificTechs.TryGetValue(techType, out List<TechType> types))
{
if (!types.Contains(targetTechType))
{
types.Add(targetTechType);
}
}
if (KnownTechPatcher.AnalysisTech.TryGetValue(techType, out var analysisTech) && analysisTech.unlockTechTypes.Remove(targetTechType))
InternalLogger.Debug($"Removed unlock for {targetTechType.AsString()} from {techType} that was added by another mod.");

if (KnownTechPatcher.RemoveFromSpecificTechs.TryGetValue(techType, out HashSet<TechType> types))
types.Add(targetTechType);
else
{
KnownTechPatcher.RemoveFromSpecificTechs[techType] = new List<TechType>() { targetTechType };
}
KnownTechPatcher.RemoveFromSpecificTechs[techType] = new HashSet<TechType>() { targetTechType };
}

Reinitialize();
}

internal static void RemoveAnalysisTechEntry(TechType targetTechType)
{
if (KnownTechPatcher.AnalysisTech.Remove(targetTechType))
{
InternalLogger.Debug($"Removed Analysis Tech for {targetTechType.AsString()} that was added by another mod!");
}

foreach (KnownTech.AnalysisTech tech in KnownTechPatcher.AnalysisTech.Values)
{
if (tech.unlockTechTypes.Remove(targetTechType))
Expand Down Expand Up @@ -186,6 +208,7 @@ internal static void RemoveAnalysisTechEntry(TechType targetTechType)
/// Allows you to define which TechTypes are unlocked when a certain TechType is unlocked, i.e., "analysed".
/// If there is already an existing AnalysisTech entry for a TechType, all the TechTypes in "techTypesToUnlock" will be
/// added to the existing AnalysisTech entry unlocks.
/// ***Note: This will not remove any original unlock and if you need to do so you should use <see cref="RemoveAllCurrentAnalysisTechEntry"/> before calling this method.
/// </summary>
/// <param name="techTypeToBeAnalysed">This TechType is the criteria for all of the "unlock TechTypes"; when this TechType is unlocked, so are all the ones in that list</param>
/// <param name="techTypesToUnlock">The TechTypes that will be unlocked when "techTypeToSet" is unlocked.</param>
Expand All @@ -198,6 +221,7 @@ public static void SetAnalysisTechEntry(TechType techTypeToBeAnalysed, IEnumerab
/// Allows you to define which TechTypes are unlocked when a certain TechType is unlocked, i.e., "analysed".
/// If there is already an existing AnalysisTech entry for a TechType, all the TechTypes in "techTypesToUnlock" will be
/// added to the existing AnalysisTech entry unlocks.
/// ***Note: This will not remove any original unlock and if you need to do so you should use <see cref="RemoveAllCurrentAnalysisTechEntry"/> before calling this method.
/// </summary>
/// <param name="techTypeToBeAnalysed">This TechType is the criteria for all of the "unlock TechTypes"; when this TechType is unlocked, so are all the ones in that list</param>
/// <param name="techTypesToUnlock">The TechTypes that will be unlocked when "techTypeToSet" is unlocked.</param>
Expand All @@ -211,6 +235,7 @@ public static void SetAnalysisTechEntry(TechType techTypeToBeAnalysed, IEnumerab
/// Allows you to define which TechTypes are unlocked when a certain TechType is unlocked, i.e., "analysed".
/// If there is already an existing AnalysisTech entry for a TechType, all the TechTypes in "techTypesToUnlock" will be
/// added to the existing AnalysisTech entry unlocks.
/// ***Note: This will not remove any original unlock and if you need to do so you should use <see cref="RemoveAllCurrentAnalysisTechEntry"/> before calling this method.
/// </summary>
/// <param name="techTypeToBeAnalysed">This TechType is the criteria for all of the "unlock TechTypes"; when this TechType is unlocked, so are all the ones in that list</param>
/// <param name="techTypesToUnlock">The TechTypes that will be unlocked when "techTypeToSet" is unlocked.</param>
Expand All @@ -224,6 +249,7 @@ public static void SetAnalysisTechEntry(TechType techTypeToBeAnalysed, IEnumerab
/// Allows you to define which TechTypes are unlocked when a certain TechType is unlocked, i.e., "analysed".
/// If there is already an exisitng AnalysisTech entry for a TechType, all the TechTypes in "techTypesToUnlock" will be
/// added to the existing AnalysisTech entry unlocks.
/// ***Note: This will not remove any original unlock and if you need to do so you should use <see cref="RemoveAllCurrentAnalysisTechEntry"/> before calling this method.
/// </summary>
/// <param name="techTypeToBeAnalysed">This TechType is the criteria for all of the "unlock TechTypes"; when this TechType is unlocked, so are all the ones in that list</param>
/// <param name="techTypesToUnlock">The TechTypes that will be unlocked when "techTypeToSet" is unlocked.</param>
Expand All @@ -237,6 +263,7 @@ public static void SetAnalysisTechEntry(TechType techTypeToBeAnalysed, IEnumerab
/// Allows you to define which TechTypes are unlocked when a certain TechType is unlocked, i.e., "analysed".
/// If there is already an existing AnalysisTech entry for a TechType, all the TechTypes in "techTypesToUnlock" will be
/// added to the existing AnalysisTech entry unlocks.
/// ***Note: This will not remove any original unlock and if you need to do so you should use <see cref="RemoveAllCurrentAnalysisTechEntry"/> before calling this method.
/// </summary>
/// <param name="techTypeToBeAnalysed">This TechType is the criteria for all of the "unlock TechTypes"; when this TechType is unlocked, so are all the ones in that list</param>
/// <param name="techTypesToUnlock">The TechTypes that will be unlocked when "techTypeToSet" is unlocked.</param>
Expand All @@ -251,6 +278,7 @@ public static void SetAnalysisTechEntry(TechType techTypeToBeAnalysed, IEnumerab
/// Allows you to define which TechTypes are unlocked when a certain TechType is unlocked, i.e., "analysed".
/// If there is already an existing AnalysisTech entry for a TechType, all the TechTypes in "techTypesToUnlock" will be
/// added to the existing AnalysisTech entry unlocks.
/// ***Note: This will not remove any original unlock and if you need to do so you should use <see cref="RemoveAllCurrentAnalysisTechEntry"/> before calling this method.
/// </summary>
/// <param name="techTypeToBeAnalysed">This TechType is the criteria for all of the "unlock TechTypes"; when this TechType is unlocked, so are all the ones in that list</param>
/// <param name="techTypesToUnlock">The TechTypes that will be unlocked when "techTypeToSet" is unlocked.</param>
Expand All @@ -265,6 +293,7 @@ public static void SetAnalysisTechEntry(TechType techTypeToBeAnalysed, IEnumerab
/// Allows you to define which TechTypes are unlocked when a certain TechType is unlocked, i.e., "analysed".
/// If there is already an existing AnalysisTech entry for a TechType, all the TechTypes in "techTypesToUnlock" will be
/// added to the existing AnalysisTech entry unlocks.
/// ***Note: This will not remove any original unlock and if you need to do so you should use <see cref="RemoveAllCurrentAnalysisTechEntry"/> before calling this method.
/// </summary>
/// <param name="techTypeToBeAnalysed">This TechType is the criteria for all of the "unlock TechTypes"; when this TechType is unlocked, so are all the ones in that list</param>
/// <param name="techTypesToUnlock">The TechTypes that will be unlocked when "techTypeToSet" is unlocked.</param>
Expand All @@ -279,6 +308,7 @@ public static void SetAnalysisTechEntry(TechType techTypeToBeAnalysed, IEnumerab
/// Allows you to define which TechTypes are unlocked when a certain TechType is unlocked, i.e., "analysed".
/// If there is already an existing AnalysisTech entry for a TechType, all the TechTypes in <see cref="KnownTech.AnalysisTech.unlockTechTypes"/> will be
/// added to the existing AnalysisTech entry unlocks.
/// ***Note: This will not remove any original unlock and if you need to do so you should use <see cref="RemoveAllCurrentAnalysisTechEntry"/> before calling this method.
/// </summary>
/// <param name="analysisTech">The analysis tech entry to add.</param>
public static void SetAnalysisTechEntry(KnownTech.AnalysisTech analysisTech)
Expand All @@ -290,6 +320,7 @@ public static void SetAnalysisTechEntry(KnownTech.AnalysisTech analysisTech)
/// Allows you to define which TechTypes are unlocked when a certain TechType is unlocked, i.e., "analysed".
/// If there is already an existing AnalysisTech entry for a TechType, all the TechTypes in "techTypesToUnlock" will be
/// added to the existing AnalysisTech entry unlocks.
/// ***Note: This will not remove any original unlock and if you need to do so you should use <see cref="RemoveAllCurrentAnalysisTechEntry"/> before calling this method.
/// </summary>
/// <param name="techTypeToBeAnalysed">This TechType is the criteria for all of the "unlock TechTypes"; when this TechType is unlocked, so are all the ones in that list</param>
/// <param name="techTypesToUnlock">The TechTypes that will be unlocked when "techTypeToSet" is unlocked.</param>
Expand All @@ -303,7 +334,7 @@ public static void SetAnalysisTechEntry(TechType techTypeToBeAnalysed, IEnumerab

/// <summary>
/// Allows you to set up a custom Compound Unlock requiring multiple techtypes to be unlocked before 1 is.
/// ***Note: This will not remove any original unlock and if you need to do so you should use <see cref="RemoveAnalysisTechEntryFromSpecific"/> or <see cref="RemoveAllCurrentAnalysisTechEntry"/>
/// ***Note: This will not remove any original unlock and if you need to do so you should use <see cref="RemoveAllCurrentAnalysisTechEntry"/> before calling this method.
/// </summary>
/// <param name="techType"></param>
/// <param name="compoundTechsForUnlock"></param>
Expand Down Expand Up @@ -344,10 +375,12 @@ public static void RemoveDefaultUnlock(TechType techType)
{
var modName = ReflectionHelper.CallingAssemblyByStackTrace().GetName().Name;
if (!KnownTechPatcher.DefaultRemovalTechs.TryGetValue(modName, out var techTypes))
techTypes = new List<TechType>();
techTypes = new HashSet<TechType>();
techTypes.Add(techType);

KnownTechPatcher.DefaultRemovalTechs[modName] = techTypes;
if (KnownTechPatcher.UnlockedAtStart.Remove(techType))
InternalLogger.Debug($"Removed Default unlock for {techType} that was added by another mod.");
Reinitialize();
}

Expand Down
Loading

0 comments on commit 718fb56

Please sign in to comment.