using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using StardewModdingAPI; using StardewModdingAPI.Events; using StardewValley; using StardewValley.Menus; using StardewValley.Objects; using StardewValley.Tools; using TrainerMod.Framework; using TrainerMod.ItemData; using Object = StardewValley.Object; namespace TrainerMod { /// The main entry point for the mod. public class TrainerMod : Mod { /********* ** Properties *********/ /// The time of day at which to freeze time. private int FrozenTime; /// Whether to keep the player's health at its maximum. private bool InfiniteHealth; /// Whether to keep the player's stamina at its maximum. private bool InfiniteStamina; /// Whether to keep the player's money at a set value. private bool InfiniteMoney; /// Whether to freeze time. private bool FreezeTime; /********* ** Public methods *********/ /// The mod entry point, called after the mod is first loaded. /// Provides simplified APIs for writing mods. public override void Entry(IModHelper helper) { this.RegisterCommands(); GameEvents.UpdateTick += this.ReceiveUpdateTick; } /********* ** Private methods *********/ /**** ** Implementation ****/ /// The method invoked when the game updates its state. /// The event sender. /// The event arguments. private void ReceiveUpdateTick(object sender, EventArgs e) { if (Game1.player == null) return; if (this.InfiniteHealth) Game1.player.health = Game1.player.maxHealth; if (this.InfiniteStamina) Game1.player.stamina = Game1.player.MaxStamina; if (this.InfiniteMoney) Game1.player.money = 999999; if (this.FreezeTime) Game1.timeOfDay = this.FrozenTime; } /// Register all trainer commands. private void RegisterCommands() { Command.RegisterCommand("types", "Lists all value types | types").CommandFired += this.HandleTypes; Command.RegisterCommand("save", "Saves the game? Doesn't seem to work. | save").CommandFired += this.HandleSave; Command.RegisterCommand("load", "Shows the load screen | load").CommandFired += this.HandleLoad; Command.RegisterCommand("exit", "Closes the game | exit").CommandFired += this.HandleExit; Command.RegisterCommand("stop", "Closes the game | stop").CommandFired += this.HandleExit; Command.RegisterCommand("player_setname", "Sets the player's name | player_setname ", new[] { "(player, pet, farm) (String) The target name" }).CommandFired += this.HandlePlayerSetName; Command.RegisterCommand("player_setmoney", "Sets the player's money | player_setmoney |inf", new[] { "(Int32) The target money" }).CommandFired += this.HandlePlayerSetMoney; Command.RegisterCommand("player_setstamina", "Sets the player's stamina | player_setstamina |inf", new[] { "(Int32) The target stamina" }).CommandFired += this.HandlePlayerSetStamina; Command.RegisterCommand("player_setmaxstamina", "Sets the player's max stamina | player_setmaxstamina ", new[] { "(Int32) The target max stamina" }).CommandFired += this.HandlePlayerSetMaxStamina; Command.RegisterCommand("player_sethealth", "Sets the player's health | player_sethealth |inf", new[] { "(Int32) The target health" }).CommandFired += this.HandlePlayerSetHealth; Command.RegisterCommand("player_setmaxhealth", "Sets the player's max health | player_setmaxhealth ", new[] { "(Int32) The target max health" }).CommandFired += this.HandlePlayerSetMaxHealth; Command.RegisterCommand("player_setimmunity", "Sets the player's immunity | player_setimmunity ", new[] { "(Int32) The target immunity" }).CommandFired += this.HandlePlayerSetImmunity; Command.RegisterCommand("player_setlevel", "Sets the player's specified skill to the specified value | player_setlevel ", new[] { "(luck, mining, combat, farming, fishing, foraging) (1-10) The target level" }).CommandFired += this.HandlePlayerSetLevel; Command.RegisterCommand("player_setspeed", "Sets the player's speed to the specified value?", new[] { "(Int32) The target speed [0 is normal]" }).CommandFired += this.HandlePlayerSetSpeed; Command.RegisterCommand("player_changecolour", "Sets the player's colour of the specified object | player_changecolor ", new[] { "(hair, eyes, pants) (r,g,b)" }).CommandFired += this.HandlePlayerChangeColor; Command.RegisterCommand("player_changestyle", "Sets the player's style of the specified object | player_changecolor ", new[] { "(hair, shirt, skin, acc, shoe, swim, gender) (Int32)" }).CommandFired += this.HandlePlayerChangeStyle; Command.RegisterCommand("player_additem", "Gives the player an item | player_additem [count] [quality]", new[] { "(Int32) (Int32)[count] (Int32)[quality]" }).CommandFired += this.HandlePlayerAddItem; Command.RegisterCommand("player_addmelee", "Gives the player a melee item | player_addmelee ", new[] { "?" }).CommandFired += this.HandlePlayerAddMelee; Command.RegisterCommand("player_addring", "Gives the player a ring | player_addring ", new[] { "?" }).CommandFired += this.HandlePlayerAddRing; Command.RegisterCommand("list_items", "Lists items in the game data | list_items [search]", new[] { "(String)" }).CommandFired += this.HandleListItems; Command.RegisterCommand("world_settime", "Sets the time to the specified value | world_settime ", new[] { "(Int32) The target time [06:00 AM is 600]" }).CommandFired += this.HandleWorldSetTime; Command.RegisterCommand("world_freezetime", "Freezes or thaws time | world_freezetime ", new[] { "(0 - 1) Whether or not to freeze time. 0 is thawed, 1 is frozen" }).CommandFired += this.HandleWorldFreezeTime; Command.RegisterCommand("world_setday", "Sets the day to the specified value | world_setday ", new[] { "(Int32) The target day [1-28]" }).CommandFired += this.world_setDay; Command.RegisterCommand("world_setseason", "Sets the season to the specified value | world_setseason ", new[] { "(winter, spring, summer, fall) The target season" }).CommandFired += this.HandleWorldSetSeason; Command.RegisterCommand("world_downminelevel", "Goes down one mine level? | world_downminelevel", new[] { "" }).CommandFired += this.HandleWorldDownMineLevel; Command.RegisterCommand("world_setminelevel", "Sets mine level? | world_setminelevel", new[] { "(Int32) The target level" }).CommandFired += this.HandleWorldSetMineLevel; } /**** ** Command handlers ****/ /// The event raised when the 'types' command is triggered. /// The event sender. /// The event arguments. private void HandleTypes(object sender, EventArgsCommand e) { this.Monitor.Log($"[Int32: {int.MinValue} - {int.MaxValue}], [Int64: {long.MinValue} - {long.MaxValue}], [String: \"raw text\"], [Colour: r,g,b (EG: 128, 32, 255)]", LogLevel.Info); } /// The event raised when the 'save' command is triggered. /// The event sender. /// The event arguments. private void HandleSave(object sender, EventArgsCommand e) { SaveGame.Save(); } /// The event raised when the 'load' command is triggered. /// The event sender. /// The event arguments. private void HandleLoad(object sender, EventArgsCommand e) { Game1.hasLoadedGame = false; Game1.activeClickableMenu = new LoadGameMenu(); } /// The event raised when the 'exit' command is triggered. /// The event sender. /// The event arguments. private void HandleExit(object sender, EventArgsCommand e) { Program.gamePtr.Exit(); Environment.Exit(0); } /// The event raised when the 'player_setName' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerSetName(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 1) { string target = e.Command.CalledArgs[0]; string[] validTargets = { "player", "pet", "farm" }; if (validTargets.Contains(target)) { switch (target) { case "player": Game1.player.Name = e.Command.CalledArgs[1]; break; case "pet": this.Monitor.Log("Pets cannot currently be renamed.", LogLevel.Info); break; case "farm": Game1.player.farmName = e.Command.CalledArgs[1]; break; } } else this.LogObjectInvalid(); } else this.LogObjectValueNotSpecified(); } /// The event raised when the 'player_setMoney' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerSetMoney(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { string amountStr = e.Command.CalledArgs[0]; if (amountStr == "inf") this.InfiniteMoney = true; else { this.InfiniteMoney = false; int amount; if (int.TryParse(amountStr, out amount)) { Game1.player.Money = amount; this.Monitor.Log($"Set {Game1.player.Name}'s money to {Game1.player.Money}", LogLevel.Info); } else this.LogValueNotInt32(); } } else this.LogValueNotSpecified(); } /// The event raised when the 'player_setStamina' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerSetStamina(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { string amountStr = e.Command.CalledArgs[0]; if (amountStr == "inf") this.InfiniteStamina = true; else { this.InfiniteStamina = false; int amount; if (int.TryParse(amountStr, out amount)) { Game1.player.Stamina = amount; this.Monitor.Log($"Set {Game1.player.Name}'s stamina to {Game1.player.Stamina}", LogLevel.Info); } else this.LogValueNotInt32(); } } else this.LogValueNotSpecified(); } /// The event raised when the 'player_setMaxStamina' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerSetMaxStamina(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { int amount; if (int.TryParse(e.Command.CalledArgs[0], out amount)) { Game1.player.MaxStamina = amount; this.Monitor.Log($"Set {Game1.player.Name}'s max stamina to {Game1.player.MaxStamina}", LogLevel.Info); } else this.LogValueNotInt32(); } else this.LogValueNotSpecified(); } /// The event raised when the 'player_setLevel' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerSetLevel(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 1) { string skill = e.Command.CalledArgs[0]; string[] skills = { "luck", "mining", "combat", "farming", "fishing", "foraging" }; if (skills.Contains(skill)) { int level; if (int.TryParse(e.Command.CalledArgs[1], out level)) { switch (skill) { case "luck": Game1.player.LuckLevel = level; break; case "mining": Game1.player.MiningLevel = level; break; case "combat": Game1.player.CombatLevel = level; break; case "farming": Game1.player.FarmingLevel = level; break; case "fishing": Game1.player.FishingLevel = level; break; case "foraging": Game1.player.ForagingLevel = level; break; } } else this.LogValueNotInt32(); } else this.Monitor.Log(" is invalid", LogLevel.Error); } else this.Monitor.Log(" and must be specified", LogLevel.Error); } /// The event raised when the 'player_setSpeed' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerSetSpeed(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { string amountStr = e.Command.CalledArgs[0]; if (amountStr.IsInt()) { Game1.player.addedSpeed = amountStr.ToInt(); this.Monitor.Log($"Set {Game1.player.Name}'s added speed to {Game1.player.addedSpeed}", LogLevel.Info); } else this.LogValueNotInt32(); } else this.LogValueNotSpecified(); } /// The event raised when the 'player_changeColour' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerChangeColor(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 1) { string target = e.Command.CalledArgs[0]; string[] validTargets = { "hair", "eyes", "pants" }; if (validTargets.Contains(target)) { string[] colorHexes = e.Command.CalledArgs[1].Split(new[] { ',' }, 3); if (colorHexes[0].IsInt() && colorHexes[1].IsInt() && colorHexes[2].IsInt()) { var color = new Color(colorHexes[0].ToInt(), colorHexes[1].ToInt(), colorHexes[2].ToInt()); switch (target) { case "hair": Game1.player.hairstyleColor = color; break; case "eyes": Game1.player.changeEyeColor(color); break; case "pants": Game1.player.pantsColor = color; break; } } else this.Monitor.Log(" is invalid", LogLevel.Error); } else this.LogObjectInvalid(); } else this.Monitor.Log(" and must be specified", LogLevel.Error); } /// The event raised when the 'player_changeStyle' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerChangeStyle(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 1) { string target = e.Command.CalledArgs[0]; string[] validTargets = { "hair", "shirt", "skin", "acc", "shoe", "swim", "gender" }; if (validTargets.Contains(target)) { if (e.Command.CalledArgs[1].IsInt()) { var styleID = e.Command.CalledArgs[1].ToInt(); switch (target) { case "hair": Game1.player.changeHairStyle(styleID); break; case "shirt": Game1.player.changeShirt(styleID); break; case "acc": Game1.player.changeAccessory(styleID); break; case "skin": Game1.player.changeSkinColor(styleID); break; case "shoe": Game1.player.changeShoeColor(styleID); break; case "swim": if (styleID == 0) Game1.player.changeOutOfSwimSuit(); else if (styleID == 1) Game1.player.changeIntoSwimsuit(); else this.Monitor.Log(" must be 0 or 1 for this ", LogLevel.Error); break; case "gender": if (styleID == 0) Game1.player.changeGender(true); else if (styleID == 1) Game1.player.changeGender(false); else this.Monitor.Log(" must be 0 or 1 for this ", LogLevel.Error); break; } } else this.LogValueInvalid(); } else this.LogObjectInvalid(); } else this.LogObjectValueNotSpecified(); } /// The event raised when the 'world_freezeTime' command is triggered. /// The event sender. /// The event arguments. private void HandleWorldFreezeTime(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { string valueStr = e.Command.CalledArgs[0]; if (valueStr.IsInt()) { int value = valueStr.ToInt(); if (value == 0 || value == 1) { this.FreezeTime = value == 1; this.FrozenTime = this.FreezeTime ? Game1.timeOfDay : 0; this.Monitor.Log("Time is now " + (this.FreezeTime ? "frozen" : "thawed"), LogLevel.Info); } else this.Monitor.Log(" should be 0 or 1", LogLevel.Error); } else this.LogValueNotInt32(); } else this.LogValueNotSpecified(); } /// The event raised when the 'world_setTime' command is triggered. /// The event sender. /// The event arguments. private void HandleWorldSetTime(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { string timeStr = e.Command.CalledArgs[0]; if (timeStr.IsInt()) { int time = timeStr.ToInt(); if (time <= 2600 && time >= 600) { Game1.timeOfDay = e.Command.CalledArgs[0].ToInt(); this.FrozenTime = this.FreezeTime ? Game1.timeOfDay : 0; this.Monitor.Log($"Time set to: {Game1.timeOfDay}", LogLevel.Info); } else this.Monitor.Log(" should be between 600 and 2600 (06:00 AM - 02:00 AM [NEXT DAY])", LogLevel.Error); } else this.LogValueNotInt32(); } else this.LogValueNotSpecified(); } /// The event raised when the 'world_setDay' command is triggered. /// The event sender. /// The event arguments. private void world_setDay(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { string dayStr = e.Command.CalledArgs[0]; if (dayStr.IsInt()) { int day = dayStr.ToInt(); if (day <= 28 && day > 0) Game1.dayOfMonth = day; else this.Monitor.Log(" must be between 1 and 28", LogLevel.Error); } else this.LogValueNotInt32(); } else this.LogValueNotSpecified(); } /// The event raised when the 'world_setSeason' command is triggered. /// The event sender. /// The event arguments. private void HandleWorldSetSeason(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { string season = e.Command.CalledArgs[0]; string[] validSeasons = { "winter", "spring", "summer", "fall" }; if (validSeasons.Contains(season)) Game1.currentSeason = season; else this.LogValueInvalid(); } else this.LogValueNotSpecified(); } /// The event raised when the 'player_setHealth' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerSetHealth(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { string amountStr = e.Command.CalledArgs[0]; if (amountStr == "inf") this.InfiniteHealth = true; else { this.InfiniteHealth = false; if (amountStr.IsInt()) Game1.player.health = amountStr.ToInt(); else this.LogValueNotInt32(); } } else this.LogValueNotSpecified(); } /// The event raised when the 'player_setMaxHealth' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerSetMaxHealth(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { string amountStr = e.Command.CalledArgs[0]; if (amountStr.IsInt()) Game1.player.maxHealth = amountStr.ToInt(); else this.LogValueNotInt32(); } else this.LogValueNotSpecified(); } /// The event raised when the 'player_setImmunity' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerSetImmunity(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { string amountStr = e.Command.CalledArgs[0]; if (amountStr.IsInt()) Game1.player.immunity = amountStr.ToInt(); else this.LogValueNotInt32(); } else this.LogValueNotSpecified(); } /// The event raised when the 'player_addItem' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerAddItem(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { string itemIdStr = e.Command.CalledArgs[0]; if (itemIdStr.IsInt()) { int itemID = itemIdStr.ToInt(); int count = 1; int quality = 0; if (e.Command.CalledArgs.Length > 1) { if (e.Command.CalledArgs[1].IsInt()) count = e.Command.CalledArgs[1].ToInt(); else { this.Monitor.Log("[count] is invalid", LogLevel.Error); return; } if (e.Command.CalledArgs.Length > 2) { if (e.Command.CalledArgs[2].IsInt()) quality = e.Command.CalledArgs[2].ToInt(); else { this.Monitor.Log("[quality] is invalid", LogLevel.Error); return; } } } var item = new Object(itemID, count) { quality = quality }; Game1.player.addItemByMenuIfNecessary(item); } else this.Monitor.Log(" is invalid", LogLevel.Error); } else this.LogObjectValueNotSpecified(); } /// The event raised when the 'player_addMelee' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerAddMelee(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { if (e.Command.CalledArgs[0].IsInt()) { MeleeWeapon weapon = new MeleeWeapon(e.Command.CalledArgs[0].ToInt()); Game1.player.addItemByMenuIfNecessary(weapon); this.Monitor.Log($"Gave {weapon.Name} to {Game1.player.Name}", LogLevel.Info); } else this.Monitor.Log(" is invalid", LogLevel.Error); } else this.LogObjectValueNotSpecified(); } /// The event raised when the 'player_addRing' command is triggered. /// The event sender. /// The event arguments. private void HandlePlayerAddRing(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { if (e.Command.CalledArgs[0].IsInt()) { Ring ring = new Ring(e.Command.CalledArgs[0].ToInt()); Game1.player.addItemByMenuIfNecessary(ring); this.Monitor.Log($"Gave {ring.Name} to {Game1.player.Name}", LogLevel.Info); } else this.Monitor.Log(" is invalid", LogLevel.Error); } else this.LogObjectValueNotSpecified(); } /// The event raised when the 'list_items' command is triggered. /// The event sender. /// The event arguments. private void HandleListItems(object sender, EventArgsCommand e) { var matches = this.GetItems(e.Command.CalledArgs).ToArray(); // show matches string summary = "Searching...\n"; if (matches.Any()) this.Monitor.Log(summary + this.GetTableString(matches, new[] { "type", "id", "name" }, val => new[] { val.Type.ToString(), val.ID.ToString(), val.Name }), LogLevel.Info); else this.Monitor.Log(summary + "No items found", LogLevel.Info); } /// The event raised when the 'world_downMineLevel' command is triggered. /// The event sender. /// The event arguments. private void HandleWorldDownMineLevel(object sender, EventArgsCommand e) { Game1.nextMineLevel(); } /// The event raised when the 'world_setMineLevel' command is triggered. /// The event sender. /// The event arguments. private void HandleWorldSetMineLevel(object sender, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { if (e.Command.CalledArgs[0].IsInt()) Game1.enterMine(true, e.Command.CalledArgs[0].ToInt(), ""); else this.LogValueNotInt32(); } else this.LogValueNotSpecified(); } /**** ** Helpers ****/ /// Get all items which can be searched and added to the player's inventory through the console. /// The search string to find. private IEnumerable GetItems(string[] searchWords) { // normalise search term searchWords = searchWords?.Where(word => !string.IsNullOrWhiteSpace(word)).ToArray(); if (searchWords?.Any() == false) searchWords = null; // find matches return ( from item in this.GetItems() let term = $"{item.ID}|{item.Type}|{item.Name}" where searchWords == null || searchWords.All(word => term.IndexOf(word, StringComparison.CurrentCultureIgnoreCase) != -1) select item ); } /// Get all items which can be searched and added to the player's inventory through the console. private IEnumerable GetItems() { // objects foreach (int id in Game1.objectInformation.Keys) { ISearchItem obj = id >= Ring.ringLowerIndexRange && id <= Ring.ringUpperIndexRange ? new SearchableRing(id) : (ISearchItem)new SearchableObject(id); if (obj.IsValid) yield return obj; } // weapons foreach (int id in Game1.content.Load>("Data\\weapons").Keys) { ISearchItem weapon = new SearchableWeapon(id); if (weapon.IsValid) yield return weapon; } } /// Get an ASCII table for a set of tabular data. /// The data type. /// The data to display. /// The table header. /// Returns a set of fields for a data value. private string GetTableString(IEnumerable data, string[] header, Func getRow) { // get table data int[] widths = header.Select(p => p.Length).ToArray(); string[][] rows = data .Select(item => { string[] fields = getRow(item); if (fields.Length != widths.Length) throw new InvalidOperationException($"Expected {widths.Length} columns, but found {fields.Length}: {string.Join(", ", fields)}"); for (int i = 0; i < fields.Length; i++) widths[i] = Math.Max(widths[i], fields[i].Length); return fields; }) .ToArray(); // render fields List lines = new List(rows.Length + 2) { header, header.Select((value, i) => "".PadRight(widths[i], '-')).ToArray() }; lines.AddRange(rows); return string.Join( Environment.NewLine, lines.Select(line => string.Join(" | ", line.Select((field, i) => field.PadRight(widths[i], ' ')).ToArray()) ) ); } /**** ** Logging ****/ /// Log an error indicating a value must be specified. public void LogValueNotSpecified() { this.Monitor.Log(" must be specified", LogLevel.Error); } /// Log an error indicating a target and value must be specified. public void LogObjectValueNotSpecified() { this.Monitor.Log(" and must be specified", LogLevel.Error); } /// Log an error indicating a value is invalid. public void LogValueInvalid() { this.Monitor.Log(" is invalid", LogLevel.Error); } /// Log an error indicating a target is invalid. public void LogObjectInvalid() { this.Monitor.Log(" is invalid", LogLevel.Error); } /// Log an error indicating a value must be an integer. public void LogValueNotInt32() { this.Monitor.Log(" must be a whole number (Int32)", LogLevel.Error); } } }