forked from spacechase0/JsonAssets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentInjector.cs
367 lines (355 loc) · 16.8 KB
/
ContentInjector.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
using JsonAssets.Data;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI;
using StardewValley;
using System;
using System.Collections.Generic;
namespace JsonAssets
{
public class ContentInjector : IAssetEditor
{
public bool CanEdit<T>(IAssetInfo asset)
{
if (asset.AssetNameEquals("Data\\ObjectInformation"))
return true;
if (asset.AssetNameEquals("Data\\Crops"))
return true;
if (asset.AssetNameEquals("Data\\fruitTrees"))
return true;
if (asset.AssetNameEquals("Data\\CookingRecipes"))
return true;
if (asset.AssetNameEquals("Data\\CraftingRecipes"))
return true;
if (asset.AssetNameEquals("Data\\BigCraftablesInformation"))
return true;
if (asset.AssetNameEquals("Data\\hats"))
return true;
if (asset.AssetNameEquals("Data\\NPCGiftTastes"))
return true;
if (asset.AssetNameEquals("Maps\\springobjects"))
return true;
if (asset.AssetNameEquals("TileSheets\\crops"))
return true;
if (asset.AssetNameEquals("TileSheets\\fruitTrees"))
return true;
if (asset.AssetNameEquals("TileSheets\\Craftables") || asset.AssetNameEquals("TileSheets\\Craftables_indoor") || asset.AssetNameEquals("TileSheets\\Craftables_outdoor"))
return true; // _indoor/_outdoor for Seasonal Immersion compat
if (asset.AssetNameEquals("Characters\\Farmer\\hats"))
return true;
return false;
}
public void Edit<T>(IAssetData asset)
{
if (asset.AssetNameEquals("Data\\ObjectInformation"))
{
var data = asset.AsDictionary<int, string>().Data;
foreach (var obj in Mod.instance.objects)
{
try
{
Log.trace($"Injecting to objects: {obj.GetObjectId()}: {obj.GetObjectInformation()}");
data.Add(obj.GetObjectId(), obj.GetObjectInformation());
}
catch (Exception e)
{
Log.error("Exception injecting object information for " + obj.Name + ": " + e);
}
}
}
else if (asset.AssetNameEquals("Data\\Crops"))
{
var data = asset.AsDictionary<int, string>().Data;
foreach (var crop in Mod.instance.crops)
{
try
{
Log.trace($"Injecting to crops: {crop.GetSeedId()}: {crop.GetCropInformation()}");
data.Add(crop.GetSeedId(), crop.GetCropInformation());
}
catch (Exception e)
{
Log.error("Exception injecting crop for " + crop.Name + ": " + e);
}
}
}
else if (asset.AssetNameEquals("Data\\fruitTrees"))
{
var data = asset.AsDictionary<int, string>().Data;
foreach (var fruitTree in Mod.instance.fruitTrees)
{
try
{
Log.trace($"Injecting to fruit trees: {fruitTree.GetSaplingId()}: {fruitTree.GetFruitTreeInformation()}");
data.Add(fruitTree.GetSaplingId(), fruitTree.GetFruitTreeInformation());
}
catch (Exception e)
{
Log.error("Exception injecting fruit tree for " + fruitTree.Name + ": " + e);
}
}
}
else if (asset.AssetNameEquals("Data\\CookingRecipes"))
{
var data = asset.AsDictionary<string, string>().Data;
foreach (var obj in Mod.instance.objects)
{
try
{
if (obj.Recipe == null)
continue;
if (obj.Category != ObjectData.Category_.Cooking)
continue;
Log.trace($"Injecting to cooking recipes: {obj.Name}: {obj.Recipe.GetRecipeString(obj)}");
data.Add(obj.Name, obj.Recipe.GetRecipeString(obj));
}
catch (Exception e)
{
Log.error("Exception injecting cooking recipe for " + obj.Name + ": " + e);
}
}
}
else if (asset.AssetNameEquals("Data\\CraftingRecipes"))
{
var data = asset.AsDictionary<string, string>().Data;
foreach (var obj in Mod.instance.objects)
{
try
{
if (obj.Recipe == null)
continue;
if (obj.Category == ObjectData.Category_.Cooking)
continue;
Log.trace($"Injecting to crafting recipes: {obj.Name}: {obj.Recipe.GetRecipeString(obj)}");
data.Add(obj.Name, obj.Recipe.GetRecipeString(obj));
}
catch (Exception e)
{
Log.error("Exception injecting crafting recipe for " + obj.Name + ": " + e);
}
}
foreach (var big in Mod.instance.bigCraftables)
{
try
{
if (big.Recipe == null)
continue;
Log.trace($"Injecting to crafting recipes: {big.Name}: {big.Recipe.GetRecipeString(big)}");
data.Add(big.Name, big.Recipe.GetRecipeString(big));
}
catch (Exception e)
{
Log.error("Exception injecting crafting recipe for " + big.Name + ": " + e);
}
}
}
else if (asset.AssetNameEquals("Data\\BigCraftablesInformation"))
{
var data = asset.AsDictionary<int, string>().Data;
foreach (var big in Mod.instance.bigCraftables)
{
try
{
Log.trace($"Injecting to big craftables: {big.GetCraftableId()}: {big.GetCraftableInformation()}");
data.Add(big.GetCraftableId(), big.GetCraftableInformation());
}
catch (Exception e)
{
Log.error("Exception injecting object information for " + big.Name + ": " + e);
}
}
}
else if (asset.AssetNameEquals("Data\\hats"))
{
var data = asset.AsDictionary<int, string>().Data;
foreach (var hat in Mod.instance.hats)
{
try
{
Log.trace($"Injecting to hats: {hat.GetHatId()}: {hat.GetHatInformation()}");
data.Add(hat.GetHatId(), hat.GetHatInformation());
}
catch (Exception e)
{
Log.error("Exception injecting hat information for " + hat.Name + ": " + e);
}
}
}
else if (asset.AssetNameEquals("Data\\NPCGiftTastes"))
{
var data = asset.AsDictionary<string, string>().Data;
// TODO: This could be optimized from mn to... m + n?
// Basically, iterate through objects and create Dictionary<NPC name, GiftData[]>
// Iterate through objects, each section and add to dict[npc][approp. section]
// Point is, I'm doing this the lazy way right now
var newData = new Dictionary<string, string>(data);
foreach (var npc in data)
{
if (npc.Key.StartsWith("Universal_"))
continue;
string[] sections = npc.Value.Split('/');
if ( sections.Length != 11 )
{
Log.warn("Bad gift taste data for " + npc.Key + "!");
continue;
}
string loveStr = sections[0];
List<string> loveIds = new List<string>(sections[1].Split(' '));
string likeStr = sections[2];
List<string> likeIds = new List<string>(sections[3].Split(' '));
string dislikeStr = sections[4];
List<string> dislikeIds = new List<string>(sections[5].Split(' '));
string hateStr = sections[6];
List<string> hateIds = new List<string>(sections[7].Split(' '));
string neutralStr = sections[8];
List<string> neutralIds = new List<string>(sections[9].Split(' '));
foreach ( var obj in Mod.instance.objects )
{
if (obj.GiftTastes == null)
continue;
if (obj.GiftTastes.Love != null && obj.GiftTastes.Love.Contains(npc.Key))
loveIds.Add(obj.GetObjectId().ToString());
if (obj.GiftTastes.Like != null && obj.GiftTastes.Like.Contains(npc.Key))
likeIds.Add(obj.GetObjectId().ToString());
if (obj.GiftTastes.Neutral != null && obj.GiftTastes.Neutral.Contains(npc.Key))
neutralIds.Add(obj.GetObjectId().ToString());
if (obj.GiftTastes.Dislike != null && obj.GiftTastes.Dislike.Contains(npc.Key))
dislikeIds.Add(obj.GetObjectId().ToString());
if (obj.GiftTastes.Hate != null && obj.GiftTastes.Hate.Contains(npc.Key))
hateIds.Add(obj.GetObjectId().ToString());
}
string loveIdStr = string.Join(" ", loveIds);
string likeIdStr = string.Join(" ", likeIds);
string dislikeIdStr = string.Join(" ", dislikeIds);
string hateIdStr = string.Join(" ", hateIds);
string neutralIdStr = string.Join(" ", neutralIds);
newData[npc.Key] = $"{loveStr}/{loveIdStr}/{likeStr}/{likeIdStr}/{dislikeStr}/{dislikeIdStr}/{hateStr}/{hateIdStr}/{neutralStr}/{neutralIdStr}/ ";
Log.trace("Adding gift tastes for " + npc.Key + ": " + newData[ npc.Key ]);
}
asset.ReplaceWith(newData);
}
else if (asset.AssetNameEquals("Maps\\springobjects"))
{
var oldTex = asset.AsImage().Data;
Texture2D newTex = new Texture2D(Game1.graphics.GraphicsDevice, oldTex.Width, Math.Max(oldTex.Height, 4096));
asset.ReplaceWith(newTex);
asset.AsImage().PatchImage(oldTex);
foreach (var obj in Mod.instance.objects)
{
try
{
Log.trace($"Injecting {obj.Name} sprites @ {objectRect(obj.GetObjectId())}");
asset.AsImage().PatchImage(obj.texture, null, objectRect(obj.GetObjectId()));
if (obj.IsColored)
{
Log.trace($"Injecting {obj.Name} color sprites @ {objectRect(obj.GetObjectId() + 1)}");
asset.AsImage().PatchImage(obj.textureColor, null, objectRect(obj.GetObjectId() + 1));
}
}
catch ( Exception e )
{
Log.error("Exception injecting sprite for " + obj.Name + ": " + e);
}
}
}
else if (asset.AssetNameEquals("TileSheets\\crops"))
{
var oldTex = asset.AsImage().Data;
Texture2D newTex = new Texture2D(Game1.graphics.GraphicsDevice, oldTex.Width, Math.Max(oldTex.Height, 4096));
asset.ReplaceWith(newTex);
asset.AsImage().PatchImage(oldTex);
foreach (var crop in Mod.instance.crops)
{
try
{
Log.trace($"Injecting {crop.Name} crop images @ {cropRect(crop.GetCropSpriteIndex())}");
asset.AsImage().PatchImage(crop.texture, null, cropRect(crop.GetCropSpriteIndex()));
}
catch (Exception e)
{
Log.error("Exception injecting crop sprite for " + crop.Name + ": " + e);
}
}
}
else if (asset.AssetNameEquals("TileSheets\\fruitTrees"))
{
var oldTex = asset.AsImage().Data;
Texture2D newTex = new Texture2D(Game1.graphics.GraphicsDevice, oldTex.Width, Math.Max(oldTex.Height, 4096));
asset.ReplaceWith(newTex);
asset.AsImage().PatchImage(oldTex);
foreach (var fruitTree in Mod.instance.fruitTrees)
{
try
{
Log.trace($"Injecting {fruitTree.Name} fruit tree images @ {fruitTreeRect(fruitTree.GetFruitTreeIndex())}");
asset.AsImage().PatchImage(fruitTree.texture, null, fruitTreeRect(fruitTree.GetFruitTreeIndex()));
}
catch (Exception e)
{
Log.error("Exception injecting fruit tree sprite for " + fruitTree.Name + ": " + e);
}
}
}
else if (asset.AssetNameEquals("TileSheets\\Craftables") || asset.AssetNameEquals("TileSheets\\Craftables_indoor") || asset.AssetNameEquals("TileSheets\\Craftables_outdoor"))
{
var oldTex = asset.AsImage().Data;
Texture2D newTex = new Texture2D(Game1.graphics.GraphicsDevice, oldTex.Width, Math.Max(oldTex.Height, 4096));
asset.ReplaceWith(newTex);
asset.AsImage().PatchImage(oldTex);
Log.trace($"Big craftables are now ({oldTex.Width}, {Math.Max(oldTex.Height, 4096)})");
foreach (var big in Mod.instance.bigCraftables)
{
try
{
Log.trace($"Injecting {big.Name} sprites @ {bigCraftableRect(big.GetCraftableId())}");
asset.AsImage().PatchImage(big.texture, null, bigCraftableRect(big.GetCraftableId()));
}
catch (Exception e)
{
Log.error("Exception injecting sprite for " + big.Name + ": " + e);
}
}
}
else if (asset.AssetNameEquals("Characters\\Farmer\\hats"))
{
var oldTex = asset.AsImage().Data;
Texture2D newTex = new Texture2D(Game1.graphics.GraphicsDevice, oldTex.Width, Math.Max(oldTex.Height, 4096));
asset.ReplaceWith(newTex);
asset.AsImage().PatchImage(oldTex);
Log.trace($"Hats are now ({oldTex.Width}, {Math.Max(oldTex.Height, 4096)})");
foreach (var hat in Mod.instance.hats)
{
try
{
Log.trace($"Injecting {hat.Name} sprites @ {hatRect(hat.GetHatId())}");
asset.AsImage().PatchImage(hat.texture, null, hatRect(hat.GetHatId()));
}
catch (Exception e)
{
Log.error("Exception injecting sprite for " + hat.Name + ": " + e);
}
}
}
}
private Rectangle objectRect(int index)
{
return new Rectangle(index % 24 * 16, index / 24 * 16, 16, 16);
}
private Rectangle cropRect(int index)
{
return new Rectangle(index % 2 * 128, index / 2 * 32, 128, 32);
}
private Rectangle fruitTreeRect(int index)
{
return new Rectangle(0, index * 80, 432, 80);
}
private Rectangle bigCraftableRect(int index)
{
return new Rectangle(index % 8 * 16, index / 8 * 32, 16, 32);
}
private Rectangle hatRect(int index)
{
return new Rectangle(index % 12 * 20, index / 12 * 80, 20, 80);
}
}
}