Skip to content

Commit d27d803

Browse files
authored
feat: Overhaul craft data patcher and handlers (#524)
* Create RecipeExtensions.cs * Use RecipeExtensions * Overhaul CraftData.Get Patch
1 parent 21c42bc commit d27d803

File tree

4 files changed

+248
-203
lines changed

4 files changed

+248
-203
lines changed
+231
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
namespace Nautilus.Extensions;
2+
3+
using Nautilus.Crafting;
4+
using Nautilus.Utility;
5+
using System;
6+
using System.Collections.Generic;
7+
8+
/// <summary>
9+
/// Contains extensions that are specific to the <see cref="RecipeData"/> and <see cref="ITechData"/> classes.
10+
/// </summary>
11+
public static class RecipeExtensions
12+
{
13+
14+
#if SUBNAUTICA
15+
/// <summary>
16+
/// Converts a <see cref="RecipeData"/> to an <see cref="ITechData"/>.
17+
/// </summary>
18+
/// <param name="techData"> the original data to convert.</param>
19+
/// <param name="techType"> The Techtype </param>
20+
/// <returns><see cref="CraftData.TechData"/></returns>
21+
22+
public static CraftData.TechData ConvertToTechData(this ITechData techData, TechType techType)
23+
{
24+
var techDataInstance = new CraftData.TechData
25+
{
26+
_techType = techType,
27+
_craftAmount = techData?.craftAmount ?? 0
28+
};
29+
30+
var ingredientsList = new CraftData.Ingredients();
31+
32+
if (techData?.ingredientCount > 0)
33+
{
34+
for (int i = 0; i < techData.ingredientCount; i++)
35+
{
36+
IIngredient customIngredient = techData.GetIngredient(i);
37+
38+
var ingredient = new CraftData.Ingredient(customIngredient.techType, customIngredient.amount);
39+
ingredientsList.Add(customIngredient.techType, customIngredient.amount);
40+
}
41+
techDataInstance._ingredients = ingredientsList;
42+
}
43+
44+
if (techData?.linkedItemCount > 0)
45+
{
46+
var linkedItems = new List<TechType>();
47+
for (int l = 0; l < techData.linkedItemCount; l++)
48+
{
49+
linkedItems.Add(techData.GetLinkedItem(l));
50+
}
51+
techDataInstance._linkedItems = linkedItems;
52+
}
53+
54+
return techDataInstance;
55+
}
56+
57+
/// <summary>
58+
/// Converts the games ITechData into Nautilus RecipeData.
59+
/// </summary>
60+
/// <param name="iTechData"></param>
61+
public static RecipeData ConvertToRecipeData(this ITechData iTechData)
62+
{
63+
var recipeData = new RecipeData() { craftAmount = iTechData.craftAmount };
64+
65+
for (int i = 0; i < iTechData.ingredientCount; i++)
66+
{
67+
IIngredient ingredient = iTechData.GetIngredient(i);
68+
var customIngredient = new CraftData.Ingredient(ingredient.techType, ingredient.amount);
69+
recipeData.Ingredients.Add(customIngredient);
70+
}
71+
72+
for (int i = 0; i < iTechData.linkedItemCount; i++)
73+
{
74+
recipeData.LinkedItems.Add(iTechData.GetLinkedItem(i));
75+
}
76+
77+
return recipeData;
78+
}
79+
80+
/// <summary>
81+
/// Checks if the two ITechData are equal.
82+
/// </summary>
83+
/// <param name="originalTechData"></param>
84+
/// <param name="techData"></param>
85+
/// <returns></returns>
86+
public static bool SameAs(this ITechData originalTechData, ITechData techData)
87+
{
88+
if (originalTechData.craftAmount != techData.craftAmount ||
89+
originalTechData.ingredientCount != techData.ingredientCount ||
90+
originalTechData.linkedItemCount != techData.linkedItemCount)
91+
return false;
92+
93+
for (int i = 0; i < originalTechData.ingredientCount; i++)
94+
{
95+
if (originalTechData.GetIngredient(i).techType != techData.GetIngredient(i).techType)
96+
{
97+
return false;
98+
}
99+
if (originalTechData.GetIngredient(i).amount != techData.GetIngredient(i).amount)
100+
{
101+
return false;
102+
}
103+
}
104+
105+
for (int i = 0; i < originalTechData.linkedItemCount; i++)
106+
{
107+
if (originalTechData.GetLinkedItem(i) != techData.GetLinkedItem(i))
108+
{
109+
return false;
110+
}
111+
}
112+
113+
return true;
114+
}
115+
116+
#elif BELOWZERO
117+
118+
/// <summary>
119+
/// Converts the Games JsonValue data into Nautilus RecipeData.
120+
/// </summary>
121+
/// <param name="techData"></param>
122+
public static RecipeData ConvertToRecipeData(this JsonValue techData)
123+
{
124+
try
125+
{
126+
RecipeData currentRecipeData = new()
127+
{
128+
craftAmount = techData.GetInt(TechData.propertyCraftAmount, out int craftAmount, 0) ? craftAmount : TechData.defaultCraftAmount
129+
};
130+
131+
if (techData.GetArray(TechData.propertyIngredients, out JsonValue jsonValue, null))
132+
{
133+
for (int i = 0; i < jsonValue.Count; i++)
134+
{
135+
JsonValue jsonValue2 = jsonValue[i];
136+
TechType techType = (TechType) jsonValue2.GetInt(TechData.propertyTechType, 0);
137+
int int2 = jsonValue2.GetInt(TechData.propertyAmount, 0);
138+
if (techType != TechType.None && int2 > 0)
139+
{
140+
if (currentRecipeData.Ingredients == null)
141+
{
142+
currentRecipeData.Ingredients = new List<Ingredient>();
143+
}
144+
currentRecipeData.Ingredients.Add(new Ingredient(techType, int2));
145+
}
146+
}
147+
}
148+
149+
if (techData.GetArray(TechData.propertyLinkedItems, out JsonValue jsonValue3, null))
150+
{
151+
for (int j = 0; j < jsonValue3.Count; j++)
152+
{
153+
TechType techType1 = (TechType) jsonValue3[j].GetInt(0);
154+
if (currentRecipeData.LinkedItems == null)
155+
{
156+
currentRecipeData.LinkedItems = new List<TechType>();
157+
}
158+
currentRecipeData.LinkedItems.Add(techType1);
159+
}
160+
}
161+
return currentRecipeData;
162+
}
163+
catch (Exception e)
164+
{
165+
InternalLogger.Error($"Error converting TechData to RecipeData: {e.Message}");
166+
return null;
167+
}
168+
}
169+
170+
/// <summary>
171+
/// Converts the Nautilus RecipeData into the Games JsonValue data.
172+
/// </summary>
173+
/// <param name="recipeData"></param>
174+
/// <param name="techType"></param>
175+
/// <returns><see cref="JsonValue"/> or null</returns>
176+
public static JsonValue ConvertToJsonValue(this RecipeData recipeData, TechType techType)
177+
{
178+
try
179+
{
180+
JsonValue jsonValue = new JsonValue
181+
{
182+
{ TechData.PropertyToID("techType"), new JsonValue((int)techType) },
183+
{ TechData.PropertyToID("craftAmount"), new JsonValue(recipeData.craftAmount) }
184+
};
185+
186+
if (recipeData.ingredientCount > 0)
187+
{
188+
jsonValue[TechData.PropertyToID("ingredients")] = new JsonValue(JsonValue.Type.Array);
189+
JsonValue ingredientslist = jsonValue[TechData.PropertyToID("ingredients")];
190+
191+
int amount = TechData.PropertyToID("amount");
192+
int tech = TechData.PropertyToID("techType");
193+
int current = 0;
194+
195+
foreach (Ingredient i in recipeData.Ingredients)
196+
{
197+
ingredientslist.Add(new JsonValue(current));
198+
ingredientslist[current] = new JsonValue(JsonValue.Type.Object)
199+
{
200+
{ amount, new JsonValue(i.amount) },
201+
{ tech, new JsonValue((int)i.techType) }
202+
};
203+
current++;
204+
}
205+
}
206+
207+
if (recipeData.linkedItemCount > 0)
208+
{
209+
jsonValue[TechData.PropertyToID("linkedItems")] = new JsonValue(JsonValue.Type.Array);
210+
JsonValue linkedItems = jsonValue[TechData.PropertyToID("linkedItems")];
211+
212+
int current = 0;
213+
214+
foreach (TechType techType1 in recipeData.LinkedItems)
215+
{
216+
linkedItems.Add(new JsonValue(current));
217+
linkedItems[current] = new JsonValue((int)techType1);
218+
current++;
219+
}
220+
}
221+
222+
return jsonValue;
223+
}
224+
catch (Exception e)
225+
{
226+
InternalLogger.Error($"Error converting RecipeData to JsonValue: {e.Message}");
227+
return null;
228+
}
229+
}
230+
#endif
231+
}

Nautilus/Handlers/CraftDataHandler_BelowZero.cs

+4-38
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Nautilus.Handlers;
33

44
using Crafting;
5+
using Nautilus.Extensions;
56
using Patchers;
67
using System.Collections.Generic;
78

@@ -146,7 +147,7 @@ public static RecipeData GetRecipeData(TechType techType)
146147

147148
if (TechData.TryGetValue(techType, out JsonValue techData))
148149
{
149-
return ConvertToRecipeData(techData);
150+
return techData?.ConvertToRecipeData();
150151
}
151152

152153
return null;
@@ -158,42 +159,7 @@ public static RecipeData GetRecipeData(TechType techType)
158159
/// <param name="techData"></param>
159160
public static RecipeData ConvertToRecipeData(JsonValue techData)
160161
{
161-
RecipeData currentRecipeData = new()
162-
{
163-
craftAmount = techData.GetInt(TechData.propertyCraftAmount, out int craftAmount, 0) ? craftAmount : TechData.defaultCraftAmount
164-
};
165-
166-
if (techData.GetArray(TechData.propertyIngredients, out JsonValue jsonValue, null))
167-
{
168-
for (int i = 0; i < jsonValue.Count; i++)
169-
{
170-
JsonValue jsonValue2 = jsonValue[i];
171-
TechType techType = (TechType)jsonValue2.GetInt(TechData.propertyTechType, 0);
172-
int int2 = jsonValue2.GetInt(TechData.propertyAmount, 0);
173-
if (techType != TechType.None && int2 > 0)
174-
{
175-
if (currentRecipeData.Ingredients == null)
176-
{
177-
currentRecipeData.Ingredients = new List<Ingredient>();
178-
}
179-
currentRecipeData.Ingredients.Add(new Ingredient(techType, int2));
180-
}
181-
}
182-
}
183-
184-
if (techData.GetArray(TechData.propertyLinkedItems, out JsonValue jsonValue3, null))
185-
{
186-
for (int j = 0; j < jsonValue3.Count; j++)
187-
{
188-
TechType techType1 = (TechType)jsonValue3[j].GetInt(0);
189-
if (currentRecipeData.LinkedItems == null)
190-
{
191-
currentRecipeData.LinkedItems = new List<TechType>();
192-
}
193-
currentRecipeData.LinkedItems.Add(techType1);
194-
}
195-
}
196-
return currentRecipeData;
162+
return techData?.ConvertToRecipeData();
197163
}
198164

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

211177
/// <summary>

Nautilus/Handlers/CraftDataHandler_Subnautica.cs

+7-28
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#if SUBNAUTICA
2-
using Nautilus.Crafting;
3-
using Nautilus.Patchers;
4-
52
namespace Nautilus.Handlers;
63

7-
using static CraftData;
4+
using Nautilus.Crafting;
5+
using Nautilus.Extensions;
6+
using Nautilus.Patchers;
87

98
/// <summary>
109
/// A handler class for adding and editing crafted items.
@@ -209,7 +208,7 @@ public static RecipeData GetModdedRecipeData(TechType techType)
209208
{
210209
return null;
211210
}
212-
return ConvertToRecipeData(moddedTechData);
211+
return moddedTechData.ConvertToRecipeData();
213212
}
214213

