-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChildDat.cs
281 lines (241 loc) · 11 KB
/
ChildDat.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
/*
* This software is licensed under the MIT License
* https://github.com/GStefanowich/SDV-NFFTT
*
* 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.Concurrent;
using System.IO;
using System.Linq;
using System.Threading;
using StardewModdingAPI;
using StardewValley;
using StardewValley.Characters;
using StardewValley.Locations;
namespace NotFarFromTheTree {
public class ChildDat {
public static readonly ConcurrentDictionary<Child, Lazy<ChildDat>> CHILDREN_PARENT = new ConcurrentDictionary<Child, Lazy<ChildDat>>();
private Child instance;
private AnimatedSprite Sprite {
get => this.instance.Sprite;
set => this.instance.Sprite = value;
}
private string name;
private NPC parent;
private ModDataDictionary Data => this.instance.modData;
/*
* Age Checks
*/
private int Age => this.instance.Age;
private bool IsBaby => !this.IsToddler;
private bool IsToddler => this.Age == Child.toddler;
private string AgeString => this.IsToddler ? "Toddler" : "Baby";
/*
* Gender Checks
*/
private int Gender => this.instance.Gender;
private bool IsMale => this.Gender == NPC.male;
private bool IsDarkSkin => this.instance.darkSkinned.Value;
/*
* Parent Checks
*/
private bool IsEmpty => this.IsOrphan || string.IsNullOrWhiteSpace(this.ChildName);
private bool IsOrphan => string.IsNullOrWhiteSpace(this.ParentName) || !this.HasSprite;
private bool HasSprite;
/*
* Names
*/
public string ChildName {
get => this.name;
set {
this.name = value;
if (this.instance == null || this.instance.Name != value)
this.instance = ChildDat.GetByName(value);
}
}
public string ParentName {
get => this.parent?.getName();
set {
if (string.IsNullOrWhiteSpace(value)) {
this.parent = null;
} else {
NPC npc = Game1.getCharacterFromName<NPC>(value, true);
if (npc != null) {
// If the new value is different than the current value
bool updated = !npc.getName().Equals(this.ParentName);
// Set the new parent value
this.parent = npc;
if (this.parent != null && updated) {
// Load the new sprite to use
string spritePath = this.GetAssetPath(npc.getName());
this.HasSprite = Assets.LoadSprite(Assets.Wrap(spritePath), spritePath);
// Refresh the sprite
this.CleanSprite();
}
}
}
}
}
public ChildDat() {}
private ChildDat( Child child ) {
this.instance = child;
this.ChildName = child.Name;
this.ParentName = "";
}
/*
* Asset Handling Methods
*/
public string GetAssetPath() => this.IsOrphan ? this.GetDefaultAssetPath() : this.GetAssetPath(this.ParentName);
public string GetAssetPath(string parentName) => $"{parentName}{Path.DirectorySeparatorChar}{this.GetDefaultAssetPath()}";
public string GetDefaultAssetPath() => $"{this.AgeString}{(this.IsMale || this.IsBaby ? "" : "_girl") + (this.IsDarkSkin ? "_dark" : "")}";
public string GetAssetName() => this.IsOrphan ? this.GetDefaultAssetName() : Assets.Wrap(this.GetAssetPath());
public string GetDefaultAssetName() => $"Characters{Path.DirectorySeparatorChar}{this.GetDefaultAssetPath()}";
/*
* Current Sprite Handling
*/
public void CleanSprite() {
if (this.instance == null)
return;
// Get the name of the asset to use
string asset = this.GetAssetName();
if (this.Sprite == null)
this.Sprite = new AnimatedSprite(asset, 0, 22, 16);
if (this.Age >= 3) {
this.Sprite.textureName.Value = asset;
this.Sprite.SpriteWidth = 16;
this.Sprite.SpriteHeight = 32;
this.Sprite.currentFrame = 0;
this.instance.HideShadow = false;
} else {
this.Sprite.textureName.Value = asset;
this.Sprite.SpriteWidth = 22;
this.Sprite.SpriteHeight = this.instance.Age == 1 ? 32 : 16;
this.Sprite.currentFrame = 0;
if (this.Age == 1)
this.Sprite.currentFrame = 4;
else if (this.Age == 2)
this.Sprite.currentFrame = 32;
this.instance.HideShadow = true;
}
this.Sprite.UpdateSourceRect();
this.instance.Breather = false;
}
/*
* Information Handling
*/
public bool Save( bool notify = true ) {
bool saveSuccess = false;
// Save the parent of the child
ChildDat.CHILDREN_PARENT[this.instance] = new Lazy<ChildDat>(() => this);
if (Context.IsMainPlayer) {
this.Data[this.GetSaveKey()] = this.ParentName;
saveSuccess = true;
}
// Update other players in multiplayer
if (Context.IsMultiplayer && notify && this.SendToPeers())
saveSuccess = true;
return saveSuccess;
}
public string GetSaveKey() {
return $"NFFTT/Child/{this.ChildName}";
}
public bool SendToPeers() {
bool sent = false;
foreach (IMultiplayerPeer peer in ModEntry.MOD_HELPER.Multiplayer.GetConnectedPlayers()) {
// If peer is the host, have them update the save
if ((!Context.IsMainPlayer) && peer.IsHost && (peer.GetMod(ModEntry.MOD_ID) != null))
sent = true;
// Only send if host or to host
if (Context.IsMainPlayer || (sent && peer.IsHost)) {
ModEntry.MOD_HELPER.Multiplayer.SendMessage(this, "ChildUpdate", modIDs: new[] {
ModEntry.MOD_ID
});
}
}
return sent;
}
/*
* Find a child in the game
*/
public static Child GetByName( string name ) {
// Check primary farmhouse
if (Game1.getLocationFromName("FarmHouse") is FarmHouse farmHouse)
foreach (NPC character in farmHouse.characters.Where(character => (character is Child) && character.getName().Equals(name)))
return (Child) character;
Farm farm;
if ((farm = Game1.getFarm()) == null)
return null;
// Check farmhand farmhouses
Child child = null;
foreach (NPC character in farm.buildings.Where(building => building.indoors.Value != null).SelectMany(building => building.indoors.Value.characters)) {
if ((child = character as Child) != null && child.getName().Equals(name))
break;
}
return child;
}
public void LoadParent() {
if (this.Data.TryGetValue(this.GetSaveKey(), out string parentName) && parentName != null) {
this.ParentName = parentName;
} else if (Context.IsMainPlayer) {
// Attempt to read the value from the GAME DATA
Farmer farmer = Game1.getFarmerMaybeOffline(this.instance.idOfParent.Value);
NPC spouse = farmer?.getSpouse();
if (spouse != null)
this.ParentName = spouse.getName();
}
}
public static ChildDat LoadParent( Child child ) {
ChildDat data = new ChildDat(child);
if (Context.IsMainPlayer) {
// Player Must be the HOST to Read from SAVE DATA
// (Other players must be synced from the HOST to the CACHE ARRAY)
if (data.Data.TryGetValue(data.GetSaveKey(), out string parentName) && parentName != null)
data.ParentName = parentName;
else {
// Attempt to read the value from the GAME DATA
Farmer farmer = Game1.getFarmerMaybeOffline(child.idOfParent.Value);
NPC spouse = farmer?.getSpouse();
if (!string.IsNullOrWhiteSpace(spouse?.getName()))
data.ParentName = spouse.getName();
}
}
return data;
}
public static ChildDat Of( Child child ) {
return ChildDat.Of(child, null as NPC);
}
public static ChildDat Of( Child child, NPC parent ) {
return ChildDat.Of(child, parent?.getName());
}
public static ChildDat Of( Child child, string parent ) {
ChildDat data = ChildDat.CHILDREN_PARENT.GetOrAdd(child, ChildDat.Lazy(child)).Value;
if (!string.IsNullOrWhiteSpace(parent))
data.ParentName = parent;
else if (string.IsNullOrWhiteSpace(data.ParentName))
data.LoadParent();
return data;
}
private static Lazy<ChildDat> Lazy( Child child ) {
return new Lazy<ChildDat>(() => ChildDat.LoadParent(child), LazyThreadSafetyMode.ExecutionAndPublication);
}
}
}