3using System.Security.Permissions;
6using Newtonsoft.Json.Serialization;
17 public static string log;
19 public static JsonSerializerSettings
jsReadGeneral =
new JsonSerializerSettings
21 NullValueHandling = NullValueHandling.Ignore,
22 DefaultValueHandling = DefaultValueHandling.Ignore,
23 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
24 TypeNameHandling = TypeNameHandling.Auto,
28 public static JsonSerializerSettings
jsWriteGeneral =
new JsonSerializerSettings
30 NullValueHandling = NullValueHandling.Ignore,
31 DefaultValueHandling = DefaultValueHandling.Ignore,
32 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
33 TypeNameHandling = TypeNameHandling.Auto,
38 public static JsonSerializerSettings
jsWriteConfig =
new JsonSerializerSettings
40 NullValueHandling = NullValueHandling.Ignore,
41 DefaultValueHandling = DefaultValueHandling.Populate,
42 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
43 TypeNameHandling = TypeNameHandling.Auto,
48 public static Formatting
formatting = Formatting.Indented;
52 public static JsonSerializerSettings
dpSetting =
new JsonSerializerSettings
54 NullValueHandling = NullValueHandling.Ignore,
55 DefaultValueHandling = DefaultValueHandling.Include,
56 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
57 TypeNameHandling = TypeNameHandling.Auto,
61 public static Formatting
dpFormat = Formatting.Indented;
63 public static string TempPath => Application.persistentDataPath +
"/Temp";
65 public static void OnError(
object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
73 Debug.LogWarning(log);
80 return JsonConvert.SerializeObject(obj, formatting, jsWriteGeneral);
83 public static T LoadJSON<T>(
string json)
85 return JsonConvert.DeserializeObject<T>(json, jsReadGeneral);
88 public static void SaveFile(
string path,
object obj,
bool compress =
false, JsonSerializerSettings setting =
null)
90 string text = JsonConvert.SerializeObject(obj, formatting, setting ?? jsWriteGeneral);
92 Debug.Log(
"#io SaveFile;" + path);
99 File.WriteAllText(path, text);
103 public static void SaveText(
string path,
string text)
106 Debug.Log(
"#io SaveFile;" + path);
107 File.WriteAllText(path, text);
113 Debug.Log(
"#io SaveFile;" + path);
114 File.WriteAllLines(path, text);
117 public static T LoadFile<T>(
string path,
bool compress =
false, JsonSerializerSettings setting =
null)
119 if (!File.Exists(path))
121 Debug.Log(
"#io File does not exist:" + path);
125 Debug.Log(
"#io LoadFile;" + path);
126 return JsonConvert.DeserializeObject<T>(value, setting ?? jsReadGeneral);
129 public static T LoadStreamJson<T>(MemoryStream stream, JsonSerializerSettings setting =
null)
131 stream.Position = 0L;
133 using (StreamReader streamReader =
new StreamReader(stream))
135 value = streamReader.ReadToEnd();
137 return JsonConvert.DeserializeObject<T>(value, setting ?? jsReadGeneral);
142 byte[] bytes = ((compression ==
Compression.LZ4) ? LZ4Codec.Wrap(_bytes, 0, _bytes.Length) : _bytes);
143 for (
int i = 0; i < 5; i++)
145 string path = _path + ((i == 0) ?
"" : (
".b" + i));
148 File.WriteAllBytes(path, bytes);
151 catch (Exception message)
160 for (
int i = 0; i < 5; i++)
162 string text = _path + ((i == 0) ?
"" : (
".b" + i));
163 if (!File.Exists(text))
165 Debug.Log(
"Couldn't find:" + text);
168 byte[] array = File.ReadAllBytes(text);
175 catch (Exception message)
180 if (array.Length == size)
192 return LZ4Codec.Unwrap(bytes);
196 Debug.Log(
"Exception: Failed to unwrap:");
203 using FileStream fileStream = File.OpenRead(path);
204 if (fileStream.Length == 0L)
211 switch (fileStream.ReadByte())
248 public static void Compress(
string path,
string text)
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);
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();
266 catch (Exception message)
271 string text = File.ReadAllText(path);
273 return text.IsEmpty(
"");
276 public static void CopyDir(
string sourceDirectory,
string targetDirectory, Func<string, bool> funcExclude =
null)
278 DirectoryInfo directoryInfo =
new DirectoryInfo(sourceDirectory);
279 DirectoryInfo target =
new DirectoryInfo(targetDirectory);
280 if (!directoryInfo.Exists)
282 Debug.Log(
"Source dir doesn't exist:" + directoryInfo.FullName);
286 _CopyDir(directoryInfo, target, funcExclude);
290 public static void _CopyDir(DirectoryInfo source, DirectoryInfo target, Func<string, bool> funcExclude =
null)
292 if (funcExclude ==
null || !funcExclude(source.Name))
294 Directory.CreateDirectory(target.FullName);
295 FileInfo[] files = source.GetFiles();
296 foreach (FileInfo fileInfo
in files)
298 fileInfo.CopyTo(Path.Combine(target.FullName, fileInfo.Name), overwrite:
true);
300 DirectoryInfo[] directories = source.GetDirectories();
301 foreach (DirectoryInfo directoryInfo
in directories)
303 DirectoryInfo target2 = target.CreateSubdirectory(directoryInfo.Name);
304 _CopyDir(directoryInfo, target2, funcExclude);
309 public static void Copy(
string fromPath,
string toPath)
311 if (!File.Exists(fromPath))
313 Debug.Log(
"File does not exist:" + fromPath);
316 FileInfo fileInfo =
new FileInfo(fromPath);
317 DirectoryInfo directoryInfo =
new DirectoryInfo(toPath);
318 if (!Directory.Exists(directoryInfo.FullName))
322 File.Copy(fileInfo.FullName, directoryInfo.FullName +
"/" + fileInfo.Name, overwrite:
true);
325 public static void CopyAs(
string fromPath,
string toPath)
327 if (!File.Exists(fromPath))
329 Debug.LogError(
"File does not exist:" + fromPath);
333 File.Copy(fromPath, toPath, overwrite:
true);
337 public static void CopyAll(
string fromPath,
string toPath,
bool overwrite =
true)
340 string[] directories = Directory.GetDirectories(fromPath,
"*", SearchOption.AllDirectories);
341 for (
int i = 0; i < directories.Length; i++)
343 Directory.CreateDirectory(directories[i].Replace(fromPath, toPath));
345 directories = Directory.GetFiles(fromPath,
"*.*", SearchOption.AllDirectories);
346 foreach (
string text
in directories)
348 string text2 = text.Replace(fromPath, toPath);
349 if (overwrite || !File.Exists(text2))
351 File.Copy(text, text2, overwrite:
true);
358 if (!File.Exists(path))
366 catch (Exception message)
368 Debug.LogError(message);
374 if (Directory.Exists(path))
376 FileInfo[] files =
new DirectoryInfo(path).GetFiles();
377 for (
int i = 0; i < files.Length; i++)
386 if (!Directory.Exists(path))
388 Directory.CreateDirectory(path);
394 path = path.Replace(
"\\\\?\\",
"");
395 if (!Directory.Exists(path))
399 DirectoryInfo directoryInfo =
new DirectoryInfo(path);
402 new FileIOPermission(FileIOPermissionAccess.AllAccess, path).Demand();
403 if (directoryInfo.Exists)
405 directoryInfo.Delete(recursive:
true);
408 catch (Exception message)
414 public static T Duplicate<T>(T t)
416 return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(t, formatting, jsWriteGeneral), jsReadGeneral);
429 public static T LoadObject<T>(FileInfo file,
object option =
null) where T : UnityEngine.Object
431 return LoadObject<T>(file.FullName, option);
434 public static T LoadObject<T>(
string _path,
object option =
null) where T : UnityEngine.Object
436 Type typeFromHandle = typeof(T);
437 if (typeFromHandle == typeof(Sprite))
440 Texture2D texture2D =
LoadPNG(_path);
445 return Sprite.Create(texture2D,
new Rect(0f, 0f, texture2D.width, texture2D.height), spriteLoadOption?.
pivot ??
new Vector2(0.5f, 0f), 100f) as T;
447 if (typeFromHandle == typeof(Texture2D))
451 if (typeof(
ExcelData).IsAssignableFrom(typeFromHandle))
453 T val = Activator.CreateInstance<T>();
457 if (typeFromHandle == typeof(
TextData))
461 lines = File.ReadAllLines(_path)
467 public static void SavePNG(Texture2D tex,
string _path)
469 byte[] bytes = tex.EncodeToPNG();
470 File.WriteAllBytes(_path, bytes);
473 public static Texture2D
LoadPNG(
string _path, FilterMode filter = FilterMode.Point)
478 public static T DeepCopy<T>(T target)
480 return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(target, dpFormat, dpSetting), dpSetting);
485 if (!File.Exists(_path))
488 if (!File.Exists(_path))
491 return Array.Empty<
string>();
494 return File.ReadAllLines(_path);
501 string[] array2 = array;
502 foreach (
string text2
in array2)
504 text = text + text2 + Environment.NewLine;
static void CreateTempDirectory(string path=null)
static JsonSerializerSettings dpSetting
static string[] LoadTextArray(string _path)
static JsonSerializerSettings jsWriteConfig
static void DeleteDirectory(string path)
static void OnError(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
static void SavePNG(Texture2D tex, string _path)
static void Copy(string fromPath, string toPath)
static byte[] ReadLZ4(byte[] bytes)
static void SaveTextArray(string path, string[] text)
static void CopyAll(string fromPath, string toPath, bool overwrite=true)
static void _CopyDir(DirectoryInfo source, DirectoryInfo target, Func< string, bool > funcExclude=null)
static string Decompress(string path)
static void CopyAs(string fromPath, string toPath)
static void DeleteFile(string path)
static string GetJSON(object obj)
static void CreateDirectory(string path)
static void SaveText(string path, string text)
static byte[] ReadLZ4(string _path, int size, Compression compression)
static JsonSerializerSettings jsWriteGeneral
static Formatting formatting
static void DeleteFiles(string path)
static void SaveFile(string path, object obj, bool compress=false, JsonSerializerSettings setting=null)
static void DeleteTempDirectory(string path=null)
static string LoadText(string _path)
static void Compress(string path, string text)
static bool IsCompressed(string path)
static TextureImportSetting.Data importSetting
static JsonSerializerSettings jsReadGeneral
static Texture2D LoadPNG(string _path, FilterMode filter=FilterMode.Point)
static Formatting dpFormat
static void CopyDir(string sourceDirectory, string targetDirectory, Func< string, bool > funcExclude=null)
static void WriteLZ4(string _path, byte[] _bytes, Compression compression=Compression.None)
static Texture2D LoadPNG(string _path, FilterMode filter=FilterMode.Point)
static readonly ShouldSerializeContractResolver Instance