215214
/// <summary>
@@ -223,17 +222,11 @@ public static RecipeData GetRecipeData(TechType techType)
223222
{
224223
if(CraftDataPatcher.CustomRecipeData.TryGetValue(techType, out ITechData iTechData))
225224
{
226-
return ConvertToRecipeData(iTechData);
225+
return iTechData.ConvertToRecipeData();
227226
}
228227

229228
iTechData = CraftData.Get(techType, true);
230-
231-
if(iTechData != null)
232-
{
233-
return ConvertToRecipeData(iTechData);
234-
}
235-
236-
return null;
229+
return iTechData?.ConvertToRecipeData();
237230
}
238231

239232
/// <summary>
@@ -242,21 +235,7 @@ public static RecipeData GetRecipeData(TechType techType)
242235
/// <param name="iTechData"></param>
243236
public static RecipeData ConvertToRecipeData(ITechData iTechData)
244237
{
245-
var recipeData = new RecipeData() { craftAmount = iTechData.craftAmount };
246-
247-
for (int i = 0; i < iTechData.ingredientCount; i++)
248-
{
249-
IIngredient ingredient = iTechData.GetIngredient(i);
250-
var customIngredient = new Ingredient(ingredient.techType, ingredient.amount);
251-
recipeData.Ingredients.Add(customIngredient);
252-
}
253-
254-
for (int i = 0; i < iTechData.linkedItemCount; i++)
255-
{
256-
recipeData.LinkedItems.Add(iTechData.GetLinkedItem(i));
257-
}
258-
259-
return recipeData;
238+
return iTechData?.ConvertToRecipeData();
260239
}
261240
}
262241
#endif

0 commit comments

Comments
 (0)