From bb88e42f54c274db3c382eecb54b541dd2599163 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 16 Mar 2021 19:20:37 -0400 Subject: add console command to regenerate bundles --- .../Framework/Commands/Other/RegenerateBundles.cs | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs (limited to 'src/SMAPI.Mods.ConsoleCommands') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs new file mode 100644 index 00000000..d28abd13 --- /dev/null +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs @@ -0,0 +1,66 @@ +using System; +using System.Linq; +using StardewValley; + +namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other +{ + /// A command which regenerates the game's bundles. + internal class RegenerateBundlesCommand : ConsoleCommand + { + /********* + ** Public methods + *********/ + /// Construct an instance. + public RegenerateBundlesCommand() + : base("regenerate_bundles", $"Regenerate the game's community center bundle data. WARNING: this will reset all bundle progress, and may have unintended effects if you've already completed bundles. DO NOT USE THIS unless you're absolutely sure.\n\nUsage: regenerate_bundles confirm [] [ignore_seed]\nRegenerate all bundles for this save. If the is set to '{string.Join("' or '", Enum.GetNames(typeof(Game1.BundleType)))}', change the bundle type for the save. If an 'ignore_seed' option is included, remixed bundles are re-randomized without using the predetermined save seed.\n\nExample: regenerate_bundles remixed confirm") { } + + /// Handle the command. + /// Writes messages to the console and log file. + /// The command name. + /// The command arguments. + public override void Handle(IMonitor monitor, string command, ArgumentParser args) + { + // get flags + var bundleType = Game1.bundleType; + bool confirmed = false; + bool useSeed = true; + foreach (string arg in args) + { + if (arg.Equals("confirm", StringComparison.OrdinalIgnoreCase)) + confirmed = true; + else if (arg.Equals("ignore_seed", StringComparison.OrdinalIgnoreCase)) + useSeed = false; + else if (Enum.TryParse(arg, ignoreCase: true, out Game1.BundleType type)) + bundleType = type; + else + { + monitor.Log($"Invalid option '{arg}'. Type 'help {command}' for usage.", LogLevel.Error); + return; + } + } + + // require confirmation + if (!confirmed) + { + monitor.Log($"WARNING: this may have unintended consequences (type 'help {command}' for details). Are you sure?", LogLevel.Warn); + + string[] newArgs = args.Concat(new[] { "confirm" }).ToArray(); + monitor.Log($"To confirm, enter this command: '{command} {string.Join(" ", newArgs)}'.", LogLevel.Info); + return; + } + + // need a loaded save + if (!Context.IsWorldReady) + { + monitor.Log("You need to load a save to use this command.", LogLevel.Error); + return; + } + + // regenerate bundles + Game1.bundleType = bundleType; + Game1.GenerateBundles(bundleType, use_seed: useSeed); + monitor.Log("Regenerated bundles and reset bundle progress.", LogLevel.Info); + monitor.Log("This may have unintended effects if you've already completed any bundles. If you're not sure, exit your game without saving to cancel.", LogLevel.Warn); + } + } +} -- cgit From c070e34c2f18a18d03dbe9f86f5d17491215b6e2 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 21 Mar 2021 00:44:42 -0400 Subject: fix double-localization issue in regenerated bundles --- .../Framework/Commands/Other/RegenerateBundles.cs | 33 ++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands') diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs index d28abd13..9beedb96 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs @@ -1,6 +1,10 @@ using System; +using System.Collections.Generic; using System.Linq; +using System.Reflection; +using Netcode; using StardewValley; +using StardewValley.Network; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other { @@ -56,9 +60,34 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other return; } + // get private fields + IWorldState state = Game1.netWorldState.Value; + var bundleData = state.GetType().GetField("_bundleData", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)?.GetValue(state) as IDictionary + ?? throw new InvalidOperationException("Can't access '_bundleData' field on world state."); + var netBundleData = state.GetType().GetField("netBundleData", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)?.GetValue(state) as NetStringDictionary + ?? throw new InvalidOperationException("Can't access 'netBundleData' field on world state."); + + // clear bundle data + state.BundleData.Clear(); + state.Bundles.Clear(); + state.BundleRewards.Clear(); + bundleData.Clear(); + netBundleData.Clear(); + // regenerate bundles - Game1.bundleType = bundleType; - Game1.GenerateBundles(bundleType, use_seed: useSeed); + var locale = LocalizedContentManager.CurrentLanguageCode; + try + { + LocalizedContentManager.CurrentLanguageCode = LocalizedContentManager.LanguageCode.en; // the base bundle data needs to be unlocalized (the game will add localized names later) + + Game1.bundleType = bundleType; + Game1.GenerateBundles(bundleType, use_seed: useSeed); + } + finally + { + LocalizedContentManager.CurrentLanguageCode = locale; + } + monitor.Log("Regenerated bundles and reset bundle progress.", LogLevel.Info); monitor.Log("This may have unintended effects if you've already completed any bundles. If you're not sure, exit your game without saving to cancel.", LogLevel.Warn); } -- cgit From 73321eceb96f263f10857667d7b3726a5098e770 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 17 Mar 2021 20:36:31 -0400 Subject: split compile flag into separate Windows + XNA flags (#767) --- .../SMAPI.Mods.ConsoleCommands.csproj | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands') diff --git a/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj b/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj index 1e3208de..2f5adc8a 100644 --- a/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj +++ b/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj @@ -7,6 +7,8 @@ x86 + + @@ -16,19 +18,21 @@ + + + + + + - - + - - - @@ -41,5 +45,4 @@ - -- cgit From b93e3cf522ebcc8711a8662a17cca1d034752269 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 17 Mar 2021 20:36:32 -0400 Subject: Drop hardcoded 32-bit restrictions on most SMAPI assemblies (#767) SMAPI itself needs to be 32-bit to avoid errors trying to load the 32-bit game. --- src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj | 1 - 1 file changed, 1 deletion(-) (limited to 'src/SMAPI.Mods.ConsoleCommands') diff --git a/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj b/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj index 2f5adc8a..a187c1ff 100644 --- a/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj +++ b/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj @@ -4,7 +4,6 @@ StardewModdingAPI.Mods.ConsoleCommands net45 false - x86 -- cgit From 74215e844ae2af0075e5df3ab6a5f58efff4f981 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 21 Mar 2021 16:37:43 -0400 Subject: prepare for release --- src/SMAPI.Mods.ConsoleCommands/manifest.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/SMAPI.Mods.ConsoleCommands') diff --git a/src/SMAPI.Mods.ConsoleCommands/manifest.json b/src/SMAPI.Mods.ConsoleCommands/manifest.json index 2811a11c..65c66d33 100644 --- a/src/SMAPI.Mods.ConsoleCommands/manifest.json +++ b/src/SMAPI.Mods.ConsoleCommands/manifest.json @@ -1,9 +1,9 @@ { "Name": "Console Commands", "Author": "SMAPI", - "Version": "3.9.4", + "Version": "3.9.5", "Description": "Adds SMAPI console commands that let you manipulate the game.", "UniqueID": "SMAPI.ConsoleCommands", "EntryDll": "ConsoleCommands.dll", - "MinimumApiVersion": "3.9.4" + "MinimumApiVersion": "3.9.5" } -- cgit