Elin Decompiled Documentation EA 23.317 Nightly
Loading...
Searching...
No Matches
IO Class Reference

Public Types

enum  Compression { LZ4 , None }
 

Static Public Member Functions

static void OnError (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
 
static void PrintLog ()
 
static string GetJSON (object obj)
 
static T LoadJSON< T > (string json)
 
static void SaveFile (string path, object obj, bool compress=false, JsonSerializerSettings setting=null)
 
static void SaveText (string path, string text)
 
static void SaveTextArray (string path, string[] text)
 
static T LoadFile< T > (string path, bool compress=false, JsonSerializerSettings setting=null)
 
static T LoadStreamJson< T > (MemoryStream stream, JsonSerializerSettings setting=null)
 
static void WriteLZ4 (string _path, byte[] _bytes, Compression compression=Compression.None)
 
static byte[] ReadLZ4 (string _path, int size, Compression compression)
 
static byte[] ReadLZ4 (byte[] bytes)
 
static bool IsCompressed (string path)
 
static void Compress (string path, string text)
 
static string Decompress (string path)
 
static void CopyDir (string sourceDirectory, string targetDirectory, Func< string, bool > funcExclude=null)
 
static void _CopyDir (DirectoryInfo source, DirectoryInfo target, Func< string, bool > funcExclude=null)
 
static void Copy (string fromPath, string toPath)
 
static void CopyAs (string fromPath, string toPath)
 
static void CopyAll (string fromPath, string toPath, bool overwrite=true)
 
static void DeleteFile (string path)
 
static void DeleteFiles (string path)
 
static void CreateDirectory (string path)
 
static void DeleteDirectory (string path)
 
static T Duplicate< T > (T t)
 
static void CreateTempDirectory (string path=null)
 
static void DeleteTempDirectory (string path=null)
 
static T LoadObject< T > (FileInfo file, object option=null)
 
static T LoadObject< T > (string _path, object option=null)
 
static void SavePNG (Texture2D tex, string _path)
 
static Texture2D LoadPNG (string _path, FilterMode filter=FilterMode.Point)
 
static T DeepCopy< T > (T target)
 
static string[] LoadTextArray (string _path)
 
static string LoadText (string _path)
 

Static Public Attributes

static string log
 
static JsonSerializerSettings jsReadGeneral
 
static JsonSerializerSettings jsWriteGeneral
 
static JsonSerializerSettings jsWriteConfig
 
static Formatting formatting = Formatting.Indented
 
static TextureImportSetting.Data importSetting = new TextureImportSetting.Data()
 
static JsonSerializerSettings dpSetting
 
static Formatting dpFormat = Formatting.Indented
 

Properties

static string TempPath [get]
 

Detailed Description

Definition at line 9 of file IO.cs.

Member Enumeration Documentation

◆ Compression

Enumerator
LZ4 
None 

Definition at line 11 of file IO.cs.

12 {
13 LZ4,
14 None
15 }

Member Function Documentation

◆ _CopyDir()

static void IO._CopyDir ( DirectoryInfo  source,
DirectoryInfo  target,
Func< string, bool >  funcExclude = null 
)
inlinestatic

Definition at line 290 of file IO.cs.

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 }
static void _CopyDir(DirectoryInfo source, DirectoryInfo target, Func< string, bool > funcExclude=null)
Definition: IO.cs:290

References _CopyDir().

Referenced by _CopyDir(), and CopyDir().

◆ Compress()

static void IO.Compress ( string  path,
string  text 
)
inlinestatic

Definition at line 248 of file IO.cs.

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 }

References Debug.

Referenced by SaveFile().

◆ Copy()

static void IO.Copy ( string  fromPath,
string  toPath 
)
inlinestatic

Definition at line 309 of file IO.cs.

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 }
static void CreateDirectory(string path)
Definition: IO.cs:384

References CreateDirectory(), and Debug.

◆ CopyAll()

static void IO.CopyAll ( string  fromPath,
string  toPath,
bool  overwrite = true 
)
inlinestatic

Definition at line 337 of file IO.cs.

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 }

References CreateDirectory().

◆ CopyAs()

static void IO.CopyAs ( string  fromPath,
string  toPath 
)
inlinestatic

Definition at line 325 of file IO.cs.

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 }

References Debug.

◆ CopyDir()

static void IO.CopyDir ( string  sourceDirectory,
string  targetDirectory,
Func< string, bool >  funcExclude = null 
)
inlinestatic

Definition at line 276 of file IO.cs.

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 }

References _CopyDir(), and Debug.

◆ CreateDirectory()

static void IO.CreateDirectory ( string  path)
inlinestatic

Definition at line 384 of file IO.cs.

385 {
386 if (!Directory.Exists(path))
387 {
388 Directory.CreateDirectory(path);
389 }
390 }

