diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-03-14 13:44:41 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-03-14 13:44:41 -0400 |
commit | 02a4c40814caf26f21fc9afd9c64afd639da462f (patch) | |
tree | b3ac580e218650eeb72aa154824dd009d22dd3a4 /src/TrainerMod | |
parent | 8bd265fed60d32a6fe64f01b8528738274aa0083 (diff) | |
download | SMAPI-02a4c40814caf26f21fc9afd9c64afd639da462f.tar.gz SMAPI-02a4c40814caf26f21fc9afd9c64afd639da462f.tar.bz2 SMAPI-02a4c40814caf26f21fc9afd9c64afd639da462f.zip |
simplify TrainerMod int parsing with C# 7 out variables
Diffstat (limited to 'src/TrainerMod')
-rw-r--r-- | src/TrainerMod/Framework/Extensions.cs | 25 | ||||
-rw-r--r-- | src/TrainerMod/TrainerMod.cs | 73 | ||||
-rw-r--r-- | src/TrainerMod/TrainerMod.csproj | 3 |
3 files changed, 26 insertions, 75 deletions
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 -{ - /// <summary>Provides extension methods on primitive types.</summary> - internal static class Extensions - { - /********* - ** Public methods - *********/ - /// <summary>Get whether an object is a number.</summary> - /// <param name="value">The object value.</param> - public static bool IsInt(this object value) - { - int i; - return int.TryParse(value.ToString(), out i); - } - - /// <summary>Parse an object into a number.</summary> - /// <param name="value">The object value.</param> - /// <exception cref="System.FormatException">The value is not a valid number.</exception> - 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 @@ <WarningLevel>4</WarningLevel> <PlatformTarget>x86</PlatformTarget> <Prefer32Bit>false</Prefer32Bit> - <LangVersion>6</LangVersion> + <LangVersion>7</LangVersion> <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> @@ -53,7 +53,6 @@ <Compile Include="..\GlobalAssemblyInfo.cs"> <Link>Properties\GlobalAssemblyInfo.cs</Link> </Compile> - <Compile Include="Framework\Extensions.cs" /> <Compile Include="ItemData\ISearchItem.cs" /> <Compile Include="ItemData\ItemType.cs" /> <Compile Include="ItemData\SearchableObject.cs" /> |