4using System.Security.Permissions;
7using Newtonsoft.Json.Serialization;
18 public static string log;
20 public static JsonSerializerSettings
jsReadGeneral =
new JsonSerializerSettings
22 NullValueHandling = NullValueHandling.Ignore,
23 DefaultValueHandling = DefaultValueHandling.Ignore,
24 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
25 TypeNameHandling = TypeNameHandling.Auto,
29 public static JsonSerializerSettings
jsWriteGeneral =
new JsonSerializerSettings
31 NullValueHandling = NullValueHandling.Ignore,
32 DefaultValueHandling = DefaultValueHandling.Ignore,
33 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
34 TypeNameHandling = TypeNameHandling.Auto,
39 public static JsonSerializerSettings
jsWriteConfig =
new JsonSerializerSettings
41 NullValueHandling = NullValueHandling.Ignore,
42 DefaultValueHandling = DefaultValueHandling.Populate,
43 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
44 TypeNameHandling = TypeNameHandling.Auto,
49 public static Formatting
formatting = Formatting.Indented;
53 public static JsonSerializerSettings
dpSetting =
new JsonSerializerSettings
55 NullValueHandling = NullValueHandling.Ignore,
56 DefaultValueHandling = DefaultValueHandling.Include,
57 PreserveReferencesHandling = PreserveReferencesHandling.Objects,
58 TypeNameHandling = TypeNameHandling.Auto,
62 public static Formatting
dpFormat = Formatting.Indented;
64 public static string TempPath => Application.persistentDataPath +
"/Temp";
66 public static void OnError(
object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
74 Debug.LogWarning(log);
81 return JsonConvert.SerializeObject(obj, formatting, jsWriteGeneral);
84 public static T LoadJSON<T>(
string json)
86 return JsonConvert.DeserializeObject<T>(json, jsReadGeneral);
89 public static void SaveFile(
string path,
object obj,
bool compress =
false, JsonSerializerSettings setting =
null)
91 string text = JsonConvert.SerializeObject(obj, formatting, setting ?? jsWriteGeneral);
93 Debug.Log(
"#io SaveFile;" + path);
100 File.WriteAllText(path, text);
104 public static void SaveText(
string path,
string text)
107 Debug.Log(
"#io SaveFile;" + path);
108 File.WriteAllText(path, text);
114 Debug.Log(
"#io SaveFile;" + path);
115 File.WriteAllLines(path, text);
118 public static T LoadFile<T>(
string path,
bool compress =
false, JsonSerializerSettings setting =
null)
120 if (!File.Exists(path))
122 Debug.Log(
"File does not exist:" + path);
126 Debug.Log(
"#io LoadFile;" + path);
127 return JsonConvert.DeserializeObject<T>(value, setting ?? jsReadGeneral);
130 public static T LoadStreamJson<T>(MemoryStream stream, JsonSerializerSettings setting =
null)
132 stream.Position = 0L;
134 using (StreamReader streamReader =
new StreamReader(stream))
136 value = streamReader.ReadToEnd();
138 return JsonConvert.DeserializeObject<T>(value, setting ?? jsReadGeneral);
143 byte[] bytes = ((compression ==
Compression.LZ4) ? LZ4Codec.Wrap(_bytes, 0, _bytes.Length) : _bytes);
144 for (
int i = 0; i < 5; i++)
146 string path = _path + ((i == 0) ?
"" : (
".b" + i));
149 File.WriteAllBytes(path, bytes);
152 catch (Exception message)
161 for (
int i = 0; i < 5; i++)
163 string text = _path + ((i == 0) ?
"" : (
".b" + i));
164 if (!File.Exists(text))
166 Debug.Log(
"Couldn't find:" + text);
169 byte[] array = File.ReadAllBytes(text);
176 catch (Exception message)
181 if (array.Length == size)
193 return LZ4Codec.Unwrap(bytes);
197 Debug.Log(
"Exception: Failed to unwrap:");
204 using FileStream fileStream = File.OpenRead(path);
205 if (fileStream.Length == 0L)
212 switch (fileStream.ReadByte())
249 public static void Compress(
string path,
string text)
251 Debug.Log(
"Compressing: " + path);
252 using FileStream innerStream =
new FileStream(path, FileMode.Create);
253 using LZ4Stream stream =
new LZ4Stream(innerStream, LZ4StreamMode.Compress);
254 using StreamWriter streamWriter =
new StreamWriter(stream);
255 streamWriter.Write(text);
262 using FileStream innerStream =
new FileStream(path, FileMode.Open);
263 using LZ4Stream stream =
new LZ4Stream(innerStream, LZ4StreamMode.Decompress);
264 using StreamReader streamReader =
new StreamReader(stream);
265 return streamReader.ReadToEnd();
267 catch (Exception message)
272 string text = File.ReadAllText(path);
274 return text.IsEmpty(
"");
277 public static void CopyDir(
string sourceDirectory,
string targetDirectory, Func<string, bool> funcExclude =
null)
279 DirectoryInfo directoryInfo =
new DirectoryInfo(sourceDirectory);
280 DirectoryInfo target =
new DirectoryInfo(targetDirectory);
281 if (!directoryInfo.Exists)
283 Debug.Log(
"Source dir doesn't exist:" + directoryInfo.FullName);
287 _CopyDir(directoryInfo, target, funcExclude);
291 public static void _CopyDir(DirectoryInfo source, DirectoryInfo target, Func<string, bool> funcExclude =
null)
293 if (funcExclude ==
null || !funcExclude(source.Name))
295 Directory.CreateDirectory(target.FullName);
296 FileInfo[] files = source.GetFiles();
297 foreach (FileInfo fileInfo
in files)
299 fileInfo.CopyTo(Path.Combine(target.FullName, fileInfo.Name), overwrite:
true);
301 DirectoryInfo[] directories = source.GetDirectories();
302 foreach (DirectoryInfo directoryInfo
in directories)
304 DirectoryInfo target2 = target.CreateSubdirectory(directoryInfo.Name);
305 _CopyDir(directoryInfo, target2, funcExclude);
310 public static void Copy(
string fromPath,
string toPath)
312 if (!File.Exists(fromPath))
314 Debug.Log(
"File does not exist:" + fromPath);
317 FileInfo fileInfo =
new FileInfo(fromPath);
318 DirectoryInfo directoryInfo =
new DirectoryInfo(toPath);
319 if (!Directory.Exists(directoryInfo.FullName))
323 File.Copy(fileInfo.FullName, directoryInfo.FullName +
"/" + fileInfo.Name, overwrite:
true);
326 public static void CopyAs(
string fromPath,
string toPath)
328 if (!File.Exists(fromPath))
330 Debug.LogError(
"File does not exist:" + fromPath);
334 File.Copy(fromPath, toPath, overwrite:
true);
338 public static void CopyAll(
string fromPath,
string toPath,
bool overwrite =
true)
341 string[] directories = Directory.GetDirectories(fromPath,
"*", SearchOption.AllDirectories);
342 for (
int i = 0; i < directories.Length; i++)
344 Directory.CreateDirectory(directories[i].Replace(fromPath, toPath));
346 directories = Directory.GetFiles(fromPath,
"*.*", SearchOption.AllDirectories);
347 foreach (
string text
in directories)
349 string text2 = text.Replace(fromPath, toPath);
350 if (overwrite || !File.Exists(text2))
352 File.Copy(text, text2, overwrite:
true);
359 if (File.Exists(path))
367 if (Directory.Exists(path))
369 FileInfo[] files =
new DirectoryInfo(path).GetFiles();
370 for (
int i = 0; i < files.Length; i++)
379 if (!Directory.Exists(path))
381 Directory.CreateDirectory(path);
387 path = path.Replace(
"\\\\?\\",
"");
388 if (Directory.Exists(path))
390 DirectoryInfo directoryInfo =
new DirectoryInfo(path);
393 new FileIOPermission(FileIOPermissionAccess.AllAccess, path).Demand();
395 catch (SecurityException ex)
397 Debug.Log(ex.ToString());
399 if (directoryInfo.Exists)
401 directoryInfo.Delete(recursive:
true);
406 public static T Duplicate<T>(T t)
408 return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(t, formatting, jsWriteGeneral), jsReadGeneral);
421 public static T LoadObject<T>(FileInfo file,
object option =
null) where T : UnityEngine.Object
423 return LoadObject<T>(file.FullName, option);
426 public static T LoadObject<T>(
string _path,
object option =
null) where T : UnityEngine.Object
428 Type typeFromHandle = typeof(T);
429 if (typeFromHandle == typeof(Sprite))
432 Texture2D texture2D =
LoadPNG(_path);
437 return Sprite.Create(texture2D,
new Rect(0f, 0f, texture2D.width, texture2D.height), spriteLoadOption?.
pivot ??
new Vector2(0.5f, 0f), 100f) as T;
439 if (typeFromHandle == typeof(Texture2D))
443 if (typeof(
ExcelData).IsAssignableFrom(typeFromHandle))
445 T val = Activator.CreateInstance<T>();
449 if (typeFromHandle == typeof(
TextData))
453 lines = File.ReadAllLines(_path)
459 public static void SavePNG(Texture2D tex,
string _path)
461 byte[] bytes = tex.EncodeToPNG();
462 File.WriteAllBytes(_path, bytes);
465 public static Texture2D
LoadPNG(
string _path, FilterMode filter = FilterMode.Point)
470 public static T DeepCopy<T>(T target)
472 return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(target, dpFormat, dpSetting), dpSetting);
477 if (!File.Exists(_path))
480 if (!File.Exists(_path))
483 return new string[0];
486 return File.ReadAllLines(_path);
493 string[] array2 = array;
494 foreach (
string text2
in array2)
496 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