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); }
public bool IsForeignKey(IReflectedProperty reflectedProperty, IReflectedClass reflectedClass) { var potentialForeignKeyNames = reflectedClass.ReflectedProperties.Where(x => x.IsComplex).Select(x => _namingConventionManager.GetForiegnKeyByComplexProperty(reflectedClass.Name, x.Name)).ToList(); var foreignKeyNames = reflectedClass.ReflectedProperties.Where(x => potentialForeignKeyNames.Contains(x.Name)).Select(x => x.Name).ToList(); return(foreignKeyNames.Contains(reflectedProperty.Name)); }
public DynamicCollectionEntityPropertyMetadata(IReflectedProperty reflectedProperty, IEnumerable <IReflectedClass> reflectedClasses) : base(reflectedProperty, reflectedClasses) { var inversePropertyAttribute = reflectedProperty.GetAttribute <InversePropertyAttribute>(); if (inversePropertyAttribute != null) { InverseProperty = inversePropertyAttribute.Property; } }
private bool IsBoolPropertyTrue(string propertyName, object tobeCheckedForProperty) { IReflectedProperty <bool> dinosaurAreaProperty = Helper.Reflection.GetProperty <bool>(tobeCheckedForProperty, propertyName); if (dinosaurAreaProperty != null) { return(dinosaurAreaProperty.GetValue()); } return(false); }
/// <summary>Get a static property.</summary> /// <typeparam name="TValue">The property type.</typeparam> /// <param name="type">The type which has the property.</param> /// <param name="name">The property name.</param> /// <param name="required">Whether to throw an exception if the property is not found.</param> public IReflectedProperty <TValue> GetProperty <TValue>(Type type, string name, bool required = true) { // get field from hierarchy IReflectedProperty <TValue> property = this.GetPropertyFromHierarchy <TValue>(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); if (required && property == null) { throw new InvalidOperationException($"The {type.FullName} object doesn't have a '{name}' static property."); } return(property); }
/********* ** Public methods *********/ /// <summary>Construct an instance.</summary> /// <param name="contentPack">The content pack from which the patch was loaded.</param> /// <param name="name">The raw patch name to display in error messages.</param> /// <param name="patch">The patch instance from Content Patcher.</param> /// <param name="reflection">Simplifies access to private code.</param> /// <param name="reflection">Simplifies access to game content.</param> public PatchData(IContentPack contentPack, string name, object patch, IReflectionHelper reflection) { this.ContentPack = contentPack; this.Name = name; this.Reflection = reflection; this.LastChangedTickProperty = reflection.GetProperty <int>(patch, "LastChangedTick"); this.IsReadyProperty = reflection.GetProperty <bool>(patch, "IsReady"); this.IsAppliedProperty = reflection.GetProperty <bool>(patch, "IsApplied"); this.FromAssetProperty = reflection.GetProperty <string>(patch, "FromAsset"); this.TargetAssetProperty = reflection.GetProperty <IAssetName>(patch, "TargetAsset"); this.FromAreaProperty = reflection.GetField <object>(patch, "FromArea"); this.ToAreaProperty = reflection.GetField <object>(patch, "ToArea"); this.RefreshIfNeeded(); }
/**** ** Properties ****/ /// <summary>Get a instance property.</summary> /// <typeparam name="TValue">The property type.</typeparam> /// <param name="obj">The object which has the property.</param> /// <param name="name">The property name.</param> /// <param name="required">Whether to throw an exception if the property is not found.</param> public IReflectedProperty <TValue> GetProperty <TValue>(object obj, string name, bool required = true) { // validate if (obj == null) { throw new ArgumentNullException(nameof(obj), "Can't get a instance property from a null object."); } // get property from hierarchy IReflectedProperty <TValue> property = this.GetPropertyFromHierarchy <TValue>(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (required && property == null) { throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a '{name}' instance property."); } return(property); }
/// <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 } }
/// <summary>Temporarily dismount and set up the player to interact with a tile, then return it to the previous state afterwards.</summary> /// <param name="action">The action to perform.</param> private void TemporarilyFakeInteraction(Action action) { // get references SFarmer player = Game1.player; IReflectedProperty <Horse> mountField = this.Reflection.GetProperty <Horse>(Game1.player, "mount"); // save current state Horse mount = mountField.GetValue(); Vector2 mountPosition = this.Position; WateringCan wateringCan = player.CurrentTool as WateringCan; int waterInCan = wateringCan?.WaterLeft ?? 0; float stamina = player.stamina; Vector2 position = player.Position; int facingDirection = player.FacingDirection; int currentToolIndex = player.CurrentToolIndex; bool canMove = Game1.player.canMove; // fix player frozen due to animations when performing an action // move mount out of the way mountField.SetValue(null); this.Position = new Vector2(-5, -5); // perform action try { action(); } finally { // move mount back this.Position = mountPosition; mountField.SetValue(mount); // restore previous state if (wateringCan != null) { wateringCan.WaterLeft = waterInCan; } player.stamina = stamina; player.Position = position; player.FacingDirection = facingDirection; player.CurrentToolIndex = currentToolIndex; Game1.player.canMove = canMove; } }
/// <summary>Assert that mods can use the reflection helper to access the given member.</summary> /// <typeparam name="T">The property value type.</typeparam> /// <param name="property">The property being accessed.</param> /// <returns>Returns the same property instance for convenience.</returns> private IReflectedProperty <T> AssertAccessAllowed <T>(IReflectedProperty <T> property) { this.AssertAccessAllowed(property?.PropertyInfo.GetMethod?.GetBaseDefinition()); this.AssertAccessAllowed(property?.PropertyInfo.SetMethod?.GetBaseDefinition()); return(property); }
/// <summary>Assert that mods can use the reflection helper to access the given member.</summary> /// <typeparam name="T">The property value type.</typeparam> /// <param name="property">The property being accessed.</param> /// <returns>Returns the same property instance for convenience.</returns> private IReflectedProperty <T> AssertAccessAllowed <T>(IReflectedProperty <T> property) { this.AssertAccessAllowed(property?.PropertyInfo); return(property); }
public DynamicPropertyMetadata(IReflectedProperty reflectedProperty, IEnumerable <IReflectedClass> reflectedClasses) { ReflectedProperty = reflectedProperty; ReflectedClasses = reflectedClasses; }
public DynamicForiegnKeyPropertyMetadata(IReflectedProperty reflectedProperty, IEnumerable <IReflectedClass> reflectedClasses) : base(reflectedProperty, reflectedClasses) { }
public DynamicComplexPropertyMetadata(IReflectedProperty reflectedProperty, IEnumerable <IReflectedClass> reflectedClasses) : base(reflectedProperty, reflectedClasses) { }