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
Show file tree
Hide file tree
Changes from all commits
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
231 changes: 231 additions & 0 deletions Nautilus/Extensions/RecipeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
namespace Nautilus.Extensions;

using Nautilus.Crafting;
using Nautilus.Utility;
using System;
using System.Collections.Generic;

/// <summary>
/// Contains extensions that are specific to the <see cref="RecipeData"/> and <see cref="ITechData"/> classes.

Check warning on line 9 in Nautilus/Extensions/RecipeExtensions.cs

View workflow job for this annotation

GitHub Actions / BZ

XML comment has cref attribute 'ITechData' that could not be resolved
/// </summary>
public static class RecipeExtensions
{

#if SUBNAUTICA
/// <summary>
/// Converts a <see cref="RecipeData"/> to an <see cref="ITechData"/>.
/// </summary>
/// <param name="techData"> the original data to convert.</param>
/// <param name="techType"> The Techtype </param>
/// <returns><see cref="CraftData.TechData"/></returns>

public static CraftData.TechData ConvertToTechData(this ITechData techData, TechType techType)
{
var techDataInstance = new CraftData.TechData
{
_techType = techType,
_craftAmount = techData?.craftAmount ?? 0
};

var ingredientsList = new CraftData.Ingredients();

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

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

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

return techDataInstance;
}

/// <summary>
/// Converts the games ITechData into Nautilus RecipeData.
/// </summary>
/// <param name="iTechData"></param>
public static RecipeData ConvertToRecipeData(this ITechData iTechData)
{
var recipeData = new RecipeData() { craftAmount = iTechData.craftAmount };

for (int i = 0; i < iTechData.ingredientCount; i++)
{
IIngredient ingredient = iTechData.GetIngredient(i);
var customIngredient = new CraftData.Ingredient(ingredient.techType, ingredient.amount);
recipeData.Ingredients.Add(customIngredient);
}

for (int i = 0; i < iTechData.linkedItemCount; i++)
{
recipeData.LinkedItems.Add(iTechData.GetLinkedItem(i));
}

return recipeData;
}

/// <summary>
/// Checks if the two ITechData are equal.
/// </summary>
/// <param name="originalTechData"></param>
/// <param name="techData"></param>
/// <returns></returns>
public static bool SameAs(this ITechData originalTechData, ITechData techData)
{
if (originalTechData.craftAmount != techData.craftAmount ||
originalTechData.ingredientCount != techData.ingredientCount ||
originalTechData.linkedItemCount != techData.linkedItemCount)
return false;

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

for (int i = 0; i < originalTechData.linkedItemCount; i++)
{
if (originalTechData.GetLinkedItem(i) != techData.GetLinkedItem(i))
{
return false;
}
}

return true;
}

#elif BELOWZERO

/// <summary>
/// Converts the Games JsonValue data into Nautilus RecipeData.
/// </summary>
/// <param name="techData"></param>
public static RecipeData ConvertToRecipeData(this JsonValue techData)
{
try
{
RecipeData currentRecipeData = new()
{
craftAmount = techData.GetInt(TechData.propertyCraftAmount, out int craftAmount, 0) ? craftAmount : TechData.defaultCraftAmount
};

if (techData.GetArray(TechData.propertyIngredients, out JsonValue jsonValue, null))
{
for (int i = 0; i < jsonValue.Count; i++)
{
JsonValue jsonValue2 = jsonValue[i];
TechType techType = (TechType) jsonValue2.GetInt(TechData.propertyTechType, 0);
int int2 = jsonValue2.GetInt(TechData.propertyAmount, 0);
if (techType != TechType.None && int2 > 0)
{
if (currentRecipeData.Ingredients == null)
{
currentRecipeData.Ingredients = new List<Ingredient>();
}
currentRecipeData.Ingredients.Add(new Ingredient(techType, int2));
}
}
}

if (techData.GetArray(TechData.propertyLinkedItems, out JsonValue jsonValue3, null))
{
for (int j = 0; j < jsonValue3.Count; j++)
{
TechType techType1 = (TechType) jsonValue3[j].GetInt(0);
if (currentRecipeData.LinkedItems == null)
{
currentRecipeData.LinkedItems = new List<TechType>();
}
currentRecipeData.LinkedItems.Add(techType1);
}
}
return currentRecipeData;
}
catch (Exception e)
{
InternalLogger.Error($"Error converting TechData to RecipeData: {e.Message}");
return null;
}
}

/// <summary>
/// Converts the Nautilus RecipeData into the Games JsonValue data.
/// </summary>
/// <param name="recipeData"></param>
/// <param name="techType"></param>
/// <returns><see cref="JsonValue"/> or null</returns>
public static JsonValue ConvertToJsonValue(this RecipeData recipeData, TechType techType)
{
try
{
JsonValue jsonValue = new JsonValue
{
{ TechData.PropertyToID("techType"), new JsonValue((int)techType) },
{ TechData.PropertyToID("craftAmount"), new JsonValue(recipeData.craftAmount) }
};

if (recipeData.ingredientCount > 0)
{
jsonValue[TechData.PropertyToID("ingredients")] = new JsonValue(JsonValue.Type.Array);
JsonValue ingredientslist = jsonValue[TechData.PropertyToID("ingredients")];

int amount = TechData.PropertyToID("amount");
int tech = TechData.PropertyToID("techType");
int current = 0;

foreach (Ingredient i in recipeData.Ingredients)
{
ingredientslist.Add(new JsonValue(current));
ingredientslist[current] = new JsonValue(JsonValue.Type.Object)
{
{ amount, new JsonValue(i.amount) },
{ tech, new JsonValue((int)i.techType) }
};
current++;
}
}

if (recipeData.linkedItemCount > 0)
{
jsonValue[TechData.PropertyToID("linkedItems")] = new JsonValue(JsonValue.Type.Array);
JsonValue linkedItems = jsonValue[TechData.PropertyToID("linkedItems")];

int current = 0;

foreach (TechType techType1 in recipeData.LinkedItems)
{
linkedItems.Add(new JsonValue(current));
linkedItems[current] = new JsonValue((int)techType1);
current++;
}
}

return jsonValue;
}
catch (Exception e)
{
InternalLogger.Error($"Error converting RecipeData to JsonValue: {e.Message}");
return null;
}
}
#endif
}
42 changes: 4 additions & 38 deletions Nautilus/Handlers/CraftDataHandler_BelowZero.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Nautilus.Handlers;

