-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoSoilDecay.cs
291 lines (243 loc) · 10.6 KB
/
NoSoilDecay.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
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using Storm.ExternalEvent;
using Storm.StardewValley.Event;
using Storm.StardewValley.Wrapper;
using Microsoft.Xna.Framework;
using System.IO;
using Storm.StardewValley.Accessor;
using System;
using System.Text.RegularExpressions;
namespace NoSoilDecay
{
[Mod]
public class NoSoilDecay : DiskResource
{
public JsonTerrainFeatures JsonTerrainFeatures { get; set; }
public int TimeLastSaved { get; set; }
public int TimeToSaveNext { get; set; }
public bool HasDeserializedToday { get; set; }
public string TileDecayJsonFilePath { get; set; }
public string CurrentGameId { get; set; }
public bool IsFirstRun { get; set; }
public int TimeOfDay { get; set; }
public NoSoilDecay()
{
JsonTerrainFeatures = new JsonTerrainFeatures();
IsFirstRun = false;
HasDeserializedToday = false;
}
[Subscribe]
public void Initialize(InitializeEvent @e)
{
}
[Subscribe]
public void DeserializeTerrainFeaturesAfterNewDay(PostNewDayEvent @e)
{
HasDeserializedToday = false;
}
[Subscribe]
public void HaveSoilDecaySlowerOnNewDay(PostUpdateEvent @e)
{
}
[Subscribe]
public void HaveSoilDecaySlowerOnNewDay(PreUpdateEvent @e)
{
if (@e.Root.HasLoadedGame)
{
// Load the saved state the first time exiting the farmhouse of the day.
if (!HasDeserializedToday && @e.Location.Name == "Farm")
{
var listOfAtts = new List<string>();
var eyeColour = @e.Root.Player.EyeColor.ToString();
var hairColour = @e.Root.Player.HairstyleColor.ToString();
var hair = @e.Root.Player.Hair.ToString();
string name = @e.Root.Player.Name;
var farmName = @e.Root.Player.FarmName;
listOfAtts.Add(eyeColour);
listOfAtts.Add(hairColour);
listOfAtts.Add(hair);
listOfAtts.Add(name);
listOfAtts.Add(farmName);
StringBuilder fileName = new StringBuilder();
foreach (var l in listOfAtts)
{
fileName.Append(l);
}
Regex rgx = new Regex("[^a-zA-Z0-9 -]");
var finalFileName = rgx.Replace(fileName.ToString(), "");
finalFileName = finalFileName.Replace(" ", string.Empty);
finalFileName = finalFileName.ToLower();
CurrentGameId = finalFileName;
TileDecayJsonFilePath = Path.Combine(ParentPathOnDisk + "\\NoSoilDecay\\SavedTiles\\", CurrentGameId + ".json");
CheckOrCreateJsonFile();
try
{
var locations = @e.Root.Locations;
for (int i = 0; i < @e.Root.Locations.Count; i++)
{
var loc = locations[i];
if (loc.Name == "Farm")
{
JsonTerrainFeatures.HoeDirtTile = DeserializeSavedTerrain();
if (JsonTerrainFeatures.HoeDirtTile != null)
{
var tFeats = @e.Location.TerrainFeatures;
var dirtToCreate = new List<Vector2>();
foreach (var j in JsonTerrainFeatures.HoeDirtTile)
{
var location = j.Key;
if (!tFeats.ContainsKey(location))
{
dirtToCreate.Add(location);
}
}
foreach (var d in dirtToCreate)
{
@e.Location.AddHoeDirtAt(d);
}
var tFeatsTest = @e.Location.TerrainFeatures;
foreach (var j in JsonTerrainFeatures.HoeDirtTile)
{
foreach (var d in dirtToCreate)
{
if (j.Key == d)
{
tFeatsTest[d].As<HoeDirtAccessor, HoeDirt>().Fertilizer = j.Value;
}
}
}
}
}
}
HasDeserializedToday = true;
}
catch (Exception exc)
{
}
}
}
// Save state every 10 in-game minutes
TimeOfDay = @e.Root.TimeOfDay;
if (TimeLastSaved == 0)
{
TimeLastSaved = TimeOfDay;
TimeToSaveNext = TimeLastSaved + 10;
}
if (TimeOfDay == 600)
{
TimeLastSaved = @e.Root.TimeOfDay;
TimeToSaveNext = TimeLastSaved + 10;
}
if (@TimeOfDay >= TimeToSaveNext && !IsTimeWonky())
{
TimeLastSaved = @TimeOfDay;
TimeToSaveNext = TimeLastSaved + 10;
//Perform the save.
if (@e.Location.Name == "Farm")
{
var tFeats = @e.Location.TerrainFeatures;
foreach (var k in tFeats.Keys)
{
if (tFeats[k].Is<HoeDirtAccessor>())
{
var temp = tFeats[k].As<HoeDirtAccessor, HoeDirt>();
if (temp.Crop == null)
{
// If HoeDirt doesnt contain a key that TerrainFeatures does, a HoeDirt has been created and needs to be added.
if (!JsonTerrainFeatures.HoeDirtTile.ContainsKey(k))
{
JsonTerrainFeatures.HoeDirtTile.Add(k, tFeats[k].As<HoeDirtAccessor, HoeDirt>().Fertilizer);
}
else if (JsonTerrainFeatures.HoeDirtTile != null)
{
// If the HoeDirt has a fertilizer, set its value here.
JsonTerrainFeatures.HoeDirtTile[k] = tFeats[k].As<HoeDirtAccessor, HoeDirt>().Fertilizer;
}
// If HoeDirt contains key that TerrainFeatures doesn't, player has destroyed the tile. Remove from HoeDirt.
var dirtToRemove = new List<Vector2>();
if (JsonTerrainFeatures.HoeDirtTile != null)
{
foreach (var h in JsonTerrainFeatures.HoeDirtTile)
{
if (tFeats[h.Key] == null)
{
dirtToRemove.Add(h.Key);
}
}
foreach (var h in dirtToRemove)
{
if (JsonTerrainFeatures.HoeDirtTile.ContainsKey(h))
{
JsonTerrainFeatures.HoeDirtTile.Remove(h);
}
}
}
}
}
}
if (JsonTerrainFeatures.HoeDirtTile != null)
{
SerializeSavedTerrain(JsonTerrainFeatures.HoeDirtTile);
}
}
}
}
private void SerializeSavedTerrain(Dictionary<Vector2, int> terrain)
{
var terrainLocation = Path.Combine(TileDecayJsonFilePath);
File.WriteAllBytes(terrainLocation, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(terrain)));
}
private Dictionary<Vector2, int> DeserializeSavedTerrain()
{
var terrainLocation = Path.Combine(TileDecayJsonFilePath);
var jsonTerrainFeatures = new JsonTerrainFeatures().HoeDirtTile;
jsonTerrainFeatures = JsonConvert.DeserializeObject<Dictionary<Vector2, int>>(Encoding.UTF8.GetString(File.ReadAllBytes(terrainLocation)));
if (jsonTerrainFeatures == null)
{
var newJTFeat = new JsonTerrainFeatures();
return newJTFeat.HoeDirtTile;
}
return jsonTerrainFeatures;
}
private void CheckOrCreateJsonFile()
{
if (!Directory.Exists(ParentPathOnDisk + "\\NoSoilDecay\\SavedTiles\\"))
{
Directory.CreateDirectory(ParentPathOnDisk + "\\NoSoilDecay\\SavedTiles\\");
}
if (!File.Exists(Path.Combine(TileDecayJsonFilePath)))
{
File.Create(Path.Combine(TileDecayJsonFilePath));
}
}
public bool IsTimeWonky()
{
//Bad method name - time is ALWAYS wonky in SV.
var time = TimeToSaveNext.ToString();
int hour = (int)(TimeToSaveNext.ToString()[0]);
time = time.Remove(0, 1);
var newTime = Int32.Parse(time);
if (newTime >= 60)
{
hour++;
newTime = 00;
string timeString = hour.ToString();
timeString += newTime.ToString();
TimeToSaveNext = Int32.Parse(timeString);
return true;
}
return false;
}
}
}
public class JsonTerrainFeatures
{
public Dictionary<Vector2, int> HoeDirtTile;
public long UniqueId { get; set; }
public JsonTerrainFeatures()
{
HoeDirtTile = new Dictionary<Vector2, int>();
}
}