-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathModEntry.cs
184 lines (158 loc) · 8.2 KB
/
ModEntry.cs
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
/*
* This software is licensed under the MIT License
* https://github.com/GStefanowich/SDV-Forecaster
*
* Copyright (c) 2019 Gregory Stefanowich
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ForecasterText.Objects;
using GenericModConfigMenu;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
namespace ForecasterText {
public sealed class ModEntry : Mod {
internal readonly TVEvents Events;
internal readonly ForecasterConfigManager ConfigManager;
private readonly VirtualTV Television = new();
public string ModID => this.ModManifest.UniqueID;
public ModEntry() {
this.ConfigManager = new ForecasterConfigManager(this);
this.Events = new TVEvents(
this,
this.ConfigManager
);
Console.WriteLine("Testing");
}
/// <summary>
/// Mod Initializer
/// </summary>
public override void Entry(IModHelper helper) {
this.Helper.Events.GameLoop.DayStarted += this.Events.OnDayStart;
this.Helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
this.Helper.Events.Multiplayer.PeerConnected += this.OnPeerJoin;
// Reload example content when the language is changed
this.Helper.Events.Content.LocaleChanged += this.ConfigManager.ReRender;
}
/// <summary>
/// Register with the Config Mod when the game is launched
/// </summary>
private void OnGameLaunched(object? sender, GameLaunchedEventArgs args) {
if (this.Helper.ModRegistry.GetApi<IGenericModConfigMenuApi>("spacechase0.GenericModConfigMenu") is IGenericModConfigMenuApi configMenu)
this.ConfigManager.RegisterConfigManager(configMenu);
}
/// <summary>
/// When a player joins the game, make sure they know the details of the day
/// </summary>
private void OnPeerJoin(object? sender, PeerConnectedEventArgs peerConnectedEventArgs) {
Farmer? farmer = Game1.getOnlineFarmers()
.FirstOrDefault(farmer => farmer.UniqueMultiplayerID == peerConnectedEventArgs.Peer.PlayerID && !this.PlayerHasMod(farmer));
if (farmer is not null)
this.Events.OnFarmerJoin(sender, farmer);
}
/// <summary>Check if another farmer has the ForecasterTexts mod</summary>
public bool PlayerHasMod(Farmer farmer)
=> this.PlayerHasMod(farmer, this.ModID);
/// <summary>Check if another farmer has a specific mod</summary>
public bool PlayerHasMod(Farmer farmer, string modId) {
if (Game1.player == farmer)
return this.Helper.ModRegistry.Get(modId) is not null;
return this.Helper.Multiplayer.GetConnectedPlayer(farmer.UniqueMultiplayerID) is { HasSmapi: true } peer
&& peer.GetMod(modId) is not null;
}
public IRecipeFinder GetRecipeFinder(Farmer farmer)
=> new RecipeFinder(this, farmer);
private sealed class RecipeFinder : IRecipeFinder {
private readonly ModEntry Mod;
private readonly DayOfWeek DayOfWeek;
private readonly Farmer Farmer;
private FarmerTeam Team => this.Farmer.team;
private VirtualTV Television => this.Mod.Television;
public RecipeFinder(ModEntry mod, Farmer farmer) {
this.Mod = mod;
this.DayOfWeek = (DayOfWeek)(Game1.dayOfMonth % 7);
this.Farmer = farmer;
}
/// <inheritdoc/>
public string? GetAnyRecipe() => this.DayOfWeek switch {
DayOfWeek.Sunday or DayOfWeek.Wednesday => this.GetRegularRecipes(),
DayOfWeek.Friday => this.GetAnimalHusbandryRecipes(),
_ => null
};
private string? GetRegularRecipes() {
uint played = Game1.stats.DaysPlayed;
if (played < 5)
return null;
int num = (int)(Game1.stats.DaysPlayed % 224U / 7U);
if (played % 224U == 0U)
num = 32;
switch (this.DayOfWeek) {
case DayOfWeek.Sunday:
break;
case DayOfWeek.Wednesday:
if (this.Team.lastDayQueenOfSauceRerunUpdated.Value != Game1.Date.TotalDays) {
this.Team.lastDayQueenOfSauceRerunUpdated.Set(Game1.Date.TotalDays);
this.Team.queenOfSauceRerunWeek.Set(this.Television.GetRerunWeek());
}
num = this.Team.queenOfSauceRerunWeek.Value;
break;
default: return null;
}
// Dictionary of recipes
Dictionary<string, string> dictionary = DataLoader.Tv_CookingChannel(Game1.temporaryContent);
if (!dictionary.TryGetValue($"{num}", out string? translation))
return null;
// Split the translation info
string[] recipeInfo = translation.Split('/');
// Get the recipe name
return recipeInfo.Length <= 0 ? null : recipeInfo[0];
}
private string? GetAnimalHusbandryRecipes() {
if (!this.Mod.PlayerHasMod(this.Farmer, "Digus.AnimalHusbandryMod"))
return null;
if (
// Create a new instance of the Channel
Type.GetType("AnimalHusbandryMod.recipes.MeatFridayChannel, AnimalHusbandryMod") is not Type type
|| Activator.CreateInstance(type) is not object fridayChannel
// Invoke the recipe number getter
|| type.GetMethod("GetRecipeNumber", BindingFlags.NonPublic | BindingFlags.Static) is not MethodInfo getRecipeNumber
|| getRecipeNumber.Invoke(null, Array.Empty<object>()) is not int recipeNumber
// Read the private Recipes dictionary
|| type.GetField("_recipes", BindingFlags.NonPublic | BindingFlags.Instance) is not FieldInfo recipeField
|| recipeField.GetValue(fridayChannel) is not Dictionary<int, string> recipes
) {
this.Mod.Monitor.Log("Reflection failed", LogLevel.Error);
return null;
}
// Try reading the recipe number from the dictionary
if (!recipes.TryGetValue(recipeNumber, out string? recipe))
return null;
// Split the translation info
string[] recipeInfo = recipe.Split('/');
// Get the recipe name
return recipeInfo.Length <= 0 ? null : recipeInfo[0];
}
}
}
}