-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathModEntry.cs
More file actions
196 lines (163 loc) · 9.53 KB
/
ModEntry.cs
File metadata and controls
196 lines (163 loc) · 9.53 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Reflection;
using System.Globalization;
using System.Collections.Generic;
using StardewValley;
using StardewValley.Objects;
using StardewValley.Menus;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using HarmonyLib;
namespace ExpandedFridgeAGAIN
{
//* The entry point of the mod handled by SMAPI.
public class ModEntry : Mod
{
private static ModEntry _instance = null;
public ModConfig Config { get; private set; }
public FridgeManager FridgeManager { get; private set; }
private void DebugRestoreAllFridges (string command, string[] arg){
FridgeManager.MoveAllMiniFridges(false);
}
private void DebugHideAllFridges (string command, string[] arg){
FridgeManager.MoveAllMiniFridges(true);
}
//* Setup instance and load our first event-hooks
public override void Entry(IModHelper helper){
//* Setup DebugLog
_instance = this;
//* Load Config
Config = Helper.ReadConfig<ModConfig>();
DebugLog(Helper.Translation.Get("Debug.ConfigurationLoaded"));
//* Show debug info etc.
var buildTime = GetBuildDate(Assembly.GetExecutingAssembly());
buildTime = buildTime.ToLocalTime();
DebugLog("ExpandedFridgeAGAIN v" + GetType().Assembly.GetName().Version.ToString(3) +" (" + Constants.TargetPlatform + ") loaded.", LogLevel.Info);
if (Config.ShowDebugMessages){
DebugLog("Binary Compiled: " + buildTime.ToString("d/M/yyyy h:mm tt"), LogLevel.Info);
}
//* Prepare Event Hooks
Helper.Events.GameLoop.GameLaunched += onLaunched;
}
//* Setup for GenericModConfigMenu support.
private void onLaunched(object sender, GameLaunchedEventArgs e){
//* Hook into GMCM
var api = Helper.ModRegistry.GetApi<GenericModConfigMenuAPI>("spacechase0.GenericModConfigMenu");
if (api != null){
//* Register with GMCM
api.Register(ModManifest, () => Config = new ModConfig(), () => Helper.WriteConfig(Config));
//* Our Key-binds
api.AddKeybind(ModManifest, () => Config.NextFridgeTabButton, (SButton val) => Config.NextFridgeTabButton = val, () => Helper.Translation.Get("Config.NextFridgeTabButton"), () => Helper.Translation.Get("Config.NextFridgeTabButtonDesc"),"NextFridgeTabButton");
api.AddKeybind(ModManifest, () => Config.LastFridgeTabButton, (SButton val) => Config.LastFridgeTabButton = val, () => Helper.Translation.Get("Config.LastFridgeTabButton"), () => Helper.Translation.Get("Config.LastFridgeTabButtonDesc"),"LastFridgeTabButton");
//* Our Options
api.AddBoolOption(ModManifest,() => Config.ShowDebugMessages, (bool val) => Config.ShowDebugMessages = val, ()=> Helper.Translation.Get("Config.ShowDebugMessages") + ".", () => Helper.Translation.Get("Config.ShowDebugMessagesDesc"),"ShowDebugMessages");
api.AddBoolOption(ModManifest,() => Config.HideMiniFridges, (bool val) => Config.HideMiniFridges = val, () => Helper.Translation.Get("Config.HideMiniFridges") + ".", () => Helper.Translation.Get("Config.HideMiniFridgesDesc"),"HideMiniFridges");
//* Detect changes mid-game.
api.OnFieldChanged(ModManifest,onFieldChanged);
DebugLog(Helper.Translation.Get("Debug.GenericModConfigMenuFound"));
}else{ //* Could not find GenericModConfigMenu ??
DebugLog(Helper.Translation.Get("Debug.GenericModConfigMenuMissing"));
}
//* Check for incompatible mods.
//TODO: Check for more incompatible mods. Check BetterChests again?
bool BetterChestsLoaded = Helper.ModRegistry.IsLoaded("furyx639.BetterChests");
if (BetterChestsLoaded){
DebugLog(Helper.Translation.Get("Debug.BetterChestsDetected"), LogLevel.Warn);
}
bool ExpandedFridgeLoaded = Helper.ModRegistry.IsLoaded("Uwazouri.ExpandedFridge");
if (ExpandedFridgeLoaded){
DebugLog(Helper.Translation.Get("Debug.ExpandedFridgeDetected"), LogLevel.Warn);
}
//* Print options to the log
if (Config.ShowDebugMessages){
DebugLog(Helper.Translation.Get("Debug.OptionEnabledRuntime", new { option = Helper.Translation.Get("Config.ShowDebugMessages") }), LogLevel.Info);
if (Config.HideMiniFridges){
DebugLog(Helper.Translation.Get("Debug.OptionEnabledRuntime", new { option = Helper.Translation.Get("Config.HideMiniFridges") }), LogLevel.Info);
}else{
DebugLog(Helper.Translation.Get("Debug.OptionDisabledRuntime", new { option = Helper.Translation.Get("Config.HideMiniFridges") }), LogLevel.Info);
}
DebugLog(Helper.Translation.Get("Debug.KeySetTo", new { option = "NextFridgeTabButton" }) + Config.NextFridgeTabButton.ToString() + ".", LogLevel.Info);
DebugLog(Helper.Translation.Get("Debug.KeySetTo", new { option = "LastFridgeTabButton" }) + Config.LastFridgeTabButton.ToString() + ".", LogLevel.Info);
}
//*Setup Harmony
var harmony = new Harmony(this.ModManifest.UniqueID);
try{
DebugLog("Setting up Harmony Patch..");
_ = harmony.Patch(
AccessTools.Method(typeof(ItemGrabMenu), nameof(ItemGrabMenu.CanHaveColorPicker)),
postfix: new HarmonyMethod(typeof(ModEntry), nameof(ItemGrabMenu_CanHaveColorPicker_postfix)));
}
catch (Exception){
DebugLog("Failed to apply Harmony patch: ItemGrabMenu.CanHaveColorPicker()", LogLevel.Error);
}
//* Start FridgeManager once we are all setup & before the start of the first day.
FridgeManager = new FridgeManager(_instance);
//* Debug Commands
Helper.ConsoleCommands.Add("FridgeExpanded_ShowFridges", "Moves all mini-fridges back into the cabins.", this.DebugRestoreAllFridges);
Helper.ConsoleCommands.Add("FridgeExpanded_HideFridges", "Moves all mini-fridges out of view.", this.DebugHideAllFridges);
}
//* The method invoked when we detect configuration changes.
private void onFieldChanged(string str, object obj){
if (str == "ShowDebugMessages"){
if((bool)obj){
DebugLog(Helper.Translation.Get("Debug.OptionEnabledRuntime", new { option = str }));
}else{
DebugLog(Helper.Translation.Get("Debug.OptionDisabledRuntime", new { option = str }));
}
}
else if(str == "HideMiniFridges"){
if((bool)obj){
DebugLog(Helper.Translation.Get("Debug.OptionEnabledRuntime", new { option = str }));
//* Hide the mini-fridges straight away.
string[] arrTeamMembers = new string[] { "" };
DebugHideAllFridges("",arrTeamMembers);
}else{
DebugLog(Helper.Translation.Get("Debug.OptionDisabledRuntime", new { option = str }));
//* Restore the mini-fridges straight away.
string[] arrTeamMembers = new string[] { "" };
DebugRestoreAllFridges("",arrTeamMembers);
}
}
else if(str == "NextFridgeTabButton"){
DebugLog(Helper.Translation.Get("Debug.KeySetTo", new { option = str }) + obj.ToString() + ".");
}
else if(str == "LastFridgeTabButton"){
DebugLog(Helper.Translation.Get("Debug.KeySetTo", new { option = str }) + obj.ToString() + ".");
}
}
public DateTime GetBuildDate(Assembly assembly){
const string BuildVersionMetadataPrefix = "+build";
var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
if (attribute?.InformationalVersion != null){
var value = attribute.InformationalVersion;
var index = value.IndexOf(BuildVersionMetadataPrefix);
if (index > 0){
value = value.Substring(index + BuildVersionMetadataPrefix.Length);
if (DateTime.TryParseExact(value, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out var result)){
return result;
}
}
}
return default;
}
private static void ItemGrabMenu_CanHaveColorPicker_postfix(ItemGrabMenu __instance, ref bool __result){
// Taken from ColorfulChests - https://github.com/LeFauxMatt/ColorfulChests
if (!__result &&
__instance.sourceItem is Chest { playerChest.Value: true } chest &&
Game1.bigCraftableData.TryGetValue(chest.ItemId, out var bigCraftableData))
{
__result = true;
}
}
//* Prints message in console log with given log level if enabled.
public static void DebugLog(string message, LogLevel level = LogLevel.Debug){
if(level != LogLevel.Debug){
_instance.Monitor.Log(message, level);
}else{
if (_instance.Config.ShowDebugMessages){
_instance.Monitor.Log(message, level);
}
}
}
}
}