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:");
205 using (BinaryReader binaryReader =
new BinaryReader(File.OpenRead(path)))
207 binaryReader.BaseStream.Seek(0L, SeekOrigin.Begin);
208 array = binaryReader.ReadBytes(4);
210 if (array.Length > 3 && array[0] == 123 && array[1] == 13 && array[2] == 10 && array[3] == 32)
217 public static void Compress(
string path,
string text)
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);
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();
235 catch (Exception message)
240 string text = File.ReadAllText(path);
242 return text.IsEmpty(
"");
245 public static void CopyDir(
string sourceDirectory,
string targetDirectory, Func<string, bool> funcExclude =
null)
247 DirectoryInfo directoryInfo =
new DirectoryInfo(sourceDirectory);
248 DirectoryInfo target =
new DirectoryInfo(targetDirectory);
249 if (!directoryInfo.Exists)
251 Debug.Log(
"Source dir doesn't exist:" + directoryInfo.FullName);
255 _CopyDir(directoryInfo, target, funcExclude);
259 public static void _CopyDir(DirectoryInfo source, DirectoryInfo target, Func<string, bool> funcExclude =
null)
261 if (funcExclude ==
null || !funcExclude(source.Name))
263 Directory.CreateDirectory(target.FullName);
264 FileInfo[] files = source.GetFiles();
265 foreach (FileInfo fileInfo
in files)
267 fileInfo.CopyTo(Path.Combine(target.FullName, fileInfo.Name), overwrite:
true);
269 DirectoryInfo[] directories = source.GetDirectories();
270 foreach (DirectoryInfo directoryInfo
in directories)
272 DirectoryInfo target2 = target.CreateSubdirectory(directoryInfo.Name);
273 _CopyDir(directoryInfo, target2, funcExclude);
278 public static void Copy(
string fromPath,
string toPath)
280 if (!File.Exists(fromPath))
282 Debug.Log(
"File does not exist:" + fromPath);
285 FileInfo fileInfo =
new FileInfo(fromPath);
286 DirectoryInfo directoryInfo =
new DirectoryInfo(toPath);
287 if (!Directory.Exists(directoryInfo.FullName))
291 File.Copy(fileInfo.FullName, directoryInfo.FullName +
"/" + fileInfo.Name, overwrite:
true);
294 public static void CopyAs(
string fromPath,
string toPath)
296 if (!File.Exists(fromPath))
298 Debug.LogError(
"File does not exist:" + fromPath);
302 File.Copy(fromPath, toPath, overwrite:
true);
306 public static void CopyAll(
string fromPath,
string toPath,
bool overwrite =
true)
309 string[] directories = Directory.GetDirectories(fromPath,
"*", SearchOption.AllDirectories);
310 for (
int i = 0; i < directories.Length; i++)
312 Directory.CreateDirectory(directories[i].Replace(fromPath, toPath));
314 directories = Directory.GetFiles(fromPath,
"*.*", SearchOption.AllDirectories);
315 foreach (
string text
in directories)
317 string text2 = text.Replace(fromPath, toPath);
318 if (overwrite || !File.Exists(text2))
320 File.Copy(text, text2, overwrite:
true);
327 if (File.Exists(path))
335 if (Directory.Exists(path))
337 FileInfo[] files =
new DirectoryInfo(path).GetFiles();
338 for (
int i = 0; i < files.Length; i++)
347 if (!Directory.Exists(path))
349 Directory.CreateDirectory(path);
355 path = path.Replace(
"\\\\?\\",
"");
356 if (Directory.Exists(path))
358 DirectoryInfo directoryInfo =
new DirectoryInfo(path);
361 new FileIOPermission(FileIOPermissionAccess.AllAccess, path).Demand();
363 catch (SecurityException ex)
365 Debug.Log(ex.ToString());
367 if (directoryInfo.Exists)
369 directoryInfo.Delete(recursive:
true);
374 public static T Duplicate<T>(T t)
376 return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(t, formatting, jsWriteGeneral), jsReadGeneral);
389 public static T LoadObject<T>(FileInfo file,
object option =
null) where T : UnityEngine.Object
391 return LoadObject<T>(file.FullName, option);
394 public static T LoadObject<T>(
string _path,
object option =
null) where T : UnityEngine.Object
396 Type typeFromHandle = typeof(T);
397 if (typeFromHandle == typeof(Sprite))
400 Texture2D texture2D =
LoadPNG(_path);
405 return Sprite.Create(texture2D,
new Rect(0f, 0f, texture2D.width, texture2D.height), spriteLoadOption?.
pivot ??
new Vector2(0.5f, 0f), 100f) as T;
407 if (typeFromHandle == typeof(Texture2D))
411 if (typeof(
ExcelData).IsAssignableFrom(typeFromHandle))
413 T val = Activator.CreateInstance<T>();
417 if (typeFromHandle == typeof(
TextData))
421 lines = File.ReadAllLines(_path)
427 public static void SavePNG(Texture2D tex,
string _path)
429 byte[] bytes = tex.EncodeToPNG();
430 File.WriteAllBytes(_path, bytes);
433 public static Texture2D
LoadPNG(
string _path, FilterMode filter = FilterMode.Point)
435 if (!File.Exists(_path))
442 for (
int i = 0; i < 4; i++)
444 if (num + 1 < array.Length)
446 num2 = num2 * 256 + array[num++];
450 for (
int j = 0; j < 4; j++)
452 if (num + 1 < array.Length)
454 num3 = num3 * 256 + array[num++];
458 Texture2D texture2D =
new Texture2D(num2, num3, data.
format, data.
mipmap, data.
linear);
459 texture2D.LoadImage(array);
461 texture2D.filterMode = filter;
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();
477 public static T DeepCopy<T>(T target)
479 return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(target, dpFormat, dpSetting), dpSetting);
484 if (!File.Exists(_path))
487 if (!File.Exists(_path))
490 return new string[0];
493 return File.ReadAllLines(_path);
500 string[] array2 = array;
501 foreach (
string text2
in array2)
503 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 byte[] ReadPngFile(string _path)
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 readonly ShouldSerializeContractResolver Instance