-
Notifications
You must be signed in to change notification settings - Fork 2
/
Settings.cs
227 lines (193 loc) · 6.81 KB
/
Settings.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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;
using System.Windows.Forms;
namespace ModLauncher
{
class Settings
{
public static int INTERNAL_VERSION = 2;
private string GamePath;
public Settings(string gamePath)
{
this.GamePath = gamePath;
// Writing
/*
List<Mod_v2> mods = new List<Mod_v2>();
mods.Add(new Mod_v2 { Dir = "testmod", Parameters = "+map crg" });
mods.Add(new Mod_v2 { Dir = "hl2", Parameters = "+map d1_trainstation_01" });
FileStream fstream = new FileStream("ModLauncher.dat", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fstream, mods);
fstream.Close();
// Reading
fstream = new FileStream("ModLauncher.dat", FileMode.Open);
bf = new BinaryFormatter();
List<Mod_v2> modsR = (List<Mod_v2>)bf.Deserialize(fstream);
fstream.Close();
foreach (Mod_v2 mod in modsR)
{
Console.WriteLine(string.Format("{0}, {1}, {2}", mod.Dir, mod.Name, mod.Parameters));
}
*/
/*
SettingsInternal sinternal1 = new SettingsInternal();
sinternal1.GameMod = "testmod";
sinternal1.WindowSize = new WindowSize(9000, 9000);
List<Mod_v2> mods1 = new List<Mod_v2>();
mods1.Add(new Mod_v2 { Dir = "testmod", Name = "TestMod!!!", Parameters = "+map crg", ServerParameters = "" });
mods1.Add(new Mod_v2 { Dir = "hl2", Name = "I'm HL2!", Parameters = "+map d1_trainstation_01", ServerParameters = "" });
sinternal1.Mods = mods1;
FileStream fstream1 = new FileStream("ModLauncher.dat", FileMode.Create);
BinaryFormatter bf1 = new BinaryFormatter();
bf1.Serialize(fstream1, sinternal1);
fstream1.Close();
FileStream fstream2 = new FileStream("ModLauncher.dat", FileMode.Open);
BinaryFormatter bf2 = new BinaryFormatter();
SettingsInternal sinternal2 = (SettingsInternal)bf2.Deserialize(fstream2);
fstream2.Close();
Console.WriteLine(String.Format("Test restoring - GM: {0}, {1}x{2}", sinternal2.GameMod, sinternal2.WindowSize.Width, sinternal2.WindowSize.Height));
foreach (Mod_v2 mod in sinternal2.Mods)
{
Console.WriteLine(string.Format("\t{0}, {1}, {2}, {3}", mod.Dir, mod.Name, mod.Parameters, mod.ServerParameters));
}
*/
}
private bool TryConvert(FileStream fstream, SettingsInternal sint)
{
Console.WriteLine("Converting the old settings...");
fstream.Position = 0; // Clearing out
BinaryFormatter bf = new BinaryFormatter();
List<Mod> temp = (List<Mod>)bf.Deserialize(fstream);
// sinternalR.Mods = (List<Mod_v2>)temp;
sint.Mods = new List<Mod_v2>();
foreach (Mod modv1 in temp)
{
Mod_v2 tempmod = new Mod_v2();
tempmod.Dir = modv1.Dir;
tempmod.Name = modv1.Name;
tempmod.Parameters = modv1.Parameters;
tempmod.ServerParameters = modv1.ServerParameters;
}
// List<Mod_v2> temp = (List<Mod_v2>)bf.Deserialize(fstream);
// sinternalR.Mods = temp;
sint.GameMod = "";
sint.Version = INTERNAL_VERSION;
sint.WindowSize = new WindowSize(530, 200);
return true;
}
private bool TryConvert(object deserialized, ref SettingsInternal sint)
{
Console.WriteLine("Converting the old settings...");
sint = new SettingsInternal(); // Clearing out
List<Mod> temp = (List<Mod>)deserialized;
// List<Mod_v2> temp = (new List<Mod>()).ConvertAll<Mod_v2>(a => new Mod_v2());
// sinternalR.Mods = (List<Mod_v2>)temp;
sint.Mods = new List<Mod_v2>();
sint.GameMod = "";
sint.Version = INTERNAL_VERSION;
sint.WindowSize = new WindowSize(530, 200);
/*
foreach (Mod modv1 in temp)
{
Mod_v2 tempmod = new Mod_v2();
tempmod.Dir = modv1.Dir;
tempmod.Name = modv1.Name;
tempmod.Parameters = modv1.Parameters;
tempmod.ServerParameters = modv1.ServerParameters;
}
*/
sint.Mods = temp.ConvertAll<Mod_v2>(a => new Mod_v2(a));
if (sint.Mods.Count <= 0)
return false;
// List<Mod_v2> temp = (List<Mod_v2>)bf.Deserialize(fstream);
// sinternalR.Mods = temp;
return true;
}
public SettingsInternal GetSettings()
{
FileStream fstream = null;
try
{
bool isNotExist = !File.Exists(this.GamePath + "\\ModLauncher.dat");
FileAttributes fattrib = FileAttributes.Normal;
if (!isNotExist)
fattrib = File.GetAttributes(this.GamePath + "\\ModLauncher.dat");
if (((fattrib & FileAttributes.ReadOnly) != FileAttributes.ReadOnly) || isNotExist)
fstream = new FileStream(this.GamePath + "\\ModLauncher.dat", FileMode.OpenOrCreate);
else
fstream = new FileStream(this.GamePath + "\\ModLauncher.dat", FileMode.Open, FileAccess.Read); // Just try to open it
}
catch ( UnauthorizedAccessException ex ) // If the file is actually not accessible or somehow stays read-only
{
return new SettingsInternal();
}
catch ( DirectoryNotFoundException ex )
{
return new SettingsInternal();
}
if (fstream == null || fstream.Length <= 0)
{
// fstream.Close();
return new SettingsInternal();
}
BinaryFormatter bf = new BinaryFormatter();
// bf.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
object notSerialized = bf.Deserialize(fstream);
SettingsInternal sinternalR = new SettingsInternal();
try
{
sinternalR = (SettingsInternal)notSerialized;
}
catch (InvalidCastException ex)
{
// Maybe convertable?
// TryConvert(fstream, sinternalR);
if (!TryConvert(notSerialized, ref sinternalR))
{
MessageBox.Show("Conversion from the older ModLauncher.dat format was probably invalid.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
fstream.Close();
Console.WriteLine(String.Format("Test restoring222 - GM: {0}, {1}x{2}", sinternalR.GameMod, sinternalR.WindowSize.Width, sinternalR.WindowSize.Height));
Console.WriteLine("sinternalR.Mods.Count is " + sinternalR.Mods.Count);
foreach (Mod_v2 mod in sinternalR.Mods)
{
Console.WriteLine(string.Format("\t{0}, {1}, {2}, {3}", mod.Dir, mod.Name, mod.Parameters, mod.ServerParameters));
}
// Restoring older version of settings
if (sinternalR.Version < INTERNAL_VERSION)
{
// Future conversion. Older .dat format conversion is probably just ended in checks above (TryConvert)
}
return sinternalR;
}
public bool WriteSettings( SettingsInternal sinternal )
{
// if (mods.Count <= 0)
// return false;
try
{
// FileStream fstream = new FileStream("ModLauncher.dat", FileMode.OpenOrCreate);
FileStream fstream = new FileStream(this.GamePath + "\\ModLauncher.dat", FileMode.Create);
if (fstream == null)
{
fstream.Close();
return false;
}
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fstream, sinternal);
fstream.Close();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
}