Elin Decompiled Documentation EA 23.324 Nightly
Loading...
Searching...
No Matches
IO.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Security.Permissions;
4using LZ4;
5using Newtonsoft.Json;
6using Newtonsoft.Json.Serialization;
7using UnityEngine;
8
9public class IO
10{
11 public enum Compression
12 {
13 LZ4,
14 None
15 }
16
17 public static string log;
18
19 public static JsonSerializerSettings jsReadGeneral = new JsonSerializerSettings
20 {
21 NullValueHandling = NullValueHandling.Ignore,
22 DefaultValueHandling = DefaultValueHandling.Ignore,
23 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
24 TypeNameHandling = TypeNameHandling.Auto,
25 Error = OnError
26 };
27
28 public static JsonSerializerSettings jsWriteGeneral = new JsonSerializerSettings
29 {
30 NullValueHandling = NullValueHandling.Ignore,
31 DefaultValueHandling = DefaultValueHandling.Ignore,
32 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
33 TypeNameHandling = TypeNameHandling.Auto,
35 Error = OnError
36 };
37
38 public static JsonSerializerSettings jsWriteConfig = new JsonSerializerSettings
39 {
40 NullValueHandling = NullValueHandling.Ignore,
41 DefaultValueHandling = DefaultValueHandling.Populate,
42 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
43 TypeNameHandling = TypeNameHandling.Auto,
45 Error = OnError
46 };
47
48 public static Formatting formatting = Formatting.Indented;
49
51
52 public static JsonSerializerSettings dpSetting = new JsonSerializerSettings
53 {
54 NullValueHandling = NullValueHandling.Ignore,
55 DefaultValueHandling = DefaultValueHandling.Include,
56 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
57 TypeNameHandling = TypeNameHandling.Auto,
58 Error = OnError
59 };
60
61 public static Formatting dpFormat = Formatting.Indented;
62
63 public static string TempPath => Application.persistentDataPath + "/Temp";
64
65 public static void OnError(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
66 {
67 }
68
69 public static void PrintLog()
70 {
71 if (!log.IsEmpty())
72 {
73 Debug.LogWarning(log);
74 log = null;
75 }
76 }
77
78 public static string GetJSON(object obj)
79 {
80 return JsonConvert.SerializeObject(obj, formatting, jsWriteGeneral);
81 }
82
83 public static T LoadJSON<T>(string json)
84 {
85 return JsonConvert.DeserializeObject<T>(json, jsReadGeneral);
86 }
87
88 public static void SaveFile(string path, object obj, bool compress = false, JsonSerializerSettings setting = null)
89 {
90 string text = JsonConvert.SerializeObject(obj, formatting, setting ?? jsWriteGeneral);
91 CreateDirectory(Path.GetDirectoryName(path));
92 Debug.Log("#io SaveFile;" + path);
93 if (compress)
94 {
95 Compress(path, text);
96 }
97 else
98 {
99 File.WriteAllText(path, text);
100 }
101 }
102
103 public static void SaveText(string path, string text)
104 {
105 CreateDirectory(Path.GetDirectoryName(path));
106 Debug.Log("#io SaveFile;" + path);
107 File.WriteAllText(path, text);
108 }
109
110 public static void SaveTextArray(string path, string[] text)
111 {
112 CreateDirectory(Path.GetDirectoryName(path));
113 Debug.Log("#io SaveFile;" + path);
114 File.WriteAllLines(path, text);
115 }
116
117 public static T LoadFile<T>(string path, bool compress = false, JsonSerializerSettings setting = null)
118 {
119 if (!File.Exists(path))
120 {
121 Debug.Log("#io File does not exist:" + path);
122 return default(T);
123 }
124 string value = (IsCompressed(path) ? Decompress(path) : File.ReadAllText(path));
125 Debug.Log("#io LoadFile;" + path);
126 return JsonConvert.DeserializeObject<T>(value, setting ?? jsReadGeneral);
127 }
128
129 public static T LoadStreamJson<T>(MemoryStream stream, JsonSerializerSettings setting = null)
130 {
131 stream.Position = 0L;
132 string value = "";
133 using (StreamReader streamReader = new StreamReader(stream))
134 {
135 value = streamReader.ReadToEnd();
136 }
137 return JsonConvert.DeserializeObject<T>(value, setting ?? jsReadGeneral);
138 }
139
140 public static void WriteLZ4(string _path, byte[] _bytes, Compression compression = Compression.None)
141 {
142 byte[] bytes = ((compression == Compression.LZ4) ? LZ4Codec.Wrap(_bytes, 0, _bytes.Length) : _bytes);
143 for (int i = 0; i < 5; i++)
144 {
145 string path = _path + ((i == 0) ? "" : (".b" + i));
146 try
147 {
148 File.WriteAllBytes(path, bytes);
149 break;
150 }
151 catch (Exception message)
152 {
153 Debug.Log(message);
154 }
155 }
156 }
157
158 public static byte[] ReadLZ4(string _path, int size, Compression compression)
159 {
160 for (int i = 0; i < 5; i++)
161 {
162 string text = _path + ((i == 0) ? "" : (".b" + i));
163 if (!File.Exists(text))
164 {
165 Debug.Log("Couldn't find:" + text);
166 continue;
167 }
168 byte[] array = File.ReadAllBytes(text);
169 if (compression == Compression.LZ4)
170 {
171 try
172 {
173 return ReadLZ4(array);
174 }
175 catch (Exception message)
176 {
177 Debug.Log(message);
178 }
179 }
180 if (array.Length == size)
181 {
182 return array;
183 }
184 }
185 return null;
186 }
187
188 public static byte[] ReadLZ4(byte[] bytes)
189 {
190 try
191 {
192 return LZ4Codec.Unwrap(bytes);
193 }
194 catch
195 {
196 Debug.Log("Exception: Failed to unwrap:");
197 return bytes;
198 }
199 }
200
201 public static bool IsCompressed(string path)
202 {
203 using FileStream fileStream = File.OpenRead(path);
204 if (fileStream.Length == 0L)
205 {
206 return false;
207 }
208 bool flag;
209 while (true)
210 {
211 switch (fileStream.ReadByte())
212 {
213 case 9:
214 case 10:
215 case 13:
216 case 32:
217 continue;
218 case -1:
219 return false;
220 case 34:
221 case 45:
222 case 48:
223 case 49:
224 case 50:
225 case 51:
226 case 52:
227 case 53:
228 case 54:
229 case 55:
230 case 56:
231 case 57:
232 case 91:
233 case 102:
234 case 110:
235 case 116:
236 case 123:
237 flag = true;
238 break;
239 default:
240 flag = false;
241 break;
242 }
243 break;
244 }
245 return !flag;
246 }
247
248 public static void Compress(string path, string text)
249 {
250 Debug.Log("Compressing: " + path);
251 using FileStream innerStream = new FileStream(path, FileMode.Create);
252 using LZ4Stream stream = new LZ4Stream(innerStream, LZ4StreamMode.Compress);
253 using StreamWriter streamWriter = new StreamWriter(stream);
254 streamWriter.Write(text);
255 }
256
257 public static string Decompress(string path)
258 {
259 try
260 {
261 using FileStream innerStream = new FileStream(path, FileMode.Open);
262 using LZ4Stream stream = new LZ4Stream(innerStream, LZ4StreamMode.Decompress);
263 using StreamReader streamReader = new StreamReader(stream);
264 return streamReader.ReadToEnd();
265 }
266 catch (Exception message)
267 {
268 Debug.Log(message);
269 }
270 Debug.Log("Cannot decompress:" + IsCompressed(path) + "/" + path);
271 string text = File.ReadAllText(path);
272 Debug.Log(text);
273 return text.IsEmpty("");
274 }
275
276 public static void CopyDir(string sourceDirectory, string targetDirectory, Func<string, bool> funcExclude = null)
277 {
278 DirectoryInfo directoryInfo = new DirectoryInfo(sourceDirectory);
279 DirectoryInfo target = new DirectoryInfo(targetDirectory);
280 if (!directoryInfo.Exists)
281 {
282 Debug.Log("Source dir doesn't exist:" + directoryInfo.FullName);
283 }
284 else
285 {
286 _CopyDir(directoryInfo, target, funcExclude);
287 }
288 }
289
290 public static void _CopyDir(DirectoryInfo source, DirectoryInfo target, Func<string, bool> funcExclude = null)
291 {
292 if (funcExclude == null || !funcExclude(source.Name))
293 {
294 Directory.CreateDirectory(target.FullName);
295 FileInfo[] files = source.GetFiles();
296 foreach (FileInfo fileInfo in files)
297 {
298 fileInfo.CopyTo(Path.Combine(target.FullName, fileInfo.Name), overwrite: true);
299 }
300 DirectoryInfo[] directories = source.GetDirectories();
301 foreach (DirectoryInfo directoryInfo in directories)
302 {
303 DirectoryInfo target2 = target.CreateSubdirectory(directoryInfo.Name);
304 _CopyDir(directoryInfo, target2, funcExclude);
305 }
306 }
307 }
308
309 public static void Copy(string fromPath, string toPath)
310 {
311 if (!File.Exists(fromPath))
312 {
313 Debug.Log("File does not exist:" + fromPath);
314 return;
315 }
316 FileInfo fileInfo = new FileInfo(fromPath);
317 DirectoryInfo directoryInfo = new DirectoryInfo(toPath);
318 if (!Directory.Exists(directoryInfo.FullName))
319 {
320 CreateDirectory(directoryInfo.FullName);
321 }
322 File.Copy(fileInfo.FullName, directoryInfo.FullName + "/" + fileInfo.Name, overwrite: true);
323 }
324
325 public static void CopyAs(string fromPath, string toPath)
326 {
327 if (!File.Exists(fromPath))
328 {
329 Debug.LogError("File does not exist:" + fromPath);
330 }
331 else
332 {
333 File.Copy(fromPath, toPath, overwrite: true);
334 }
335 }
336
337 public static void CopyAll(string fromPath, string toPath, bool overwrite = true)
338 {
339 CreateDirectory(toPath);
340 string[] directories = Directory.GetDirectories(fromPath, "*", SearchOption.AllDirectories);
341 for (int i = 0; i < directories.Length; i++)
342 {
343 Directory.CreateDirectory(directories[i].Replace(fromPath, toPath));
344 }
345 directories = Directory.GetFiles(fromPath, "*.*", SearchOption.AllDirectories);
346 foreach (string text in directories)
347 {
348 string text2 = text.Replace(fromPath, toPath);
349 if (overwrite || !File.Exists(text2))
350 {
351 File.Copy(text, text2, overwrite: true);
352 }
353 }
354 }
355
356 public static void DeleteFile(string path)
357 {
358 if (!File.Exists(path))
359 {
360 return;
361 }
362 try
363 {
364 File.Delete(path);
365 }
366 catch (Exception message)
367 {
368 Debug.LogError(message);
369 }
370 }
371
372 public static void DeleteFiles(string path)
373 {
374 if (Directory.Exists(path))
375 {
376 FileInfo[] files = new DirectoryInfo(path).GetFiles();
377 for (int i = 0; i < files.Length; i++)
378 {
379 files[i].Delete();
380 }
381 }
382 }
383
384 public static void CreateDirectory(string path)
385 {
386 if (!Directory.Exists(path))
387 {
388 Directory.CreateDirectory(path);
389 }
390 }
391
392 public static void DeleteDirectory(string path)
393 {
394 path = path.Replace("\\\\?\\", "");
395 if (!Directory.Exists(path))
396 {
397 return;
398 }
399 DirectoryInfo directoryInfo = new DirectoryInfo(path);
400 try
401 {
402 new FileIOPermission(FileIOPermissionAccess.AllAccess, path).Demand();
403 if (directoryInfo.Exists)
404 {
405 directoryInfo.Delete(recursive: true);
406 }
407 }
408 catch (Exception message)
409 {
410 Debug.Log(message);
411 }
412 }
413
414 public static T Duplicate<T>(T t)
415 {
416 return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(t, formatting, jsWriteGeneral), jsReadGeneral);
417 }
418
419 public static void CreateTempDirectory(string path = null)
420 {
421 CreateDirectory(path ?? TempPath);
422 }
423
424 public static void DeleteTempDirectory(string path = null)
425 {
426 DeleteDirectory(path ?? TempPath);
427 }
428
429 public static T LoadObject<T>(FileInfo file, object option = null) where T : UnityEngine.Object
430 {
431 return LoadObject<T>(file.FullName, option);
432 }
433
434 public static T LoadObject<T>(string _path, object option = null) where T : UnityEngine.Object
435 {
436 Type typeFromHandle = typeof(T);
437 if (typeFromHandle == typeof(Sprite))
438 {
439 SpriteLoadOption spriteLoadOption = option as SpriteLoadOption;
440 Texture2D texture2D = LoadPNG(_path);
441 if (!texture2D)
442 {
443 return null;
444 }
445 return Sprite.Create(texture2D, new Rect(0f, 0f, texture2D.width, texture2D.height), spriteLoadOption?.pivot ?? new Vector2(0.5f, 0f), 100f) as T;
446 }
447 if (typeFromHandle == typeof(Texture2D))
448 {
449 return LoadPNG(_path) as T;
450 }
451 if (typeof(ExcelData).IsAssignableFrom(typeFromHandle))
452 {
453 T val = Activator.CreateInstance<T>();
454 (val as ExcelData).path = _path;
455 return val;
456 }
457 if (typeFromHandle == typeof(TextData))
458 {
459 return new TextData
460 {
461 lines = File.ReadAllLines(_path)
462 } as T;
463 }
464 return null;
465 }
466
467 public static void SavePNG(Texture2D tex, string _path)
468 {
469 byte[] bytes = tex.EncodeToPNG();
470 File.WriteAllBytes(_path, bytes);
471 }
472
473 public static Texture2D LoadPNG(string _path, FilterMode filter = FilterMode.Point)
474 {
475 return ImageLoader.LoadPNG(_path, filter);
476 }
477
478 public static T DeepCopy<T>(T target)
479 {
480 return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(target, dpFormat, dpSetting), dpSetting);
481 }
482
483 public static string[] LoadTextArray(string _path)
484 {
485 if (!File.Exists(_path))
486 {
487 _path += ".txt";
488 if (!File.Exists(_path))
489 {
490 Debug.Log(_path);
491 return Array.Empty<string>();
492 }
493 }
494 return File.ReadAllLines(_path);
495 }
496
497 public static string LoadText(string _path)
498 {
499 string[] array = LoadTextArray(_path);
500 string text = "";
501 string[] array2 = array;
502 foreach (string text2 in array2)
503 {
504 text = text + text2 + Environment.NewLine;
505 }
506 return text;
507 }
508}
Definition: IO.cs:10
static void CreateTempDirectory(string path=null)
Definition: IO.cs:419
static JsonSerializerSettings dpSetting
Definition: IO.cs:52
static string[] LoadTextArray(string _path)
Definition: IO.cs:483
Compression
Definition: IO.cs:12
static JsonSerializerSettings jsWriteConfig
Definition: IO.cs:38
static void DeleteDirectory(string path)
Definition: IO.cs:392
static void OnError(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
Definition: IO.cs:65
static void SavePNG(Texture2D tex, string _path)
Definition: IO.cs:467
static void Copy(string fromPath, string toPath)
Definition: IO.cs:309
static byte[] ReadLZ4(byte[] bytes)
Definition: IO.cs:188
static void SaveTextArray(string path, string[] text)
Definition: IO.cs:110
static void CopyAll(string fromPath, string toPath, bool overwrite=true)
Definition: IO.cs:337
static void _CopyDir(DirectoryInfo source, DirectoryInfo target, Func< string, bool > funcExclude=null)
Definition: IO.cs:290
static string Decompress(string path)
Definition: IO.cs:257
static void CopyAs(string fromPath, string toPath)
Definition: IO.cs:325
static void DeleteFile(string path)
Definition: IO.cs:356
static string GetJSON(object obj)
Definition: IO.cs:78
static void PrintLog()
Definition: IO.cs:69
static void CreateDirectory(string path)
Definition: IO.cs:384
static string TempPath
Definition: IO.cs:63
static void SaveText(string path, string text)
Definition: IO.cs:103
static byte[] ReadLZ4(string _path, int size, Compression compression)
Definition: IO.cs:158
static JsonSerializerSettings jsWriteGeneral
Definition: IO.cs:28
static Formatting formatting
Definition: IO.cs:48
static void DeleteFiles(string path)
Definition: IO.cs:372
static void SaveFile(string path, object obj, bool compress=false, JsonSerializerSettings setting=null)
Definition: IO.cs:88
static void DeleteTempDirectory(string path=null)
Definition: IO.cs:424
static string LoadText(string _path)
Definition: IO.cs:497
static void Compress(string path, string text)
Definition: IO.cs:248
static bool IsCompressed(string path)
Definition: IO.cs:201
static TextureImportSetting.Data importSetting
Definition: IO.cs:50
static JsonSerializerSettings jsReadGeneral
Definition: IO.cs:19
static Texture2D LoadPNG(string _path, FilterMode filter=FilterMode.Point)
Definition: IO.cs:473
static Formatting dpFormat
Definition: IO.cs:61
static void CopyDir(string sourceDirectory, string targetDirectory, Func< string, bool > funcExclude=null)
Definition: IO.cs:276
static string log
Definition: IO.cs:17
static void WriteLZ4(string _path, byte[] _bytes, Compression compression=Compression.None)
Definition: IO.cs:140
static Texture2D LoadPNG(string _path, FilterMode filter=FilterMode.Point)
Definition: ImageLoader.cs:6
static readonly ShouldSerializeContractResolver Instance