-
Notifications
You must be signed in to change notification settings - Fork 29
/
DataLoader.cs
363 lines (334 loc) · 19.8 KB
/
DataLoader.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using CustomCaskMod.integrations;
using MailFrameworkMod;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewModdingAPI.Utilities;
using StardewValley;
using StardewValley.GameData.Machines;
using StardewValley.GameData.Objects;
using StardewValley.GameData.BigCraftables;
using System.Security.AccessControl;
namespace CustomCaskMod
{
internal class DataLoader
{
private const string CaskDataJson = "CaskData.json";
private const string AgersDataJson = "AgersData.json";
private const string VanillaCaskName = "Cask";
private const string VanillaCaskQualifiedItemId = "(BC)163";
public static IModHelper Helper;
public static ITranslationHelper I18N;
public static ModConfig ModConfig;
public static Dictionary<object, float> CaskData;
public static Dictionary<string, float> CaskDataId = new();
public DataLoader(IModHelper helper, IManifest manifest)
{
Helper = helper;
I18N = helper.Translation;
ModConfig = helper.ReadConfig<ModConfig>();
CaskData = DataLoader.Helper.Data.ReadJsonFile<Dictionary<object, float>>("data\\CaskData.json") ?? new Dictionary<object, float>() { { 342, 2.66f }, { 724, 2f } };
DataLoader.Helper.Data.WriteJsonFile("data\\CaskData.json", CaskData);
IMailFrameworkModApi mailFrameworkModApi = helper.ModRegistry.GetApi<IMailFrameworkModApi>("DIGUS.MailFrameworkMod");
mailFrameworkModApi?.RegisterLetter(
new ApiLetter
{
Id = "CustomCaskRecipe", Text = "CustomCask.RecipeLetter",
Title = "CustomCask.RecipeLetter.Title", Recipe = VanillaCaskName, I18N = helper.Translation
}
, (l) => !DataLoader.ModConfig.DisableLetter && !Game1.player.mailReceived.Contains(l.Id) && !Game1.player.mailReceived.Contains("CustomCask") && (Utility.getHomeOfFarmer(Game1.player).upgradeLevel >= 3 || ModConfig.EnableCasksAnywhere) && !Game1.player.craftingRecipes.ContainsKey(VanillaCaskName)
, (l) => Game1.player.mailReceived.Add(l.Id)
);
mailFrameworkModApi?.RegisterLetter(
new ApiLetter
{
Id = "CustomCask"
, Text = "CustomCask.Letter"
, Title = "CustomCask.Letter.Title"
, I18N = helper.Translation
}
, (l) => !DataLoader.ModConfig.DisableLetter && !Game1.player.mailReceived.Contains(l.Id) && !Game1.player.mailReceived.Contains("CustomCaskRecipe") && (Utility.getHomeOfFarmer(Game1.player).upgradeLevel >= 3 || ModConfig.EnableCasksAnywhere) && Game1.player.craftingRecipes.ContainsKey(VanillaCaskName)
, (l) => Game1.player.mailReceived.Add(l.Id)
);
Helper.Events.Content.AssetRequested += this.OnAssetRequested;
CreateConfigMenu(manifest);
}
private void OnAssetRequested(object sender, AssetRequestedEventArgs e)
{
if (e.Name.IsEquivalentTo("Data/Machines"))
{
e.Edit(assetData =>
{
var machines = assetData.AsDictionary<String, MachineData>();
var machineOutputRules = machines.Data[VanillaCaskQualifiedItemId].OutputRules;
foreach (var (id, multiplier) in CaskDataId.OrderByDescending(p=>p.Key))
{
MachineOutputRule machineOutputRule = null;
machineOutputRule = ItemRegistry.Exists(id) ? CreateMachineOutputRule(ItemRegistry.Create(id), multiplier) : CreateMachineOutputRule(id, multiplier);
machineOutputRules.RemoveAll(r => r.Id.Equals(machineOutputRule.Id));
machineOutputRules.Insert(0, machineOutputRule);
}
machineOutputRules.RemoveAll(r => r.Id.Equals(GetMachineRuleId(VanillaCaskName)));
if (DataLoader.ModConfig.EnableCaskAgeEveryObject)
{
MachineOutputRule machineOutputRule = CreateMachineDefaultOutputRule(VanillaCaskName, DataLoader.ModConfig.DefaultAgingRate);
machineOutputRules.Add(machineOutputRule);
}
foreach (var customAger in AgerController.GetAgers())
{
var bigCraftables = StardewValley.DataLoader.BigCraftables(Game1.content);
var bigCraftableId = customAger.Key;
if (bigCraftableId != null)
{
machines.Data.TryGetValue(bigCraftableId, out MachineData machineData);
if (machineData == null)
{
machineData = new MachineData()
{
OutputRules = new List<MachineOutputRule>()
};
machines.Data.Add(bigCraftableId, machineData);
machineOutputRules = machineData.OutputRules;
}
foreach (var (id, multiplier) in customAger.Value.AgingDataId)
{
var machineOutputRule = ItemRegistry.Exists(id) ? CreateMachineOutputRule(ItemRegistry.Create(id), multiplier) : CreateMachineOutputRule(id, multiplier);
machineOutputRules.RemoveAll(r => r.Id.Equals(machineOutputRule.Id));
machineOutputRules.Insert(0, machineOutputRule);
}
machineOutputRules.RemoveAll(r => r.Id.Equals(GetMachineRuleId(customAger.Value.Name)));
if (customAger.Value.EnableAgeEveryObject)
{
MachineOutputRule machineOutputRule = CreateMachineDefaultOutputRule(customAger.Key, customAger.Value.DefaultAgingRate);
machineOutputRules.Add(machineOutputRule);
}
}
}
});
}
}
private static string GetMachineRuleId(string agerName)
{
return $"Digus.CustomCaskMod.{agerName}.EnableAgeEveryObject";
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060", Justification = "Needed for command methods.")]
public static void LoadContentPacksCommand(string command = null, string[] args = null)
{
AgerController.ClearAgers();
foreach (IContentPack contentPack in Helper.ContentPacks.GetOwned())
{
bool hasFile = false;
if (File.Exists(Path.Combine(contentPack.DirectoryPath, CaskDataJson)))
{
hasFile = true;
CustomCaskModEntry.ModMonitor.Log($"Reading file {AgersDataJson} from content pack: {contentPack.Manifest.Name} {contentPack.Manifest.Version} from {contentPack.DirectoryPath}");
Dictionary<object, float> caskData = contentPack.ReadJsonFile<Dictionary<object, float>>(CaskDataJson);
foreach (var caskItem in caskData)
{
DataLoader.CaskData[caskItem.Key] = caskItem.Value;
}
}
if (File.Exists(Path.Combine(contentPack.DirectoryPath, AgersDataJson)))
{
hasFile = true;
CustomCaskModEntry.ModMonitor.Log($"Reading file {AgersDataJson} from content pack: {contentPack.Manifest.Name} {contentPack.Manifest.Version} from {contentPack.DirectoryPath}");
List<CustomAger> agersData = contentPack.ReadJsonFile<List<CustomAger>>(AgersDataJson);
foreach (CustomAger customAger in agersData)
{
if (customAger.Name != VanillaCaskName && customAger.QualifiedItemId != VanillaCaskQualifiedItemId)
{
customAger.ModUniqueID = contentPack.Manifest.UniqueID;
if (customAger.QualifiedItemId == null)
{
var foundAgers = Game1.bigCraftableData.Where(b => b.Value.Name.Equals(customAger.Name));
if (!foundAgers.Any())
{
CustomCaskModEntry.ModMonitor.Log($"There is no ager with the name '{customAger.Name}'. This data will be ignored.", LogLevel.Warn);
continue;
}
else
{
customAger.QualifiedItemId = ItemRegistry.type_bigCraftable + foundAgers.First().Key;
if (foundAgers.Count() > 1)
{
CustomCaskModEntry.ModMonitor.Log($"There is more than one ager with the name '{customAger.Name}'. Ager of qualified item id '{customAger.QualifiedItemId}' will be used.", LogLevel.Warn);
}
}
}
if (AgerController.GetAger(customAger.QualifiedItemId) is CustomAger oldAger && oldAger.ModUniqueID != customAger.ModUniqueID)
{
if (oldAger.OverrideMod.Contains(customAger.ModUniqueID) && customAger.OverrideMod.Contains(oldAger.ModUniqueID))
{
CustomCaskModEntry.ModMonitor.Log($"Both mods '{oldAger.ModUniqueID}' and '{customAger.ModUniqueID}' are saying they should override data for '{customAger.QualifiedItemId}'. You should report the problem to these mod's authors. Data from mod '{oldAger.ModUniqueID}' will be used.", LogLevel.Warn);
continue;
}
else if (customAger.OverrideMod.Contains(oldAger.ModUniqueID))
{
CustomCaskModEntry.ModMonitor.Log($"Mod '{customAger.ModUniqueID}' is overriding mod '{oldAger.ModUniqueID}' data for ager '{customAger.QualifiedItemId}'." , LogLevel.Debug);
}
else if (oldAger.OverrideMod.Contains(customAger.ModUniqueID))
{
CustomCaskModEntry.ModMonitor.Log($"Mod '{oldAger.ModUniqueID}' is overriding mod '{customAger.ModUniqueID}' data for ager '{oldAger.QualifiedItemId}'." , LogLevel.Debug);
continue;
}
else if (customAger.MergeIntoMod.Contains(oldAger.ModUniqueID))
{
if (oldAger.MergeIntoMod.Contains(customAger.ModUniqueID))
{
CustomCaskModEntry.ModMonitor.Log($"Both mods '{oldAger.ModUniqueID}' and '{customAger.ModUniqueID}' are saying they should merge data for '{customAger.QualifiedItemId}'. You should report the problem to these mod's authors. Data from mod '{customAger.ModUniqueID}' will be merge into '{oldAger.ModUniqueID}'.", LogLevel.Warn);
}
CustomCaskModEntry.ModMonitor.Log($"Mod '{customAger.ModUniqueID}' is merging with mod '{oldAger.ModUniqueID}' data for ager '{customAger.QualifiedItemId}'.", LogLevel.Debug);
foreach (var (key, value) in customAger.AgingData)
{
oldAger.AgingData[key] = value;
}
FillDataIds(oldAger.AgingData, oldAger.AgingDataId);
continue;
}
else if (oldAger.MergeIntoMod.Contains(oldAger.ModUniqueID))
{
CustomCaskModEntry.ModMonitor.Log($"Mod '{oldAger.ModUniqueID}' is merging with mod '{customAger.ModUniqueID}' data for ager '{customAger.QualifiedItemId}'.", LogLevel.Debug);
foreach (var (key, value) in oldAger.AgingData)
{
customAger.AgingData[key] = value;
}
}
else
{
CustomCaskModEntry.ModMonitor.Log($"Mod '{customAger.ModUniqueID}' can't override mod '{oldAger.ModUniqueID}' data for '{customAger.QualifiedItemId}'. This data will be ignored.", LogLevel.Warn);
continue;
}
}
FillDataIds(customAger.AgingData, customAger.AgingDataId);
AgerController.SetAger(customAger);
}
else
{
CustomCaskModEntry.ModMonitor.Log($"Cask data can't be added on {AgersDataJson} file. Use {CaskDataJson} file instead.", LogLevel.Warn);
}
}
}
if (!hasFile)
{
CustomCaskModEntry.ModMonitor.Log($"Ignoring content pack: {contentPack.Manifest.Name} {contentPack.Manifest.Version} from {contentPack.DirectoryPath}\nIt doesn't have both {CaskDataJson} and {AgersDataJson} files.", LogLevel.Warn);
}
}
FillDataIds(CaskData, CaskDataId);
CustomCaskModEntry.Helper.GameContent.InvalidateCache("Data/Machines");
}
public static void FillDataIds(Dictionary<object, float> data, Dictionary<string, float> dataIds)
{
data.ToList().ForEach(c =>
{
var (key, value) = c;
var id = GetId(key);
if (id != null)
{
dataIds[id] = value;
}
});
}
private static string GetId(object identifier)
{
if (ItemRegistry.IsQualifiedItemId(identifier.ToString()) && ItemRegistry.Exists(identifier.ToString()))
{
return identifier.ToString();
}
if (Int32.TryParse(identifier.ToString(), out int id))
{
return id >= 0 ? ItemRegistry.QualifyItemId(id.ToString()) : identifier.ToString();
}
else
{
var pair = Game1.objectData.FirstOrDefault(o => identifier.Equals(o.Value.Name));
if (pair.Value != null)
{
return ItemRegistry.QualifyItemId(pair.Key);
}
}
return null;
}
private static MachineOutputRule CreateMachineDefaultOutputRule(string name, float agingRate)
{
return new MachineOutputRule
{
Id = GetMachineRuleId(name),
Triggers = new List<MachineOutputTriggerRule>
{
new MachineOutputTriggerRule()
},
OutputItem = new List<MachineItemOutput>
{
new MachineItemOutput
{
CustomData = new Dictionary<string, string> { { "AgingMultiplier", agingRate.ToString() } },
OutputMethod = "StardewValley.Objects.Cask, Stardew Valley: OutputCask"
}
}
};
}
private static MachineOutputRule CreateMachineOutputRule(Item itemInput, float agingMultiplier)
{
var result = new MachineOutputRule
{
Id = "Digus.CustomCaskMod." + itemInput.Name,
Triggers = new List<MachineOutputTriggerRule>
{
new MachineOutputTriggerRule()
{
RequiredItemId = itemInput.QualifiedItemId
}
},
OutputItem = new List<MachineItemOutput>
{
new MachineItemOutput
{
CustomData = new Dictionary<string, string> { { "AgingMultiplier", agingMultiplier.ToString() } },
OutputMethod = "StardewValley.Objects.Cask, Stardew Valley: OutputCask"
}
}
};
return result;
}
private static MachineOutputRule CreateMachineOutputRule(string category, float agingMultiplier)
{
var result = new MachineOutputRule
{
Id = "Digus.CustomCaskMod.Category" + category,
Triggers = new List<MachineOutputTriggerRule>
{
new MachineOutputTriggerRule()
{
Condition = "ITEM_CATEGORY INPUT "+category
}
},
OutputItem = new List<MachineItemOutput>
{
new MachineItemOutput
{
CustomData = new Dictionary<string, string> { { "AgingMultiplier", agingMultiplier.ToString() } },
OutputMethod = "StardewValley.Objects.Cask, Stardew Valley: OutputCask"
}
}
};
return result;
}
private static void CreateConfigMenu(IManifest manifest)
{
GenericModConfigMenuApi api = Helper.ModRegistry.GetApi<GenericModConfigMenuApi>("spacechase0.GenericModConfigMenu");
if (api != null)
{
api.Register(manifest, () => DataLoader.ModConfig = new ModConfig(), () => Helper.WriteConfig(DataLoader.ModConfig));
api.AddBoolOption(manifest, () => DataLoader.ModConfig.DisableLetter, (bool val) => DataLoader.ModConfig.DisableLetter = val, ()=>"Disable Letter", ()=>"You won't receive the letter about Custom Cask changes and the cask recipe in case you don't know it.");
api.AddBoolOption(manifest, () => DataLoader.ModConfig.EnableCasksAnywhere, (bool val) => DataLoader.ModConfig.EnableCasksAnywhere = val, () => "Casks Anywhere", () => "Casks will accept items anywhere.");
api.AddBoolOption(manifest, () => DataLoader.ModConfig.EnableCustomAgersAnywhere, (bool val) => DataLoader.ModConfig.EnableCustomAgersAnywhere = val, () => "Custom Agers Anywhere", () => "Custom Agers will accept items anywhere.");
api.AddBoolOption(manifest, () => DataLoader.ModConfig.EnableMoreThanOneQualityIncrementPerDay, (bool val) => DataLoader.ModConfig.EnableMoreThanOneQualityIncrementPerDay = val, () => "Quality++", () => "Casks will be able to increase more than one quality lever per day.");
api.AddBoolOption(manifest, () => DataLoader.ModConfig.EnableCaskAgeEveryObject, (bool val) => DataLoader.ModConfig.EnableCaskAgeEveryObject = val, () => "Cask Age Every Object", () => "Casks will be able to age every object.");
api.AddNumberOption(manifest, () => DataLoader.ModConfig.DefaultAgingRate, (float val) => DataLoader.ModConfig.DefaultAgingRate = val, () => "Default Aging Rate", () => "Rate that will be used for non declared objects.");
}
}
}
}