summaryrefslogtreecommitdiff
path: root/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs
blob: 81a8c570290b51c5981f64fd90460e0cd8ce07b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#nullable disable

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Netcode;
using StardewValley;
using StardewValley.Network;

namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other
{
    /// <summary>A command which regenerates the game's bundles.</summary>
    [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Loaded using reflection")]
    internal class RegenerateBundlesCommand : ConsoleCommand
    {
        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        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 [<type>] [ignore_seed]\nRegenerate all bundles for this save. If the <type> 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") { }

        /// <summary>Handle the command.</summary>
        /// <param name="monitor">Writes messages to the console and log file.</param>
        /// <param name="command">The command name.</param>
        /// <param name="args">The command arguments.</param>
        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;
            }

            // get private fields
            IWorldState state = Game1.netWorldState.Value;
            var bundleData = state.GetType().GetField("_bundleData", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)?.GetValue(state) as IDictionary<string, string>
                ?? 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<string, NetString>
                ?? 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
            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);
        }
    }
}