Referenced by Copy(), CopyAll(), CreateTempDirectory(), SaveFile(), SaveText(), and SaveTextArray().

◆ CreateTempDirectory()

static void IO.CreateTempDirectory ( string  path = null)
inlinestatic

Definition at line 419 of file IO.cs.

420 {
421 CreateDirectory(path ?? TempPath);
422 }

References CreateDirectory().

◆ Decompress()

static string IO.Decompress ( string  path)
inlinestatic

Definition at line 257 of file IO.cs.

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 }
static bool IsCompressed(string path)
Definition: IO.cs:201

References Debug, and IsCompressed().

Referenced by LoadFile< T >().

◆ DeepCopy< T >()

static T IO.DeepCopy< T > ( target)
inlinestatic

Definition at line 478 of file IO.cs.

479 {
480 return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(target, dpFormat, dpSetting), dpSetting);
481 }
static JsonSerializerSettings dpSetting
Definition: IO.cs:52

◆ DeleteDirectory()

static void IO.DeleteDirectory ( string  path)
inlinestatic

Definition at line 392 of file IO.cs.

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 }

References Debug.

Referenced by DeleteTempDirectory().

◆ DeleteFile()

static void IO.DeleteFile ( string  path)
inlinestatic

Definition at line 356 of file IO.cs.

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 }

References Debug.

◆ DeleteFiles()

static void IO.DeleteFiles ( string  path)
inlinestatic

Definition at line 372 of file IO.cs.

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 }

◆ DeleteTempDirectory()

static void IO.DeleteTempDirectory ( string  path = null)
inlinestatic

Definition at line 424 of file IO.cs.

425 {
426 DeleteDirectory(path ?? TempPath);
427 }
static void DeleteDirectory(string path)
Definition: IO.cs:392

References DeleteDirectory().

◆ Duplicate< T >()

static T IO.Duplicate< T > ( t)
inlinestatic

Definition at line 414 of file IO.cs.

415 {
416 return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(t, formatting, jsWriteGeneral), jsReadGeneral);
417 }
static JsonSerializerSettings jsReadGeneral
Definition: IO.cs:19

◆ GetJSON()

static string IO.GetJSON ( object  obj)
inlinestatic

Definition at line 78 of file IO.cs.

79 {
80 return JsonConvert.SerializeObject(obj, formatting, jsWriteGeneral);
81 }

◆ IsCompressed()

static bool IO.IsCompressed ( string  path)
inlinestatic

Definition at line 201 of file IO.cs.

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 }

Referenced by Decompress(), and LoadFile< T >().

◆ LoadFile< T >()

static T IO.LoadFile< T > ( string  path,
bool  compress = false,
JsonSerializerSettings  setting = null 
)
inlinestatic

Definition at line 117 of file IO.cs.

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 }
static string Decompress(string path)
Definition: IO.cs:257

References Debug, Decompress(), and IsCompressed().

◆ LoadJSON< T >()

static T IO.LoadJSON< T > ( string  json)
inlinestatic

Definition at line 83 of file IO.cs.

84 {
85 return JsonConvert.DeserializeObject<T>(json, jsReadGeneral);
86 }

◆ LoadObject< T >() [1/2]

static T IO.LoadObject< T > ( FileInfo  file,
object  option = null 
)
inlinestatic
Type Constraints
T :UnityEngine.Object 

Definition at line 429 of file IO.cs.

429 : UnityEngine.Object
430 {
431 return LoadObject<T>(file.FullName, option);
432 }

◆ LoadObject< T >() [2/2]

static T IO.LoadObject< T > ( string  _path,
object  option = null 
)
inlinestatic
Type Constraints
T :UnityEngine.Object 

Definition at line 434 of file IO.cs.

434 : 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 }
static Texture2D LoadPNG(string _path, FilterMode filter=FilterMode.Point)
Definition: IO.cs:473

References LoadPNG(), and SpriteLoadOption.pivot.

◆ LoadPNG()

static Texture2D IO.LoadPNG ( string  _path,
FilterMode  filter = FilterMode::Point 
)
inlinestatic

Definition at line 473 of file IO.cs.

474 {
475 return ImageLoader.LoadPNG(_path, filter);
476 }
static Texture2D LoadPNG(string _path, FilterMode filter=FilterMode.Point)
Definition: ImageLoader.cs:6

References ImageLoader.LoadPNG().

Referenced by LoadObject< T >().

◆ LoadStreamJson< T >()

static T IO.LoadStreamJson< T > ( MemoryStream  stream,
JsonSerializerSettings  setting = null 
)
inlinestatic

Definition at line 129 of file IO.cs.

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 }

◆ LoadText()

static string IO.LoadText ( string  _path)
inlinestatic

Definition at line 497 of file IO.cs.

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 }
static string[] LoadTextArray(string _path)
Definition: IO.cs:483

References LoadTextArray().

◆ LoadTextArray()

