| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- using System;
- using System.IO;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Text;
- public class FileSystem
- {
- private static string CachedSerializeRootPath;
- public static string serializeRootPath
- {
- get
- {
- if (CachedSerializeRootPath == null)
- {
- #if UNITY_EDITOR
- string folder = string.Format("{0}/../../serialize/", Application.dataPath);
- #elif UNITY_STANDALONE
- string folder = string.Format("{0}/../serialize/", Application.dataPath);
- #else
- string folder = string.Format("{0}/serialize/", Application.persistentDataPath);
- #endif
- if (!Directory.Exists(folder))
- {
- Directory.CreateDirectory(folder);
- }
- CachedSerializeRootPath = folder;
- }
- return CachedSerializeRootPath;
- }
- }
- /// <summary>
- /// 依据不同平台,获取assetbunldes目录
- /// </summary>
- ///
- private static string m_PackagePath = string.Empty;
- public static string PackagePath()
- {
- if (string.IsNullOrEmpty(m_PackagePath))
- {
- #if UNITY_IPHONE
- #if UNITY_EDITOR
- m_PackagePath = Application.streamingAssetsPath + "/ios/";
- #else
- m_PackagePath = Application.dataPath + "/Raw/ios/";
- #endif
- #elif UNITY_ANDROID
- #if UNITY_EDITOR
- m_PackagePath = Application.streamingAssetsPath + "/AssetsAndroid/";
- #else
- m_PackagePath = Application.dataPath + "!/assets/AssetsAndroid/";
- #endif
- #else
- m_PackagePath = Application.streamingAssetsPath + "/AssetsPC/";
- #endif
- }
- return m_PackagePath;
- }
- private static string m_LocalPackagePath = string.Empty;
- public static string LocalPackagePath
- {
- get
- {
- if (string.IsNullOrEmpty(m_LocalPackagePath))
- {
- m_LocalPackagePath = PackagePath();
- #if UNITY_EDITOR //editor
- //m_LocalPackagePath = "file://" + m_LocalPackagePath;
- #elif UNITY_ANDROID //android release;
- m_LocalPackagePath = "jar:file://" + m_LocalPackagePath;
- #elif UNITY_IPHONE //ios release;
- //m_LocalPackagePath = "file://" + m_LocalPackagePath;
- #else //pc release;
- m_PackagePath = Application.streamingAssetsPath + "/AssetsPC/";
- #endif
- }
- return m_LocalPackagePath;
- }
- }
- public static string LocalUrlPath
- {
- get
- {
- if (string.IsNullOrEmpty(m_LocalPackagePath))
- {
- m_LocalPackagePath = PackagePath();
- #if UNITY_EDITOR //editor
- m_LocalPackagePath = "file://" + m_LocalPackagePath;
- #elif UNITY_ANDROID //android release;
- m_LocalPackagePath = "jar:file://" + m_LocalPackagePath;
- #elif UNITY_IPHONE || UNITY_IOS
- m_LocalPackagePath = "file://" + m_LocalPackagePath;
- #else //pc release;
- m_PackagePath = Application.streamingAssetsPath + "/AssetsPC/";
- #endif
- }
- return m_LocalPackagePath;
- }
- }
- private static string m_LocalDocumentPath = string.Empty;
- public static string LocalDocumentPath
- {
- get
- {
- if (string.IsNullOrEmpty(m_LocalDocumentPath))
- {
- m_LocalDocumentPath = GetUserDocumentPath();
- #if UNITY_IPHONE
- #if UNITY_EDITOR
- m_LocalDocumentPath = Application.dataPath + "/../Local/ios/";
- #else
- m_LocalDocumentPath = m_LocalDocumentPath + "ios/";
- #endif
- #elif UNITY_ANDROID
- #if UNITY_EDITOR
- m_LocalDocumentPath = Application.dataPath + "/../Local/AssetsAndroid/";
- #else
- m_LocalDocumentPath = m_LocalDocumentPath + "AssetsAndroid/";
- #endif
- #else
- m_LocalDocumentPath = Application.streamingAssetsPath +"/AssetsPC/";
- #endif
- }
- return m_LocalDocumentPath;
- }
- }
- private static string m_TempDocumentPath = string.Empty;
- public static string TempDocumentPath
- {
- get
- {
- if (string.IsNullOrEmpty(m_TempDocumentPath))
- {
- m_TempDocumentPath = GetUserDocumentPath();
- #if UNITY_IPHONE
- #if UNITY_EDITOR
- m_TempDocumentPath = Application.dataPath + "/../Local/Temp/ios/";
- #else
- m_TempDocumentPath = m_TempDocumentPath + "/Temp/ios/";
- #endif
- #elif UNITY_ANDROID
- #if UNITY_EDITOR
- m_TempDocumentPath = Application.dataPath + "/../Local/Temp/AssetsAndroid/";
- #else
- m_TempDocumentPath = m_TempDocumentPath + "/Temp/AssetsAndroid/";
- #endif
- #else
- m_TempDocumentPath = Application.streamingAssetsPath +"/Temp/AssetsPC/";
- #endif
- }
- return m_TempDocumentPath;
- }
- }
- private static string m_PlatformPath = string.Empty;
- public static string PlatformPath
- {
- get
- {
- if (string.IsNullOrEmpty(m_PlatformPath))
- {
- #if UNITY_IPHONE
- m_PlatformPath = "ios/";
- #elif UNITY_ANDROID
- m_PlatformPath = "AssetsAndroid/";
- #else
- m_PlatformPath = "AssetsPC/";
- #endif
- }
- return m_PlatformPath;
- }
- }
- private static string m_PatchConfigFile = string.Empty;
- public static string PatchConfigFile
- {
- get
- {
- if (string.IsNullOrEmpty(m_PatchConfigFile))
- {
- #if UNITY_EDITOR //editor
- m_PatchConfigFile = string.Format("{0}/appbuildconfig.xml", Application.dataPath);
- #else
- m_PatchConfigFile = string.Format("{0}appbuildconfig.xml", FileSystem.LocalPackagePath);
- #endif
- }
- return m_PatchConfigFile;
- }
- }
- /// <summary>
- /// 依据不同平台,获取用户文档目录,即app可自由读写的目录
- /// </summary>
- public static string GetUserDocumentPath()
- {
- string path = null;
- #if UNITY_IPHONE
- path = GetiPhoneDocumentsPath();
- #elif UNITY_ANDROID
- path = GetAndroidDocumentsPath();
- #else
- path = GetWindowsDocumentsPath();
- #endif
- return path;
- }
- /// <summary>
- /// 判断指定文件是否存在
- /// </summary>
- /// <returns>true 表示存在</returns>
- public static bool Exists(string file_path)
- {
- bool is_exist = false;
- is_exist = File.Exists(file_path);
- return is_exist;
- }
- private static string GetiPhoneDocumentsPath()
- {
- // Your game has read+write access to /var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/Library
- // Application.dataPath returns
- // /var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/myappname.app/Data
- //string path = Application.dataPath.Substring(0, Application.dataPath.Length - 5);
- //path = path.Substring(0, path.LastIndexOf('/'));
- //return path + "/Library/";
- //return path + "/Documents/";
- return Application.persistentDataPath + "/";
- }
- private static string GetAndroidDocumentsPath()
- {
- return Application.persistentDataPath + "/";//"";//
- }
- private static string GetWindowsDocumentsPath()
- {
- string path = Application.dataPath + "/../";
- return path;
- }
- private static bool CheckEXT(string fileName )
- {
- if (fileName.EndsWith(".prefab") || fileName.EndsWith(".txt") || fileName.EndsWith(".xml") ||
- fileName.EndsWith(".lua") || fileName.EndsWith(".csv") ||
- fileName.EndsWith(".ogg") || fileName.EndsWith(".wav")||
- fileName.EndsWith(".TTF") || fileName.EndsWith(".ttf") ||
- fileName.EndsWith(".bytes") || fileName.EndsWith(".pb"))
- return true;
- else
- return false;
- }
- public static List<string> getAllFilesPath(string path, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
- {
- List<string> all = new List<string>();
- string[] allFiles = Directory.GetFiles(path, searchPattern, searchOption);
- for (int j = 0; j < allFiles.Length; ++j)
- {
- string fileName = allFiles[j];
- if (CheckEXT(fileName))
- {
- fileName = fileName.Replace(Application.dataPath, "Assets");
- all.Add(fileName);
- }
- }
- return all;
- }
- public static List<string> getAllDirPath(string path, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
- {
- List<string> all = new List<string>();
- string[] allFiles = Directory.GetDirectories(path, searchPattern, searchOption);
- for (int j = 0; j < allFiles.Length; ++j)
- {
- string fileName = allFiles[j];
- all.Add(fileName);
- }
- return all;
- }
- public static List<string> getAllFilesPathEX(string path,string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
- {
- List<string> all = new List<string>();
- string[] allFiles = Directory.GetFiles(path, searchPattern, searchOption);
- for (int j = 0; j < allFiles.Length; ++j)
- {
- string fileName = allFiles[j];
- if (CheckEXT(fileName))
- {
- all.Add(fileName);
- }
- }
- return all;
- }
- public static string ReadFileLineOne(string path)
- {
- string[] result = null;
- try
- {
- result = File.ReadAllLines(path);
- if(result.Length > 0)
- {
- return result[0];
- }
- }
- catch (Exception e)
- {
- DebugHelper.LogError(e);
- }
- return string.Empty;
- }
- public static Byte[] ReadFileByte(string path)
- {
- Byte[] result = null;
- try
- {
- result = File.ReadAllBytes(path);
- }
- catch (Exception e)
- {
- DebugHelper.LogError(e);
- }
- return result;
- }
- public static string[] ReadFileLines(string path)
- {
- string[] result = null;
- try
- {
- result = File.ReadAllLines(path);
- }
- catch (Exception e)
- {
- DebugHelper.LogError(e);
- }
- return result;
- }
- public static void WriteAllBytes(string path, byte[] data)
- {
- WriteAllBytes(path, data, 0, data.Length);
- }
- public static void WriteAllBytes(string path, byte[] data, int start, int count)
- {
- string directoryName = Path.GetDirectoryName(path);
- CreateDirIfNotExist(directoryName);
- if (File.Exists(path))
- {
- File.Delete(path);
- }
- using (FileStream fileStream = File.OpenWrite(path))
- {
- fileStream.Write(data, start, count);
- fileStream.Close();
- }
- }
- public static void CreateDirIfNotExist(string dir)
- {
- if (!Directory.Exists(dir))
- {
- Directory.CreateDirectory(dir);
- }
- }
- public static void ReleaseFilesFromDirToDir(string from,string to)
- {
- CopyFolder(from, to);
- }
- /// <summary>
- /// 获取文件名字,去掉文件的目录
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public static string ExtractPureName(string filePath)
- {
- filePath = filePath.Replace('\\', '/');
- int pos = filePath.LastIndexOf("/");
- if (pos == -1)
- return filePath;
- return filePath.Substring(pos + 1, filePath.Length - pos - 1);
- }
- /// <summary>
- /// 去掉文件后缀
- /// </summary>
- /// <param name="fileName">文件名字</param>
- /// <returns></returns>
- public static string RemoveExtension(string fileName)
- {
- int pos = fileName.LastIndexOf(".");
- if (pos == -1)
- return fileName;
- return fileName.Substring(0, pos);
- }
- private static void CopyFolder(string from, string to)
- {
- string[] dirs = Directory.GetDirectories(from);
- FileSystem.CreateDirIfNotExist(to);
- foreach (string sub in dirs)
- {
- string path = to + Path.GetFileName(sub);
- if (!Directory.Exists(path))
- Directory.CreateDirectory(to + Path.GetFileName(sub));
- CopyFolder(sub + "\\", path + "\\");
- }
- string[] files = Directory.GetFiles(from);
- foreach (string file in files)
- File.Copy(file, to + Path.GetFileName(file), true);
- }
- }
|