From 95a93a05b39d2b27b538ecdb0e6a18f28096c5c2 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 7 Feb 2017 20:50:41 -0500 Subject: remove oldest deprecated code (#231) Since Stardew Valley 1.2 breaks most mods anyway, this commits removes the oldest deprecations and fixes the issues that are easiest for mods to update. See documentation for details. --- src/TrainerMod/TrainerMod.cs | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'src/TrainerMod') diff --git a/src/TrainerMod/TrainerMod.cs b/src/TrainerMod/TrainerMod.cs index c76bb78c..a7cedb6a 100644 --- a/src/TrainerMod/TrainerMod.cs +++ b/src/TrainerMod/TrainerMod.cs @@ -81,9 +81,6 @@ namespace TrainerMod 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; @@ -142,15 +139,6 @@ namespace TrainerMod 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. -- cgit From 845fbaab12a040464e403aa693a0eaaa9c55c9ae Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 13 Feb 2017 01:19:02 -0500 Subject: migrate TrainerMod to new API (#199) --- src/StardewModdingAPI/Program.cs | 2 +- src/TrainerMod/TrainerMod.cs | 1027 +++++++++++++++++--------------------- 2 files changed, 468 insertions(+), 561 deletions(-) (limited to 'src/TrainerMod') diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index ff0dff29..58b86e8f 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -578,7 +578,7 @@ namespace StardewModdingAPI if (result == null) Program.Monitor.Log("There's no command with that name.", LogLevel.Error); else - Program.Monitor.Log($"{result.Name}: {result.Documentation} [added by {result.ModName}]", LogLevel.Info); + Program.Monitor.Log($"{result.Name}: {result.Documentation}\n(Added by {result.ModName}.)", LogLevel.Info); } else Program.Monitor.Log("Commands: " + string.Join(", ", Program.CommandManager.GetAll().Select(p => p.Name)), LogLevel.Info); diff --git a/src/TrainerMod/TrainerMod.cs b/src/TrainerMod/TrainerMod.cs index a7cedb6a..1690d5b5 100644 --- a/src/TrainerMod/TrainerMod.cs +++ b/src/TrainerMod/TrainerMod.cs @@ -44,7 +44,7 @@ namespace TrainerMod /// Provides simplified APIs for writing mods. public override void Entry(IModHelper helper) { - this.RegisterCommands(); + this.RegisterCommands(helper); GameEvents.UpdateTick += this.ReceiveUpdateTick; } @@ -73,634 +73,541 @@ namespace TrainerMod 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("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.RegisterCommand("show_game_files", "Opens the game folder. | show_game_files").CommandFired += this.HandleShowGameFiles; - Command.RegisterCommand("show_data_files", "Opens the folder containing the save and log files. | show_data_files").CommandFired += this.HandleShowDataFiles; - } - /**** - ** Command handlers + ** Command definitions ****/ - /// 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); - } + /// Register all trainer commands. + /// Provides simplified APIs for writing mods. + private void RegisterCommands(IModHelper helper) + { + helper.ConsoleCommands + .Add("types", "Lists all value types.", this.HandleCommand) + .Add("save", "Saves the game? Doesn't seem to work.", this.HandleCommand) + .Add("load", "Shows the load screen.", this.HandleCommand) + .Add("player_setname", "Sets the player's name.\n\nUsage: player_setname \n- target: what to rename (one of 'player' or 'farm').\n- name: the new name to set.", this.HandleCommand) + .Add("player_setmoney", "Sets the player's money.\n\nUsage: player_setmoney \n- value: an integer amount, or 'inf' for infinite money.", this.HandleCommand) + .Add("player_setstamina", "Sets the player's stamina.\n\nUsage: player_setstamina \n- value: an integer amount, or 'inf' for infinite stamina.", this.HandleCommand) + .Add("player_setmaxstamina", "Sets the player's max stamina.\n\nUsage: player_setmaxstamina \n- value: an integer amount.", this.HandleCommand) + .Add("player_sethealth", "Sets the player's health.\n\nUsage: player_sethealth \n- value: an integer amount, or 'inf' for infinite health.", this.HandleCommand) + .Add("player_setmaxhealth", "Sets the player's max health.\n\nUsage: player_setmaxhealth \n- value: an integer amount.", this.HandleCommand) + .Add("player_setimmunity", "Sets the player's immunity.\n\nUsage: player_setimmunity \n- value: an integer amount.", this.HandleCommand) + + .Add("player_setlevel", "Sets the player's specified skill to the specified value.\n\nUsage: player_setlevel \n- skill: the skill to set (one of 'luck', 'mining', 'combat', 'farming', 'fishing', or 'foraging').\n- value: the target level (a number from 1 to 10).", this.HandleCommand) + .Add("player_setspeed", "Sets the player's speed to the specified value?\n\nUsage: player_setspeed \n- value: an integer amount (0 is normal).", this.HandleCommand) + .Add("player_changecolour", "Sets the colour of a player feature.\n\nUsage: player_changecolor \n- target: what to change (one of 'hair', 'eyes', or 'pants').\n- colour: a colour value in RGB format, like (255,255,255).", this.HandleCommand) + .Add("player_changestyle", "Sets the style of a player feature.\n\nUsage: player_changecolor .\n- target: what to change (one of 'hair', 'shirt', 'skin', 'acc', 'shoe', 'swim', or 'gender').\n- value: the integer style ID.", this.HandleCommand) + + .Add("player_additem", $"Gives the player an item.\n\nUsage: player_additem [count] [quality]\n- item: the item ID (use the 'list_items' command to see a list).\n- count (optional): how many of the item to give.\n- quality (optional): one of {Object.lowQuality} (normal), {Object.medQuality} (silver), {Object.highQuality} (gold), or {Object.bestQuality} (iridium).", this.HandleCommand) + .Add("player_addmelee", "Gives the player a melee weapon.\n\nUsage: player_addmelee \n- item: the melee weapon ID (use the 'list_items' command to see a list).", this.HandleCommand) + .Add("player_addring", "Gives the player a ring.\n\nUsage: player_addring \n- item: the ring ID (use the 'list_items' command to see a list).", this.HandleCommand) + + .Add("list_items", "Lists and searches items in the game data.\n\nUsage: list_items [search]\n- search (optional): an arbitrary search string to filter by.", this.HandleCommand) + + .Add("world_settime", "Sets the time to the specified value.\n\nUsage: world_settime \n- value: the target time in military time (like 0600 for 6am and 1800 for 6pm)", this.HandleCommand) + .Add("world_freezetime", "Freezes or resumes time.\n\nUsage: world_freezetime \n- value: one of 0 (resume) or 1 (freeze).", this.HandleCommand) + .Add("world_setday", "Sets the day to the specified value.\n\nUsage: world_setday .\n- value: the target day (a number from 1 to 28).", this.HandleCommand) + .Add("world_setseason", "Sets the season to the specified value.\n\nUsage: world_setseason \n- value: the target season (one of 'spring', 'summer', 'fall', 'winter').", this.HandleCommand) + .Add("world_downminelevel", "Goes down one mine level?", this.HandleCommand) + .Add("world_setminelevel", "Sets the mine level?\n\nUsage: world_setminelevel \n- value: The target level (a number between 1 and 120).", this.HandleCommand) + + .Add("show_game_files", "Opens the game folder.", this.HandleCommand) + .Add("show_data_files", "Opens the folder containing the save and log files.", this.HandleCommand); + } + + /// Handle a TrainerMod command. + /// The command name. + /// The command arguments. + private void HandleCommand(string name, string[] args) + { + switch (name) + { + case "type": + 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); + break; - /// The event raised when the 'save' command is triggered. - /// The event sender. - /// The event arguments. - private void HandleSave(object sender, EventArgsCommand e) - { - SaveGame.Save(); - } + case "save": + SaveGame.Save(); + break; - /// 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(); - } + case "load": + Game1.hasLoadedGame = false; + Game1.activeClickableMenu = new LoadGameMenu(); + break; - /// 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_setname": + if (args.Length > 1) { - 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; + string target = args[0]; + string[] validTargets = { "player", "farm" }; + if (validTargets.Contains(target)) + { + switch (target) + { + case "player": + Game1.player.Name = args[1]; + break; + case "farm": + Game1.player.farmName = args[1]; + break; + } + } + else + this.LogObjectInvalid(); } - } - else - this.LogObjectInvalid(); - } - else - this.LogObjectValueNotSpecified(); - } + else + this.LogObjectValueNotSpecified(); + break; - /// 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)) + case "player_setmoney": + if (args.Any()) { - Game1.player.Money = amount; - this.Monitor.Log($"Set {Game1.player.Name}'s money to {Game1.player.Money}", LogLevel.Info); + string amountStr = args[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.LogValueNotInt32(); - } - } - else - this.LogValueNotSpecified(); - } + this.LogValueNotSpecified(); + break; - /// 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)) + case "playet_setstamina": + if (args.Any()) { - Game1.player.Stamina = amount; - this.Monitor.Log($"Set {Game1.player.Name}'s stamina to {Game1.player.Stamina}", LogLevel.Info); + string amountStr = args[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.LogValueNotInt32(); - } - } - else - this.LogValueNotSpecified(); - } + this.LogValueNotSpecified(); + break; - /// 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)) + case "player_setmaxstamina": + if (args.Any()) { - switch (skill) + int amount; + if (int.TryParse(args[0], out amount)) { - 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; + 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.LogValueNotInt32(); - } - else - this.Monitor.Log(" is invalid", LogLevel.Error); - } - else - this.Monitor.Log(" and must be specified", LogLevel.Error); - } + this.LogValueNotSpecified(); + break; - /// 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()) + case "player_setlevel": + if (args.Length > 1) { - var color = new Color(colorHexes[0].ToInt(), colorHexes[1].ToInt(), colorHexes[2].ToInt()); - switch (target) + string skill = args[0]; + string[] skills = { "luck", "mining", "combat", "farming", "fishing", "foraging" }; + if (skills.Contains(skill)) { - case "hair": - Game1.player.hairstyleColor = color; - break; - case "eyes": - Game1.player.changeEyeColor(color); - break; - case "pants": - Game1.player.pantsColor = color; - break; + int level; + if (int.TryParse(args[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(" is invalid", LogLevel.Error); - } - else - this.LogObjectInvalid(); - } - else - this.Monitor.Log(" and must be specified", LogLevel.Error); - } + this.Monitor.Log(" and must be specified", LogLevel.Error); + break; - /// 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()) + case "player_setspeed": + if (args.Any()) { - var styleID = e.Command.CalledArgs[1].ToInt(); - switch (target) + string amountStr = args[0]; + if (amountStr.IsInt()) { - 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; + 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.LogValueInvalid(); - } - else - this.LogObjectInvalid(); - } - else - this.LogObjectValueNotSpecified(); - } + this.LogValueNotSpecified(); + break; - /// 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) + case "player_changecolour": + if (args.Length > 1) { - this.FreezeTime = value == 1; - this.FrozenTime = this.FreezeTime ? Game1.timeOfDay : 0; - this.Monitor.Log("Time is now " + (this.FreezeTime ? "frozen" : "thawed"), LogLevel.Info); + string target = args[0]; + string[] validTargets = { "hair", "eyes", "pants" }; + if (validTargets.Contains(target)) + { + string[] colorHexes = args[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(" should be 0 or 1", LogLevel.Error); - } - else - this.LogValueNotInt32(); - } - else - this.LogValueNotSpecified(); - } + this.Monitor.Log(" and must be specified", LogLevel.Error); + break; - /// 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(); + case "player_changestyle": + if (args.Length > 1) + { + string target = args[0]; + string[] validTargets = { "hair", "shirt", "skin", "acc", "shoe", "swim", "gender" }; + if (validTargets.Contains(target)) + { + if (args[1].IsInt()) + { + var styleID = args[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(); + break; - if (time <= 2600 && time >= 600) + case "world_freezetime": + if (args.Any()) { - Game1.timeOfDay = e.Command.CalledArgs[0].ToInt(); - this.FrozenTime = this.FreezeTime ? Game1.timeOfDay : 0; - this.Monitor.Log($"Time set to: {Game1.timeOfDay}", LogLevel.Info); + string valueStr = args[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.Monitor.Log(" should be between 600 and 2600 (06:00 AM - 02:00 AM [NEXT DAY])", LogLevel.Error); - } - else - this.LogValueNotInt32(); - } - else - this.LogValueNotSpecified(); - } + this.LogValueNotSpecified(); + break; - /// 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]; + case "world_settime": + if (args.Any()) + { + string timeStr = args[0]; + if (timeStr.IsInt()) + { + int time = timeStr.ToInt(); - if (dayStr.IsInt()) - { - int day = dayStr.ToInt(); - if (day <= 28 && day > 0) - Game1.dayOfMonth = day; + if (time <= 2600 && time >= 600) + { + Game1.timeOfDay = args[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.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(); - } + this.LogValueNotSpecified(); + break; - /// 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]; + case "world_setday": + if (args.Any()) + { + string dayStr = args[0]; - if (amountStr == "inf") - this.InfiniteHealth = true; - else - { - this.InfiniteHealth = false; - if (amountStr.IsInt()) - Game1.player.health = amountStr.ToInt(); + 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.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(); - } + this.LogValueNotSpecified(); + break; - /// 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(); - } + case "world_setseason": + if (args.Any()) + { + string season = args[0]; + string[] validSeasons = { "winter", "spring", "summer", "fall" }; + if (validSeasons.Contains(season)) + Game1.currentSeason = season; + else + this.LogValueInvalid(); + } + else + this.LogValueNotSpecified(); + break; - /// 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) + case "player_sethealth": + if (args.Any()) { - if (e.Command.CalledArgs[1].IsInt()) - count = e.Command.CalledArgs[1].ToInt(); + string amountStr = args[0]; + + if (amountStr == "inf") + this.InfiniteHealth = true; else { - this.Monitor.Log("[count] is invalid", LogLevel.Error); - return; + this.InfiniteHealth = false; + if (amountStr.IsInt()) + Game1.player.health = amountStr.ToInt(); + else + this.LogValueNotInt32(); } + } + else + this.LogValueNotSpecified(); + break; + + case "player_setmaxhealth": + if (args.Any()) + { + string amountStr = args[0]; + if (amountStr.IsInt()) + Game1.player.maxHealth = amountStr.ToInt(); + else + this.LogValueNotInt32(); + } + else + this.LogValueNotSpecified(); + break; + + case "player_setimmunity": + if (args.Any()) + { + string amountStr = args[0]; + if (amountStr.IsInt()) + Game1.player.immunity = amountStr.ToInt(); + else + this.LogValueNotInt32(); + } + else + this.LogValueNotSpecified(); + break; - if (e.Command.CalledArgs.Length > 2) + case "player_additem": + if (args.Any()) + { + string itemIdStr = args[0]; + if (itemIdStr.IsInt()) { - if (e.Command.CalledArgs[2].IsInt()) - quality = e.Command.CalledArgs[2].ToInt(); - else + int itemID = itemIdStr.ToInt(); + int count = 1; + int quality = 0; + if (args.Length > 1) { - this.Monitor.Log("[quality] is invalid", LogLevel.Error); - return; + if (args[1].IsInt()) + count = args[1].ToInt(); + else + { + this.Monitor.Log("[count] is invalid", LogLevel.Error); + return; + } + + if (args.Length > 2) + { + if (args[2].IsInt()) + quality = args[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(); + break; - var item = new Object(itemID, count) { quality = quality }; + case "player_addmelee": + if (args.Any()) + { + if (args[0].IsInt()) + { + MeleeWeapon weapon = new MeleeWeapon(args[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(); + break; - Game1.player.addItemByMenuIfNecessary(item); - } - else - this.Monitor.Log(" is invalid", LogLevel.Error); - } - else - this.LogObjectValueNotSpecified(); - } + case "player_addring": + if (args.Any()) + { + if (args[0].IsInt()) + { + Ring ring = new Ring(args[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(); + break; - /// 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(); - } + case "list_items": + { + var matches = this.GetItems(args).ToArray(); - /// 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(); - } + // 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); + } + break; - /// 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); - } + case "world_downminelevel": + Game1.nextMineLevel(); + break; - /// 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(); - } + case "world_setminelevel": + if (args.Any()) + { + if (args[0].IsInt()) + Game1.enterMine(true, args[0].ToInt(), ""); + else + this.LogValueNotInt32(); + } + else + this.LogValueNotSpecified(); + break; - /// 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(); - } + case "show_game_files": + Process.Start(Constants.ExecutionPath); + break; - /// The event raised when the 'show_game_files' command is triggered. - /// The event sender. - /// The event arguments. - private void HandleShowGameFiles(object sender, EventArgsCommand e) - { - Process.Start(Constants.ExecutionPath); - } + case "show_data_files": + Process.Start(Constants.DataPath); + break; - /// The event raised when the 'show_data_files' command is triggered. - /// The event sender. - /// The event arguments. - private void HandleShowDataFiles(object sender, EventArgsCommand e) - { - Process.Start(Constants.DataPath); + default: + throw new NotImplementedException($"TrainerMod received unknown command '{name}'."); + } } /**** -- cgit From d956a7b223fdeeea0ed12a4e2eb1ed1ba2a4fb25 Mon Sep 17 00:00:00 2001 From: Efreak Date: Mon, 27 Feb 2017 13:33:10 -0800 Subject: typo fix a typo preventing usage of player_setstamina --- src/TrainerMod/TrainerMod.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/TrainerMod') diff --git a/src/TrainerMod/TrainerMod.cs b/src/TrainerMod/TrainerMod.cs index 1690d5b5..a1eef561 100644 --- a/src/TrainerMod/TrainerMod.cs +++ b/src/TrainerMod/TrainerMod.cs @@ -181,7 +181,7 @@ namespace TrainerMod this.LogValueNotSpecified(); break; - case "playet_setstamina": + case "player_setstamina": if (args.Any()) { string amountStr = args[0]; -- cgit From 49a801b2b0dce4bd3b4abeee3e45ba0d7f2bee4d Mon Sep 17 00:00:00 2001 From: Efreak Date: Tue, 28 Feb 2017 14:59:10 -0800 Subject: toggle timefreeze when no value given --- src/TrainerMod/TrainerMod.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/TrainerMod') diff --git a/src/TrainerMod/TrainerMod.cs b/src/TrainerMod/TrainerMod.cs index a1eef561..22ec2e66 100644 --- a/src/TrainerMod/TrainerMod.cs +++ b/src/TrainerMod/TrainerMod.cs @@ -104,7 +104,7 @@ namespace TrainerMod .Add("list_items", "Lists and searches items in the game data.\n\nUsage: list_items [search]\n- search (optional): an arbitrary search string to filter by.", this.HandleCommand) .Add("world_settime", "Sets the time to the specified value.\n\nUsage: world_settime \n- value: the target time in military time (like 0600 for 6am and 1800 for 6pm)", this.HandleCommand) - .Add("world_freezetime", "Freezes or resumes time.\n\nUsage: world_freezetime \n- value: one of 0 (resume) or 1 (freeze).", this.HandleCommand) + .Add("world_freezetime", "Freezes or resumes time.\n\nUsage: world_freezetime [value]\n- value: one of 0 (resume), 1 (freeze) or blank (toggle).", this.HandleCommand) .Add("world_setday", "Sets the day to the specified value.\n\nUsage: world_setday .\n- value: the target day (a number from 1 to 28).", this.HandleCommand) .Add("world_setseason", "Sets the season to the specified value.\n\nUsage: world_setseason \n- value: the target season (one of 'spring', 'summer', 'fall', 'winter').", this.HandleCommand) .Add("world_downminelevel", "Goes down one mine level?", this.HandleCommand) @@ -381,13 +381,24 @@ namespace TrainerMod this.Monitor.Log("Time is now " + (this.FreezeTime ? "frozen" : "thawed"), LogLevel.Info); } else - this.Monitor.Log(" should be 0 or 1", LogLevel.Error); + this.Monitor.Log(" should be 0, 1, or empty", LogLevel.Error); } else this.LogValueNotInt32(); } else - this.LogValueNotSpecified(); + int valu = 1; + if (this.FreezeTime == false) + { + valu = 1; + } + else + { + valu = 0; + } + this.FreezeTime = valu == 1; + this.FrozenTime = this.FreezeTime ? Game1.timeOfDay : 0; + this.Monitor.Log("Time is now " + (this.FreezeTime ? "frozen" : "thawed"), LogLevel.Info); break; case "world_settime": -- cgit From 3863c159ae10632b7632ca347555dd17494ca7d0 Mon Sep 17 00:00:00 2001 From: Efreak Date: Tue, 28 Feb 2017 15:04:11 -0800 Subject: Return current values for max/current health/stamina/immunity instead of errors when no value given --- src/TrainerMod/TrainerMod.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/TrainerMod') diff --git a/src/TrainerMod/TrainerMod.cs b/src/TrainerMod/TrainerMod.cs index 22ec2e66..1a85b9b9 100644 --- a/src/TrainerMod/TrainerMod.cs +++ b/src/TrainerMod/TrainerMod.cs @@ -201,7 +201,7 @@ namespace TrainerMod } } else - this.LogValueNotSpecified(); + this.Monitor.Log($"{Game1.player.Name}'s stamina is {Game1.player.Stamina}", LogLevel.Info); break; case "player_setmaxstamina": @@ -217,7 +217,7 @@ namespace TrainerMod this.LogValueNotInt32(); } else - this.LogValueNotSpecified(); + this.Monitor.Log($"{Game1.player.Name}'s maxstamina is {Game1.player.MaxStamina}", LogLevel.Info); break; case "player_setlevel": @@ -476,7 +476,7 @@ namespace TrainerMod } } else - this.LogValueNotSpecified(); + this.Monitor.Log($"Health is: {Game1.player.health}", LogLevel.Info); break; case "player_setmaxhealth": @@ -489,7 +489,7 @@ namespace TrainerMod this.LogValueNotInt32(); } else - this.LogValueNotSpecified(); + this.Monitor.Log($"MaxHealth is: {Game1.player.maxHealth}", LogLevel.Info); break; case "player_setimmunity": @@ -502,7 +502,7 @@ namespace TrainerMod this.LogValueNotInt32(); } else - this.LogValueNotSpecified(); + this.Monitor.Log($"Immunity is: {Game1.player.immunity}", LogLevel.Info); break; case "player_additem": -- cgit From aba50aba45c81c8ed18cf7ee47baa0071455ff97 Mon Sep 17 00:00:00 2001 From: Efreak Date: Tue, 28 Feb 2017 15:27:15 -0800 Subject: Update <> to [] for changed commands --- src/TrainerMod/TrainerMod.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/TrainerMod') diff --git a/src/TrainerMod/TrainerMod.cs b/src/TrainerMod/TrainerMod.cs index 1a85b9b9..1298c5cd 100644 --- a/src/TrainerMod/TrainerMod.cs +++ b/src/TrainerMod/TrainerMod.cs @@ -86,11 +86,11 @@ namespace TrainerMod .Add("load", "Shows the load screen.", this.HandleCommand) .Add("player_setname", "Sets the player's name.\n\nUsage: player_setname \n- target: what to rename (one of 'player' or 'farm').\n- name: the new name to set.", this.HandleCommand) .Add("player_setmoney", "Sets the player's money.\n\nUsage: player_setmoney \n- value: an integer amount, or 'inf' for infinite money.", this.HandleCommand) - .Add("player_setstamina", "Sets the player's stamina.\n\nUsage: player_setstamina \n- value: an integer amount, or 'inf' for infinite stamina.", this.HandleCommand) - .Add("player_setmaxstamina", "Sets the player's max stamina.\n\nUsage: player_setmaxstamina \n- value: an integer amount.", this.HandleCommand) - .Add("player_sethealth", "Sets the player's health.\n\nUsage: player_sethealth \n- value: an integer amount, or 'inf' for infinite health.", this.HandleCommand) - .Add("player_setmaxhealth", "Sets the player's max health.\n\nUsage: player_setmaxhealth \n- value: an integer amount.", this.HandleCommand) - .Add("player_setimmunity", "Sets the player's immunity.\n\nUsage: player_setimmunity \n- value: an integer amount.", this.HandleCommand) + .Add("player_setstamina", "Sets the player's stamina.\n\nUsage: player_setstamina [value]\n- value: an integer amount, or 'inf' for infinite stamina.", this.HandleCommand) + .Add("player_setmaxstamina", "Sets the player's max stamina.\n\nUsage: player_setmaxstamina [value]\n- value: an integer amount.", this.HandleCommand) + .Add("player_sethealth", "Sets the player's health.\n\nUsage: player_sethealth [value]\n- value: an integer amount, or 'inf' for infinite health.", this.HandleCommand) + .Add("player_setmaxhealth", "Sets the player's max health.\n\nUsage: player_setmaxhealth [value]\n- value: an integer amount.", this.HandleCommand) + .Add("player_setimmunity", "Sets the player's immunity.\n\nUsage: player_setimmunity [value]\n- value: an integer amount.", this.HandleCommand) .Add("player_setlevel", "Sets the player's specified skill to the specified value.\n\nUsage: player_setlevel \n- skill: the skill to set (one of 'luck', 'mining', 'combat', 'farming', 'fishing', or 'foraging').\n- value: the target level (a number from 1 to 10).", this.HandleCommand) .Add("player_setspeed", "Sets the player's speed to the specified value?\n\nUsage: player_setspeed \n- value: an integer amount (0 is normal).", this.HandleCommand) -- cgit From 6de4888a1b3e62f3b3976ba013521fbd79405288 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 1 Mar 2017 19:17:34 -0500 Subject: improve TrainerMod feedback to user, standardise color/colour spelling --- release-notes.md | 1 + src/StardewModdingAPI/Program.cs | 5 +- src/TrainerMod/TrainerMod.cs | 260 +++++++++++++++++++++++---------------- 3 files changed, 160 insertions(+), 106 deletions(-) (limited to 'src/TrainerMod') diff --git a/release-notes.md b/release-notes.md index d157c06a..81d770f0 100644 --- a/release-notes.md +++ b/release-notes.md @@ -8,6 +8,7 @@ For players: * Simplified log filename. * Simplified error messages when a mod can't be loaded. * Simple nested mod folders are now recognised by SMAPI (e.g. `ModName-1.0\ModName\manifest.json`). +* Improved TrainerMod command handling & feedback. * Fixed game's debug output being shown in the console for all users. * Fixed the game-outdated error not pausing before exit. * Fixed installer errors for some players when deleting files. diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 18a83e32..07857512 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -554,7 +554,10 @@ namespace StardewModdingAPI this.Monitor.Log($"{result.Name}: {result.Documentation}\n(Added by {result.ModName}.)", LogLevel.Info); } else - this.Monitor.Log("Commands: " + string.Join(", ", this.CommandManager.GetAll().Select(p => p.Name)), LogLevel.Info); + { + this.Monitor.Log("The following commands are registered: " + string.Join(", ", this.CommandManager.GetAll().Select(p => p.Name)) + ".", LogLevel.Info); + this.Monitor.Log("For more information about a command, type 'help command_name'.", LogLevel.Info); + } } /// Show a 'press any key to exit' message, and exit when they press a key. diff --git a/src/TrainerMod/TrainerMod.cs b/src/TrainerMod/TrainerMod.cs index 1298c5cd..b89cbf5c 100644 --- a/src/TrainerMod/TrainerMod.cs +++ b/src/TrainerMod/TrainerMod.cs @@ -94,7 +94,7 @@ namespace TrainerMod .Add("player_setlevel", "Sets the player's specified skill to the specified value.\n\nUsage: player_setlevel \n- skill: the skill to set (one of 'luck', 'mining', 'combat', 'farming', 'fishing', or 'foraging').\n- value: the target level (a number from 1 to 10).", this.HandleCommand) .Add("player_setspeed", "Sets the player's speed to the specified value?\n\nUsage: player_setspeed \n- value: an integer amount (0 is normal).", this.HandleCommand) - .Add("player_changecolour", "Sets the colour of a player feature.\n\nUsage: player_changecolor \n- target: what to change (one of 'hair', 'eyes', or 'pants').\n- colour: a colour value in RGB format, like (255,255,255).", this.HandleCommand) + .Add("player_changecolor", "Sets the color of a player feature.\n\nUsage: player_changecolor \n- target: what to change (one of 'hair', 'eyes', or 'pants').\n- color: a color value in RGB format, like (255,255,255).", this.HandleCommand) .Add("player_changestyle", "Sets the style of a player feature.\n\nUsage: player_changecolor .\n- target: what to change (one of 'hair', 'shirt', 'skin', 'acc', 'shoe', 'swim', or 'gender').\n- value: the integer style ID.", this.HandleCommand) .Add("player_additem", $"Gives the player an item.\n\nUsage: player_additem [count] [quality]\n- item: the item ID (use the 'list_items' command to see a list).\n- count (optional): how many of the item to give.\n- quality (optional): one of {Object.lowQuality} (normal), {Object.medQuality} (silver), {Object.highQuality} (gold), or {Object.bestQuality} (iridium).", this.HandleCommand) @@ -104,7 +104,7 @@ namespace TrainerMod .Add("list_items", "Lists and searches items in the game data.\n\nUsage: list_items [search]\n- search (optional): an arbitrary search string to filter by.", this.HandleCommand) .Add("world_settime", "Sets the time to the specified value.\n\nUsage: world_settime \n- value: the target time in military time (like 0600 for 6am and 1800 for 6pm)", this.HandleCommand) - .Add("world_freezetime", "Freezes or resumes time.\n\nUsage: world_freezetime [value]\n- value: one of 0 (resume), 1 (freeze) or blank (toggle).", this.HandleCommand) + .Add("world_freezetime", "Freezes or resumes time.\n\nUsage: world_freezetime [value]\n- value: one of 0 (resume), 1 (freeze), or blank (toggle).", this.HandleCommand) .Add("world_setday", "Sets the day to the specified value.\n\nUsage: world_setday .\n- value: the target day (a number from 1 to 28).", this.HandleCommand) .Add("world_setseason", "Sets the season to the specified value.\n\nUsage: world_setseason \n- value: the target season (one of 'spring', 'summer', 'fall', 'winter').", this.HandleCommand) .Add("world_downminelevel", "Goes down one mine level?", this.HandleCommand) @@ -115,21 +115,23 @@ namespace TrainerMod } /// Handle a TrainerMod command. - /// The command name. + /// The command name. /// The command arguments. - private void HandleCommand(string name, string[] args) + private void HandleCommand(string command, string[] args) { - switch (name) + switch (command) { case "type": - 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); + this.Monitor.Log($"[Int32: {int.MinValue} - {int.MaxValue}], [Int64: {long.MinValue} - {long.MaxValue}], [String: \"raw text\"], [Color: r,g,b (EG: 128, 32, 255)]", LogLevel.Info); break; case "save": + this.Monitor.Log("Saving the game...", LogLevel.Info); SaveGame.Save(); break; case "load": + this.Monitor.Log("Triggering load menu...", LogLevel.Info); Game1.hasLoadedGame = false; Game1.activeClickableMenu = new LoadGameMenu(); break; @@ -145,17 +147,19 @@ namespace TrainerMod { case "player": Game1.player.Name = args[1]; + this.Monitor.Log($"OK, your player's name is now {Game1.player.Name}.", LogLevel.Info); break; case "farm": Game1.player.farmName = args[1]; + this.Monitor.Log($"OK, your farm's name is now {Game1.player.Name}.", LogLevel.Info); break; } } else - this.LogObjectInvalid(); + this.LogArgumentsInvalid(command); } else - this.LogObjectValueNotSpecified(); + this.Monitor.Log($"Your name is currently '{Game1.player.Name}'. Type 'help player_setname' for usage.", LogLevel.Info); break; case "player_setmoney": @@ -163,7 +167,10 @@ namespace TrainerMod { string amountStr = args[0]; if (amountStr == "inf") + { this.InfiniteMoney = true; + this.Monitor.Log("OK, you now have infinite money.", LogLevel.Info); + } else { this.InfiniteMoney = false; @@ -171,14 +178,14 @@ namespace TrainerMod 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); + this.Monitor.Log($"OK, you now have {Game1.player.Money} gold.", LogLevel.Info); } else - this.LogValueNotInt32(); + this.LogArgumentNotInt(command); } } else - this.LogValueNotSpecified(); + this.Monitor.Log($"You currently have {(this.InfiniteMoney ? "infinite" : Game1.player.Money.ToString())} gold. Specify a value to change it.", LogLevel.Info); break; case "player_setstamina": @@ -186,7 +193,10 @@ namespace TrainerMod { string amountStr = args[0]; if (amountStr == "inf") + { this.InfiniteStamina = true; + this.Monitor.Log("OK, you now have infinite stamina.", LogLevel.Info); + } else { this.InfiniteStamina = false; @@ -194,14 +204,14 @@ namespace TrainerMod 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); + this.Monitor.Log($"OK, you now have {Game1.player.Stamina} stamina.", LogLevel.Info); } else - this.LogValueNotInt32(); + this.LogArgumentNotInt(command); } } else - this.Monitor.Log($"{Game1.player.Name}'s stamina is {Game1.player.Stamina}", LogLevel.Info); + this.Monitor.Log($"You currently have {(this.InfiniteStamina ? "infinite" : Game1.player.Stamina.ToString())} stamina. Specify a value to change it.", LogLevel.Info); break; case "player_setmaxstamina": @@ -211,13 +221,13 @@ namespace TrainerMod if (int.TryParse(args[0], out amount)) { Game1.player.MaxStamina = amount; - this.Monitor.Log($"Set {Game1.player.Name}'s max stamina to {Game1.player.MaxStamina}", LogLevel.Info); + this.Monitor.Log($"OK, you now have {Game1.player.MaxStamina} max stamina.", LogLevel.Info); } else - this.LogValueNotInt32(); + this.LogArgumentNotInt(command); } else - this.Monitor.Log($"{Game1.player.Name}'s maxstamina is {Game1.player.MaxStamina}", LogLevel.Info); + this.Monitor.Log($"You currently have {Game1.player.MaxStamina} max stamina. Specify a value to change it.", LogLevel.Info); break; case "player_setlevel": @@ -234,32 +244,38 @@ namespace TrainerMod { case "luck": Game1.player.LuckLevel = level; + this.Monitor.Log($"OK, your luck skill is now {Game1.player.LuckLevel}.", LogLevel.Info); break; case "mining": Game1.player.MiningLevel = level; + this.Monitor.Log($"OK, your mining skill is now {Game1.player.MiningLevel}.", LogLevel.Info); break; case "combat": Game1.player.CombatLevel = level; + this.Monitor.Log($"OK, your combat skill is now {Game1.player.CombatLevel}.", LogLevel.Info); break; case "farming": Game1.player.FarmingLevel = level; + this.Monitor.Log($"OK, your farming skill is now {Game1.player.FarmingLevel}.", LogLevel.Info); break; case "fishing": Game1.player.FishingLevel = level; + this.Monitor.Log($"OK, your fishing skill is now {Game1.player.FishingLevel}.", LogLevel.Info); break; case "foraging": Game1.player.ForagingLevel = level; + this.Monitor.Log($"OK, your foraging skill is now {Game1.player.ForagingLevel}.", LogLevel.Info); break; } } else - this.LogValueNotInt32(); + this.LogArgumentNotInt(command); } else - this.Monitor.Log(" is invalid", LogLevel.Error); + this.LogUsageError("That isn't a valid skill.", command); } else - this.Monitor.Log(" and must be specified", LogLevel.Error); + this.LogArgumentsInvalid(command); break; case "player_setspeed": @@ -269,16 +285,16 @@ namespace TrainerMod if (amountStr.IsInt()) { Game1.player.addedSpeed = amountStr.ToInt(); - this.Monitor.Log($"Set {Game1.player.Name}'s added speed to {Game1.player.addedSpeed}", LogLevel.Info); + this.Monitor.Log($"OK, your added speed is now {Game1.player.addedSpeed}.", LogLevel.Info); } else - this.LogValueNotInt32(); + this.LogArgumentNotInt(command); } else - this.LogValueNotSpecified(); + this.Monitor.Log($"You currently have {Game1.player.addedSpeed} added speed. Specify a value to change it.", LogLevel.Info); break; - case "player_changecolour": + case "player_changecolor": if (args.Length > 1) { string target = args[0]; @@ -293,23 +309,26 @@ namespace TrainerMod { case "hair": Game1.player.hairstyleColor = color; + this.Monitor.Log("OK, your hair color is updated.", LogLevel.Info); break; case "eyes": Game1.player.changeEyeColor(color); + this.Monitor.Log("OK, your eye color is updated.", LogLevel.Info); break; case "pants": Game1.player.pantsColor = color; + this.Monitor.Log("OK, your pants color is updated.", LogLevel.Info); break; } } else - this.Monitor.Log(" is invalid", LogLevel.Error); + this.LogUsageError("The color should be an RBG value like '255,150,0'.", command); } else - this.LogObjectInvalid(); + this.LogArgumentsInvalid(command); } else - this.Monitor.Log(" and must be specified", LogLevel.Error); + this.LogArgumentsInvalid(command); break; case "player_changestyle": @@ -326,45 +345,66 @@ namespace TrainerMod { case "hair": Game1.player.changeHairStyle(styleID); + this.Monitor.Log("OK, your hair style is updated.", LogLevel.Info); break; case "shirt": Game1.player.changeShirt(styleID); + this.Monitor.Log("OK, your shirt style is updated.", LogLevel.Info); break; case "acc": Game1.player.changeAccessory(styleID); + this.Monitor.Log("OK, your accessory style is updated.", LogLevel.Info); break; case "skin": Game1.player.changeSkinColor(styleID); + this.Monitor.Log("OK, your skin color is updated.", LogLevel.Info); break; case "shoe": Game1.player.changeShoeColor(styleID); + this.Monitor.Log("OK, your shoe style is updated.", LogLevel.Info); 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); + switch (styleID) + { + case 0: + Game1.player.changeOutOfSwimSuit(); + this.Monitor.Log("OK, you're no longer in your swimming suit.", LogLevel.Info); + break; + case 1: + Game1.player.changeIntoSwimsuit(); + this.Monitor.Log("OK, you're now in your swimming suit.", LogLevel.Info); + break; + default: + this.LogUsageError("The swim value should be 0 (no swimming suit) or 1 (swimming suit).", command); + break; + } 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); + switch (styleID) + { + case 0: + Game1.player.changeGender(true); + this.Monitor.Log("OK, you're now male.", LogLevel.Info); + break; + case 1: + Game1.player.changeGender(false); + this.Monitor.Log("OK, you're now female.", LogLevel.Info); + break; + default: + this.LogUsageError("The gender value should be 0 (male) or 1 (female).", command); + break; + } break; } } else - this.LogValueInvalid(); + this.LogArgumentsInvalid(command); } else - this.LogObjectInvalid(); + this.LogArgumentsInvalid(command); } else - this.LogObjectValueNotSpecified(); + this.LogArgumentsInvalid(command); break; case "world_freezetime": @@ -378,27 +418,20 @@ namespace TrainerMod { this.FreezeTime = value == 1; this.FrozenTime = this.FreezeTime ? Game1.timeOfDay : 0; - this.Monitor.Log("Time is now " + (this.FreezeTime ? "frozen" : "thawed"), LogLevel.Info); + this.Monitor.Log($"OK, time is now {(this.FreezeTime ? "frozen" : "resumed")}.", LogLevel.Info); } else - this.Monitor.Log(" should be 0, 1, or empty", LogLevel.Error); + this.LogUsageError("The value should be 0 (not frozen), 1 (frozen), or empty (toggle).", command); } else - this.LogValueNotInt32(); + this.LogArgumentNotInt(command); } else - int valu = 1; - if (this.FreezeTime == false) - { - valu = 1; - } - else - { - valu = 0; - } - this.FreezeTime = valu == 1; + { + this.FreezeTime = !this.FreezeTime; this.FrozenTime = this.FreezeTime ? Game1.timeOfDay : 0; - this.Monitor.Log("Time is now " + (this.FreezeTime ? "frozen" : "thawed"), LogLevel.Info); + this.Monitor.Log($"OK, time is now {(this.FreezeTime ? "frozen" : "resumed")}.", LogLevel.Info); + } break; case "world_settime": @@ -413,16 +446,16 @@ namespace TrainerMod { Game1.timeOfDay = args[0].ToInt(); this.FrozenTime = this.FreezeTime ? Game1.timeOfDay : 0; - this.Monitor.Log($"Time set to: {Game1.timeOfDay}", LogLevel.Info); + this.Monitor.Log($"OK, the time is now {Game1.timeOfDay.ToString().PadLeft(4, '0')}.", LogLevel.Info); } else - this.Monitor.Log(" should be between 600 and 2600 (06:00 AM - 02:00 AM [NEXT DAY])", LogLevel.Error); + this.LogUsageError("That isn't a valid time.", command); } else - this.LogValueNotInt32(); + this.LogArgumentNotInt(command); } else - this.LogValueNotSpecified(); + this.Monitor.Log($"The current time is {Game1.timeOfDay}. Specify a value to change it.", LogLevel.Info); break; case "world_setday": @@ -434,15 +467,18 @@ namespace TrainerMod { int day = dayStr.ToInt(); if (day <= 28 && day > 0) + { Game1.dayOfMonth = day; + this.Monitor.Log($"OK, the date is now {Game1.currentSeason} {Game1.dayOfMonth}.", LogLevel.Info); + } else - this.Monitor.Log(" must be between 1 and 28", LogLevel.Error); + this.LogUsageError("That isn't a valid day.", command); } else - this.LogValueNotInt32(); + this.LogArgumentNotInt(command); } else - this.LogValueNotSpecified(); + this.Monitor.Log($"The current date is {Game1.currentSeason} {Game1.dayOfMonth}. Specify a value to change the day.", LogLevel.Info); break; case "world_setseason": @@ -451,12 +487,15 @@ namespace TrainerMod string season = args[0]; string[] validSeasons = { "winter", "spring", "summer", "fall" }; if (validSeasons.Contains(season)) + { Game1.currentSeason = season; + this.Monitor.Log($"OK, the date is now {Game1.currentSeason} {Game1.dayOfMonth}.", LogLevel.Info); + } else - this.LogValueInvalid(); + this.LogUsageError("That isn't a valid season name.", command); } else - this.LogValueNotSpecified(); + this.Monitor.Log($"The current season is {Game1.currentSeason}. Specify a value to change it.", LogLevel.Info); break; case "player_sethealth": @@ -465,18 +504,24 @@ namespace TrainerMod string amountStr = args[0]; if (amountStr == "inf") + { this.InfiniteHealth = true; + this.Monitor.Log("OK, you now have infinite health.", LogLevel.Info); + } else { this.InfiniteHealth = false; if (amountStr.IsInt()) + { Game1.player.health = amountStr.ToInt(); + this.Monitor.Log($"OK, you now have {Game1.player.health} health.", LogLevel.Info); + } else - this.LogValueNotInt32(); + this.LogArgumentNotInt(command); } } else - this.Monitor.Log($"Health is: {Game1.player.health}", LogLevel.Info); + this.Monitor.Log($"You currently have {(this.InfiniteHealth ? "infinite" : Game1.player.health.ToString())} health. Specify a value to change it.", LogLevel.Info); break; case "player_setmaxhealth": @@ -484,12 +529,15 @@ namespace TrainerMod { string amountStr = args[0]; if (amountStr.IsInt()) + { Game1.player.maxHealth = amountStr.ToInt(); + this.Monitor.Log($"OK, you now have {Game1.player.maxHealth} max health.", LogLevel.Info); + } else - this.LogValueNotInt32(); + this.LogArgumentNotInt(command); } else - this.Monitor.Log($"MaxHealth is: {Game1.player.maxHealth}", LogLevel.Info); + this.Monitor.Log($"You currently have {Game1.player.maxHealth} max health. Specify a value to change it.", LogLevel.Info); break; case "player_setimmunity": @@ -497,12 +545,15 @@ namespace TrainerMod { string amountStr = args[0]; if (amountStr.IsInt()) + { Game1.player.immunity = amountStr.ToInt(); + this.Monitor.Log($"OK, you now have {Game1.player.immunity} immunity.", LogLevel.Info); + } else - this.LogValueNotInt32(); + this.LogArgumentNotInt(command); } else - this.Monitor.Log($"Immunity is: {Game1.player.immunity}", LogLevel.Info); + this.Monitor.Log($"You currently have {Game1.player.immunity} immunity. Specify a value to change it.", LogLevel.Info); break; case "player_additem": @@ -520,7 +571,7 @@ namespace TrainerMod count = args[1].ToInt(); else { - this.Monitor.Log("[count] is invalid", LogLevel.Error); + this.LogUsageError("The optional count is invalid.", command); return; } @@ -530,21 +581,21 @@ namespace TrainerMod quality = args[2].ToInt(); else { - this.Monitor.Log("[quality] is invalid", LogLevel.Error); + this.LogUsageError("The optional quality is invalid.", command); return; } } } var item = new Object(itemID, count) { quality = quality }; - Game1.player.addItemByMenuIfNecessary(item); + this.Monitor.Log($"OK, added {item.Name} to your inventory.", LogLevel.Info); } else - this.Monitor.Log(" is invalid", LogLevel.Error); + this.LogUsageError("The item ID must be an integer.", command); } else - this.LogObjectValueNotSpecified(); + this.LogArgumentsInvalid(command); break; case "player_addmelee": @@ -554,13 +605,13 @@ namespace TrainerMod { MeleeWeapon weapon = new MeleeWeapon(args[0].ToInt()); Game1.player.addItemByMenuIfNecessary(weapon); - this.Monitor.Log($"Gave {weapon.Name} to {Game1.player.Name}", LogLevel.Info); + this.Monitor.Log($"OK, added {weapon.Name} to your inventory.", LogLevel.Info); } else - this.Monitor.Log(" is invalid", LogLevel.Error); + this.LogUsageError("The weapon ID must be an integer.", command); } else - this.LogObjectValueNotSpecified(); + this.LogArgumentsInvalid(command); break; case "player_addring": @@ -570,13 +621,13 @@ namespace TrainerMod { Ring ring = new Ring(args[0].ToInt()); Game1.player.addItemByMenuIfNecessary(ring); - this.Monitor.Log($"Gave {ring.Name} to {Game1.player.Name}", LogLevel.Info); + this.Monitor.Log($"OK, added {ring.Name} to your inventory.", LogLevel.Info); } else this.Monitor.Log(" is invalid", LogLevel.Error); } else - this.LogObjectValueNotSpecified(); + this.LogArgumentsInvalid(command); break; case "list_items": @@ -594,30 +645,37 @@ namespace TrainerMod case "world_downminelevel": Game1.nextMineLevel(); + this.Monitor.Log("OK, warping you to the next mine level.", LogLevel.Info); break; case "world_setminelevel": if (args.Any()) { if (args[0].IsInt()) - Game1.enterMine(true, args[0].ToInt(), ""); + { + int level = args[0].ToInt(); + Game1.enterMine(true, level, ""); + this.Monitor.Log($"OK, warping you to mine level {level}.", LogLevel.Info); + } else - this.LogValueNotInt32(); + this.LogArgumentNotInt(command); } else - this.LogValueNotSpecified(); + this.LogArgumentsInvalid(command); break; case "show_game_files": Process.Start(Constants.ExecutionPath); + this.Monitor.Log($"OK, opening {Constants.ExecutionPath}.", LogLevel.Info); break; case "show_data_files": Process.Start(Constants.DataPath); + this.Monitor.Log($"OK, opening {Constants.DataPath}.", LogLevel.Info); break; default: - throw new NotImplementedException($"TrainerMod received unknown command '{name}'."); + throw new NotImplementedException($"TrainerMod received unknown command '{command}'."); } } @@ -706,34 +764,26 @@ namespace TrainerMod /**** ** Logging ****/ - /// Log an error indicating a value must be specified. - public void LogValueNotSpecified() + /// Log an error indicating incorrect usage. + /// A sentence explaining the problem. + /// The name of the command. + private void LogUsageError(string error, string command) { - this.Monitor.Log(" must be specified", LogLevel.Error); + this.Monitor.Log($"{error} Type 'help {command}' for usage.", LogLevel.Error); } - /// Log an error indicating a target and value must be specified. - public void LogObjectValueNotSpecified() + /// Log an error indicating a value must be an integer. + /// The name of the command. + private void LogArgumentNotInt(string command) { - this.Monitor.Log(" and must be specified", LogLevel.Error); + this.LogUsageError("The value must be a whole number.", command); } /// 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() + /// The name of the command. + private void LogArgumentsInvalid(string command) { - this.Monitor.Log(" must be a whole number (Int32)", LogLevel.Error); + this.LogUsageError("The arguments are invalid.", command); } } } -- cgit From 035f94f7cbe693a107e76ae97d29c8e7da2ccb07 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 1 Mar 2017 19:19:09 -0500 Subject: remove unneeded 'types' TrainerMod command --- src/TrainerMod/TrainerMod.cs | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src/TrainerMod') diff --git a/src/TrainerMod/TrainerMod.cs b/src/TrainerMod/TrainerMod.cs index b89cbf5c..1e668b42 100644 --- a/src/TrainerMod/TrainerMod.cs +++ b/src/TrainerMod/TrainerMod.cs @@ -81,7 +81,6 @@ namespace TrainerMod private void RegisterCommands(IModHelper helper) { helper.ConsoleCommands - .Add("types", "Lists all value types.", this.HandleCommand) .Add("save", "Saves the game? Doesn't seem to work.", this.HandleCommand) .Add("load", "Shows the load screen.", this.HandleCommand) .Add("player_setname", "Sets the player's name.\n\nUsage: player_setname \n- target: what to rename (one of 'player' or 'farm').\n- name: the new name to set.", this.HandleCommand) @@ -121,10 +120,6 @@ namespace TrainerMod { switch (command) { - case "type": - this.Monitor.Log($"[Int32: {int.MinValue} - {int.MaxValue}], [Int64: {long.MinValue} - {long.MaxValue}], [String: \"raw text\"], [Color: r,g,b (EG: 128, 32, 255)]", LogLevel.Info); - break; - case "save": this.Monitor.Log("Saving the game...", LogLevel.Info); SaveGame.Save(); -- cgit From 51ac0127e8fcee3d89eb4e3193250ed58fec8de9 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 1 Mar 2017 19:20:11 -0500 Subject: fix TrainerMod letting player add invalid items --- release-notes.md | 1 + src/TrainerMod/TrainerMod.cs | 30 +++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) (limited to 'src/TrainerMod') diff --git a/release-notes.md b/release-notes.md index 797da19d..3dfa42ad 100644 --- a/release-notes.md +++ b/release-notes.md @@ -14,6 +14,7 @@ For players: * Fixed installer errors for some players when deleting files. * Fixed installer not ignoring potential game folders that don't contain a Stardew Valley exe. * Fixed installer not recognising Linux/Mac paths starting with `~/` or containing an escaped space. +* Fixed TrainerMod letting you add invalid items which may crash the game. * Fixed rare issue where mod dependencies would override SMAPI dependencies and cause unpredictable bugs. * Fixed errors in console command handlers causing the game to crash. diff --git a/src/TrainerMod/TrainerMod.cs b/src/TrainerMod/TrainerMod.cs index 1e668b42..5cbd1ae6 100644 --- a/src/TrainerMod/TrainerMod.cs +++ b/src/TrainerMod/TrainerMod.cs @@ -583,8 +583,13 @@ namespace TrainerMod } var item = new Object(itemID, count) { quality = quality }; - Game1.player.addItemByMenuIfNecessary(item); - this.Monitor.Log($"OK, added {item.Name} to your inventory.", LogLevel.Info); + if (item.Name == "Error Item") + this.Monitor.Log("There is no such item ID.", LogLevel.Error); + else + { + Game1.player.addItemByMenuIfNecessary(item); + this.Monitor.Log($"OK, added {item.Name} to your inventory.", LogLevel.Info); + } } else this.LogUsageError("The item ID must be an integer.", command); @@ -599,8 +604,13 @@ namespace TrainerMod if (args[0].IsInt()) { MeleeWeapon weapon = new MeleeWeapon(args[0].ToInt()); - Game1.player.addItemByMenuIfNecessary(weapon); - this.Monitor.Log($"OK, added {weapon.Name} to your inventory.", LogLevel.Info); + if (weapon.Name == null) + this.Monitor.Log("There is no such weapon ID.", LogLevel.Error); + else + { + Game1.player.addItemByMenuIfNecessary(weapon); + this.Monitor.Log($"OK, added {weapon.Name} to your inventory.", LogLevel.Info); + } } else this.LogUsageError("The weapon ID must be an integer.", command); @@ -614,9 +624,15 @@ namespace TrainerMod { if (args[0].IsInt()) { - Ring ring = new Ring(args[0].ToInt()); - Game1.player.addItemByMenuIfNecessary(ring); - this.Monitor.Log($"OK, added {ring.Name} to your inventory.", LogLevel.Info); + int ringID = args[0].ToInt(); + if (ringID < Ring.ringLowerIndexRange || ringID > Ring.ringUpperIndexRange) + this.Monitor.Log($"There is no such ring ID (must be between {Ring.ringLowerIndexRange} and {Ring.ringUpperIndexRange}).", LogLevel.Error); + else + { + Ring ring = new Ring(ringID); + Game1.player.addItemByMenuIfNecessary(ring); + this.Monitor.Log($"OK, added {ring.Name} to your inventory.", LogLevel.Info); + } } else this.Monitor.Log(" is invalid", LogLevel.Error); -- cgit From 5270240c01a32b19d949a24566b65731dbc3d865 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 1 Mar 2017 19:21:58 -0500 Subject: update TrainerMod manifest --- src/TrainerMod/manifest.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/TrainerMod') diff --git a/src/TrainerMod/manifest.json b/src/TrainerMod/manifest.json index 72a3f40a..8bddf02d 100644 --- a/src/TrainerMod/manifest.json +++ b/src/TrainerMod/manifest.json @@ -1,13 +1,13 @@ { "Name": "Trainer Mod", - "Author": "Zoryn", + "Author": "SMAPI", "Version": { "MajorVersion": 1, - "MinorVersion": 0, + "MinorVersion": 9, "PatchVersion": 0, - "Build": "" + "Build": null }, - "Description": "Registers several commands to use. Most commands are trainer-like in that they offer forms of cheating.", - "UniqueID": "Zoryn.TrainerMod", + "Description": "Adds SMAPI console commands that let you manipulate the game.", + "UniqueID": "SMAPI.TrainerMod", "EntryDll": "TrainerMod.dll" -} \ No newline at end of file +} -- cgit From 5ed6eb09860050f1a669539a75df21200d59f554 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 1 Mar 2017 20:06:03 -0500 Subject: fix `world_downminelevel` command not working, prevent invalid `world_setminelevel` value --- release-notes.md | 1 + src/TrainerMod/TrainerMod.cs | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'src/TrainerMod') diff --git a/release-notes.md b/release-notes.md index 029246b0..7a936694 100644 --- a/release-notes.md +++ b/release-notes.md @@ -15,6 +15,7 @@ For players: * Fixed installer not ignoring potential game folders that don't contain a Stardew Valley exe. * Fixed installer not recognising Linux/Mac paths starting with `~/` or containing an escaped space. * Fixed TrainerMod letting you add invalid items which may crash the game. +* Fixed TrainerMod's `world_downminelevel` command not working. * Fixed rare issue where mod dependencies would override SMAPI dependencies and cause unpredictable bugs. * Fixed errors in console command handlers causing the game to crash. diff --git a/src/TrainerMod/TrainerMod.cs b/src/TrainerMod/TrainerMod.cs index 5cbd1ae6..1d4e0b17 100644 --- a/src/TrainerMod/TrainerMod.cs +++ b/src/TrainerMod/TrainerMod.cs @@ -6,6 +6,7 @@ using Microsoft.Xna.Framework; using StardewModdingAPI; using StardewModdingAPI.Events; using StardewValley; +using StardewValley.Locations; using StardewValley.Menus; using StardewValley.Objects; using StardewValley.Tools; @@ -655,18 +656,21 @@ namespace TrainerMod break; case "world_downminelevel": - Game1.nextMineLevel(); - this.Monitor.Log("OK, warping you to the next mine level.", LogLevel.Info); - break; + { + int level = (Game1.currentLocation as MineShaft)?.mineLevel ?? 0; + this.Monitor.Log($"OK, warping you to mine level {level + 1}.", LogLevel.Info); + Game1.enterMine(false, level + 1, ""); + break; + } case "world_setminelevel": if (args.Any()) { if (args[0].IsInt()) { - int level = args[0].ToInt(); - Game1.enterMine(true, level, ""); + int level = Math.Max(1, args[0].ToInt()); this.Monitor.Log($"OK, warping you to mine level {level}.", LogLevel.Info); + Game1.enterMine(true, level, ""); } else this.LogArgumentNotInt(command); -- cgit From 02a4c40814caf26f21fc9afd9c64afd639da462f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 14 Mar 2017 13:44:41 -0400 Subject: simplify TrainerMod int parsing with C# 7 out variables --- src/TrainerMod/Framework/Extensions.cs | 25 ------------ src/TrainerMod/TrainerMod.cs | 73 ++++++++++++---------------------- src/TrainerMod/TrainerMod.csproj | 3 +- 3 files changed, 26 insertions(+), 75 deletions(-) delete mode 100644 src/TrainerMod/Framework/Extensions.cs (limited to 'src/TrainerMod') diff --git a/src/TrainerMod/Framework/Extensions.cs b/src/TrainerMod/Framework/Extensions.cs deleted file mode 100644 index da3a2f03..00000000 --- a/src/TrainerMod/Framework/Extensions.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace TrainerMod.Framework -{ - /// Provides extension methods on primitive types. - internal static class Extensions - { - /********* - ** Public methods - *********/ - /// Get whether an object is a number. - /// The object value. - public static bool IsInt(this object value) - { - int i; - return int.TryParse(value.ToString(), out i); - } - - /// Parse an object into a number. - /// The object value. - /// The value is not a valid number. - public static int ToInt(this object value) - { - return int.Parse(value.ToString()); - } - } -} diff --git a/src/TrainerMod/TrainerMod.cs b/src/TrainerMod/TrainerMod.cs index 1d4e0b17..d6990a14 100644 --- a/src/TrainerMod/TrainerMod.cs +++ b/src/TrainerMod/TrainerMod.cs @@ -10,7 +10,6 @@ using StardewValley.Locations; using StardewValley.Menus; using StardewValley.Objects; using StardewValley.Tools; -using TrainerMod.Framework; using TrainerMod.ItemData; using Object = StardewValley.Object; @@ -277,10 +276,9 @@ namespace TrainerMod case "player_setspeed": if (args.Any()) { - string amountStr = args[0]; - if (amountStr.IsInt()) + if (int.TryParse(args[0], out int addedSpeed)) { - Game1.player.addedSpeed = amountStr.ToInt(); + Game1.player.addedSpeed = addedSpeed; this.Monitor.Log($"OK, your added speed is now {Game1.player.addedSpeed}.", LogLevel.Info); } else @@ -298,9 +296,9 @@ namespace TrainerMod if (validTargets.Contains(target)) { string[] colorHexes = args[1].Split(new[] { ',' }, 3); - if (colorHexes[0].IsInt() && colorHexes[1].IsInt() && colorHexes[2].IsInt()) + if (int.TryParse(colorHexes[0], out int r) && int.TryParse(colorHexes[1], out int g) && int.TryParse(colorHexes[2], out int b)) { - var color = new Color(colorHexes[0].ToInt(), colorHexes[1].ToInt(), colorHexes[2].ToInt()); + Color color = new Color(r, g, b); switch (target) { case "hair": @@ -334,9 +332,8 @@ namespace TrainerMod string[] validTargets = { "hair", "shirt", "skin", "acc", "shoe", "swim", "gender" }; if (validTargets.Contains(target)) { - if (args[1].IsInt()) + if (int.TryParse(args[1], out int styleID)) { - var styleID = args[1].ToInt(); switch (target) { case "hair": @@ -406,10 +403,8 @@ namespace TrainerMod case "world_freezetime": if (args.Any()) { - string valueStr = args[0]; - if (valueStr.IsInt()) + if (int.TryParse(args[0], out int value)) { - int value = valueStr.ToInt(); if (value == 0 || value == 1) { this.FreezeTime = value == 1; @@ -433,14 +428,11 @@ namespace TrainerMod case "world_settime": if (args.Any()) { - string timeStr = args[0]; - if (timeStr.IsInt()) + if (int.TryParse(args[0], out int time)) { - int time = timeStr.ToInt(); - if (time <= 2600 && time >= 600) { - Game1.timeOfDay = args[0].ToInt(); + Game1.timeOfDay = time; this.FrozenTime = this.FreezeTime ? Game1.timeOfDay : 0; this.Monitor.Log($"OK, the time is now {Game1.timeOfDay.ToString().PadLeft(4, '0')}.", LogLevel.Info); } @@ -457,11 +449,8 @@ namespace TrainerMod case "world_setday": if (args.Any()) { - string dayStr = args[0]; - - if (dayStr.IsInt()) + if (int.TryParse(args[0], out int day)) { - int day = dayStr.ToInt(); if (day <= 28 && day > 0) { Game1.dayOfMonth = day; @@ -507,9 +496,9 @@ namespace TrainerMod else { this.InfiniteHealth = false; - if (amountStr.IsInt()) + if (int.TryParse(amountStr, out int amount)) { - Game1.player.health = amountStr.ToInt(); + Game1.player.health = amount; this.Monitor.Log($"OK, you now have {Game1.player.health} health.", LogLevel.Info); } else @@ -523,10 +512,9 @@ namespace TrainerMod case "player_setmaxhealth": if (args.Any()) { - string amountStr = args[0]; - if (amountStr.IsInt()) + if (int.TryParse(args[0], out int maxHealth)) { - Game1.player.maxHealth = amountStr.ToInt(); + Game1.player.maxHealth = maxHealth; this.Monitor.Log($"OK, you now have {Game1.player.maxHealth} max health.", LogLevel.Info); } else @@ -539,10 +527,9 @@ namespace TrainerMod case "player_setimmunity": if (args.Any()) { - string amountStr = args[0]; - if (amountStr.IsInt()) + if (int.TryParse(args[0], out int amount)) { - Game1.player.immunity = amountStr.ToInt(); + Game1.player.immunity = amount; this.Monitor.Log($"OK, you now have {Game1.player.immunity} immunity.", LogLevel.Info); } else @@ -555,31 +542,22 @@ namespace TrainerMod case "player_additem": if (args.Any()) { - string itemIdStr = args[0]; - if (itemIdStr.IsInt()) + if (int.TryParse(args[0], out int itemID)) { - int itemID = itemIdStr.ToInt(); int count = 1; int quality = 0; if (args.Length > 1) { - if (args[1].IsInt()) - count = args[1].ToInt(); - else + if (!int.TryParse(args[1], out count)) { this.LogUsageError("The optional count is invalid.", command); return; } - if (args.Length > 2) + if (args.Length > 2 && !int.TryParse(args[2], out quality)) { - if (args[2].IsInt()) - quality = args[2].ToInt(); - else - { - this.LogUsageError("The optional quality is invalid.", command); - return; - } + this.LogUsageError("The optional quality is invalid.", command); + return; } } @@ -602,9 +580,9 @@ namespace TrainerMod case "player_addmelee": if (args.Any()) { - if (args[0].IsInt()) + if (int.TryParse(args[0], out int weaponID)) { - MeleeWeapon weapon = new MeleeWeapon(args[0].ToInt()); + MeleeWeapon weapon = new MeleeWeapon(weaponID); if (weapon.Name == null) this.Monitor.Log("There is no such weapon ID.", LogLevel.Error); else @@ -623,9 +601,8 @@ namespace TrainerMod case "player_addring": if (args.Any()) { - if (args[0].IsInt()) + if (int.TryParse(args[0], out int ringID)) { - int ringID = args[0].ToInt(); if (ringID < Ring.ringLowerIndexRange || ringID > Ring.ringUpperIndexRange) this.Monitor.Log($"There is no such ring ID (must be between {Ring.ringLowerIndexRange} and {Ring.ringUpperIndexRange}).", LogLevel.Error); else @@ -666,9 +643,9 @@ namespace TrainerMod case "world_setminelevel": if (args.Any()) { - if (args[0].IsInt()) + if (int.TryParse(args[0], out int level)) { - int level = Math.Max(1, args[0].ToInt()); + level = Math.Max(1, level); this.Monitor.Log($"OK, warping you to mine level {level}.", LogLevel.Info); Game1.enterMine(true, level, ""); } diff --git a/src/TrainerMod/TrainerMod.csproj b/src/TrainerMod/TrainerMod.csproj index 1457ac2b..a6303767 100644 --- a/src/TrainerMod/TrainerMod.csproj +++ b/src/TrainerMod/TrainerMod.csproj @@ -22,7 +22,7 @@ 4 x86 false - 6 + 7 true @@ -53,7 +53,6 @@ Properties\GlobalAssemblyInfo.cs - -- cgit From da630efc1d5c95816493c2969736ae883e723757 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 14 Mar 2017 14:15:50 -0400 Subject: downgrade to .NET Framework 4.0 for better compatibility on Windows 7–8.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- release-notes.md | 1 + .../PlatformAssemblyMap.cs | 2 +- .../StardewModdingAPI.AssemblyRewriters.csproj | 14 +++++------ .../packages.config | 2 +- .../StardewModdingAPI.Installer.csproj | 2 +- src/StardewModdingAPI/App.config | 2 +- .../Events/ContentEventHandler.cs | 8 ++++++ src/StardewModdingAPI/Events/ContentEvents.cs | 2 +- .../Framework/InternalExtensions.cs | 29 ++++++++++++++++++++-- .../Framework/Reflection/PrivateProperty.cs | 4 +-- src/StardewModdingAPI/Framework/UpdateHelper.cs | 7 +++--- src/StardewModdingAPI/Program.cs | 24 ++++++------------ src/StardewModdingAPI/StardewModdingAPI.csproj | 20 ++++++++------- src/StardewModdingAPI/packages.config | 4 +-- src/TrainerMod/TrainerMod.csproj | 4 +-- src/TrainerMod/packages.config | 2 +- 16 files changed, 77 insertions(+), 50 deletions(-) create mode 100644 src/StardewModdingAPI/Events/ContentEventHandler.cs (limited to 'src/TrainerMod') diff --git a/release-notes.md b/release-notes.md index f91ef733..01d49dd7 100644 --- a/release-notes.md +++ b/release-notes.md @@ -20,6 +20,7 @@ For players: * Simplified error messages when a mod can't be loaded. * Simple nested mod folders are now recognised by SMAPI (e.g. `ModName-1.0\ModName\manifest.json`). * Improved TrainerMod command handling & feedback. +* Reduced minimum .NET Framework version to 4.0 for improved compatibility on Windows 7–8.1. * Fixed game's debug output being shown in the console for all users. * Fixed the game-outdated error not pausing before exit. * Fixed installer errors for some players when deleting files. diff --git a/src/StardewModdingAPI.AssemblyRewriters/PlatformAssemblyMap.cs b/src/StardewModdingAPI.AssemblyRewriters/PlatformAssemblyMap.cs index fce2b187..3ca90149 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/PlatformAssemblyMap.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/PlatformAssemblyMap.cs @@ -49,7 +49,7 @@ namespace StardewModdingAPI.AssemblyRewriters // cache assembly metadata this.Targets = targetAssemblies; this.TargetReferences = this.Targets.ToDictionary(assembly => assembly, assembly => AssemblyNameReference.Parse(assembly.FullName)); - this.TargetModules = this.Targets.ToDictionary(assembly => assembly, assembly => ModuleDefinition.ReadModule(assembly.Modules.Single().FullyQualifiedName)); + this.TargetModules = this.Targets.ToDictionary(assembly => assembly, assembly => ModuleDefinition.ReadModule(assembly.GetModules().Single().FullyQualifiedName)); } } } diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj index a3322e67..15bb15ac 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj @@ -9,7 +9,7 @@ Properties StardewModdingAPI.AssemblyRewriters StardewModdingAPI.AssemblyRewriters - v4.5 + v4.0 512 @@ -49,16 +49,16 @@ - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.dll - True + ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.dll - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Mdb.dll - True + ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Mdb.dll - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Pdb.dll - True + ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Pdb.dll + + + ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Rocks.dll diff --git a/src/StardewModdingAPI.AssemblyRewriters/packages.config b/src/StardewModdingAPI.AssemblyRewriters/packages.config index 88fbc79d..70ba1aed 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/packages.config +++ b/src/StardewModdingAPI.AssemblyRewriters/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/src/StardewModdingAPI.Installer/StardewModdingAPI.Installer.csproj b/src/StardewModdingAPI.Installer/StardewModdingAPI.Installer.csproj index e31a1452..366e1c6e 100644 --- a/src/StardewModdingAPI.Installer/StardewModdingAPI.Installer.csproj +++ b/src/StardewModdingAPI.Installer/StardewModdingAPI.Installer.csproj @@ -9,7 +9,7 @@ Properties StardewModdingAPI.Installer StardewModdingAPI.Installer - v4.5 + v4.0 512 true diff --git a/src/StardewModdingAPI/App.config b/src/StardewModdingAPI/App.config index 27cdf0f7..314845f7 100644 --- a/src/StardewModdingAPI/App.config +++ b/src/StardewModdingAPI/App.config @@ -1,7 +1,7 @@ - + diff --git a/src/StardewModdingAPI/Events/ContentEventHandler.cs b/src/StardewModdingAPI/Events/ContentEventHandler.cs new file mode 100644 index 00000000..2a7e75d1 --- /dev/null +++ b/src/StardewModdingAPI/Events/ContentEventHandler.cs @@ -0,0 +1,8 @@ +namespace StardewModdingAPI.Events +{ + /// Represents a method that will handle a content event. + /// The source of the event. + /// The event arguments. + /// This deviates from in allowing T to be an interface instead of a concrete class. While .NET Framework 4.5 allows that, the current .NET Framework 4.0 targeted by SMAPI to improve compatibility does not. + public delegate void ContentEventHandler(object sender, IContentEventHelper e); +} diff --git a/src/StardewModdingAPI/Events/ContentEvents.cs b/src/StardewModdingAPI/Events/ContentEvents.cs index 9418673a..339e90fd 100644 --- a/src/StardewModdingAPI/Events/ContentEvents.cs +++ b/src/StardewModdingAPI/Events/ContentEvents.cs @@ -28,7 +28,7 @@ namespace StardewModdingAPI.Events public static event EventHandler> AfterLocaleChanged; /// Raised when an XNB file is being read into the cache. Mods can change the data here before it's cached. - internal static event EventHandler AfterAssetLoaded; + internal static event ContentEventHandler AfterAssetLoaded; /********* diff --git a/src/StardewModdingAPI/Framework/InternalExtensions.cs b/src/StardewModdingAPI/Framework/InternalExtensions.cs index 4ca79518..46c76656 100644 --- a/src/StardewModdingAPI/Framework/InternalExtensions.cs +++ b/src/StardewModdingAPI/Framework/InternalExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using StardewModdingAPI.Events; namespace StardewModdingAPI.Framework { @@ -59,12 +60,36 @@ namespace StardewModdingAPI.Framework /// The event handlers. /// The event sender. /// The event arguments. - public static void SafelyRaiseGenericEvent(this IMonitor monitor, string name, IEnumerable handlers, object sender, TEventArgs args) + public static void SafelyRaiseGenericEvent(this IMonitor monitor, string name, IEnumerable handlers, object sender, TEventArgs args) where TEventArgs : EventArgs { if (handlers == null) return; - foreach (EventHandler handler in Enumerable.Cast>(handlers)) + foreach (EventHandler handler in handlers.Cast>()) + { + try + { + handler.Invoke(sender, args); + } + catch (Exception ex) + { + monitor.Log($"A mod failed handling the {name} event:\n{ex.GetLogSummary()}", LogLevel.Error); + } + } + } + + /// Safely raise an event, and intercept any exceptions thrown by its handlers. + /// Encapsulates monitoring and logging. + /// The event name for error messages. + /// The event handlers. + /// The event sender. + /// The event arguments. + public static void SafelyRaiseGenericEvent(this IMonitor monitor, string name, IEnumerable handlers, object sender, IContentEventHelper args) + { + if (handlers == null) + return; + + foreach (ContentEventHandler handler in handlers.Cast()) { try { diff --git a/src/StardewModdingAPI/Framework/Reflection/PrivateProperty.cs b/src/StardewModdingAPI/Framework/Reflection/PrivateProperty.cs index 08204b7e..d89e8e44 100644 --- a/src/StardewModdingAPI/Framework/Reflection/PrivateProperty.cs +++ b/src/StardewModdingAPI/Framework/Reflection/PrivateProperty.cs @@ -60,7 +60,7 @@ namespace StardewModdingAPI.Framework.Reflection { try { - return (TValue)this.PropertyInfo.GetValue(this.Parent); + return (TValue)this.PropertyInfo.GetValue(this.Parent, null); } catch (InvalidCastException) { @@ -78,7 +78,7 @@ namespace StardewModdingAPI.Framework.Reflection { try { - this.PropertyInfo.SetValue(this.Parent, value); + this.PropertyInfo.SetValue(this.Parent, value, null); } catch (InvalidCastException) { diff --git a/src/StardewModdingAPI/Framework/UpdateHelper.cs b/src/StardewModdingAPI/Framework/UpdateHelper.cs index e01e55c8..342a08cf 100644 --- a/src/StardewModdingAPI/Framework/UpdateHelper.cs +++ b/src/StardewModdingAPI/Framework/UpdateHelper.cs @@ -1,7 +1,6 @@ using System.IO; using System.Net; using System.Reflection; -using System.Threading.Tasks; using Newtonsoft.Json; using StardewModdingAPI.Framework.Models; @@ -15,17 +14,17 @@ namespace StardewModdingAPI.Framework *********/ /// Get the latest release from a GitHub repository. /// The name of the repository from which to fetch releases (like "cjsu/SMAPI"). - public static async Task GetLatestVersionAsync(string repository) + public static GitRelease GetLatestVersion(string repository) { // build request // (avoid HttpClient for Mac compatibility) - HttpWebRequest request = WebRequest.CreateHttp($"https://api.github.com/repos/{repository}/releases/latest"); + HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"https://api.github.com/repos/{repository}/releases/latest"); AssemblyName assembly = typeof(UpdateHelper).Assembly.GetName(); request.UserAgent = $"{assembly.Name}/{assembly.Version}"; request.Accept = "application/vnd.github.v3+json"; // fetch data - using (WebResponse response = await request.GetResponseAsync()) + using (WebResponse response = request.GetResponse()) using (Stream responseStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream)) { diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index db7a3df6..81e6518e 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -187,7 +187,7 @@ namespace StardewModdingAPI { try { - GitRelease release = UpdateHelper.GetLatestVersionAsync(Constants.GitHubRepository).Result; + GitRelease release = UpdateHelper.GetLatestVersion(Constants.GitHubRepository); ISemanticVersion latestVersion = new SemanticVersion(release.Tag); if (latestVersion.IsNewerThan(Constants.ApiVersion)) this.Monitor.Log($"You can update SMAPI from version {Constants.ApiVersion} to {latestVersion}", LogLevel.Alert); @@ -446,26 +446,18 @@ namespace StardewModdingAPI continue; } - // validate assembly + // initialise mod try { - if (modAssembly.DefinedTypes.Count(x => x.BaseType == typeof(Mod)) == 0) + // get mod entry type + Type modEntryType = modAssembly.GetExportedTypes().FirstOrDefault(x => x.BaseType == typeof(Mod)); + if(modEntryType == null) { - this.Monitor.Log($"{skippedPrefix} because its DLL has no 'Mod' subclass.", LogLevel.Error); + this.Monitor.Log($"{skippedPrefix} because its DLL has no {typeof(Mod).FullName} entry class.", LogLevel.Error); continue; } - } - catch (Exception ex) - { - this.Monitor.Log($"{skippedPrefix} because its DLL couldn't be loaded.\n{ex.GetLogSummary()}", LogLevel.Error); - continue; - } - - // initialise mod - try - { - // get implementation - TypeInfo modEntryType = modAssembly.DefinedTypes.First(x => x.BaseType == typeof(Mod)); + + // get mod class Mod mod = (Mod)modAssembly.CreateInstance(modEntryType.ToString()); if (mod == null) { diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index dceae74e..3a2bb756 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -9,7 +9,7 @@ Properties StardewModdingAPI StardewModdingAPI - v4.5 + v4.0 512 false @@ -60,7 +60,7 @@ $(SolutionDir)\..\bin\Debug\SMAPI $(SolutionDir)\..\bin\Debug\SMAPI\StardewModdingAPI.xml true - 6 + 7 x86 @@ -79,19 +79,22 @@ - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.dll + ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.dll True - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Mdb.dll + ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Mdb.dll True - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Pdb.dll + ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Pdb.dll True + + ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Rocks.dll + - ..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll + ..\packages\Newtonsoft.Json.8.0.3\lib\net40\Newtonsoft.Json.dll True @@ -116,6 +119,7 @@ Properties\GlobalAssemblyInfo.cs + @@ -215,12 +219,10 @@ Designer - - Designer - Always + Always diff --git a/src/StardewModdingAPI/packages.config b/src/StardewModdingAPI/packages.config index e5fa3c3a..1dee2c2a 100644 --- a/src/StardewModdingAPI/packages.config +++ b/src/StardewModdingAPI/packages.config @@ -1,5 +1,5 @@  - - + + \ No newline at end of file diff --git a/src/TrainerMod/TrainerMod.csproj b/src/TrainerMod/TrainerMod.csproj index a6303767..7845bd8c 100644 --- a/src/TrainerMod/TrainerMod.csproj +++ b/src/TrainerMod/TrainerMod.csproj @@ -9,7 +9,7 @@ Properties TrainerMod TrainerMod - v4.5 + v4.0 512 @@ -39,7 +39,7 @@ - ..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll + ..\packages\Newtonsoft.Json.8.0.3\lib\net40\Newtonsoft.Json.dll True diff --git a/src/TrainerMod/packages.config b/src/TrainerMod/packages.config index 75e68e71..2c6c3f12 100644 --- a/src/TrainerMod/packages.config +++ b/src/TrainerMod/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file -- cgit From 6f37e43a9b331f7c36f1062994e14cedf6f854c9 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 14 Mar 2017 15:18:57 -0400 Subject: use default C# version instead of specifying version --- src/StardewModdingAPI/StardewModdingAPI.csproj | 2 -- src/TrainerMod/TrainerMod.csproj | 2 -- 2 files changed, 4 deletions(-) (limited to 'src/TrainerMod') diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 3a2bb756..dcb299a2 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -60,7 +60,6 @@ $(SolutionDir)\..\bin\Debug\SMAPI $(SolutionDir)\..\bin\Debug\SMAPI\StardewModdingAPI.xml true - 7 x86 @@ -70,7 +69,6 @@ TRACE true true - 6 pdbonly true diff --git a/src/TrainerMod/TrainerMod.csproj b/src/TrainerMod/TrainerMod.csproj index 7845bd8c..c66f2ab8 100644 --- a/src/TrainerMod/TrainerMod.csproj +++ b/src/TrainerMod/TrainerMod.csproj @@ -22,7 +22,6 @@ 4 x86 false - 7 true @@ -33,7 +32,6 @@ prompt 4 false - 6 true x86 -- cgit From 06871a0603fa59ca6734fd913e118a4fe4a4c64c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 14 Mar 2017 16:17:02 -0400 Subject: avoid C# 7 until MonoDevelop supports it --- src/TrainerMod/TrainerMod.cs | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'src/TrainerMod') diff --git a/src/TrainerMod/TrainerMod.cs b/src/TrainerMod/TrainerMod.cs index d6990a14..7187a358 100644 --- a/src/TrainerMod/TrainerMod.cs +++ b/src/TrainerMod/TrainerMod.cs @@ -276,7 +276,8 @@ namespace TrainerMod case "player_setspeed": if (args.Any()) { - if (int.TryParse(args[0], out int addedSpeed)) + int addedSpeed; + if (int.TryParse(args[0], out addedSpeed)) { Game1.player.addedSpeed = addedSpeed; this.Monitor.Log($"OK, your added speed is now {Game1.player.addedSpeed}.", LogLevel.Info); @@ -296,7 +297,8 @@ namespace TrainerMod if (validTargets.Contains(target)) { string[] colorHexes = args[1].Split(new[] { ',' }, 3); - if (int.TryParse(colorHexes[0], out int r) && int.TryParse(colorHexes[1], out int g) && int.TryParse(colorHexes[2], out int b)) + int r, g, b; + if (int.TryParse(colorHexes[0], out r) && int.TryParse(colorHexes[1], out g) && int.TryParse(colorHexes[2], out b)) { Color color = new Color(r, g, b); switch (target) @@ -332,7 +334,8 @@ namespace TrainerMod string[] validTargets = { "hair", "shirt", "skin", "acc", "shoe", "swim", "gender" }; if (validTargets.Contains(target)) { - if (int.TryParse(args[1], out int styleID)) + int styleID; + if (int.TryParse(args[1], out styleID)) { switch (target) { @@ -403,7 +406,8 @@ namespace TrainerMod case "world_freezetime": if (args.Any()) { - if (int.TryParse(args[0], out int value)) + int value; + if (int.TryParse(args[0], out value)) { if (value == 0 || value == 1) { @@ -428,7 +432,8 @@ namespace TrainerMod case "world_settime": if (args.Any()) { - if (int.TryParse(args[0], out int time)) + int time; + if (int.TryParse(args[0], out time)) { if (time <= 2600 && time >= 600) { @@ -449,7 +454,8 @@ namespace TrainerMod case "world_setday": if (args.Any()) { - if (int.TryParse(args[0], out int day)) + int day; + if (int.TryParse(args[0], out day)) { if (day <= 28 && day > 0) { @@ -496,7 +502,8 @@ namespace TrainerMod else { this.InfiniteHealth = false; - if (int.TryParse(amountStr, out int amount)) + int amount; + if (int.TryParse(amountStr, out amount)) { Game1.player.health = amount; this.Monitor.Log($"OK, you now have {Game1.player.health} health.", LogLevel.Info); @@ -512,7 +519,8 @@ namespace TrainerMod case "player_setmaxhealth": if (args.Any()) { - if (int.TryParse(args[0], out int maxHealth)) + int maxHealth; + if (int.TryParse(args[0], out maxHealth)) { Game1.player.maxHealth = maxHealth; this.Monitor.Log($"OK, you now have {Game1.player.maxHealth} max health.", LogLevel.Info); @@ -527,7 +535,8 @@ namespace TrainerMod case "player_setimmunity": if (args.Any()) { - if (int.TryParse(args[0], out int amount)) + int amount; + if (int.TryParse(args[0], out amount)) { Game1.player.immunity = amount; this.Monitor.Log($"OK, you now have {Game1.player.immunity} immunity.", LogLevel.Info); @@ -542,7 +551,8 @@ namespace TrainerMod case "player_additem": if (args.Any()) { - if (int.TryParse(args[0], out int itemID)) + int itemID; + if (int.TryParse(args[0], out itemID)) { int count = 1; int quality = 0; @@ -580,7 +590,8 @@ namespace TrainerMod case "player_addmelee": if (args.Any()) { - if (int.TryParse(args[0], out int weaponID)) + int weaponID; + if (int.TryParse(args[0], out weaponID)) { MeleeWeapon weapon = new MeleeWeapon(weaponID); if (weapon.Name == null) @@ -601,7 +612,8 @@ namespace TrainerMod case "player_addring": if (args.Any()) { - if (int.TryParse(args[0], out int ringID)) + int ringID; + if (int.TryParse(args[0], out ringID)) { if (ringID < Ring.ringLowerIndexRange || ringID > Ring.ringUpperIndexRange) this.Monitor.Log($"There is no such ring ID (must be between {Ring.ringLowerIndexRange} and {Ring.ringUpperIndexRange}).", LogLevel.Error); @@ -643,7 +655,8 @@ namespace TrainerMod case "world_setminelevel": if (args.Any()) { - if (int.TryParse(args[0], out int level)) + int level; + if (int.TryParse(args[0], out level)) { level = Math.Max(1, level); this.Monitor.Log($"OK, warping you to mine level {level}.", LogLevel.Info); -- cgit From 307304a03edb82f3a1f5cfa6c47cb17687560ccb Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 14 Mar 2017 18:16:44 -0400 Subject: revert all projects except installer to .NET Framework 4.5 This caused obscure invalid-IL crashes when compiled through MonoDevelop on Linux. --- release-notes.md | 1 - .../PlatformAssemblyMap.cs | 2 +- .../StardewModdingAPI.AssemblyRewriters.csproj | 14 +++++------ .../packages.config | 2 +- src/StardewModdingAPI/App.config | 2 +- .../Events/ContentEventHandler.cs | 8 ------ src/StardewModdingAPI/Events/ContentEvents.cs | 2 +- .../Framework/InternalExtensions.cs | 29 ++-------------------- .../Framework/Reflection/PrivateProperty.cs | 4 +-- src/StardewModdingAPI/Framework/UpdateHelper.cs | 7 +++--- src/StardewModdingAPI/Program.cs | 24 ++++++++++++------ src/StardewModdingAPI/StardewModdingAPI.csproj | 20 +++++++-------- src/StardewModdingAPI/packages.config | 4 +-- src/TrainerMod/TrainerMod.csproj | 4 +-- src/TrainerMod/packages.config | 2 +- 15 files changed, 49 insertions(+), 76 deletions(-) delete mode 100644 src/StardewModdingAPI/Events/ContentEventHandler.cs (limited to 'src/TrainerMod') diff --git a/release-notes.md b/release-notes.md index 01d49dd7..f91ef733 100644 --- a/release-notes.md +++ b/release-notes.md @@ -20,7 +20,6 @@ For players: * Simplified error messages when a mod can't be loaded. * Simple nested mod folders are now recognised by SMAPI (e.g. `ModName-1.0\ModName\manifest.json`). * Improved TrainerMod command handling & feedback. -* Reduced minimum .NET Framework version to 4.0 for improved compatibility on Windows 7–8.1. * Fixed game's debug output being shown in the console for all users. * Fixed the game-outdated error not pausing before exit. * Fixed installer errors for some players when deleting files. diff --git a/src/StardewModdingAPI.AssemblyRewriters/PlatformAssemblyMap.cs b/src/StardewModdingAPI.AssemblyRewriters/PlatformAssemblyMap.cs index 3ca90149..fce2b187 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/PlatformAssemblyMap.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/PlatformAssemblyMap.cs @@ -49,7 +49,7 @@ namespace StardewModdingAPI.AssemblyRewriters // cache assembly metadata this.Targets = targetAssemblies; this.TargetReferences = this.Targets.ToDictionary(assembly => assembly, assembly => AssemblyNameReference.Parse(assembly.FullName)); - this.TargetModules = this.Targets.ToDictionary(assembly => assembly, assembly => ModuleDefinition.ReadModule(assembly.GetModules().Single().FullyQualifiedName)); + this.TargetModules = this.Targets.ToDictionary(assembly => assembly, assembly => ModuleDefinition.ReadModule(assembly.Modules.Single().FullyQualifiedName)); } } } diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj index 15bb15ac..a3322e67 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj @@ -9,7 +9,7 @@ Properties StardewModdingAPI.AssemblyRewriters StardewModdingAPI.AssemblyRewriters - v4.0 + v4.5 512 @@ -49,16 +49,16 @@ - ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.dll + ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.dll + True - ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Mdb.dll + ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Mdb.dll + True - ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Pdb.dll - - - ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Rocks.dll + ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Pdb.dll + True diff --git a/src/StardewModdingAPI.AssemblyRewriters/packages.config b/src/StardewModdingAPI.AssemblyRewriters/packages.config index 70ba1aed..88fbc79d 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/packages.config +++ b/src/StardewModdingAPI.AssemblyRewriters/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/src/StardewModdingAPI/App.config b/src/StardewModdingAPI/App.config index 314845f7..27cdf0f7 100644 --- a/src/StardewModdingAPI/App.config +++ b/src/StardewModdingAPI/App.config @@ -1,7 +1,7 @@ - + diff --git a/src/StardewModdingAPI/Events/ContentEventHandler.cs b/src/StardewModdingAPI/Events/ContentEventHandler.cs deleted file mode 100644 index 2a7e75d1..00000000 --- a/src/StardewModdingAPI/Events/ContentEventHandler.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace StardewModdingAPI.Events -{ - /// Represents a method that will handle a content event. - /// The source of the event. - /// The event arguments. - /// This deviates from in allowing T to be an interface instead of a concrete class. While .NET Framework 4.5 allows that, the current .NET Framework 4.0 targeted by SMAPI to improve compatibility does not. - public delegate void ContentEventHandler(object sender, IContentEventHelper e); -} diff --git a/src/StardewModdingAPI/Events/ContentEvents.cs b/src/StardewModdingAPI/Events/ContentEvents.cs index 339e90fd..9418673a 100644 --- a/src/StardewModdingAPI/Events/ContentEvents.cs +++ b/src/StardewModdingAPI/Events/ContentEvents.cs @@ -28,7 +28,7 @@ namespace StardewModdingAPI.Events public static event EventHandler> AfterLocaleChanged; /// Raised when an XNB file is being read into the cache. Mods can change the data here before it's cached. - internal static event ContentEventHandler AfterAssetLoaded; + internal static event EventHandler AfterAssetLoaded; /********* diff --git a/src/StardewModdingAPI/Framework/InternalExtensions.cs b/src/StardewModdingAPI/Framework/InternalExtensions.cs index 46c76656..4ca79518 100644 --- a/src/StardewModdingAPI/Framework/InternalExtensions.cs +++ b/src/StardewModdingAPI/Framework/InternalExtensions.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using StardewModdingAPI.Events; namespace StardewModdingAPI.Framework { @@ -60,36 +59,12 @@ namespace StardewModdingAPI.Framework /// The event handlers. /// The event sender. /// The event arguments. - public static void SafelyRaiseGenericEvent(this IMonitor monitor, string name, IEnumerable handlers, object sender, TEventArgs args) where TEventArgs : EventArgs + public static void SafelyRaiseGenericEvent(this IMonitor monitor, string name, IEnumerable handlers, object sender, TEventArgs args) { if (handlers == null) return; - foreach (EventHandler handler in handlers.Cast>()) - { - try - { - handler.Invoke(sender, args); - } - catch (Exception ex) - { - monitor.Log($"A mod failed handling the {name} event:\n{ex.GetLogSummary()}", LogLevel.Error); - } - } - } - - /// Safely raise an event, and intercept any exceptions thrown by its handlers. - /// Encapsulates monitoring and logging. - /// The event name for error messages. - /// The event handlers. - /// The event sender. - /// The event arguments. - public static void SafelyRaiseGenericEvent(this IMonitor monitor, string name, IEnumerable handlers, object sender, IContentEventHelper args) - { - if (handlers == null) - return; - - foreach (ContentEventHandler handler in handlers.Cast()) + foreach (EventHandler handler in Enumerable.Cast>(handlers)) { try { diff --git a/src/StardewModdingAPI/Framework/Reflection/PrivateProperty.cs b/src/StardewModdingAPI/Framework/Reflection/PrivateProperty.cs index d89e8e44..08204b7e 100644 --- a/src/StardewModdingAPI/Framework/Reflection/PrivateProperty.cs +++ b/src/StardewModdingAPI/Framework/Reflection/PrivateProperty.cs @@ -60,7 +60,7 @@ namespace StardewModdingAPI.Framework.Reflection { try { - return (TValue)this.PropertyInfo.GetValue(this.Parent, null); + return (TValue)this.PropertyInfo.GetValue(this.Parent); } catch (InvalidCastException) { @@ -78,7 +78,7 @@ namespace StardewModdingAPI.Framework.Reflection { try { - this.PropertyInfo.SetValue(this.Parent, value, null); + this.PropertyInfo.SetValue(this.Parent, value); } catch (InvalidCastException) { diff --git a/src/StardewModdingAPI/Framework/UpdateHelper.cs b/src/StardewModdingAPI/Framework/UpdateHelper.cs index 342a08cf..e01e55c8 100644 --- a/src/StardewModdingAPI/Framework/UpdateHelper.cs +++ b/src/StardewModdingAPI/Framework/UpdateHelper.cs @@ -1,6 +1,7 @@ using System.IO; using System.Net; using System.Reflection; +using System.Threading.Tasks; using Newtonsoft.Json; using StardewModdingAPI.Framework.Models; @@ -14,17 +15,17 @@ namespace StardewModdingAPI.Framework *********/ /// Get the latest release from a GitHub repository. /// The name of the repository from which to fetch releases (like "cjsu/SMAPI"). - public static GitRelease GetLatestVersion(string repository) + public static async Task GetLatestVersionAsync(string repository) { // build request // (avoid HttpClient for Mac compatibility) - HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"https://api.github.com/repos/{repository}/releases/latest"); + HttpWebRequest request = WebRequest.CreateHttp($"https://api.github.com/repos/{repository}/releases/latest"); AssemblyName assembly = typeof(UpdateHelper).Assembly.GetName(); request.UserAgent = $"{assembly.Name}/{assembly.Version}"; request.Accept = "application/vnd.github.v3+json"; // fetch data - using (WebResponse response = request.GetResponse()) + using (WebResponse response = await request.GetResponseAsync()) using (Stream responseStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream)) { diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 81e6518e..db7a3df6 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -187,7 +187,7 @@ namespace StardewModdingAPI { try { - GitRelease release = UpdateHelper.GetLatestVersion(Constants.GitHubRepository); + GitRelease release = UpdateHelper.GetLatestVersionAsync(Constants.GitHubRepository).Result; ISemanticVersion latestVersion = new SemanticVersion(release.Tag); if (latestVersion.IsNewerThan(Constants.ApiVersion)) this.Monitor.Log($"You can update SMAPI from version {Constants.ApiVersion} to {latestVersion}", LogLevel.Alert); @@ -446,18 +446,26 @@ namespace StardewModdingAPI continue; } - // initialise mod + // validate assembly try { - // get mod entry type - Type modEntryType = modAssembly.GetExportedTypes().FirstOrDefault(x => x.BaseType == typeof(Mod)); - if(modEntryType == null) + if (modAssembly.DefinedTypes.Count(x => x.BaseType == typeof(Mod)) == 0) { - this.Monitor.Log($"{skippedPrefix} because its DLL has no {typeof(Mod).FullName} entry class.", LogLevel.Error); + this.Monitor.Log($"{skippedPrefix} because its DLL has no 'Mod' subclass.", LogLevel.Error); continue; } - - // get mod class + } + catch (Exception ex) + { + this.Monitor.Log($"{skippedPrefix} because its DLL couldn't be loaded.\n{ex.GetLogSummary()}", LogLevel.Error); + continue; + } + + // initialise mod + try + { + // get implementation + TypeInfo modEntryType = modAssembly.DefinedTypes.First(x => x.BaseType == typeof(Mod)); Mod mod = (Mod)modAssembly.CreateInstance(modEntryType.ToString()); if (mod == null) { diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index dcb299a2..99666f08 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -9,7 +9,7 @@ Properties StardewModdingAPI StardewModdingAPI - v4.0 + v4.5 512 false @@ -77,22 +77,19 @@ - ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.dll + ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.dll True - ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Mdb.dll + ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Mdb.dll True - ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Pdb.dll + ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Pdb.dll True - - ..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Rocks.dll - - ..\packages\Newtonsoft.Json.8.0.3\lib\net40\Newtonsoft.Json.dll + ..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll True @@ -117,7 +114,6 @@ Properties\GlobalAssemblyInfo.cs - @@ -217,10 +213,12 @@ Designer + + Designer + Always - Always @@ -282,4 +280,4 @@ - \ No newline at end of file + diff --git a/src/StardewModdingAPI/packages.config b/src/StardewModdingAPI/packages.config index 1dee2c2a..e5fa3c3a 100644 --- a/src/StardewModdingAPI/packages.config +++ b/src/StardewModdingAPI/packages.config @@ -1,5 +1,5 @@  - - + + \ No newline at end of file diff --git a/src/TrainerMod/TrainerMod.csproj b/src/TrainerMod/TrainerMod.csproj index c66f2ab8..0bd667d4 100644 --- a/src/TrainerMod/TrainerMod.csproj +++ b/src/TrainerMod/TrainerMod.csproj @@ -9,7 +9,7 @@ Properties TrainerMod TrainerMod - v4.0 + v4.5 512 @@ -37,7 +37,7 @@ - ..\packages\Newtonsoft.Json.8.0.3\lib\net40\Newtonsoft.Json.dll + ..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll True diff --git a/src/TrainerMod/packages.config b/src/TrainerMod/packages.config index 2c6c3f12..75e68e71 100644 --- a/src/TrainerMod/packages.config +++ b/src/TrainerMod/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file -- cgit