/// <summary>
        /// Replaces the recipes with the ones defined by the processor.
        /// </summary>
        public static bool Prefix(IMachine __instance, ref bool __result, IStorage input)
        {
            try
            {
                IReflectedProperty <SObject> instanceMachine = Util.Helper.Reflection.GetProperty <SObject>(__instance, "Machine");
                if (instanceMachine.GetValue() is Processor processor)
                {
                    IReflectedField <IRecipe[]> privateRecipes = Util.Helper.Reflection.GetField <IRecipe[]>(__instance, "Recipes");

                    IRecipe[] recipes = RecipeManager.GetRecipeAdaptorsFor(processor, privateRecipes?.GetValue());

                    IConsumable consumable      = null;
                    IRecipe     acceptingRecipe = null;

                    foreach (ITrackedStack item in input.GetItems())
                    {
                        acceptingRecipe = recipes.FirstOrDefault(recipe => recipe.AcceptsInput(item));
                        if (acceptingRecipe != null)
                        {
                            input.TryGetIngredient(item.Sample.ParentSheetIndex, acceptingRecipe.InputCount, out consumable);
                            break;
                        }
                    }

                    if (acceptingRecipe != null && consumable != null)
                    {
                        processor.heldObject.Value  = acceptingRecipe.Output(consumable.Take());
                        processor.MinutesUntilReady = acceptingRecipe.Minutes;
                        __result = true;
                        return(false);
                    }

                    __result = false;
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                ModEntry.StaticMonitor.Log($"Failed overriding Automate.\n{ex}", LogLevel.Error);
                return(true); // run original code instead
            }
        }
Exemple #2
0
        public static bool Prefix(object __instance, ref bool __result, IStorage input)
        {
            IReflectedProperty <SObject> instanceMachine = ModEntry.StaticHelper.Reflection.GetProperty <SObject>(__instance, "Machine");

            if (instanceMachine.GetValue() is Processor processor)
            {
                IConsumable consumable      = null;
                Recipe      acceptingRecipe = processor.Recipes.FirstOrDefault(recipe =>
                                                                               recipe.PossibleIngredients.Any(pair =>
                                                                                                              input.TryGetIngredient(pair.Key, pair.Value, out consumable)
                                                                                                              )
                                                                               );

                if (acceptingRecipe == null)
                {
                    __result = false;
                    return(false);
                }

                processor.heldObject.Value        = Processor.WithQuality(acceptingRecipe.Process)(consumable.Take() as SObject);
                processor.minutesUntilReady.Value = acceptingRecipe.Minutes;
                __result = true;
                return(false);
            }

            return(true);
        }