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);
111 public static T LoadFile<T>(
string path,
bool compress =
false, JsonSerializerSettings setting =
null)
113 if (!File.Exists(path))
115 Debug.Log(
"File does not exist:" + path);
119 Debug.Log(
"#io LoadFile;" + path);
120 return JsonConvert.DeserializeObject<T>(value, setting ?? jsReadGeneral);
123 public static T LoadStreamJson<T>(MemoryStream stream, JsonSerializerSettings setting =
null)
125 stream.Position = 0L;
127 using (StreamReader streamReader =
new StreamReader(stream))
129 value = streamReader.ReadToEnd();
131 return JsonConvert.DeserializeObject<T>(value, setting ?? jsReadGeneral);
134 public static void WriteLZ4(
string _path,
byte[] _bytes)
136 for (
int i = 0; i < 5; i++)
138 string path = _path + ((i == 0) ?
"" : (
".b" + i));
141 File.WriteAllBytes(path, _bytes);
144 catch (Exception message)
153 for (
int i = 0; i < 5; i++)
155 string text = _path + ((i == 0) ?
"" : (
".b" + i));
156 if (!File.Exists(text))
158 Debug.Log(
"Couldn't find:" + text);
161 byte[] array = File.ReadAllBytes(text);
162 if (array.Length == size)
172 catch (Exception message)
185 return LZ4Codec.Unwrap(bytes);
189 Debug.Log(
"Exception: Failed to unwrap:");
197 using (BinaryReader binaryReader =
new BinaryReader(File.OpenRead(path)))
199 binaryReader.BaseStream.Seek(0L, SeekOrigin.Begin);
200 array = binaryReader.ReadBytes(4);
202 if (array.Length > 3 && array[0] == 123 && array[1] == 13 && array[2] == 10 && array[3] == 32)
209 public static void Compress(
string path,
string text)
211 File.WriteAllText(path, text);
218 using FileStream innerStream =
new FileStream(path, FileMode.Open);
219 using LZ4Stream stream =
new LZ4Stream(innerStream, LZ4StreamMode.Decompress);
220 using StreamReader streamReader =
new StreamReader(stream);
221 return streamReader.ReadToEnd();
223 catch (Exception message)
228 string text = File.ReadAllText(path);
230 return text.IsEmpty(
"");
233 public static void CopyDir(
string sourceDirectory,
string targetDirectory, Func<string, bool> funcExclude =
null)
235 DirectoryInfo directoryInfo =
new DirectoryInfo(sourceDirectory);
236 DirectoryInfo target =
new DirectoryInfo(targetDirectory);
237 if (!directoryInfo.Exists)
239 Debug.Log(
"Source dir doesn't exist:" + directoryInfo.FullName);
243 _CopyDir(directoryInfo, target, funcExclude);
247 public static void _CopyDir(DirectoryInfo source, DirectoryInfo target, Func<string, bool> funcExclude =
null)
249 if (funcExclude ==
null || !funcExclude(source.Name))
251 Directory.CreateDirectory(target.FullName);
252 FileInfo[] files = source.GetFiles();
253 foreach (FileInfo fileInfo
in files)
255 fileInfo.CopyTo(Path.Combine(target.FullName, fileInfo.Name), overwrite:
true);
257 DirectoryInfo[] directories = source.GetDirectories();
258 foreach (DirectoryInfo directoryInfo
in directories)
260 DirectoryInfo target2 = target.CreateSubdirectory(directoryInfo.Name);
261 _CopyDir(directoryInfo, target2, funcExclude);
266 public static void Copy(
string fromPath,
string toPath)
268 if (!File.Exists(fromPath))
270 Debug.Log(
"File does not exist:" + fromPath);
273 FileInfo fileInfo =
new FileInfo(fromPath);
274 DirectoryInfo directoryInfo =
new DirectoryInfo(toPath);
275 if (!Directory.Exists(directoryInfo.FullName))
279 File.Copy(fileInfo.FullName, directoryInfo.FullName +
"/" + fileInfo.Name, overwrite:
true);
282 public static void CopyAs(
string fromPath,
string toPath)
284 if (!File.Exists(fromPath))
286 Debug.LogError(
"File does not exist:" + fromPath);
290 File.Copy(fromPath, toPath, overwrite:
true);
294 public static void CopyAll(
string fromPath,
string toPath,
bool overwrite =
true)
297 string[] directories = Directory.GetDirectories(fromPath,
"*", SearchOption.AllDirectories);
298 for (
int i = 0; i < directories.Length; i++)
300 Directory.CreateDirectory(directories[i].Replace(fromPath, toPath));
302 directories = Directory.GetFiles(fromPath,
"*.*", SearchOption.AllDirectories);
303 foreach (
string text
in directories)
305 string text2 = text.Replace(fromPath, toPath);
306 if (overwrite || !File.Exists(text2))
308 File.Copy(text, text2, overwrite:
true);
315 if (File.Exists(path))
323 if (Directory.Exists(path))
325 FileInfo[] files =
new DirectoryInfo(path).GetFiles();
326 for (
int i = 0; i < files.Length; i++)
335 if (!Directory.Exists(path))
337 Directory.CreateDirectory(path);
343 path = path.Replace(
"\\\\?\\",
"");
344 if (Directory.Exists(path))
346 DirectoryInfo directoryInfo =
new DirectoryInfo(path);
349 new FileIOPermission(FileIOPermissionAccess.AllAccess, path).Demand();
351 catch (SecurityException ex)
353 Debug.Log(ex.ToString());
355 if (directoryInfo.Exists)
357 directoryInfo.Delete(recursive:
true);
362 public static T Duplicate<T>(T t)
364 return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(t, formatting, jsWriteGeneral), jsReadGeneral);
377 public static T LoadObject<T>(FileInfo file,
object option =
null) where T : UnityEngine.Object
379 return LoadObject<T>(file.FullName, option);
382 public static T LoadObject<T>(
string _path,
object option =
null) where T : UnityEngine.Object
384 Type typeFromHandle = typeof(T);
385 if (typeFromHandle == typeof(Sprite))
388 Texture2D texture2D =
LoadPNG(_path);
393 return Sprite.Create(texture2D,
new Rect(0f, 0f, texture2D.width, texture2D.height), spriteLoadOption?.
pivot ??
new Vector2(0.5f, 0f), 100f) as T;
395 if (typeFromHandle == typeof(Texture2D))
399 if (typeof(
ExcelData).IsAssignableFrom(typeFromHandle))
401 T val = Activator.CreateInstance<T>();
405 if (typeFromHandle == typeof(
TextData))
409 lines = File.ReadAllLines(_path)
415 public static void SavePNG(Texture2D tex,
string _path)
417 byte[] bytes = tex.EncodeToPNG();
418 File.WriteAllBytes(_path, bytes);
421 public static Texture2D
LoadPNG(
string _path, FilterMode filter = FilterMode.Point)
423 if (!File.Exists(_path))
430 for (
int i = 0; i < 4; i++)
432 if (num + 1 < array.Length)
434 num2 = num2 * 256 + array[num++];
438 for (
int j = 0; j < 4; j++)
440 if (num + 1 < array.Length)
442 num3 = num3 * 256 + array[num++];
446 Texture2D texture2D =
new Texture2D(num2, num3, data.
format, data.
mipmap, data.
linear);
447 texture2D.LoadImage(array);
449 texture2D.filterMode = filter;
457 FileStream fileStream =
new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
458 BinaryReader binaryReader =
new BinaryReader(fileStream);
459 byte[] result = binaryReader.ReadBytes((
int)binaryReader.BaseStream.Length);
460 binaryReader.Close();
465 public static T DeepCopy<T>(T target)
467 return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(target, dpFormat, dpSetting), dpSetting);
472 if (!File.Exists(_path))
475 if (!File.Exists(_path))
478 return new string[0];
481 return File.ReadAllLines(_path);
488 string[] array2 = array;
489 foreach (
string text2
in array2)
491 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 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 WriteLZ4(string _path, byte[] _bytes)
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 readonly ShouldSerializeContractResolver Instance