forked from umaira05/save-file-total
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveFileTotalModule.cs
More file actions
195 lines (170 loc) · 9.72 KB
/
SaveFileTotalModule.cs
File metadata and controls
195 lines (170 loc) · 9.72 KB
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
using System;
using System.IO;
using System.Collections.Generic;
using Celeste.Mod.UI;
using YamlDotNet.Serialization;
namespace Celeste.Mod.SaveFileTotal {
public class SaveFileTotalModule : EverestModule {
public static SaveFileTotalModule Instance;
public SaveFileTotalModule() {
Instance = this;
}
public override Type SettingsType => typeof(SaveFileTotalModuleSettings);
public static SaveFileTotalModuleSettings Settings => (SaveFileTotalModuleSettings)Instance._Settings;
private bool SaveFileTotal_TryDelete(On.Celeste.SaveData.orig_TryDelete orig, int slot) {
if (slot == -1 && !File.Exists(UserIO.GetSaveFilePath(SaveData.GetFilename(-1)))) {
long debugTime = SaveData.Instance.Time;
int debugDeaths = SaveData.Instance.TotalDeaths;
if (orig(slot)) {
Settings.DeletedDebugTime += debugTime;
Settings.DeletedDebugDeaths += debugDeaths;
Logger.Log(LogLevel.Info, "SaveFileTotal", "Added deleted debug save data to totals");
return true;
}
return false;
}
List<string> stats = Settings.FetchSaveFileStats(slot);
long time = long.Parse(stats[1]);
int deaths = int.Parse(stats[2]);
if (orig(slot)) {
if (slot == -1) {
Settings.DeletedDebugTime += time;
Settings.DeletedDebugDeaths += deaths;
Logger.Log(LogLevel.Info, "SaveFileTotal", "Added deleted debug save data to totals");
return true;
}
Settings.DeletedSaveTime += time;
Settings.DeletedSaveDeaths += deaths;
Logger.Log(LogLevel.Info, "SaveFileTotal", "Added deleted save data to totals");
return true;
}
return false;
}
public override void Load() {
On.Celeste.SaveData.TryDelete += SaveFileTotal_TryDelete;
}
public override void Unload() {
On.Celeste.SaveData.TryDelete -= SaveFileTotal_TryDelete;
}
}
[SettingName("modoptions_savefiletotal")]
[SettingInGame(false)]
public class SaveFileTotalModuleSettings : EverestModuleSettings {
[SettingIgnore]
public long DeletedSaveTime { get; set; } = 0;
[SettingIgnore]
public int DeletedSaveDeaths { get; set; } = 0;
[SettingIgnore]
public long DeletedDebugTime { get; set; } = 0;
[SettingIgnore]
public int DeletedDebugDeaths { get; set; } = 0;
[YamlIgnore]
public int SaveFileSubmenu { get; set; } = 0;
public void CreateSaveFileSubmenuEntry(TextMenu menu, bool inGame) {
menu.Add(new TextMenu.Button("Total Time")
.Pressed(() => OuiGenericMenu.Goto<OuiSaveTimeSubmenu>(overworld => overworld.Goto<OuiModOptions>(), new object[0])));
menu.Add(new TextMenu.Button("Total Deaths")
.Pressed(() => OuiGenericMenu.Goto<OuiSaveDeathsSubmenu>(overworld => overworld.Goto<OuiModOptions>(), new object[0])));
if (DeletedDebugTime != 0 || DeletedDebugDeaths != 0 ||
(File.Exists(UserIO.GetSaveFilePath(SaveData.GetFilename(-1))) && (FetchSaveFileStats(-1)[1] != "0" || FetchSaveFileStats(-1)[2] != "0"))) {
menu.Add(new TextMenu.Button("Debug Stats")
.Pressed(() => OuiGenericMenu.Goto<OuiDebugSaveStatsSubmenu>(overworld => overworld.Goto<OuiModOptions>(), new object[0])));
}
}
public string SfTimeToStr(long time) {
TimeSpan ts = TimeSpan.FromMilliseconds(time / 10000);
return ((int)ts.TotalHours).ToString("N0") + ts.ToString(@"\:mm\:ss\.fff");
}
public List<string> FetchSaveFileStats(int slot) {
// default empty stats in case the vanilla file no longer exists
List<string> stats = new List<string> { "", "0", "0" };
string saveFilePath = UserIO.GetSaveFilePath(SaveData.GetFilename(slot));
// the vanilla file may not exist, return empty stats in this case
if (!File.Exists(saveFilePath)) {
Logger.Log(LogLevel.Info, "SaveFileTotal", $"Vanilla file for slot {slot} did not exist when deleting, ignoring stats");
return stats;
}
foreach (string line in File.ReadLines(saveFilePath)) {
if (line.Contains("<Name>"))
stats[0] = line.Substring(line.IndexOf(">") + 1, line.IndexOf("</") - line.IndexOf(">") - 1);
else if (line.Contains("<Time>"))
stats[1] = line.Substring(line.IndexOf(">") + 1, line.IndexOf("</") - line.IndexOf(">") - 1);
else if (line.Contains("<TotalDeaths>"))
stats[2] = line.Substring(line.IndexOf(">") + 1, line.IndexOf("</") - line.IndexOf(">") - 1);
else if (!stats.Contains("")) break;
}
return stats;
}
public List<int> FetchSaveFileIndexes() {
string saveFilePath = UserIO.GetSaveFilePath();
List<int> saveFileIndexes = new List<int>();
if (Directory.Exists(saveFilePath)) {
foreach (string filePath in Directory.GetFiles(saveFilePath, "*.celeste")) {
string fileName = Path.GetFileName(filePath);
if (int.TryParse(fileName.Substring(0, fileName.Length - 8), out int fileIndex))
saveFileIndexes.Add(fileIndex);
}
}
saveFileIndexes.Sort();
return saveFileIndexes;
}
}
class OuiSaveTimeSubmenu : OuiGenericMenu, OuiModOptions.ISubmenu {
public override string MenuName => "Total Time";
protected override void addOptionsToMenu(TextMenu menu) {
List<int> saveFileIndexes = SaveFileTotalModule.Settings.FetchSaveFileIndexes();
ReadSaveFiles(ref menu, ref saveFileIndexes, saveFileIndexes.Count - 1, SaveFileTotalModule.Settings.DeletedSaveTime);
}
private void ReadSaveFiles(ref TextMenu menu, ref List<int> saveFileIndexes, int index, long sumOfTimes) {
if (index == -1) {
menu.Add(new TextMenu.Button("Total save time: " + SaveFileTotalModule.Settings.SfTimeToStr(sumOfTimes)));
menu.Add(new TextMenu.Button("Deleted save time: " + SaveFileTotalModule.Settings.SfTimeToStr(SaveFileTotalModule.Settings.DeletedSaveTime)));
return;
}
List<string> stats = SaveFileTotalModule.Settings.FetchSaveFileStats(saveFileIndexes[index]);
string filename = stats[0];
long time = long.Parse(stats[1]);
ReadSaveFiles(ref menu, ref saveFileIndexes, index - 1, sumOfTimes + time);
string fileNum = SaveData.GetFilename(saveFileIndexes[index]);
menu.Add(new TextMenu.Button("File " + fileNum + ": " + filename + ", " + SaveFileTotalModule.Settings.SfTimeToStr(time)));
}
}
class OuiSaveDeathsSubmenu : OuiGenericMenu, OuiModOptions.ISubmenu {
public override string MenuName => "Total Deaths";
protected override void addOptionsToMenu(TextMenu menu) {
List<int> saveFileIndexes = SaveFileTotalModule.Settings.FetchSaveFileIndexes();
ReadSaveFiles(ref menu, ref saveFileIndexes, saveFileIndexes.Count - 1, SaveFileTotalModule.Settings.DeletedSaveDeaths);
}
private void ReadSaveFiles(ref TextMenu menu, ref List<int> saveFileIndexes, int index, int sumOfDeaths) {
if (index == -1) {
menu.Add(new TextMenu.Button("Total save deaths: " + sumOfDeaths.ToString("N0")));
menu.Add(new TextMenu.Button("Deleted save deaths: " + SaveFileTotalModule.Settings.DeletedSaveDeaths.ToString("N0")));
return;
}
List<string> stats = SaveFileTotalModule.Settings.FetchSaveFileStats(saveFileIndexes[index]);
string filename = stats[0];
int deaths = int.Parse(stats[2]);
ReadSaveFiles(ref menu, ref saveFileIndexes, index - 1, sumOfDeaths + deaths);
string fileNum = SaveData.GetFilename(saveFileIndexes[index]);
menu.Add(new TextMenu.Button("File " + fileNum + ": " + filename + ", " + deaths.ToString("N0")));
}
}
class OuiDebugSaveStatsSubmenu : OuiGenericMenu, OuiModOptions.ISubmenu {
public override string MenuName => "Debug Stats";
protected override void addOptionsToMenu(TextMenu menu) {
long time = 0;
int deaths = 0;
if (File.Exists(UserIO.GetSaveFilePath(SaveData.GetFilename(-1)))) {
List<string> stats = SaveFileTotalModule.Settings.FetchSaveFileStats(-1);
time = long.Parse(stats[1]);
deaths = int.Parse(stats[2]);
}
menu.Add(new TextMenu.Button("Total debug save time: " + SaveFileTotalModule.Settings.SfTimeToStr(time + SaveFileTotalModule.Settings.DeletedDebugTime)));
menu.Add(new TextMenu.Button("Deleted debug save time: " + SaveFileTotalModule.Settings.SfTimeToStr(SaveFileTotalModule.Settings.DeletedDebugTime)));
menu.Add(new TextMenu.Button("Debug save time: " + SaveFileTotalModule.Settings.SfTimeToStr(time)));
menu.Add(new TextMenu.Button("Total debug save deaths: " + (deaths + SaveFileTotalModule.Settings.DeletedDebugDeaths).ToString("N0")));
menu.Add(new TextMenu.Button("Deleted debug save deaths: " + SaveFileTotalModule.Settings.DeletedDebugDeaths.ToString("N0")));
menu.Add(new TextMenu.Button("Debug save deaths: " + deaths.ToString("N0")));
}
}
}