/********* ** Public methods *********/ /// <summary>Construct an instance.</summary> /// <param name="serviceProvider">The service provider to use to locate services.</param> /// <param name="rootDirectory">The root directory to search for content.</param> /// <param name="currentCulture">The current culture for which to localise content.</param> /// <param name="languageCodeOverride">The current language code for which to localise content.</param> /// <param name="monitor">Encapsulates monitoring and logging.</param> public SContentManager(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, string languageCodeOverride, IMonitor monitor) : base(serviceProvider, rootDirectory, currentCulture, languageCodeOverride) { // validate if (monitor == null) { throw new ArgumentNullException(nameof(monitor)); } // initialise var reflection = new Reflector(); this.Monitor = monitor; // get underlying fields for interception this.Cache = reflection.GetPrivateField <Dictionary <string, object> >(this, "loadedAssets").GetValue(); this.GetKeyLocale = reflection.GetPrivateMethod(this, "languageCode"); // get asset key normalisation logic if (Constants.TargetPlatform == Platform.Windows) { IPrivateMethod method = reflection.GetPrivateMethod(typeof(TitleContainer), "GetCleanPath"); this.NormaliseAssetNameForPlatform = path => method.Invoke <string>(path); } else { this.NormaliseAssetNameForPlatform = key => key.Replace('\\', '/'); // based on MonoGame's ContentManager.Load<T> logic } // get asset data this.CoreAssets = new CoreAssets(this.NormaliseAssetName); this.KeyLocales = this.GetKeyLocales(reflection); }
/// <summary>Get a private static method.</summary> /// <param name="type">The type which has the method.</param> /// <param name="name">The field name.</param> /// <param name="required">Whether to throw an exception if the private field is not found.</param> public IPrivateMethod GetPrivateMethod(Type type, string name, bool required = true) { // get method from hierarchy IPrivateMethod method = this.GetMethodFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); if (required && method == null) { throw new InvalidOperationException($"The {type.FullName} object doesn't have a private '{name}' static method."); } return(method); }
/********* ** Public methods *********/ /**** ** Constructor ****/ /// <summary>Construct an instance.</summary> /// <param name="serviceProvider">The service provider to use to locate services.</param> /// <param name="rootDirectory">The root directory to search for content.</param> /// <param name="currentCulture">The current culture for which to localise content.</param> /// <param name="languageCodeOverride">The current language code for which to localise content.</param> /// <param name="monitor">Encapsulates monitoring and logging.</param> /// <param name="reflection">Simplifies access to private code.</param> public SContentManager(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, string languageCodeOverride, IMonitor monitor, Reflector reflection) : base(serviceProvider, rootDirectory, currentCulture, languageCodeOverride) { // init this.Monitor = monitor ?? throw new ArgumentNullException(nameof(monitor)); this.Cache = new ContentCache(this, reflection, SContentManager.PossiblePathSeparators, SContentManager.PreferredPathSeparator); this.GetKeyLocale = reflection.GetPrivateMethod(this, "languageCode"); this.ModContentPrefix = this.GetAssetNameFromFilePath(Constants.ModPath); // get asset data this.CoreAssets = new CoreAssets(this.NormaliseAssetName); this.KeyLocales = this.GetKeyLocales(reflection); }
/**** ** Methods ****/ /// <summary>Get a private instance method.</summary> /// <param name="obj">The object which has the method.</param> /// <param name="name">The field name.</param> /// <param name="required">Whether to throw an exception if the private field is not found.</param> public IPrivateMethod GetPrivateMethod(object obj, string name, bool required = true) { // validate if (obj == null) { throw new ArgumentNullException(nameof(obj), "Can't get a private instance method from a null object."); } // get method from hierarchy IPrivateMethod method = this.GetMethodFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (required && method == null) { throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a private '{name}' instance method."); } return(method); }
private string getFortuneForecast() { IPrivateMethod method = CustomTVMod.Modhelper.Reflection.GetPrivateMethod(tv, "getFortuneForecast"); return(method.Invoke <string>(new object[0])); }
private void setFortuneOverlay() { IPrivateMethod method = CustomTVMod.Modhelper.Reflection.GetPrivateMethod(tv, "setFortuneOverlay"); method.Invoke(new object[0]); }
private string[] getWeeklyRecipe() { IPrivateMethod method = CustomTVMod.Modhelper.Reflection.GetPrivateMethod(tv, "getWeeklyRecipe"); return(method.Invoke <string[]>(new object[0])); }
private string getTodaysTip() { IPrivateMethod method = CustomTVMod.Modhelper.Reflection.GetPrivateMethod(tv, "getTodaysTip"); return(method.Invoke <string>(new object[0])); }
private string getWeatherChannelOpening() { IPrivateMethod method = CustomTVMod.Modhelper.Reflection.GetPrivateMethod(tv, "getWeatherChannelOpening"); return(method.Invoke <string>(new object[0])); }
private void DrawShopHarvestPrices(object sender, EventArgs e) { if (Game1.activeClickableMenu is ShopMenu) { ShopMenu menu = Game1.activeClickableMenu as ShopMenu; Item hoverItem = typeof(ShopMenu).GetField("hoveredItem", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(menu) as Item; if (hoverItem != null) { String text = string.Empty; bool itemHasPriceInfo = Tools.GetTruePrice(hoverItem) > 0; if (hoverItem is StardewValley.Object && (hoverItem as StardewValley.Object).type == "Seeds" && itemHasPriceInfo && hoverItem.Name != "Mixed Seeds" && hoverItem.Name != "Winter Seeds") { StardewValley.Object temp = new StardewValley.Object( new Debris( new Crop( hoverItem.parentSheetIndex, 0, 0) .indexOfHarvest, Game1.player.position, Game1.player.position).chunkType, 1); text = " " + temp.price; } Item heldItem = typeof(ShopMenu).GetField("heldItem", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(menu) as Item; if (heldItem == null) { int value = 0; switch (hoverItem.parentSheetIndex) { case 628: value = 50; break; case 629: value = 80; break; case 630: case 633: value = 100; break; case 631: case 632: value = 140; break; } if (value > 0) { text = " " + value; } if (text != "" && (hoverItem as StardewValley.Object).type == "Seeds") { String textToRender = _helper.SafeGetString( LanguageKeys.HarvestPrice); int xPosition = menu.xPositionOnScreen - 30; int yPosition = menu.yPositionOnScreen + 580; IClickableMenu.drawTextureBox( Game1.spriteBatch, xPosition + 20, yPosition - 52, 264, 108, Color.White); Game1.spriteBatch.DrawString( Game1.dialogueFont, textToRender, new Vector2(xPosition + 30, yPosition - 38), Color.Black * 0.2f); Game1.spriteBatch.DrawString( Game1.dialogueFont, textToRender, new Vector2(xPosition + 32, yPosition - 40), Color.Black * 0.8f); xPosition += 80; Game1.spriteBatch.Draw( Game1.mouseCursors, new Vector2(xPosition, yPosition), new Rectangle(60, 428, 10, 10), Color.White, 0, Vector2.Zero, Game1.pixelZoom, SpriteEffects.None, 0.85f); Game1.spriteBatch.Draw( Game1.debrisSpriteSheet, new Vector2(xPosition + 32, yPosition + 10), Game1.getSourceRectForStandardTileSheet(Game1.debrisSpriteSheet, 8, 16, 16), Color.White, 0, new Vector2(8, 8), 4, SpriteEffects.None, 0.95f); Game1.spriteBatch.DrawString( Game1.dialogueFont, text, new Vector2(xPosition - 2, yPosition + 6), Color.Black * 0.2f); Game1.spriteBatch.DrawString( Game1.dialogueFont, text, new Vector2(xPosition, yPosition + 4), Color.Black * 0.8f); String hoverText = _helper.Reflection.GetPrivateField <String>(menu, "hoverText").GetValue(); String hoverTitle = _helper.Reflection.GetPrivateField <String>(menu, "boldTitleText").GetValue(); Item hoverItem2 = _helper.Reflection.GetPrivateField <Item>(menu, "hoveredItem").GetValue(); int currency = _helper.Reflection.GetPrivateField <int>(menu, "currency").GetValue(); int hoverPrice = _helper.Reflection.GetPrivateField <int>(menu, "hoverPrice").GetValue(); IPrivateMethod getHoveredItemExtraItemIndex = _helper.Reflection.GetPrivateMethod(menu, "getHoveredItemExtraItemIndex"); IPrivateMethod getHoveredItemExtraItemAmount = _helper.Reflection.GetPrivateMethod(menu, "getHoveredItemExtraItemAmount"); IClickableMenu.drawToolTip( Game1.spriteBatch, hoverText, hoverTitle, hoverItem2, heldItem != null, -1, currency, getHoveredItemExtraItemIndex.Invoke <int>(new object[0]), getHoveredItemExtraItemAmount.Invoke <int>(new object[0]), null, hoverPrice); } } } } }
/// <summary>Assert that mods can use the reflection helper to access the given member.</summary> /// <param name="method">The method being accessed.</param> /// <returns>Returns the same method instance for convenience.</returns> private IPrivateMethod AssertAccessAllowed(IPrivateMethod method) { this.AssertAccessAllowed(method?.MethodInfo); return(method); }