|
| 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 | +} |
0 commit comments