forked from Pathoschild/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataParser.cs
483 lines (431 loc) · 21.9 KB
/
DataParser.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
using System;
using System.Collections.Generic;
using System.Linq;
using Pathoschild.Stardew.Common;
using Pathoschild.Stardew.Common.Items.ItemData;
using Pathoschild.Stardew.LookupAnything.Framework;
using Pathoschild.Stardew.LookupAnything.Framework.Constants;
using Pathoschild.Stardew.LookupAnything.Framework.Data;
using Pathoschild.Stardew.LookupAnything.Framework.Models;
using Pathoschild.Stardew.LookupAnything.Framework.Models.FishData;
using StardewModdingAPI;
using StardewValley;
using StardewValley.Buildings;
using StardewValley.Characters;
using StardewValley.GameData.FishPond;
using SFarmer = StardewValley.Farmer;
using SObject = StardewValley.Object;
namespace Pathoschild.Stardew.LookupAnything
{
/// <summary>Parses the raw game data into usable models. These may be expensive operations and should be cached.</summary>
internal class DataParser
{
/*********
** Fields
*********/
/// <summary>Provides utility methods for interacting with the game code.</summary>
private readonly GameHelper GameHelper;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="gameHelper">Provides utility methods for interacting with the game code.</param>
public DataParser(GameHelper gameHelper)
{
this.GameHelper = gameHelper;
}
/// <summary>Read parsed data about the Community Center bundles.</summary>
/// <param name="monitor">The monitor with which to log errors.</param>
/// <remarks>Derived from the <see cref="StardewValley.Locations.CommunityCenter"/> constructor and <see cref="StardewValley.Menus.JunimoNoteMenu.openRewardsMenu"/>.</remarks>
public IEnumerable<BundleModel> GetBundles(IMonitor monitor)
{
foreach ((string key, string value) in Game1.netWorldState.Value.BundleData)
{
BundleModel bundle;
try
{
// parse key
string[] keyParts = key.Split('/');
string area = keyParts[0];
int id = int.Parse(keyParts[1]);
// parse bundle info
string[] valueParts = value.Split('/');
string name = valueParts[0];
string reward = valueParts[1];
string displayName = LocalizedContentManager.CurrentLanguageCode == LocalizedContentManager.LanguageCode.en
? name // field isn't present in English
: valueParts.Last(); // number of fields varies, but display name is always last
// parse ingredients
List<BundleIngredientModel> ingredients = new List<BundleIngredientModel>();
string[] ingredientData = valueParts[2].Split(' ');
for (int i = 0; i < ingredientData.Length; i += 3)
{
int index = i / 3;
int itemID = int.Parse(ingredientData[i]);
int stack = int.Parse(ingredientData[i + 1]);
ItemQuality quality = (ItemQuality)int.Parse(ingredientData[i + 2]);
ingredients.Add(new BundleIngredientModel(index, itemID, stack, quality));
}
// create bundle
bundle = new BundleModel(
ID: id,
Name: name,
DisplayName: displayName,
Area: area,
RewardData: reward,
Ingredients: ingredients.ToArray()
);
}
catch (Exception ex)
{
monitor.LogOnce($"Couldn't parse community center bundle '{key}' due to an invalid format.\nRecipe data: '{value}'\nError: {ex}", LogLevel.Warn);
continue;
}
yield return bundle;
}
}
/// <summary>Read parsed data about a fish pond's population gates for a specific fish.</summary>
/// <param name="data">The fish pond data.</param>
public IEnumerable<FishPondPopulationGateData> GetFishPondPopulationGates(FishPondData data)
{
foreach (var gate in data.PopulationGates)
{
// get required items
FishPondPopulationGateQuestItemData[] questItems = gate.Value
.Select(entry =>
{
// parse ID
string[] parts = entry.Split(' ');
if (parts.Length is < 1 or > 3 || !int.TryParse(parts[0], out int id))
return null;
// parse counts
int minCount = 1;
int maxCount = 1;
if (parts.Length >= 2)
int.TryParse(parts[1], out minCount);
if (parts.Length >= 3)
int.TryParse(parts[1], out maxCount);
// normalize counts
minCount = Math.Max(1, minCount);
maxCount = Math.Max(1, maxCount);
if (maxCount < minCount)
maxCount = minCount;
// build entry
return new FishPondPopulationGateQuestItemData(id, minCount, maxCount);
})
.WhereNotNull()
.ToArray();
// build entry
yield return new FishPondPopulationGateData(gate.Key, questItems);
}
}
/// <summary>Read parsed data about a fish pond's item drops for a specific fish.</summary>
/// <param name="data">The fish pond data.</param>
public IEnumerable<FishPondDropData> GetFishPondDrops(FishPondData data)
{
foreach (FishPondReward drop in data.ProducedItems)
yield return new FishPondDropData(drop.RequiredPopulation, drop.ItemID, drop.MinQuantity, drop.MaxQuantity, drop.Chance);
}
/// <summary>Read parsed data about the spawn rules for a specific fish.</summary>
/// <param name="fishID">The fish ID.</param>
/// <param name="metadata">Provides metadata that's not available from the game data directly.</param>
/// <remarks>Derived from <see cref="GameLocation.getFish"/>.</remarks>
public FishSpawnData? GetFishSpawnRules(int fishID, Metadata metadata)
{
// get raw fish data
string[] fishFields;
{
if (!Game1.content.Load<Dictionary<int, string>>("Data\\Fish").TryGetValue(fishID, out string? rawData))
return null;
fishFields = rawData.Split('/');
if (fishFields.Length < 13)
return null;
}
// parse location data
var locations = new List<FishSpawnLocationData>();
foreach ((string locationName, string value) in Game1.content.Load<Dictionary<string, string>>("Data\\Locations"))
{
if (metadata.IgnoreFishingLocations.Contains(locationName))
continue; // ignore event data
List<FishSpawnLocationData> curLocations = new List<FishSpawnLocationData>();
// get locations
string[] locationFields = value.Split('/');
for (int s = 4; s <= 7; s++)
{
string[] seasonFields = locationFields[s].Split(' ');
string season = s switch
{
4 => "spring",
5 => "summer",
6 => "fall",
7 => "winter",
_ => throw new NotSupportedException() // should never happen
};
for (int i = 0, last = seasonFields.Length + 1; i + 1 < last; i += 2)
{
if (!int.TryParse(seasonFields[i], out int curFishID) || curFishID != fishID || !int.TryParse(seasonFields[i + 1], out int areaID))
continue;
curLocations.Add(new FishSpawnLocationData(locationName, areaID, new[] { season }));
}
}
// combine seasons for same area
locations.AddRange(
from areaGroup in curLocations.GroupBy(p => p.Area)
let seasons = areaGroup.SelectMany(p => p.Seasons).Distinct().ToArray()
select new FishSpawnLocationData(locationName, areaGroup.Key, seasons)
);
}
// parse fish data
var timesOfDay = new List<FishSpawnTimeOfDayData>();
FishSpawnWeather weather = FishSpawnWeather.Both;
int minFishingLevel = 0;
bool isUnique = false;
if (locations.Any()) // ignore default spawn criteria if the fish doesn't spawn naturally; in that case it should be specified explicitly in custom data below (if any)
{
// times of day
string[] timeFields = fishFields[5].Split(' ');
for (int i = 0, last = timeFields.Length + 1; i + 1 < last; i += 2)
{
if (int.TryParse(timeFields[i], out int minTime) && int.TryParse(timeFields[i + 1], out int maxTime))
timesOfDay.Add(new FishSpawnTimeOfDayData(minTime, maxTime));
}
// weather
if (!Enum.TryParse(fishFields[7], true, out weather))
weather = FishSpawnWeather.Both;
// min fishing level
if (!int.TryParse(fishFields[12], out minFishingLevel))
minFishingLevel = 0;
}
// read custom data
if (metadata.CustomFishSpawnRules.TryGetValue(fishID, out FishSpawnData? customRules))
{
if (customRules.MinFishingLevel > minFishingLevel)
minFishingLevel = customRules.MinFishingLevel;
if (customRules.Weather != FishSpawnWeather.Unknown)
weather = customRules.Weather;
isUnique = isUnique || customRules.IsUnique;
if (customRules.TimesOfDay != null)
timesOfDay.AddRange(customRules.TimesOfDay);
if (customRules.Locations != null)
locations.AddRange(customRules.Locations);
}
// build model
return new FishSpawnData(
FishID: fishID,
Locations: locations.ToArray(),
TimesOfDay: timesOfDay.ToArray(),
Weather: weather,
MinFishingLevel: minFishingLevel,
IsUnique: isUnique
);
}
/// <summary>Get parsed data about the friendship between a player and NPC.</summary>
/// <param name="player">The player.</param>
/// <param name="npc">The NPC.</param>
/// <param name="friendship">The current friendship data.</param>
/// <param name="metadata">Provides metadata that's not available from the game data directly.</param>
public FriendshipModel GetFriendshipForVillager(SFarmer player, NPC npc, Friendship friendship, Metadata metadata)
{
return new FriendshipModel(player, npc, friendship, metadata.Constants);
}
/// <summary>Get parsed data about the friendship between a player and NPC.</summary>
/// <param name="player">The player.</param>
/// <param name="pet">The pet.</param>
public FriendshipModel GetFriendshipForPet(SFarmer player, Pet pet)
{
return new FriendshipModel(pet.friendshipTowardFarmer.Value, Pet.maxFriendship / 10, Pet.maxFriendship);
}
/// <summary>Get parsed data about the friendship between a player and NPC.</summary>
/// <param name="player">The player.</param>
/// <param name="animal">The farm animal.</param>
/// <param name="metadata">Provides metadata that's not available from the game data directly.</param>
public FriendshipModel GetFriendshipForAnimal(SFarmer player, FarmAnimal animal, Metadata metadata)
{
return new FriendshipModel(animal.friendshipTowardFarmer.Value, metadata.Constants.AnimalFriendshipPointsPerLevel, metadata.Constants.AnimalFriendshipMaxPoints);
}
/// <summary>Parse monster data.</summary>
/// <remarks>Reverse engineered from <see cref="StardewValley.Monsters.Monster.parseMonsterInfo"/>, <see cref="GameLocation.monsterDrop"/>, and the <see cref="Debris"/> constructor.</remarks>
public IEnumerable<MonsterData> GetMonsters()
{
Dictionary<string, string> data = Game1.content.Load<Dictionary<string, string>>("Data\\Monsters");
foreach (var entry in data)
{
// monster fields
string[] fields = entry.Value.Split('/');
string name = entry.Key;
int health = int.Parse(fields[0]);
int damageToFarmer = int.Parse(fields[1]);
//int minCoins = int.Parse(fields[2]);
//int maxCoins = int.Parse(fields[3]) + 1;
bool isGlider = bool.Parse(fields[4]);
int durationOfRandomMovements = int.Parse(fields[5]);
int resilience = int.Parse(fields[7]);
double jitteriness = double.Parse(fields[8]);
int moveTowardsPlayerThreshold = int.Parse(fields[9]);
int speed = int.Parse(fields[10]);
double missChance = double.Parse(fields[11]);
bool isMineMonster = bool.Parse(fields[12]);
// drops
var drops = new List<ItemDropData>();
string[] dropFields = fields[6].Split(' ');
for (int i = 0; i < dropFields.Length; i += 2)
{
// get drop info
int itemID = int.Parse(dropFields[i]);
float chance = float.Parse(dropFields[i + 1]);
int maxDrops = 1;
// if itemID is negative, game randomly drops 1-3
if (itemID < 0)
{
itemID = -itemID;
maxDrops = 3;
}
// some item IDs have special meaning
if (itemID == Debris.copperDebris)
itemID = SObject.copper;
else if (itemID == Debris.ironDebris)
itemID = SObject.iron;
else if (itemID == Debris.coalDebris)
itemID = SObject.coal;
else if (itemID == Debris.goldDebris)
itemID = SObject.gold;
else if (itemID == Debris.coinsDebris)
continue; // no drop
else if (itemID == Debris.iridiumDebris)
itemID = SObject.iridium;
else if (itemID == Debris.woodDebris)
itemID = SObject.wood;
else if (itemID == Debris.stoneDebris)
itemID = SObject.stone;
// add drop
drops.Add(new ItemDropData(itemID, 1, maxDrops, chance));
}
if (isMineMonster && Game1.player.timesReachedMineBottom >= 1)
{
drops.Add(new ItemDropData(SObject.diamondIndex, 1, 1, 0.008f));
drops.Add(new ItemDropData(SObject.prismaticShardIndex, 1, 1, 0.008f));
}
// yield data
yield return new MonsterData(
Name: name,
Health: health,
DamageToFarmer: damageToFarmer,
IsGlider: isGlider,
DurationOfRandomMovements: durationOfRandomMovements,
Resilience: resilience,
Jitteriness: jitteriness,
MoveTowardsPlayerThreshold: moveTowardsPlayerThreshold,
Speed: speed,
MissChance: missChance,
IsMineMonster: isMineMonster,
Drops: drops.ToArray()
);
}
}
/// <summary>Get the recipe ingredients.</summary>
/// <param name="metadata">Provides metadata that's not available from the game data directly.</param>
/// <param name="reflectionHelper">Simplifies access to private game code.</param>
/// <param name="monitor">The monitor with which to log errors.</param>
public RecipeModel[] GetRecipes(Metadata metadata, IReflectionHelper reflectionHelper, IMonitor monitor)
{
List<RecipeModel> recipes = new List<RecipeModel>();
// cooking/crafting recipes
var craftingRecipes =
(from pair in CraftingRecipe.cookingRecipes select new { pair.Key, pair.Value, IsCookingRecipe = true })
.Concat(from pair in CraftingRecipe.craftingRecipes select new { pair.Key, pair.Value, IsCookingRecipe = false });
foreach (var entry in craftingRecipes)
{
try
{
var recipe = new CraftingRecipe(entry.Key, entry.IsCookingRecipe);
recipes.Add(new RecipeModel(recipe));
}
catch (Exception ex)
{
monitor.Log($"Couldn't parse {(entry.IsCookingRecipe ? "cooking" : "crafting")} recipe '{entry.Key}' due to an invalid format.\nRecipe data: '{entry.Value}'\nError: {ex}", LogLevel.Warn);
}
}
// machine recipes
recipes.AddRange(
from entry in metadata.MachineRecipes
let machine = this.GameHelper.GetObjectBySpriteIndex(entry.MachineID, bigcraftable: true)
from recipe in entry.Recipes
from output in recipe.PossibleOutputs
from outputId in output.Ids
select new RecipeModel(
key: null,
type: RecipeType.MachineInput,
displayType: machine.DisplayName,
ingredients: recipe.Ingredients.Select(p => new RecipeIngredientModel(p)),
item: ingredient => this.CreateRecipeItem(ingredient?.ParentSheetIndex, outputId, output),
isKnown: () => true,
exceptIngredients: recipe.ExceptIngredients?.Select(p => new RecipeIngredientModel(p)),
outputItemIndex: outputId,
minOutput: output.MinOutput,
maxOutput: output.MaxOutput,
outputChance: output.OutputChance,
machineParentSheetIndex: entry.MachineID,
isForMachine: p => p is SObject obj && obj.GetItemType() == ItemType.BigCraftable && obj.ParentSheetIndex == entry.MachineID
)
);
// building recipes
recipes.AddRange(
from entry in metadata.BuildingRecipes
let building = new BluePrint(entry.BuildingKey)
select new RecipeModel(
key: null,
type: RecipeType.BuildingInput,
displayType: building.displayName,
ingredients: entry.Ingredients.Select(p => new RecipeIngredientModel(p.Key, p.Value)),
item: ingredient => this.CreateRecipeItem(ingredient?.ParentSheetIndex, entry.Output, null),
isKnown: () => true,
outputItemIndex: entry.Output,
minOutput: entry.OutputCount ?? 1,
exceptIngredients: entry.ExceptIngredients?.Select(p => new RecipeIngredientModel(p, 1)),
machineParentSheetIndex: null,
isForMachine: p => p is Building target && target.buildingType.Value == entry.BuildingKey
)
);
return recipes.ToArray();
}
/*********
** Private methods
*********/
/// <summary>Create a custom recipe output.</summary>
/// <param name="inputID">The input ingredient ID.</param>
/// <param name="outputID">The output item ID.</param>
/// <param name="output">The output data, if applicable.</param>
private SObject CreateRecipeItem(int? inputID, int outputID, MachineRecipeOutputData? output)
{
SObject item = this.GameHelper.GetObjectBySpriteIndex(outputID);
if (inputID != null)
{
switch (outputID)
{
case 342:
item.preserve.Value = SObject.PreserveType.Pickle;
item.preservedParentSheetIndex.Value = inputID.Value;
break;
case 344:
item.preserve.Value = SObject.PreserveType.Jelly;
item.preservedParentSheetIndex.Value = inputID.Value;
break;
case 348:
item.preserve.Value = SObject.PreserveType.Wine;
item.preservedParentSheetIndex.Value = inputID.Value;
break;
case 350:
item.preserve.Value = SObject.PreserveType.Juice;
item.preservedParentSheetIndex.Value = inputID.Value;
break;
}
}
if (output != null)
{
item.preservedParentSheetIndex.Value = output.PreservedParentSheetIndex ?? item.preservedParentSheetIndex.Value;
item.preserve.Value = output.PreserveType ?? item.preserve.Value;
}
return item;
}
}
}