Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul craft data patcher and handlers #524

Merged
merged 3 commits into from
Jan 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Overhaul CraftData.Get Patch
  • Loading branch information
MrPurple6411 committed Jan 2, 2024
commit 9c8ce8dc9ec6ba7a7dee6e13e5cae273431ec215
143 changes: 6 additions & 137 deletions Nautilus/Patchers/CraftDataPatcher_Subnautica.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using BepInEx.Logging;
using HarmonyLib;
using Nautilus.Extensions;
using Nautilus.Utility;

#if SUBNAUTICA
Expand Down Expand Up @@ -149,147 +150,15 @@ private static void GetBuildablePrefix(TechType recipe)
PatchUtils.PatchList(CraftData.buildables, CustomBuildables);
}

[HarmonyPrefix]
[HarmonyPostfix]
[HarmonyPatch(typeof(CraftData), nameof(CraftData.Get))]
private static void NeedsPatchingCheckPrefix(TechType techType)
private static void GetRecipePostfix(TechType techType, ref ITechData __result)
{
bool techExists = CraftData.techData.TryGetValue(techType, out CraftData.TechData techData);

bool sameData = techExists;
if (techExists && CustomRecipeData.TryGetValue(techType, out ITechData customTechData))
{
sameData = customTechData.craftAmount == techData.craftAmount &&
customTechData.ingredientCount == techData.ingredientCount &&
customTechData.linkedItemCount == techData.linkedItemCount;

if (sameData)
for (int i = 0; i < customTechData.ingredientCount; i++)
{
if (customTechData.GetIngredient(i).techType != techData.GetIngredient(i).techType)
{
sameData = false;
break;
}
if (customTechData.GetIngredient(i).amount != techData.GetIngredient(i).amount)
{
sameData = false;
break;
}
}

if (sameData)
for (int i = 0; i < customTechData.linkedItemCount; i++)
{
if (customTechData.GetLinkedItem(i) != techData.GetLinkedItem(i))
{
sameData = false;
break;
}
}
}
if (!techExists || !sameData)
if (CustomRecipeData.TryGetValue(techType, out var customTechData) && (__result == null || !customTechData.SameAs(__result)))
{
PatchCustomTechData();
}
}


private static void PatchCustomTechData()
{
short added = 0;
short replaced = 0;
foreach (TechType techType in CustomRecipeData.Keys)
{
bool techExists = CraftData.techData.TryGetValue(techType, out CraftData.TechData techData);
ITechData customTechData = CustomRecipeData[techType];
bool sameData = false;

if (techExists && customTechData != null)
{

sameData = customTechData.craftAmount == techData.craftAmount &&
customTechData.ingredientCount == techData.ingredientCount &&
customTechData.linkedItemCount == techData.linkedItemCount;

if (sameData)
for (int i = 0; i < customTechData.ingredientCount; i++)
{
if (customTechData.GetIngredient(i).techType != techData.GetIngredient(i).techType)
{
sameData = false;
break;
}
if (customTechData.GetIngredient(i).amount != techData.GetIngredient(i).amount)
{
sameData = false;
break;
}
}

if (sameData)
for (int i = 0; i < customTechData.linkedItemCount; i++)
{
if (customTechData.GetLinkedItem(i) != techData.GetLinkedItem(i))
{
sameData = false;
break;
}
}
}

if (!techExists || !sameData)
{
var techDataInstance = new CraftData.TechData
{
_techType = techType,
_craftAmount = customTechData?.craftAmount ?? 0
};

var ingredientsList = new CraftData.Ingredients();

if (customTechData?.ingredientCount > 0)
{
for (int i = 0; i < customTechData.ingredientCount; i++)
{
IIngredient customIngredient = customTechData.GetIngredient(i);

var ingredient = new CraftData.Ingredient(customIngredient.techType, customIngredient.amount);
ingredientsList.Add(customIngredient.techType, customIngredient.amount);
}
techDataInstance._ingredients = ingredientsList;
}

if (customTechData?.linkedItemCount > 0)
{
var linkedItems = new List<TechType>();
for (int l = 0; l < customTechData.linkedItemCount; l++)
{
linkedItems.Add(customTechData.GetLinkedItem(l));
}
techDataInstance._linkedItems = linkedItems;
}

if (techExists)
{
CraftData.techData.Remove(techType);
InternalLogger.Log($"{techType} TechType already existed in the CraftData.techData dictionary. Original value was replaced.", LogLevel.Warning);
replaced++;
}
else
{
added++;
}
CraftData.techData.Add(techType, techDataInstance);
}
CraftData.techData[techType] = customTechData.ConvertToTechData(techType);
__result = customTechData;
}

if (added > 0)
InternalLogger.Log($"Added {added} new entries to the CraftData.techData dictionary.", LogLevel.Debug);

if (replaced > 0)
InternalLogger.Log($"Replaced {replaced} existing entries to the CraftData.techData dictionary.", LogLevel.Debug);
}


}
#endif
Loading