forked from spacechase0/JsonAssets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mod.cs
622 lines (558 loc) · 28 KB
/
Mod.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using JsonAssets.Data;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Menus;
using StardewValley.Locations;
using StardewValley.TerrainFeatures;
using StardewValley.Objects;
using System.Reflection;
// TODO: Refactor recipes
namespace JsonAssets
{
public class Mod : StardewModdingAPI.Mod
{
public static Mod instance;
public override void Entry(IModHelper helper)
{
instance = this;
MenuEvents.MenuChanged += menuChanged;
//SaveEvents.AfterLoad += afterLoad;
SaveEvents.AfterSave += afterSave;
PlayerEvents.InventoryChanged += invChanged;
SpecialisedEvents.UnvalidatedUpdateTick += unsafeUpdate;
Log.info("Loading content packs...");
foreach (IContentPack contentPack in this.Helper.GetContentPacks())
loadData(contentPack);
if (Directory.Exists(Path.Combine(Helper.DirectoryPath, "ContentPacks")))
{
foreach (string dir in Directory.EnumerateDirectories(Path.Combine(Helper.DirectoryPath, "ContentPacks")))
loadData(dir);
}
}
private IApi api;
public override object GetApi()
{
if (api == null)
api = new Api(this.loadData);
return api;
}
private void loadData(string dir)
{
// read info
if (!File.Exists(Path.Combine(dir, "content-pack.json")))
{
Log.warn($"\tNo {dir}/content-pack.json!");
return;
}
ContentPackData info = this.Helper.ReadJsonFile<ContentPackData>(Path.Combine(dir, "content-pack.json"));
// load content pack
IContentPack contentPack = this.Helper.CreateTransitionalContentPack(dir, id: Guid.NewGuid().ToString("N"), name: info.Name, description: info.Description, author: info.Author, version: new SemanticVersion(info.Version));
this.loadData(contentPack);
}
private void loadData(IContentPack contentPack)
{
Log.info($"\t{contentPack.Manifest.Name} {contentPack.Manifest.Version} by {contentPack.Manifest.Author} - {contentPack.Manifest.Description}");
// load objects
DirectoryInfo objectsDir = new DirectoryInfo(Path.Combine(contentPack.DirectoryPath, "Objects"));
if (objectsDir.Exists)
{
foreach (DirectoryInfo dir in objectsDir.EnumerateDirectories())
{
string relativePath = $"Objects/{dir.Name}";
// load data
ObjectData obj = contentPack.ReadJsonFile<ObjectData>($"{relativePath}/object.json");
if (obj == null)
continue;
// save object
obj.texture = contentPack.LoadAsset<Texture2D>($"{relativePath}/object.png");
if (obj.IsColored)
obj.textureColor = contentPack.LoadAsset<Texture2D>($"{relativePath}/color.png");
this.objects.Add(obj);
// save ring
if (obj.Category == ObjectData.Category_.Ring)
this.myRings.Add(obj);
}
}
// load crops
DirectoryInfo cropsDir = new DirectoryInfo(Path.Combine(contentPack.DirectoryPath, "Crops"));
if (cropsDir.Exists)
{
foreach (DirectoryInfo dir in cropsDir.EnumerateDirectories())
{
string relativePath = $"Crops/{dir.Name}";
// load data
CropData crop = contentPack.ReadJsonFile<CropData>($"{relativePath}/crop.json");
if (crop == null)
continue;
// save crop
crop.texture = contentPack.LoadAsset<Texture2D>($"{relativePath}/crop.png");
crops.Add(crop);
// save seeds
crop.seed = new ObjectData
{
texture = contentPack.LoadAsset<Texture2D>($"{relativePath}/seeds.png"),
Name = crop.SeedName,
Description = crop.SeedDescription,
Category = ObjectData.Category_.Seeds,
Price = crop.SeedPurchasePrice,
CanPurchase = true,
PurchaseFrom = crop.SeedPurchaseFrom,
PurchasePrice = crop.SeedPurchasePrice,
PurchaseRequirements = crop.SeedPurchaseRequirements ?? new List<string>()
};
string[] excludeSeasons = new[] { "spring", "summer", "fall", "winter" }.Except(crop.Seasons).ToArray();
crop.seed.PurchaseRequirements.Add($"z {string.Join(" ", excludeSeasons)}");
objects.Add(crop.seed);
}
}
// load fruit trees
DirectoryInfo fruitTreesDir = new DirectoryInfo(Path.Combine(contentPack.DirectoryPath, "FruitTrees"));
if (fruitTreesDir.Exists)
{
foreach (DirectoryInfo dir in fruitTreesDir.EnumerateDirectories())
{
string relativePath = $"FruitTrees/{dir.Name}";
// load data
FruitTreeData tree = contentPack.ReadJsonFile<FruitTreeData>($"{relativePath}/tree.json");
if (tree == null)
continue;
// save fruit tree
tree.texture = contentPack.LoadAsset<Texture2D>($"{relativePath}/tree.png");
fruitTrees.Add(tree);
// save seed
tree.sapling = new ObjectData
{
texture = contentPack.LoadAsset<Texture2D>($"{relativePath}/sapling.png"),
Name = tree.SaplingName,
Description = tree.SaplingDescription,
Category = ObjectData.Category_.Seeds,
Price = tree.SaplingPurchasePrice,
CanPurchase = true,
PurchaseRequirements = tree.SaplingPurchaseRequirements,
PurchaseFrom = tree.SaplingPurchaseFrom,
PurchasePrice = tree.SaplingPurchasePrice
};
objects.Add(tree.sapling);
}
}
// load big craftables
DirectoryInfo bigCraftablesDir = new DirectoryInfo(Path.Combine(contentPack.DirectoryPath, "BigCraftables"));
if (bigCraftablesDir.Exists)
{
foreach (DirectoryInfo dir in bigCraftablesDir.EnumerateDirectories())
{
string relativePath = $"BigCraftables/{dir.Name}";
// load data
BigCraftableData craftable = contentPack.ReadJsonFile<BigCraftableData>($"{relativePath}/big-craftable.json");
if (craftable == null)
continue;
// save craftable
craftable.texture = contentPack.LoadAsset<Texture2D>($"{relativePath}/big-craftable.png");
bigCraftables.Add(craftable);
}
}
// load objects
DirectoryInfo hatsDir = new DirectoryInfo(Path.Combine(contentPack.DirectoryPath, "Hats"));
if (hatsDir.Exists)
{
foreach (DirectoryInfo dir in hatsDir.EnumerateDirectories())
{
string relativePath = $"Hats/{dir.Name}";
// load data
HatData hat = contentPack.ReadJsonFile<HatData>($"{relativePath}/hat.json");
if (hat == null)
continue;
// save object
hat.texture = contentPack.LoadAsset<Texture2D>($"{relativePath}/hat.png");
hats.Add(hat);
}
}
}
private void unsafeUpdate(object sender, EventArgs args)
{
// I need the items to register before most other things do
var saveLoaded = (bool) typeof(Context).GetProperty("IsSaveLoaded", BindingFlags.NonPublic | BindingFlags.Static).GetValue( null );
if (saveLoaded && !SaveGame.IsProcessing)
{
Log.debug("Loading stuff early");
afterLoad(sender, args);
SpecialisedEvents.UnvalidatedUpdateTick -= unsafeUpdate;
}
}
private void menuChanged(object sender, EventArgsClickableMenuChanged args)
{
if ( args.NewMenu is TitleMenu )
{
// When we go back to the title menu we need to reset things so things don't break when
// going back to a save. Also, this is where it is initially done, too.
oldObjectIds = Helper.ReadJsonFile<Dictionary<string, int>>(Path.Combine(Helper.DirectoryPath, $"ids-objects.json"));
oldCropIds = Helper.ReadJsonFile<Dictionary<string, int>>(Path.Combine(Helper.DirectoryPath, $"ids-crops.json"));
oldFruitTreeIds = Helper.ReadJsonFile<Dictionary<string, int>>(Path.Combine(Helper.DirectoryPath, $"ids-fruittrees.json"));
oldBigCraftableIds = Helper.ReadJsonFile<Dictionary<string, int>>(Path.Combine(Helper.DirectoryPath, $"ids-big-craftables.json"));
oldHatIds = Helper.ReadJsonFile<Dictionary<string, int>>(Path.Combine(Helper.DirectoryPath, $"ids-hats.json"));
if (objectIds != null)
{
clearIds(ref objectIds, objects.ToList<DataNeedsId>());
clearIds(ref cropIds, crops.ToList<DataNeedsId>());
clearIds(ref fruitTreeIds, fruitTrees.ToList<DataNeedsId>());
clearIds(ref bigCraftableIds, bigCraftables.ToList<DataNeedsId>());
clearIds(ref hatIds, hats.ToList<DataNeedsId>());
}
var editor = Helper.Content.AssetEditors.Where(x => x is ContentInjector);
if ( editor.Count() > 0 )
Helper.Content.AssetEditors.Remove(editor.ElementAt(0));
return;
}
var menu = args.NewMenu as ShopMenu;
bool hatMouse = false;
if (menu != null && menu.potraitPersonDialogue == Game1.parseText(Game1.content.LoadString("Strings\\StringsFromCSFiles:ShopMenu.cs.11494"), Game1.dialogueFont, Game1.tileSize * 5 - Game1.pixelZoom * 4))
hatMouse = true;
if (menu == null || menu.portraitPerson == null && !hatMouse)
return;
//if (menu.portraitPerson.name == "Pierre")
{
Log.trace($"Adding objects to {menu.portraitPerson?.name}'s shop");
var forSale = Helper.Reflection.GetField<List<Item>>(menu, "forSale").GetValue();
var itemPriceAndStock = Helper.Reflection.GetField<Dictionary<Item, int[]>>(menu, "itemPriceAndStock").GetValue();
var precondMeth = Helper.Reflection.GetMethod(Game1.currentLocation, "checkEventPrecondition");
foreach (var obj in objects)
{
if (obj.Recipe != null && obj.Recipe.CanPurchase)
{
if (obj.Recipe.PurchaseFrom != menu.portraitPerson?.name || (obj.Recipe.PurchaseFrom == "HatMouse" && hatMouse) )
continue;
if (Game1.player.craftingRecipes.ContainsKey(obj.Name) || Game1.player.cookingRecipes.ContainsKey(obj.Name))
continue;
if (obj.Recipe.PurchaseRequirements != null && obj.Recipe.PurchaseRequirements.Count > 0 &&
precondMeth.Invoke<int>(new object[] { obj.Recipe.GetPurchaseRequirementString() }) == -1)
continue;
var recipeObj = new StardewValley.Object(obj.id, 1, true, obj.Recipe.PurchasePrice, 0);
forSale.Add(recipeObj);
itemPriceAndStock.Add(recipeObj, new int[] { obj.Recipe.PurchasePrice, 1 });
Log.trace($"\tAdding recipe for {obj.Name}");
}
if (!obj.CanPurchase)
continue;
if (obj.PurchaseFrom != menu.portraitPerson?.name || (obj.PurchaseFrom == "HatMouse" && hatMouse))
continue;
if (obj.PurchaseRequirements != null && obj.PurchaseRequirements.Count > 0 &&
precondMeth.Invoke<int>(new object[] { obj.GetPurchaseRequirementString() }) == -1)
continue;
Item item = new StardewValley.Object(Vector2.Zero, obj.id, int.MaxValue);
forSale.Add(item);
itemPriceAndStock.Add(item, new int[] { obj.PurchasePrice, int.MaxValue });
Log.trace($"\tAdding {obj.Name}");
}
foreach (var big in bigCraftables)
{
if (big.Recipe != null && big.Recipe.CanPurchase)
{
if (big.Recipe.PurchaseFrom != menu.portraitPerson?.name || (big.Recipe.PurchaseFrom == "HatMouse" && hatMouse))
continue;
if (Game1.player.craftingRecipes.ContainsKey(big.Name) || Game1.player.cookingRecipes.ContainsKey(big.Name))
continue;
if (big.Recipe.PurchaseRequirements != null && big.Recipe.PurchaseRequirements.Count > 0 &&
precondMeth.Invoke<int>(new object[] { big.Recipe.GetPurchaseRequirementString() }) == -1)
continue;
var recipeObj = new StardewValley.Object(new Vector2(0, 0), big.id, true);
forSale.Add(recipeObj);
itemPriceAndStock.Add(recipeObj, new int[] { big.Recipe.PurchasePrice, 1 });
Log.trace($"\tAdding recipe for {big.Name}");
}
if (!big.CanPurchase)
continue;
if (big.PurchaseFrom != menu.portraitPerson?.name || (big.PurchaseFrom == "HatMouse" && hatMouse))
continue;
if (big.PurchaseRequirements != null && big.PurchaseRequirements.Count > 0 &&
precondMeth.Invoke<int>(new object[] { big.GetPurchaseRequirementString() }) == -1)
continue;
Item item = new StardewValley.Object(Vector2.Zero, big.id, false);
forSale.Add(item);
itemPriceAndStock.Add(item, new int[] { big.PurchasePrice, int.MaxValue });
Log.trace($"\tAdding {big.Name}");
}
if ( hatMouse )
{
foreach ( var hat in hats )
{
Item item = new Hat(hat.GetHatId());
forSale.Add(item);
itemPriceAndStock.Add(item, new int[] { hat.PurchasePrice, int.MaxValue });
Log.trace($"\tAdding {hat.Name}");
}
}
}
( ( Api ) api ).InvokeAddedItemsToShop();
}
private void afterLoad(object sender, EventArgs args)
{
if (File.Exists(Path.Combine(Constants.CurrentSavePath, "JsonAssets", "ids-objects.json")))
{
oldObjectIds = Helper.ReadJsonFile<Dictionary<string, int>>(Path.Combine(Constants.CurrentSavePath, "JsonAssets", $"ids-objects.json"));
oldCropIds = Helper.ReadJsonFile<Dictionary<string, int>>(Path.Combine(Constants.CurrentSavePath, "JsonAssets", $"ids-crops.json"));
oldFruitTreeIds = Helper.ReadJsonFile<Dictionary<string, int>>(Path.Combine(Constants.CurrentSavePath, "JsonAssets", $"ids-fruittrees.json"));
oldBigCraftableIds = Helper.ReadJsonFile<Dictionary<string, int>>(Path.Combine(Constants.CurrentSavePath, "JsonAssets", $"ids-big-craftables.json"));
oldHatIds = Helper.ReadJsonFile<Dictionary<string, int>>(Path.Combine(Constants.CurrentSavePath, "JsonAssets", $"ids-hats.json"));
}
else
Directory.CreateDirectory(Path.Combine(Constants.CurrentSavePath, "JsonAssets"));
if (oldObjectIds == null)
{
oldObjectIds = new Dictionary<string, int>();
oldCropIds = new Dictionary<string, int>();
oldFruitTreeIds = new Dictionary<string, int>();
oldBigCraftableIds = new Dictionary<string, int>();
oldHatIds = new Dictionary<string, int>();
}
objectIds = AssignIds("objects", StartingObjectId, objects.ToList<DataNeedsId>());
cropIds = AssignIds("crops", StartingCropId, crops.ToList<DataNeedsId>());
fruitTreeIds = AssignIds("fruittrees", StartingFruitTreeId, fruitTrees.ToList<DataNeedsId>());
bigCraftableIds = AssignIds("big-craftables", StartingBigCraftableId, bigCraftables.ToList<DataNeedsId>());
hatIds = AssignIds("hats", StartingHatId, hats.ToList<DataNeedsId>());
fixIdsEverywhere();
(api as Api).InvokeIdsAssigned();
Helper.Content.AssetEditors.Add(new ContentInjector());
foreach (var obj in objects)
{
if (obj.Recipe != null && obj.Recipe.IsDefault && !Game1.player.knowsRecipe(obj.Name))
{
if (obj.Category == ObjectData.Category_.Cooking)
{
Game1.player.cookingRecipes.Add(obj.Name, 0);
}
else
{
Game1.player.craftingRecipes.Add(obj.Name, 0);
}
}
}
foreach (var big in bigCraftables)
{
if (big.Recipe != null && big.Recipe.IsDefault && !Game1.player.knowsRecipe(big.Name))
{
Game1.player.craftingRecipes.Add(big.Name, 0);
}
}
}
private void afterSave(object sender, EventArgs args)
{
Helper.WriteJsonFile(Path.Combine(Constants.CurrentSavePath, "JsonAssets", $"ids-objects.json"), objectIds);
Helper.WriteJsonFile(Path.Combine(Constants.CurrentSavePath, "JsonAssets", $"ids-crops.json"), cropIds);
Helper.WriteJsonFile(Path.Combine(Constants.CurrentSavePath, "JsonAssets", $"ids-fruittrees.json"), fruitTreeIds);
Helper.WriteJsonFile(Path.Combine(Constants.CurrentSavePath, "JsonAssets", $"ids-big-craftables.json"), bigCraftableIds);
Helper.WriteJsonFile(Path.Combine(Constants.CurrentSavePath, "JsonAssets", $"ids-hats.json"), hatIds);
}
private IList<ObjectData> myRings = new List<ObjectData>();
private void invChanged(object sender, EventArgsInventoryChanged args)
{
IList<int> ringIds = new List<int>();
foreach (var ring in myRings)
ringIds.Add(ring.id);
for (int i = 0; i < Game1.player.items.Count; ++i)
{
var item = Game1.player.items[i];
if (item is StardewValley.Object obj && ringIds.Contains(obj.parentSheetIndex))
{
Log.trace($"Turning a ring-object of {obj.parentSheetIndex} into a proper ring");
Game1.player.items[i] = new StardewValley.Objects.Ring(obj.parentSheetIndex);
}
}
}
private const int StartingObjectId = 2000;
private const int StartingCropId = 100;
private const int StartingFruitTreeId = 10;
private const int StartingBigCraftableId = 300;
private const int StartingHatId = 50;
internal IList<ObjectData> objects = new List<ObjectData>();
internal IList<CropData> crops = new List<CropData>();
internal IList<FruitTreeData> fruitTrees = new List<FruitTreeData>();
internal IList<BigCraftableData> bigCraftables = new List<BigCraftableData>();
internal IList<HatData> hats = new List<HatData>();
internal IDictionary<string, int> objectIds;
internal IDictionary<string, int> cropIds;
internal IDictionary<string, int> fruitTreeIds;
internal IDictionary<string, int> bigCraftableIds;
internal IDictionary<string, int> hatIds;
internal IDictionary<string, int> oldObjectIds;
internal IDictionary<string, int> oldCropIds;
internal IDictionary<string, int> oldFruitTreeIds;
internal IDictionary<string, int> oldBigCraftableIds;
internal IDictionary<string, int> oldHatIds;
public int ResolveObjectId(object data)
{
if (data.GetType() == typeof(long))
return (int)(long)data;
else
{
if (!objectIds.ContainsKey((string)data))
Log.warn("Resolving for object " + data + " which we don't have?");
return objectIds[(string)data];
}
}
private Dictionary<string, int> AssignIds(string type, int starting, IList<DataNeedsId> data)
{
Dictionary<string, int> ids = new Dictionary<string, int>();
int currId = starting;
foreach (var d in data)
{
if (d.id == -1)
{
Log.trace("New ID: " + d.Name + " = " + currId);
ids.Add(d.Name, currId++);
if (type == "objects" && ((ObjectData)d).IsColored)
++currId;
d.id = ids[d.Name];
}
}
return ids;
}
private void clearIds(ref IDictionary<string, int> ids, List<DataNeedsId> objs)
{
ids = null;
foreach ( DataNeedsId obj in objs )
{
obj.id = -1;
}
}
private void fixIdsEverywhere()
{
fixItemList(Game1.player.items);
foreach ( var loc in Game1.locations )
fixLocation(loc);
}
private void fixLocation( GameLocation loc )
{
if (loc is FarmHouse fh)
{
if (fh.fridge != null && fh.fridge.Value.items != null)
fixItemList(fh.fridge.Value.items);
}
IList<Vector2> toRemove = new List<Vector2>();
foreach ( var tf in loc.terrainFeatures )
{
if (tf.Count > 0)
{
if (tf.Last().Value is HoeDirt hd)
{
if (hd.crop == null)
continue;
if (fixId(oldCropIds, cropIds, hd.crop.rowInSpriteSheet, Game1.content.Load<Dictionary<int, string>>("Data\\Crops")))
hd.crop = null;
else
{
try
{
var key = cropIds.Last(x => x.Value == hd.crop.rowInSpriteSheet).Key;
var c = crops.Last(x => x.Name == key);
if (c != null) // Non-JA crop
hd.crop.indexOfHarvest.Value = ResolveObjectId(c.Product);
}
catch { continue; }
}
}
else if (tf.Last().Value is FruitTree ft)
{
if (fixId(oldFruitTreeIds, fruitTreeIds, ft.treeType, Game1.content.Load<Dictionary<int, string>>("Data\\fruitTrees")))
toRemove.Add(tf.Last().Key);
else
{
var key = oldFruitTreeIds.Last(x => x.Value == ft.treeType).Key;
var ftt = fruitTrees.Last(x => x.Name == key);
if (ftt != null) // Non-JA fruit tree
ft.indexOfFruit.Value = ResolveObjectId(ftt.Product);
}
}
}
}
foreach (var rem in toRemove)
loc.terrainFeatures.Remove(rem);
toRemove.Clear();
foreach ( var obj in loc.objects )
{
if (obj.Count > 0)
{
if (obj.Last().Value is Chest chest)
{
fixItemList(chest.items);
}
else
{
if (!obj.Last().Value.bigCraftable)
{
if (fixId(oldObjectIds, objectIds, obj.Last().Value.parentSheetIndex, Game1.objectInformation))
toRemove.Add(obj.Last().Key);
}
else
{
if (fixId(oldBigCraftableIds, bigCraftableIds, obj.Last().Value.parentSheetIndex, Game1.bigCraftablesInformation))
toRemove.Add(obj.Last().Key);
}
}
if (obj.Last().Value.heldObject.Value != null)
{
if (fixId(oldObjectIds, objectIds, obj.Last().Value.heldObject.Last().parentSheetIndex, Game1.objectInformation))
obj.Last().Value.heldObject.Value = null;
}
}
}
foreach (var rem in toRemove)
loc.objects.Remove(rem);
if (loc is BuildableGameLocation buildLoc)
foreach (var building in buildLoc.buildings)
if (building.indoors != null)
fixLocation(building.indoors);
}
private void fixItemList( Netcode.NetObjectList< Item > items )
{
var Game1hats = Game1.content.Load<Dictionary<int, string>>("Data\\hats");
for ( int i = 0; i < items.Count; ++i )
{
var item = items[i];
if ( item is StardewValley.Object obj )
{
if (!obj.bigCraftable)
{
if (fixId(oldObjectIds, objectIds, obj.parentSheetIndex, Game1.objectInformation))
items[i] = null;
}
else
{
if (fixId(oldBigCraftableIds, bigCraftableIds, obj.parentSheetIndex, Game1.bigCraftablesInformation))
items[i] = null;
}
}
else if ( item is Hat hat )
{
if (fixId(oldHatIds, hatIds, hat.which, Game1hats))
items[i] = null;
}
}
}
// Return true if the item should be deleted, false otherwise.
// Only remove something if old has it but not new
private bool fixId(IDictionary<string, int> oldIds, IDictionary<string, int> newIds, Netcode.NetInt id, IDictionary<int, string> origData )
{
if (origData.ContainsKey(id))
return false;
if (oldIds.Values.Contains(id))
{
int id_ = id;
var key = oldIds.Last(x => x.Value == id_).Key;
if (newIds.ContainsKey(key))
{
id.Value = newIds[key];
return false;
}
else return true;
}
else return false;
}
}
}