-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfiniteBackupsSettings.cs
206 lines (182 loc) · 8.92 KB
/
InfiniteBackupsSettings.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Celeste.Mod.InfiniteBackups.Utils;
using Microsoft.Xna.Framework;
using MonoMod.Utils;
namespace Celeste.Mod.InfiniteBackups {
[SettingName(DialogId.ModName)]
public class InfiniteBackupsSettings : EverestModuleSettings {
public bool Enabled { get; set; } = true;
#if DEBUG
public bool LogToIngameConsole { get; set; } = true;
#endif
public bool BackupAsZipFile { get; set; } = true;
public bool AutoDeleteOldBackups { get; set; } = false;
public int DeleteBackupsOlderThanDays { get; set; } = -1;
public int DeleteBackupsAfterAmount { get; set; } = -1;
private Dictionary<string, TextMenu.Item> menuItems = new Dictionary<string, TextMenu.Item>(StringComparer.OrdinalIgnoreCase);
public void CreateEnabledEntry(TextMenu textMenu, bool inGame) {
TextMenu.Item item = new TextMenu.OnOff(DialogId.Options.Enabled.DialogClean(), Enabled)
.Change(value => {
Enabled = value;
if (Enabled) {
InfiniteBackupsModule.Instance.Load();
} else {
InfiniteBackupsModule.Instance.Unload();
}
RefreshItemsStates();
});
textMenu.Add(item);
menuItems.Add(DialogId.Options.Enabled, item);
}
#if DEBUG
public void CreateLogToIngameConsoleEntry(TextMenu textMenu, bool inGame) {
TextMenu.Item item = new TextMenu.OnOff("Log to ingame console [DEBUG]", LogToIngameConsole)
.Change(value => {
LogToIngameConsole = value;
});
textMenu.Add(item);
}
#endif
public void CreateBackupAsZipFileEntry(TextMenu textMenu, bool inGame) {
TextMenu.Item item = new TextMenu.OnOff(DialogId.Options.BackupAsZipFile.DialogClean(), BackupAsZipFile)
.Change(value => {
BackupAsZipFile = value;
});
textMenu.Add(item);
menuItems.Add(DialogId.Options.BackupAsZipFile, item);
}
public void CreateAutoDeleteOldBackupsEntry(TextMenu textMenu, bool inGame) {
TextMenu.Item item = new TextMenu.OnOff(DialogId.Options.AutoDeleteOldBackups.DialogClean(), AutoDeleteOldBackups)
.Change(value => {
AutoDeleteOldBackups = value;
RefreshItemsStates();
});
textMenu.Add(item);
menuItems.Add(DialogId.Options.AutoDeleteOldBackups, item);
}
public void CreateDeleteBackupsOlderThanDaysEntry(TextMenu textMenu, bool inGame) {
BetterIntSlider item = new BetterIntSlider(DialogId.Options.DeleteBackupsOlderThanDays.DialogClean(),
i => i == -1
? DialogId.OptionValues.Disabled.DialogClean()
: string.Format(DialogId.OptionValues.Days.DialogGet(), i),
-1,
100,
DeleteBackupsOlderThanDays);
item.Change(value => {
// skip 0 days
if (value > -1 && value < 1) {
item.Index = item.LastDir > 0 ? 1 : -1;
value = item.Index;
}
DeleteBackupsOlderThanDays = value;
});
item.ValueWidthFunc = () => {
float width = 0;
width = Math.Max(width, ActiveFont.Measure(item.ValuesFunc(item.Min)).X);
width = Math.Max(width, ActiveFont.Measure(item.ValuesFunc(item.Max)).X);
return width;
};
textMenu.Add(item);
menuItems.Add(DialogId.Options.DeleteBackupsOlderThanDays, item);
}
public void CreateDeleteBackupsAfterAmountEntry(TextMenu textMenu, bool inGame) {
BetterIntSlider item = new BetterIntSlider(DialogId.Options.DeleteBackupsAfterAmount.DialogClean(),
i => i == -1 ? DialogId.OptionValues.Disabled.DialogClean() : $"{i}",
-1,
500,
DeleteBackupsAfterAmount);
item.Change(value => {
// skip 0
if (value > -1 && value < 1) {
item.Index = item.LastDir > 0 ? 1 : -1;
value = item.Index;
}
DeleteBackupsAfterAmount = value;
});
item.ValueWidthFunc = () => {
float width = 0;
width = Math.Max(width, ActiveFont.Measure(item.ValuesFunc(item.Min)).X);
width = Math.Max(width, ActiveFont.Measure(item.ValuesFunc(item.Max)).X);
return width;
};
textMenu.Add(item);
menuItems.Add(DialogId.Options.DeleteBackupsAfterAmount, item);
}
public void CreateOpenBackupFolderEntry(TextMenu textMenu, bool inGame) {
TextMenu.Item item = new TextMenu.Button(DialogId.Options.OpenBackupFolder.DialogClean())
.Pressed(() => {
try {
string backupPath = Modules.InfiniteBackups.BackupPath;
if (!Directory.Exists(backupPath)) {
Directory.CreateDirectory(backupPath);
}
Process.Start(backupPath);
} catch (Exception err) {
LogUtil.Log("Open backup folder failed!", LogLevel.Warn);
err.LogDetailed();
TextMenu.Item openBackupFolder = menuItems[DialogId.Options.OpenBackupFolder];
TextMenu.Item openFailed = menuItems[DialogId.Subtext.OpenBackupFolderFailed];
if (!textMenu.Items.Contains(openFailed)) {
textMenu.Insert(textMenu.IndexOf(openBackupFolder) + 1, openFailed);
}
}
});
textMenu.Add(item);
menuItems.Add(DialogId.Options.OpenBackupFolder, item);
// split the string into multiple lines to prevent off-screen menus caused by long path
string[] descriptionLines = string.Format(DialogId.Subtext.BackupLocation.DialogGet(), Path.GetFullPath(Modules.InfiniteBackups.BackupPath)).SplitIntoFixedLength(50);
TextMenuExt.EaseInSubHeaderExt backupLocationSubtext = new TextMenuExt.EaseInSubHeaderExt(string.Join("\n", descriptionLines), false, textMenu) {
TextColor = Color.Gray
};
// increase the height and make it vertical align
backupLocationSubtext.HeightExtra = (backupLocationSubtext.Title.Split('\n').Length - 1) * ActiveFont.LineHeight * 0.6f;
backupLocationSubtext.Offset = new Vector2(0f, -Math.Max(0f, -16f + backupLocationSubtext.HeightExtra) + textMenu.ItemSpacing);
textMenu.Add(backupLocationSubtext);
TextMenuExt.EaseInSubHeaderExt openFailedSubtext = new TextMenuExt.EaseInSubHeaderExt(DialogId.Subtext.OpenBackupFolderFailed.DialogClean(), false, textMenu) {
TextColor = Color.OrangeRed,
HeightExtra = 0f
};
item.OnEnter += delegate {
openFailedSubtext.FadeVisible = true;
backupLocationSubtext.FadeVisible = true;
};
item.OnLeave += delegate {
openFailedSubtext.FadeVisible = false;
backupLocationSubtext.FadeVisible = false;
};
menuItems.Add(DialogId.Subtext.BackupLocation, backupLocationSubtext);
menuItems.Add(DialogId.Subtext.OpenBackupFolderFailed, openFailedSubtext);
}
public void CreateMenu(TextMenu menu, bool inGame) {
menuItems.Clear();
CreateEnabledEntry(menu, inGame);
#if DEBUG
CreateLogToIngameConsoleEntry(menu, inGame);
#endif
CreateBackupAsZipFileEntry(menu, inGame);
CreateAutoDeleteOldBackupsEntry(menu, inGame);
CreateDeleteBackupsOlderThanDaysEntry(menu, inGame);
CreateDeleteBackupsAfterAmountEntry(menu, inGame);
CreateOpenBackupFolderEntry(menu, inGame);
RefreshItemsStates();
}
private void RefreshItemsStates() {
TextMenu.Item[] visibilitySkipItems = {
menuItems[DialogId.Options.Enabled],
menuItems[DialogId.Options.OpenBackupFolder]
};
foreach (TextMenu.Item menuItem in menuItems.Values) {
if (visibilitySkipItems.Contains(menuItem)) {
continue;
}
menuItem.Visible = Enabled;
}
menuItems[DialogId.Options.DeleteBackupsOlderThanDays].Disabled = !AutoDeleteOldBackups;
menuItems[DialogId.Options.DeleteBackupsAfterAmount].Disabled = !AutoDeleteOldBackups;
}
}
}