static string[] IO.LoadTextArray ( string  _path)
inlinestatic

Definition at line 483 of file IO.cs.

484 {
485 if (!File.Exists(_path))
486 {
487 _path += ".txt";
488 if (!File.Exists(_path))
489 {
490 Debug.Log(_path);
491 return new string[0];
492 }
493 }
494 return File.ReadAllLines(_path);
495 }

References Debug.

Referenced by LoadText().

◆ OnError()

static void IO.OnError ( object  sender,
Newtonsoft::Json::Serialization::ErrorEventArgs  args 
)
inlinestatic

Definition at line 65 of file IO.cs.

66 {
67 }

◆ PrintLog()

static void IO.PrintLog ( )
inlinestatic

Definition at line 69 of file IO.cs.

70 {
71 if (!log.IsEmpty())
72 {
73 Debug.LogWarning(log);
74 log = null;
75 }
76 }
static string log
Definition: IO.cs:17

References Debug.

◆ ReadLZ4() [1/2]

static byte[] IO.ReadLZ4 ( byte[]  bytes)
inlinestatic

Definition at line 188 of file IO.cs.

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 }

References Debug.

◆ ReadLZ4() [2/2]

static byte[] IO.ReadLZ4 ( string  _path,
int  size,
Compression  compression 
)
inlinestatic

Definition at line 158 of file IO.cs.

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 }
Compression
Definition: IO.cs:12
static byte[] ReadLZ4(string _path, int size, Compression compression)
Definition: IO.cs:158

References Debug, and ReadLZ4().

Referenced by ReadLZ4().

◆ SaveFile()

static void IO.SaveFile ( string  path,
object  obj,
bool  compress = false,
JsonSerializerSettings  setting = null 
)
inlinestatic

Definition at line 88 of file IO.cs.

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 }
static void Compress(string path, string text)
Definition: IO.cs:248

References Compress(), CreateDirectory(), and Debug.

◆ SavePNG()

static void IO.SavePNG ( Texture2D  tex,
string  _path 
)
inlinestatic

Definition at line 467 of file IO.cs.

468 {
469 byte[] bytes = tex.EncodeToPNG();
470 File.WriteAllBytes(_path, bytes);
471 }

◆ SaveText()

static void IO.SaveText ( string  path,
string  text 
)
inlinestatic

Definition at line 103 of file IO.cs.

104 {
105 CreateDirectory(Path.GetDirectoryName(path));
106 Debug.Log("#io SaveFile;" + path);
107 File.WriteAllText(path, text);
108 }

References CreateDirectory(), and Debug.

◆ SaveTextArray()

static void IO.SaveTextArray ( string  path,
string[]  text 
)
inlinestatic

Definition at line 110 of file IO.cs.

111 {
112 CreateDirectory(Path.GetDirectoryName(path));
113 Debug.Log("#io SaveFile;" + path);
114 File.WriteAllLines(path, text);
115 }

References CreateDirectory(), and Debug.

◆ WriteLZ4()

static void IO.WriteLZ4 ( string  _path,
byte[]  _bytes,
Compression  compression = Compression::None 
)
inlinestatic

Definition at line 140 of file IO.cs.

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 }

References Debug.

Member Data Documentation

◆ dpFormat

Formatting IO.dpFormat = Formatting.Indented
static

Definition at line 61 of file IO.cs.

◆ dpSetting

JsonSerializerSettings IO.dpSetting
static
Initial value:
= new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Include,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
TypeNameHandling = TypeNameHandling.Auto,
Error = OnError
}
static void OnError(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
Definition: IO.cs:65

Definition at line 52 of file IO.cs.

◆ formatting

Formatting IO.formatting = Formatting.Indented
static

Definition at line 48 of file IO.cs.

◆ importSetting

TextureImportSetting.Data IO.importSetting = new TextureImportSetting.Data()
static

Definition at line 50 of file IO.cs.

◆ jsReadGeneral

JsonSerializerSettings IO.jsReadGeneral
static
Initial value:
= new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
TypeNameHandling = TypeNameHandling.Auto,
Error = OnError
}

Definition at line 19 of file IO.cs.

◆ jsWriteConfig

JsonSerializerSettings IO.jsWriteConfig
static
Initial value:
= new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Populate,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
TypeNameHandling = TypeNameHandling.Auto,
Error = OnError
}
static readonly ShouldSerializeContractResolver Instance

Definition at line 38 of file IO.cs.

◆ jsWriteGeneral

JsonSerializerSettings IO.jsWriteGeneral
static
Initial value:
= new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
TypeNameHandling = TypeNameHandling.Auto,
Error = OnError
}

Definition at line 28 of file IO.cs.

◆ log

string IO.log
static

Definition at line 17 of file IO.cs.

Property Documentation

◆ TempPath

string IO.TempPath
staticget

Definition at line 63 of file IO.cs.


The documentation for this class was generated from the following file: