/// <summary> /// Check target file exist /// </summary> public static Boolean ExistFile(String path) { if (path.IsNullOrEmpty()) { BenLogger.Error("ExistFile Path is empty!"); return false; } return File.Exists(path); }
/// <summary> /// Get json data by read text(First Cache, Second Resources) /// </summary> public static Boolean TryReadTextByAll(String path, out JsonData totalJson) { totalJson = null; if (File.Exists(path)) { Boolean isRet = false; String jsonStr = ReadTextByStream(path); if (false == jsonStr.IsNullOrEmpty() && jsonStr != "\r" && jsonStr != "\n" && jsonStr != "\r\n") { try { totalJson = JsonMapper.ToObject(jsonStr); isRet = true; } catch (Exception ex) { BenLogger.Error("Resources info convert to <LitJson.JsonData> error, delete file! Path: " + path + " || Exception: " + ex.Message); isRet = false; } } else { BenLogger.Error("File exist, but info is null, delete file! Path: " + path); isRet = false; } if (totalJson == null) { DeleteFile(path); isRet = TryReadTextByResource(DirectoryConst.TABLE + path.LastSlashToPoint(), out totalJson); } return isRet; } else { return TryReadTextByResource(DirectoryConst.TABLE + path.LastSlashToPoint(), out totalJson); } }
/// <summary> /// Check Directory /// </summary> public static void CheckDirectory(String path) { if (path.IsNullOrEmpty()) { BenLogger.Error("CheckDirectory Path is empty!"); return; } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } }
/// <summary> /// Write string by IO.FileStream /// </summary> public static void WriteTextByIO(String path, String info) { try { FileStream fs = new FileStream(path, FileMode.Create); Byte[] data = ENCODING_FORMAT.GetBytes(info); fs.Write(data, 0, data.Length); fs.Flush(); fs.Dispose(); fs = null; } catch (Exception e) { BenLogger.Error("The file could not be write! Path: " + path + " || Message: " + e.Message); } }
/// <summary> /// Delete target file /// </summary> public static void DeleteFile(String path) { if (path.IsNullOrEmpty()) { BenLogger.Error("DeleteFile Path is empty!"); return; } if (File.Exists(path)) { File.Delete(path); } CheckDirectory(path.StartToLastSlash()); }
/// <summary> /// Get Sprite by Unity WWW /// </summary> public static IEnumerator GetSpriteByWWW(String srcPath, Sprite sprite) { WWW www = new WWW(srcPath); yield return www; if (www != null && www.error.IsNullOrEmpty()) { Texture2D texture = www.texture; sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), VECTOR2_CENTER); } else { BenLogger.Error("Load Error Path: " + srcPath); } www.Dispose(); www = null; }
/// <summary> /// Get string by IO.FileStream /// </summary> public static String ReadTextByIO(String path) { if (File.Exists(path)) { Byte[] byteArray = _GetBytesByStream(path); if (byteArray != null) { return ENCODING_FORMAT.GetString(byteArray); } else { BenLogger.Error("The file could not be read! Path: " + path); } } else { BenLogger.Error("No exist file! Path: " + path); } return ""; }
/// <summary> /// Write string by StreamWriter /// </summary> public static void WriteTextByStream(String path, String info) { try { FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs, ENCODING_FORMAT); sw.Write(info); sw.Flush(); sw.Dispose(); sw = null; fs = null; } catch (Exception e) { BenLogger.Error("The file could not be write! Path: " + path + " || Message: " + e.Message); } }
/// <summary> /// Get string by read Resources text /// </summary> public static Boolean TryReadTextByResource(String relativePath, out String info) { info = null; TextAsset textAsset = Resources.Load(relativePath) as TextAsset; if (textAsset != null) { info = textAsset.text; } if (false == info.IsNullOrEmpty()) { return true; } else { BenLogger.Error("Resources info is null!Path: " + relativePath); return false; } }
/// <summary> /// Get string by read text(First Cache, Then Resources) /// </summary> public static Boolean TryReadTextByAll(String path, out String info) { info = null; if (File.Exists(path)) { info = ReadTextByStream(path); if (false == info.IsNullOrEmpty()) { return true; } else { BenLogger.Error("File exist, but info is null, delete file! Path: " + path); return false; } } else { return TryReadTextByResource(DirectoryConst.TABLE + path.LastSlashToPoint(), out info); } }
/// <summary> /// Get string by read text /// </summary> public static Boolean TryReadText(String path, out String info) { if (File.Exists(path)) { info = ReadTextByStream(path); if (false == info.IsNullOrEmpty()) { return true; } else { DeleteFile(path); BenLogger.Error("File exist, but info is null, delete file! Path: " + path); return false; } } else { info = ""; return false; } }
/// <summary> /// Get byte array by IO.FileStream /// </summary> static Byte[] _GetBytesByStream(String path) { if (File.Exists(path)) { try { FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); fileStream.Seek(0, SeekOrigin.Begin); Byte[] bytes = new Byte[fileStream.Length]; fileStream.Read(bytes, 0, (Int32)fileStream.Length); fileStream.Dispose(); fileStream = null; return bytes; } catch (Exception e) { BenLogger.Error("The file could not be read! Path: " + path + " || Message: " + e.Message); } } else { BenLogger.Error("No Exist File! Path: " + path); } return null; }
/// <summary> /// Clear all file and directory of the target directory /// </summary> public static void ClearDirectoryFiles(String path) { try { DirectoryInfo dir = new DirectoryInfo(path); FileSystemInfo[] fileInfo = dir.GetFileSystemInfos(); foreach (FileSystemInfo i in fileInfo) { if (i is DirectoryInfo) { DirectoryInfo subDir = new DirectoryInfo(i.FullName); subDir.Delete(true); } else { File.Delete(i.FullName); } } BenLogger.Error("Delete Path: " + path + " All Info Success!"); } catch (Exception e) { BenLogger.Error("FileClearDir: " + path + " Exception: " + e.Message); throw; } }
/// <summary> /// Get json data by read text /// </summary> public static Boolean TryReadText(String path, out JsonData totalJson) { if (File.Exists(path)) { String jsonStr = ReadTextByStream(path); if (false == jsonStr.IsNullOrEmpty() && jsonStr != "\r" && jsonStr != "\n" && jsonStr != "\r\n") { try { totalJson = JsonMapper.ToObject(jsonStr); return true; } catch (Exception ex) { DeleteFile(path); BenLogger.Error("File convert to <LitJson.JsonData> error, delete file! Path: " + path + " || Exception: " + ex.Message); } } else { DeleteFile(path); BenLogger.Error("File exist, but info is null, delete file! Path: " + path); } } totalJson = null; return false; }
/// <summary> /// Get string by StreamReader /// </summary> public static String ReadTextByStream(String path) { String result = ""; if (File.Exists(path)) { try { //using (StreamReader sr = new StreamReader(path, ENCODING_FORMAT)) { // result = sr.ReadToEnd(); //} StreamReader sr = new StreamReader(path, ENCODING_FORMAT); result = sr.ReadToEnd(); sr.Dispose(); sr = null; } catch (Exception e) { BenLogger.Error("The file could not be read! Path: " + path + " || Message: " + e.Message); } } else { BenLogger.Error("No exist file! Path: " + path); } return result; }
/// <summary> /// Get string by read line text /// </summary> static String ReadTextByStreamLine(String path) { String result = ""; if (File.Exists(path)) { try { StreamReader sr = new StreamReader(path, ENCODING_FORMAT); String line; while ((line = sr.ReadLine()) != null) { result += line; } sr.Dispose(); sr = null; } catch (Exception e) { BenLogger.Error("The file could not be read! Path: " + path + " || Message: " + e.Message); } } else { BenLogger.Error("No exist file! Path: " + path); } return result; }
/// <summary> /// Get json data by read Resources text /// </summary> public static Boolean TryReadTextByResource(String relativePath, out JsonData totalJson) { String jsonStr = null; TextAsset textAsset = Resources.Load(relativePath) as TextAsset; if (textAsset != null) { jsonStr = textAsset.text; if (false == jsonStr.IsNullOrEmpty()) { try { totalJson = JsonMapper.ToObject(jsonStr); return true; } catch (Exception ex) { BenLogger.Error("Resources info convert to <LitJson.JsonData> error, delete file! Path: " + relativePath + " || Exception: " + ex.Message); } } else { BenLogger.Error("Resources info is null!Path: " + relativePath); } } totalJson = null; return false; }
/// <summary> /// Get node by read text /// </summary> public static Boolean TryReadText(String path, out XmlNode rootNode) { rootNode = null; StreamReader sr; XmlDocument xmlDoc = new XmlDocument(); if (!File.Exists(path)) { return false; } else if (CheckTextIsNull(path)) { DeleteFile(path); BenLogger.Error("File exist, but info is null, delete file! Path: " + path); return false; } else { sr = new StreamReader(path, ENCODING_FORMAT); XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; XmlReader reader = XmlReader.Create(sr, settings); xmlDoc.Load(reader); } rootNode = xmlDoc.SelectSingleNode("Data"); if (rootNode == null) { sr.Close(); xmlDoc = null; DeleteFile(path); BenLogger.Error("XML root node <Data> is not exist, delete file! Path: " + path); return false; } if (rootNode.ChildNodes.Count == 0) { sr.Close(); rootNode = null; xmlDoc = null; DeleteFile(path); BenLogger.Error("XML root node <Data> info is null, delete file! Path: " + path); return false; } sr.Close(); return true; }
/// <summary> /// Get Selection GameObject MeshRenderer Bones /// </summary> public static Transform[] GetMeshRendererBones(this Transform trans, Transform skinRootTrans) { List<Transform> resultBones = new List<Transform>(); if (trans != null) { List<Transform> smrBones = new List<Transform>(), aloneBones = new List<Transform>(), bodyBones = new List<Transform>(); // All SkinnedMeshRenderer foreach (SkinnedMeshRenderer smr in trans.GetComponentsInChildren<SkinnedMeshRenderer>(true)) { smrBones.AddRange(smr.bones); } smrBones.RemoveNull(); smrBones = smrBones.Distinct().ToList(); // Child index List<int> childIndexList = new List<int>(); for (int i = 0; i < smrBones.Count; ++i) { for (int j = 0; j < smrBones.Count; ++j) { if (i != j) { if (smrBones[i] == smrBones[j].parent) { childIndexList.Add(j); } } } } // Remove the child of the parent in the bonelist for (int i = childIndexList.Count - 1; i >= 0; --i) { smrBones.RemoveAt(i); } // Find the node of bip foreach (Transform bone in smrBones) { bool bodyBip = false, aloneBone = false; Transform temp = bone; while (true) { if (temp.name.Equals("Bip001")) { bodyBip = true; break; } // Alone bone root transform if (temp.parent == skinRootTrans) { aloneBone = true; break; } if (temp.parent == null) { BenLogger.Error("Parent is null!"); break; } temp = temp.parent; } if (bodyBip) { bodyBones.Add(bone); } else { if (aloneBone) { aloneBones.Add(temp); } else { BenLogger.Debug("Bone < " + bone.name + " > Top Parent is null!"); } } } bodyBones = bodyBones.Distinct().ToList(); aloneBones = aloneBones.Distinct().ToList(); resultBones.AddRange(bodyBones); resultBones.AddRange(aloneBones); } return resultBones.ToArray(); }