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