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/Framework/Commands/Other') 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/Framework/Commands/Other') 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