forked from executionByFork/MCC_Mod_Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cs
303 lines (278 loc) · 11.5 KB
/
Config.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using Newtonsoft.Json;
using System.Linq;
using System.IO.Compression;
namespace MCC_Mod_Manager
{
public class mainCfg
{
public string version = Config.version;
public string MCC_version;
public string MCC_home;
public string backup_dir;
public string modpack_dir;
public bool deleteOldBaks;
public Dictionary<string, Dictionary<string,string>> patched = new Dictionary<string, Dictionary<string, string>>();
}
static class Config
{
public static readonly string version = "v0.7";
private static readonly string _cfgLocation = @".\MCC_Mod_Manager.cfg";
private static readonly string _bakcfgName = @"\backups.cfg";
// UI elements
public static string dirtyPadding = " ";
public static readonly Point delBtnPoint = new Point(0, 3);
public static readonly Point delBtnPointAlt = new Point(0, 15);
public static readonly Point sourceTextBoxPoint = new Point(20, 1);
public static readonly Point sourceBtnPoint = new Point(203, 0);
public static readonly Point origTextBoxPoint = new Point(20, 26);
public static readonly Point origBtnPoint = new Point(203, 25);
public static readonly Point arrowPoint = new Point(245, -5);
public static readonly Point arrowPointAlt = new Point(245, 7);
public static readonly Point destTextBoxPoint = new Point(278, 1);
public static readonly Point destTextBoxPointAlt = new Point(278, 14);
public static readonly Point destBtnPoint = new Point(461, 0);
public static readonly Point destBtnPointAlt = new Point(461, 13);
public static readonly Font btnFont = new Font("Lucida Console", 10, FontStyle.Regular);
public static readonly Font arrowFont = new Font("Reem Kufi", 12, FontStyle.Bold);
public static Form1 form1; // this is set on form load
private static mainCfg _cfg = new mainCfg(); // this is set on form load
public static bool fullBakPath = false;
public static string MCC_version {
get {
return _cfg.MCC_version;
}
set {
_cfg.MCC_version = value;
}
}
public static string MCC_home {
get {
return _cfg.MCC_home;
}
set {
_cfg.MCC_home = value;
}
}
public static string backup_dir {
get {
return _cfg.backup_dir;
}
set {
_cfg.backup_dir = value;
}
}
public static string backupCfg {
get {
return _cfg.backup_dir + _bakcfgName;
}
}
public static string modpack_dir {
get {
return _cfg.modpack_dir;
}
set {
_cfg.modpack_dir = value;
}
}
public static bool deleteOldBaks {
get {
return _cfg.deleteOldBaks;
}
set {
_cfg.deleteOldBaks = value;
}
}
public static Dictionary<string, Dictionary<string, string>> patched {
get {
return _cfg.patched;
}
set {
_cfg.patched = value;
}
}
public static List<string> getEnabledModpacks()
{
List<string> list = new List<string>();
foreach (KeyValuePair<string, Dictionary<string, string>> modpack in patched) {
list.Add(modpack.Key);
}
return list;
}
public static bool isPatched(string modpackName)
{
try {
return patched.ContainsKey(modpackName);
} catch (NullReferenceException) {
return false;
}
}
public static bool addPatched(string modpackName)
{
Dictionary<string, string> modfiles = new Dictionary<string, string>();
modpackCfg mCfg = Modpacks.getModpackConfig(modpackName);
using (ZipArchive archive = ZipFile.OpenRead(Config.modpack_dir + @"\" + modpackName + ".zip")) {
if (mCfg == null) {
form1.showMsg("Cannot set state to enabled. The file '" + modpackName + ".zip' is either not a compatible modpack or the config is corrupted.", "Error");
return false;
}
List<string> patched = new List<string>(); // track patched files in case of failure mid patch
foreach (modpackEntry entry in mCfg.entries) {
modfiles[entry.dest] = Modpacks.getMD5(Modpacks.expandPath(entry.dest));
}
}
patched[modpackName] = modfiles;
return true;
}
public static void rmPatched(string modpackName)
{
patched.Remove(modpackName);
}
public static string getCurrentBuild()
{
return IO.readFirstLine(MCC_home + @"\build_tag.txt");
}
public static void doResetApp()
{
patched = new Dictionary<string, Dictionary<string, string>>();
MCC_version = getCurrentBuild();
saveCfg();
Modpacks.loadModpacks();
if (!Backups.deleteAll(true)) {
form1.showMsg("There was an issue deleting at least one backup. Please delete these in the Backups tab to avoid restoring an old " +
"version of the file in the future.", "Error");
}
Backups.loadBackups();
}
public static bool createDefaultCfg()
{
_cfg = new mainCfg();
// default values declared here so that mainCfg class does not implicitly set defaults and bypass warning triggers
MCC_home = @"C:\Program Files (x86)\Steam\steamapps\common\Halo The Master Chief Collection";
MCC_version = getCurrentBuild(); // sets MCC_version to null if not found
backup_dir = @".\backups";
modpack_dir = @".\modpacks";
deleteOldBaks = false;
patched = new Dictionary<string, Dictionary<string, string>>();
saveCfg();
form1.showMsg("A default configuration file has been created. Please review and update it as needed.", "Info");
return true;
}
public static void saveCfg()
{
string json = JsonConvert.SerializeObject(_cfg, Formatting.Indented);
using (FileStream fs = File.Create(_cfgLocation)) {
byte[] info = new UTF8Encoding(true).GetBytes(json);
fs.Write(info, 0, info.Length);
}
}
private static int readCfg()
{
string json = File.ReadAllText(_cfgLocation);
try {
mainCfg values = JsonConvert.DeserializeObject<mainCfg>(json);
// TODO: implement old config version handling
if (String.IsNullOrEmpty(values.version)) {
return 2;
}
MCC_home = values.MCC_home;
MCC_version = String.IsNullOrEmpty(values.MCC_version) ? getCurrentBuild() : values.MCC_version;
backup_dir = values.backup_dir;
modpack_dir = values.modpack_dir;
deleteOldBaks = values.deleteOldBaks;
patched = values.patched;
} catch (JsonSerializationException) {
return 1;
} catch (JsonReaderException) {
return 1;
} catch (KeyNotFoundException) {
return 1;
}
if (patched == null) {
return 2;
}
return 0;
}
public static int loadCfg()
{
bool stabilize = false;
bool needsStabilize = false;
if (!File.Exists(_cfgLocation)) {
createDefaultCfg();
} else {
int r = readCfg();
if (r == 1) {
DialogResult ans = form1.showMsg("Your configuration has formatting errors, would you like to overwrite it with a default config?", "Question");
if (ans == DialogResult.No) {
return 3;
}
createDefaultCfg();
} else if (r == 2) {
DialogResult ans = form1.showMsg("Your config file is using an old format, would you like to overwrite it with a default config?", "Question");
if (ans == DialogResult.No) {
return 3;
}
createDefaultCfg();
} else {
// check if game was updated
if (MCC_version != getCurrentBuild()) {
DialogResult ans = form1.showMsg("It appears that MCC has been updated. MCC Mod Manager needs to stabilize the game by uninstalling certain modpacks." +
"\r\nWould you like to do this now? Selecting 'No' will disable features.", "Question");
if (ans == DialogResult.Yes) {
stabilize = true;
} else {
needsStabilize = true;
}
}
}
}
bool msg = false;
List<string> tmp = new List<string>();
foreach (KeyValuePair<string, Dictionary<string,string>> modpack in patched) {
if (!Modpacks.verifyExists(modpack.Key)) {
if (!msg) {
msg = true;
form1.showMsg("The '" + modpack.Key + "' modpack is missing from the modpacks folder. If this modpack is actually installed, " +
"MCC Mod Manager won't be able to uninstall it. You should restore from backups or verify the game files through Steam." +
"\r\nThis warning will only show once.", "Warning");
}
tmp.Add(modpack.Key);
}
}
foreach (string modpack in tmp) {
rmPatched(modpack);
}
saveCfg();
// Update config tab
form1.cfgTextBox1Text = MCC_home;
form1.cfgTextBox2Text = backup_dir;
form1.cfgTextBox3Text = modpack_dir;
form1.delOldBaks = deleteOldBaks;
if (stabilize) {
return 1;
} else if (needsStabilize) {
return 2;
} else {
return 0;
}
}
public static bool chkHomeDir(String dir)
{
if (!File.Exists(dir + @"\haloreach\haloreach.dll")) {
return false;
}
if (!File.Exists(dir + @"\MCC\Content\Paks\MCC-WindowsNoEditor.pak")) {
return false;
}
if (!File.Exists(dir + @"\mcclauncher.exe")) {
return false;
}
return true;
}
}
}