Files
7d2dXG/Mods/zzz_REBIRTH__Utils/Harmony/Harmony_RecipesFromXml.cs
Nathaniel Cosford e06f2bd282 Add All Mods
2025-05-29 23:33:28 +09:30

214 lines
10 KiB
C#

using System.Xml.Linq;
namespace Harmony.RecipesFromXmlPatches
{
[HarmonyPatch(typeof(RecipesFromXml))]
[HarmonyPatch("LoadRecipies")]
public class LoadRecipiesPatches
{
public static bool Prefix(RecipesFromXml __instance, XmlFile _xmlFile)
{
//Log.Out("RecipesFromXml-LoadRecipies START");
XElement root = _xmlFile.XmlDoc.Root;
if (!root.HasElements)
{
throw new Exception("No element <recipes> found!");
}
foreach (XElement xelement in root.Elements("recipe"))
{
Recipe recipe = new Recipe();
string recipeName = "";
string craftArea = "";
foreach (XAttribute xattribute in xelement.Attributes())
{
if (xattribute.Name == "name")
{
recipeName = xattribute.Value;
}
else if (xattribute.Name == "craft_area")
{
craftArea = xattribute.Value;
}
}
if (recipeName.Trim().Length > 0)
{
recipe.itemValueType = ItemClass.GetItem(recipeName, false).type;
bool hasOutput = false;
RecipeOutputRebirth output = new RecipeOutputRebirth();
foreach (XElement xelement2 in xelement.Elements())
{
if (xelement2.Name == "output")
{
if (!xelement2.HasAttribute("name"))
{
throw new Exception("Attribute 'name' missing on ingredient in recipe '" + recipeName + "'");
}
string name = xelement2.GetAttribute("name");
ItemValue item = ItemClass.GetItem(name, false);
if (item.IsEmpty())
{
throw new Exception("No item/block/material with name '" + name + "' existing");
}
int count = 1;
if (xelement2.HasAttribute("count"))
{
count = int.Parse(xelement2.GetAttribute("count"));
}
hasOutput = true;
output.recipeName = recipeName;
output.craftingArea = craftArea;
//Log.Out("RecipesFromXmlPatches-LoadRecipies craftArea: " + craftArea);
ItemValue itemValue = ItemClass.GetItem(name, false);
ItemStack itemStack = new ItemStack(itemValue, count);
output.OutputItems.Add(itemStack);
//Log.Out("RecipesFromXmlPatches-LoadRecipies output.recipeName: " + output.recipeName + " [" + name + "]");
}
}
if (hasOutput)
{
RebirthUtilities.recipeOutputs.Add(output);
//Log.Out("RecipesFromXmlPatches-LoadRecipies RebirthUtilities.recipeOutputs.Count: " + RebirthUtilities.recipeOutputs.Count);
}
}
}
//Base Game Code
MicroStopwatch msw = new MicroStopwatch(true);
foreach (XElement element1 in root.Elements((XName)"recipe"))
{
Recipe _recipe = new Recipe();
string _itemName1 = element1.HasAttribute((XName)"name") ? element1.GetAttribute((XName)"name") : throw new Exception("Attribute 'name' missing on recipe");
//Log.Out("RecipesFromXmlPatches-LoadRecipies _itemName1: " + _itemName1);
bool shouldSkip = false;
foreach (XElement element2 in element1.Elements())
{
if (element2.Name == (XName)"scenario")
{
//Log.Out("RecipesFromXmlPatches-LoadRecipies FOUND SCENARIO ELEMENT: " + _itemName1);
if (element2.HasAttribute((XName)"name"))
{
string name = element2.GetAttribute((XName)"name");
//Log.Out("RecipesFromXmlPatches-LoadRecipies FOUND SCENARIO NAME: " + name);
//Log.Out("RecipesFromXmlPatches-LoadRecipies RebirthVariables.customScenario: " + RebirthVariables.customScenario);
if (RebirthVariables.customScenario != name)
{
shouldSkip = true;
continue;
}
}
}
}
if (shouldSkip)
{
//Log.Out("RecipesFromXmlPatches-LoadRecipies SKIP");
continue;
}
_recipe.itemValueType = ItemClass.GetItem(_itemName1).type;
if (_recipe.itemValueType == 0)
throw new Exception("No item/block with name '" + _itemName1 + "' existing");
_recipe.count = 1;
if (element1.HasAttribute((XName)"count"))
_recipe.count = int.Parse(element1.GetAttribute((XName)"count"));
_recipe.scrapable = false;
if (element1.HasAttribute((XName)"scrapable"))
_recipe.scrapable = StringParsers.ParseBool(element1.GetAttribute((XName)"scrapable"));
_recipe.materialBasedRecipe = false;
if (element1.HasAttribute((XName)"material_based"))
_recipe.materialBasedRecipe = StringParsers.ParseBool(element1.GetAttribute((XName)"material_based"));
if (element1.HasAttribute((XName)"tags"))
_recipe.tags = FastTags<TagGroup.Global>.Parse(element1.GetAttribute((XName)"tags") + "," + _itemName1);
else if (element1.HasAttribute((XName)"tag"))
_recipe.tags = FastTags<TagGroup.Global>.Parse(element1.GetAttribute((XName)"tag") + "," + _itemName1);
if (element1.HasAttribute((XName)"tooltip"))
_recipe.tooltip = element1.GetAttribute((XName)"tooltip");
if (element1.HasAttribute((XName)"craft_area"))
{
string attribute = element1.GetAttribute((XName)"craft_area");
_recipe.craftingArea = attribute;
}
else
_recipe.craftingArea = "";
if (element1.HasAttribute((XName)"craft_tool"))
{
_recipe.craftingToolType = ItemClass.GetItem(element1.GetAttribute((XName)"craft_tool")).type;
ItemClass.list[ItemClass.GetItem(element1.GetAttribute((XName)"craft_tool")).type].bCraftingTool = true;
}
else
_recipe.craftingToolType = 0;
if (element1.HasAttribute((XName)"craft_time"))
{
float _result = 0.0f;
StringParsers.TryParseFloat(element1.GetAttribute((XName)"craft_time"), out _result);
_recipe.craftingTime = _result;
}
else
_recipe.craftingTime = -1f;
if (element1.HasAttribute((XName)"learn_exp_gain"))
{
float _result = 0.0f;
_recipe.unlockExpGain = !StringParsers.TryParseFloat(element1.GetAttribute((XName)"learn_exp_gain"), out _result) ? 20 : (int)_result;
}
else
_recipe.unlockExpGain = -1;
if (element1.HasAttribute((XName)"craft_exp_gain"))
{
float _result = 0.0f;
_recipe.craftExpGain = !StringParsers.TryParseFloat(element1.GetAttribute((XName)"craft_exp_gain"), out _result) ? 1 : (int)_result;
}
else
_recipe.craftExpGain = -1;
_recipe.IsTrackable = !element1.HasAttribute((XName)"is_trackable") || StringParsers.ParseBool(element1.GetAttribute((XName)"is_trackable"));
_recipe.UseIngredientModifier = true;
if (element1.HasAttribute((XName)"use_ingredient_modifier"))
_recipe.UseIngredientModifier = StringParsers.ParseBool(element1.GetAttribute((XName)"use_ingredient_modifier"));
_recipe.Effects = MinEffectController.ParseXml(element1);
foreach (XElement element2 in element1.Elements())
{
if (element2.Name == (XName)"ingredient")
{
XElement _element = element2;
string _itemName2 = _element.HasAttribute((XName)"name") ? _element.GetAttribute((XName)"name") : throw new Exception("Attribute 'name' missing on ingredient in recipe '" + _itemName1 + "'");
ItemValue _itemValue = ItemClass.GetItem(_itemName2);
if (_itemValue.IsEmpty())
throw new Exception("No item/block/material with name '" + _itemName2 + "' existing");
int _count = 1;
if (_element.HasAttribute((XName)"count"))
_count = int.Parse(_element.GetAttribute((XName)"count"));
_recipe.AddIngredient(_itemValue, _count);
}
else if (element2.Name == (XName)"wildcard_forge_category")
_recipe.wildcardForgeCategory = true;
}
_recipe.Init();
CraftingManager.AddRecipe(_recipe);
/*if (msw.ElapsedMilliseconds > (long)Constants.cMaxLoadTimePerFrameMillis)
{
yield return (object)null;
msw.ResetAndRestart();
}*/
}
CraftingManager.PostInit();
return false;
}
}
}