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