-
Notifications
You must be signed in to change notification settings - Fork 4
/
LogoutTweaks.cs
207 lines (180 loc) · 6.45 KB
/
LogoutTweaks.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
using System;
using System.Collections.Generic;
using HarmonyLib;
using UnityEngine.SceneManagement;
using static VentureValheim.LogoutTweaks.StatusEffectManager;
namespace VentureValheim.LogoutTweaks
{
public class LogoutTweaks
{
private LogoutTweaks()
{
}
private static readonly LogoutTweaks _instance = new LogoutTweaks();
public static LogoutTweaks Instance
{
get => _instance;
}
private const string PLAYER_SAVE_KEY = "VV_LogoutData";
private readonly struct FileData
{
public List<StatusEffectData> StatusEffects { get; }
public FileData(List<StatusEffectData> effects)
{
StatusEffects = effects;
}
public FileData(string saveString)
{
StatusEffects = new List<StatusEffectData>();
if (saveString != null)
{
var data = saveString.Split(';');
if (data != null)
{
for (int lcv = 0; lcv < data.Length; lcv++)
{
var effect = new StatusEffectData(data[lcv]);
StatusEffects.Add(effect);
}
}
}
}
public override string ToString()
{
string saveString = "";
foreach (var effect in StatusEffects)
{
saveString += effect.ToString() + ";";
}
return saveString;
}
}
/// <summary>
/// Saves the effects data to the player custom data list.
/// </summary>
/// <param name="player"></param>
/// <param name="data"></param>
private void SaveData(ref Player player, FileData data)
{
if (player == null)
{
return;
}
if (player.m_customData.ContainsKey(PLAYER_SAVE_KEY))
{
player.m_customData[PLAYER_SAVE_KEY] = data.ToString();
}
else
{
player.m_customData.Add(PLAYER_SAVE_KEY, data.ToString());
}
}
/// <summary>
/// Clears the effects data from the player custom data list.
/// </summary>
/// <param name="player"></param>
private void ClearData(ref Player player)
{
if (player != null && player.m_customData.ContainsKey(PLAYER_SAVE_KEY))
{
player.m_customData[PLAYER_SAVE_KEY] = "";
}
}
/// <summary>
/// Attempts to load the player effects data from the player custom data list.
/// </summary>
/// <param name="player"></param>
/// <returns></returns>
private FileData? LoadData(ref Player player)
{
if (player != null && player.m_customData.ContainsKey(PLAYER_SAVE_KEY))
{
return new FileData(player.m_customData[PLAYER_SAVE_KEY]);
}
return null;
}
public static bool IsInTheMainScene()
{
return SceneManager.GetActiveScene().name.Equals("main");
}
#region Patches
[HarmonyPatch(typeof(Player), nameof(Player.Save))]
public static class Patch_Player_Save
{
private static void Prefix(Player __instance)
{
if (!IsInTheMainScene())
{
return;
}
try
{
List<StatusEffectData> data = new List<StatusEffectData>();
var effects = __instance.m_seman.GetStatusEffects();
if (effects == null)
{
LogoutTweaksPlugin.LogoutTweaksLogger.LogInfo("Unable to determine status effects, this can indicate a mod conflict.");
}
else
{
for (int lcv = 0; lcv < effects.Count; lcv++)
{
if (SupportedStatusEffect(effects[lcv].m_nameHash))
{
StatusEffectData effect = new StatusEffectData(effects[lcv]);
data.Add(effect);
}
}
}
FileData fileData = new FileData(data);
Instance.SaveData(ref __instance, fileData);
}
catch (Exception e)
{
LogoutTweaksPlugin.LogoutTweaksLogger.LogError("Error trying to parse and save extra data.");
LogoutTweaksPlugin.LogoutTweaksLogger.LogError(e);
}
}
}
[HarmonyPatch(typeof(Player), nameof(Player.Load))]
public static class Patch_Player_Load
{
private static void Postfix(Player __instance)
{
if (!IsInTheMainScene())
{
return;
}
var data = Instance.LoadData(ref __instance);
if (data == null)
{
return;
}
FileData logoutData = data.Value;
if (logoutData.StatusEffects != null)
{
var effects = logoutData.StatusEffects;
for (int lcv = 0; lcv < effects.Count; lcv++)
{
try
{
StatusEffect statusEffect = BuildStatusEffect(effects[lcv]);
if (statusEffect != null)
{
__instance.m_seman.AddStatusEffect(statusEffect);
}
}
catch (Exception e)
{
LogoutTweaksPlugin.LogoutTweaksLogger.LogWarning($"Status Effect {effects[lcv].Name} could not be restored.");
LogoutTweaksPlugin.LogoutTweaksLogger.LogDebug(e);
}
}
}
// Wipe data after loading
Instance.ClearData(ref __instance);
}
}
#endregion
}
}