forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSystem.cs
311 lines (264 loc) · 13.1 KB
/
FileSystem.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
304
305
306
307
308
309
310
311
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Text;
using System.Security.Cryptography;
namespace System.IO
{
public static class FileSystem
{
private static readonly BinaryFormatter _binaryFormatter;
static FileSystem()
{
_binaryFormatter = new BinaryFormatter();
}
public static bool FileExists(string filePath)
=> File.Exists(filePath);
public static bool DirectoryExists(string directoryPath)
=> Directory.Exists(directoryPath);
public static void CreateDirectory(string directoryPath)
=> Directory.CreateDirectory(directoryPath);
/// <summary>
/// Writes the given object instance to a binary file.
/// <para>Object type (and all child types) must be decorated with the [Serializable] attribute.</para>
/// <para>To prevent a variable from being serialized, decorate it with the [NonSerialized] attribute; cannot be applied to properties.</para>
/// </summary>
/// <typeparam name="T">The type of object being written to the binary file.</typeparam>
/// <param name="filePath">The file path to write the object instance to.</param>
/// <param name="objectToWrite">The object instance to write to the binary file.</param>
/// <param name="append">If false the file will be overwritten if it already exists. If true the contents will be appended to the file.</param>
public static void WriteToBinaryFile<T>(string filePath, T objectToWrite, bool append = false)
{
using (var stream = File.Open(filePath, append ? FileMode.Append : FileMode.Create))
{
_binaryFormatter.Serialize(stream, objectToWrite);
}
}
public static void WriteToBinaryFile<T>(string filePath, T objectToWrite, ICryptoTransform encryptor, bool append = false)
{
using (var innerStream = File.Open(filePath, append ? FileMode.Append : FileMode.Create))
{
using (var cryptoStream = new CryptoStream(innerStream, encryptor, CryptoStreamMode.Write))
{
_binaryFormatter.Serialize(cryptoStream, objectToWrite);
}
}
}
public static void WriteToBinaryFile<T>(string directoryPath, string filePath, T objectToWrite, bool append = false)
{
if (!DirectoryExists(directoryPath))
CreateDirectory(directoryPath);
WriteToBinaryFile(filePath, objectToWrite, append);
}
public static void WriteToBinaryFile<T>(string directoryPath, string filePath, T objectToWrite, ICryptoTransform encryptor, bool append = false)
{
if (!DirectoryExists(directoryPath))
CreateDirectory(directoryPath);
WriteToBinaryFile(filePath, objectToWrite, encryptor, append);
}
/// <summary>
/// Reads an object instance from a binary file.
/// </summary>
/// <typeparam name="T">The type of object to read from the binary file.</typeparam>
/// <param name="filePath">The file path to read the object instance from.</param>
/// <param name="default">The default value to return if the file cannot be read.</param>
/// <returns>Returns a new instance of the object read from the binary file.</returns>
public static T ReadFromBinaryFile<T>(string filePath, T @default = default)
{
if (File.Exists(filePath))
{
using (var stream = File.Open(filePath, FileMode.Open))
{
return (T)_binaryFormatter.Deserialize(stream);
}
}
return @default;
}
public static T ReadFromBinaryFile<T>(string filePath, ICryptoTransform decryptor, T @default = default)
{
if (File.Exists(filePath))
{
using (var innerStream = File.Open(filePath, FileMode.Open))
{
using (var cryptoStream = new CryptoStream(innerStream, decryptor, CryptoStreamMode.Read))
{
return (T)_binaryFormatter.Deserialize(cryptoStream);
}
}
}
return @default;
}
public static T ReadFromBinaryFile<T>(string directoryPath, string filePath, T @default = default)
{
if (!DirectoryExists(directoryPath))
CreateDirectory(directoryPath);
return ReadFromBinaryFile(filePath, @default);
}
public static T ReadFromBinaryFile<T>(string directoryPath, string filePath, ICryptoTransform decryptor, T @default = default)
{
if (!DirectoryExists(directoryPath))
CreateDirectory(directoryPath);
return ReadFromBinaryFile(filePath, decryptor, @default);
}
/// <summary>
/// Writes the given object instance to a text file.
/// <para>Object type (and all child types) must be decorated with the [Serializable] attribute.</para>
/// <para>To prevent a variable from being serialized, decorate it with the [NonSerialized] attribute; cannot be applied to properties.</para>
/// </summary>
/// <typeparam name="T">The type of object being written to the text file.</typeparam>
/// <param name="filePath">The file path to write the object instance to.</param>
/// <param name="objectToWrite">The object instance to write to the text file.</param>
/// <param name="formatter">The formatter to serialize the object.</param>
/// <param name="append">If false the file will be overwritten if it already exists. If true the contents will be appended to the file.</param>
public static void WriteToTextFile<T>(string filePath, T objectToWrite, ITextFormatter formatter, bool append = false)
{
using (var writer = new StreamWriter(filePath, append))
{
formatter.Serialize(writer, objectToWrite);
}
}
public static void WriteToTextFile<T>(string directoryPath, string filePath, T objectToWrite, ITextFormatter formatter, bool append = false)
{
if (!DirectoryExists(directoryPath))
CreateDirectory(directoryPath);
WriteToTextFile(filePath, objectToWrite, formatter, append);
}
public static void WriteToTextFile<T>(string filePath, T objectToWrite, ITextFormatter formatter, ICryptoTransform encryptor, bool append = false)
{
using (var innerStream = File.Open(filePath, append ? FileMode.Append : FileMode.Create))
{
using (var cryptoStream = new CryptoStream(innerStream, encryptor, CryptoStreamMode.Write))
{
using (var writer = new StreamWriter(cryptoStream))
{
formatter.Serialize(writer, objectToWrite);
}
}
}
}
public static void WriteToTextFile<T>(string directoryPath, string filePath, T objectToWrite, ITextFormatter formatter, ICryptoTransform encryptor, bool append = false)
{
if (!DirectoryExists(directoryPath))
CreateDirectory(directoryPath);
WriteToTextFile(filePath, objectToWrite, formatter, encryptor, append);
}
public static void WriteToTextFile<T>(string filePath, T objectToWrite, ITextFormatter<T> formatter, bool append = false)
{
using (var writer = new StreamWriter(filePath, append))
{
formatter.Serialize(writer, objectToWrite);
}
}
public static void WriteToTextFile<T>(string directoryPath, string filePath, T objectToWrite, ITextFormatter<T> formatter, bool append = false)
{
if (!DirectoryExists(directoryPath))
CreateDirectory(directoryPath);
WriteToTextFile(filePath, objectToWrite, formatter, append);
}
public static void WriteToTextFile<T>(string filePath, T objectToWrite, ITextFormatter<T> formatter, ICryptoTransform encryptor, bool append = false)
{
using (var innerStream = File.Open(filePath, append ? FileMode.Append : FileMode.Create))
{
using (var cryptoStream = new CryptoStream(innerStream, encryptor, CryptoStreamMode.Write))
{
using (var writer = new StreamWriter(cryptoStream))
{
formatter.Serialize(writer, objectToWrite);
}
}
}
}
public static void WriteToTextFile<T>(string directoryPath, string filePath, T objectToWrite, ITextFormatter<T> formatter, ICryptoTransform encryptor, bool append = false)
{
if (!DirectoryExists(directoryPath))
CreateDirectory(directoryPath);
WriteToTextFile(filePath, objectToWrite, formatter, encryptor, append);
}
/// <summary>
/// Reads an object instance from a text file.
/// </summary>
/// <typeparam name="T">The type of object to read from the text file.</typeparam>
/// <param name="filePath">The file path to read the object instance from.</param>
/// <param name="formatter">The formatter to deserialize the text file.</param>
/// <param name="default">The default value to return if the file cannot be read.</param>
/// <returns>Returns a new instance of the object read from the text file.</returns>
public static T ReadFromTextFile<T>(string filePath, ITextFormatter formatter, T @default = default)
{
if (File.Exists(filePath))
{
using (var reader = new StreamReader(filePath))
{
return formatter.Deserialize<T>(reader);
}
}
return @default;
}
public static T ReadFromTextFile<T>(string directoryPath, string filePath, ITextFormatter formatter, T @default = default)
{
if (!DirectoryExists(directoryPath))
CreateDirectory(directoryPath);
return ReadFromTextFile(filePath, formatter, @default);
}
public static T ReadFromTextFile<T>(string filePath, ITextFormatter formatter, ICryptoTransform decryptor, T @default = default)
{
if (File.Exists(filePath))
{
using (var innerStream = File.Open(filePath, FileMode.Open))
{
using (var cryptoStream = new CryptoStream(innerStream, decryptor, CryptoStreamMode.Read))
{
using (var reader = new StreamReader(cryptoStream))
{
return formatter.Deserialize<T>(reader);
}
}
}
}
return @default;
}
public static T ReadFromTextFile<T>(string directoryPath, string filePath, ITextFormatter formatter, ICryptoTransform decryptor, T @default = default)
{
if (!DirectoryExists(directoryPath))
CreateDirectory(directoryPath);
return ReadFromTextFile(filePath, formatter, decryptor, @default);
}
public static T ReadFromTextFile<T>(string filePath, ITextFormatter<T> formatter, T @default = default)
{
if (File.Exists(filePath))
{
using (var reader = new StreamReader(filePath))
{
return formatter.Deserialize(reader);
}
}
return @default;
}
public static T ReadFromTextFile<T>(string directoryPath, string filePath, ITextFormatter<T> formatter, T @default = default)
{
if (!DirectoryExists(directoryPath))
CreateDirectory(directoryPath);
return ReadFromTextFile(filePath, formatter, @default);
}
public static T ReadFromTextFile<T>(string filePath, ITextFormatter<T> formatter, ICryptoTransform decryptor, T @default = default)
{
if (File.Exists(filePath))
{
using (var innerStream = File.Open(filePath, FileMode.Open))
{
using (var cryptoStream = new CryptoStream(innerStream, decryptor, CryptoStreamMode.Read))
{
using (var reader = new StreamReader(cryptoStream))
{
return formatter.Deserialize(reader);
}
}
}
}
return @default;
}
public static T ReadFromTextFile<T>(string directoryPath, string filePath, ITextFormatter<T> formatter, ICryptoTransform decryptor, T @default = default)
{
if (!DirectoryExists(directoryPath))
CreateDirectory(directoryPath);
return ReadFromTextFile(filePath, formatter, decryptor, @default);
}
}
}