using Crafting;
using Nautilus.Extensions;
using Patchers;
using System.Collections.Generic;

Expand Down Expand Up @@ -146,7 +147,7 @@ public static RecipeData GetRecipeData(TechType techType)

if (TechData.TryGetValue(techType, out JsonValue techData))
{
return ConvertToRecipeData(techData);
return techData?.ConvertToRecipeData();
}

return null;
Expand All @@ -158,42 +159,7 @@ public static RecipeData GetRecipeData(TechType techType)
/// <param name="techData"></param>
public static RecipeData ConvertToRecipeData(JsonValue techData)
{
RecipeData currentRecipeData = new()
{
craftAmount = techData.GetInt(TechData.propertyCraftAmount, out int craftAmount, 0) ? craftAmount : TechData.defaultCraftAmount
};

if (techData.GetArray(TechData.propertyIngredients, out JsonValue jsonValue, null))
{
for (int i = 0; i < jsonValue.Count; i++)
{
JsonValue jsonValue2 = jsonValue[i];
TechType techType = (TechType)jsonValue2.GetInt(TechData.propertyTechType, 0);
int int2 = jsonValue2.GetInt(TechData.propertyAmount, 0);
if (techType != TechType.None && int2 > 0)
{
if (currentRecipeData.Ingredients == null)
{
currentRecipeData.Ingredients = new List<Ingredient>();
}
currentRecipeData.Ingredients.Add(new Ingredient(techType, int2));
}
}
}

if (techData.GetArray(TechData.propertyLinkedItems, out JsonValue jsonValue3, null))
{
for (int j = 0; j < jsonValue3.Count; j++)
{
TechType techType1 = (TechType)jsonValue3[j].GetInt(0);
if (currentRecipeData.LinkedItems == null)
{
currentRecipeData.LinkedItems = new List<TechType>();
}
currentRecipeData.LinkedItems.Add(techType1);
}
}
return currentRecipeData;
return techData?.ConvertToRecipeData();
}

/// <summary>
Expand All @@ -205,7 +171,7 @@ public static RecipeData ConvertToRecipeData(JsonValue techData)
/// <returns>The RecipeData from the modded item if it exists; Otherwise, returns <c>null</c>.</returns>
public static RecipeData GetModdedRecipeData(TechType techType)
{
return CraftDataPatcher.CustomRecipeData.TryGetValue(techType, out JsonValue techData) ? ConvertToRecipeData(techData) : null;
return CraftDataPatcher.CustomRecipeData.TryGetValue(techType, out JsonValue techData) ? techData.ConvertToRecipeData() : null;
}

/// <summary>
Expand Down
35 changes: 7 additions & 28 deletions Nautilus/Handlers/CraftDataHandler_Subnautica.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#if SUBNAUTICA
using Nautilus.Crafting;
using Nautilus.Patchers;

namespace Nautilus.Handlers;

using static CraftData;
using Nautilus.Crafting;
using Nautilus.Extensions;
using Nautilus.Patchers;

/// <summary>
/// A handler class for adding and editing crafted items.
Expand Down Expand Up @@ -209,7 +208,7 @@ public static RecipeData GetModdedRecipeData(TechType techType)
{
return null;
}
return ConvertToRecipeData(moddedTechData);
return moddedTechData.ConvertToRecipeData();
}

/// <summary>
Expand All @@ -223,17 +222,11 @@ public static RecipeData GetRecipeData(TechType techType)
{
if(CraftDataPatcher.CustomRecipeData.TryGetValue(techType, out ITechData iTechData))
{
return ConvertToRecipeData(iTechData);
return iTechData.ConvertToRecipeData();
}

iTechData = CraftData.Get(techType, true);

if(iTechData != null)
{
return ConvertToRecipeData(iTechData);
}

return null;
return iTechData?.ConvertToRecipeData();
}

/// <summary>
Expand All @@ -242,21 +235,7 @@ public static RecipeData GetRecipeData(TechType techType)
/// <param name="iTechData"></param>
public static RecipeData ConvertToRecipeData(ITechData iTechData)
{
var recipeData = new RecipeData() { craftAmount = iTechData.craftAmount };

for (int i = 0; i < iTechData.ingredientCount; i++)
{
IIngredient ingredient = iTechData.GetIngredient(i);
var customIngredient = new Ingredient(ingredient.techType, ingredient.amount);
recipeData.Ingredients.Add(customIngredient);
}

for (int i = 0; i < iTechData.linkedItemCount; i++)
{
recipeData.LinkedItems.Add(iTechData.GetLinkedItem(i));
}

return recipeData;
return iTechData?.ConvertToRecipeData();
}
}
#endif
Loading
Loading