FileSystem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using System.Text;
  6. public class FileSystem
  7. {
  8. private static string CachedSerializeRootPath;
  9. public static string serializeRootPath
  10. {
  11. get
  12. {
  13. if (CachedSerializeRootPath == null)
  14. {
  15. #if UNITY_EDITOR
  16. string folder = string.Format("{0}/../../serialize/", Application.dataPath);
  17. #elif UNITY_STANDALONE
  18. string folder = string.Format("{0}/../serialize/", Application.dataPath);
  19. #else
  20. string folder = string.Format("{0}/serialize/", Application.persistentDataPath);
  21. #endif
  22. if (!Directory.Exists(folder))
  23. {
  24. Directory.CreateDirectory(folder);
  25. }
  26. CachedSerializeRootPath = folder;
  27. }
  28. return CachedSerializeRootPath;
  29. }
  30. }
  31. /// <summary>
  32. /// 依据不同平台,获取assetbunldes目录
  33. /// </summary>
  34. ///
  35. private static string m_PackagePath = string.Empty;
  36. public static string PackagePath()
  37. {
  38. if (string.IsNullOrEmpty(m_PackagePath))
  39. {
  40. #if UNITY_IPHONE
  41. #if UNITY_EDITOR
  42. m_PackagePath = Application.streamingAssetsPath + "/ios/";
  43. #else
  44. m_PackagePath = Application.dataPath + "/Raw/ios/";
  45. #endif
  46. #elif UNITY_ANDROID
  47. #if UNITY_EDITOR
  48. m_PackagePath = Application.streamingAssetsPath + "/AssetsAndroid/";
  49. #else
  50. m_PackagePath = Application.dataPath + "!/assets/AssetsAndroid/";
  51. #endif
  52. #else
  53. m_PackagePath = Application.streamingAssetsPath + "/AssetsPC/";
  54. #endif
  55. }
  56. return m_PackagePath;
  57. }
  58. private static string m_LocalPackagePath = string.Empty;
  59. public static string LocalPackagePath
  60. {
  61. get
  62. {
  63. if (string.IsNullOrEmpty(m_LocalPackagePath))
  64. {
  65. m_LocalPackagePath = PackagePath();
  66. #if UNITY_EDITOR //editor
  67. //m_LocalPackagePath = "file://" + m_LocalPackagePath;
  68. #elif UNITY_ANDROID //android release;
  69. m_LocalPackagePath = "jar:file://" + m_LocalPackagePath;
  70. #elif UNITY_IPHONE //ios release;
  71. //m_LocalPackagePath = "file://" + m_LocalPackagePath;
  72. #else //pc release;
  73. m_PackagePath = Application.streamingAssetsPath + "/AssetsPC/";
  74. #endif
  75. }
  76. return m_LocalPackagePath;
  77. }
  78. }
  79. public static string LocalUrlPath
  80. {
  81. get
  82. {
  83. if (string.IsNullOrEmpty(m_LocalPackagePath))
  84. {
  85. m_LocalPackagePath = PackagePath();
  86. #if UNITY_EDITOR //editor
  87. m_LocalPackagePath = "file://" + m_LocalPackagePath;
  88. #elif UNITY_ANDROID //android release;
  89. m_LocalPackagePath = "jar:file://" + m_LocalPackagePath;
  90. #elif UNITY_IPHONE || UNITY_IOS
  91. m_LocalPackagePath = "file://" + m_LocalPackagePath;
  92. #else //pc release;
  93. m_PackagePath = Application.streamingAssetsPath + "/AssetsPC/";
  94. #endif
  95. }
  96. return m_LocalPackagePath;
  97. }
  98. }
  99. private static string m_LocalDocumentPath = string.Empty;
  100. public static string LocalDocumentPath
  101. {
  102. get
  103. {
  104. if (string.IsNullOrEmpty(m_LocalDocumentPath))
  105. {
  106. m_LocalDocumentPath = GetUserDocumentPath();
  107. #if UNITY_IPHONE
  108. #if UNITY_EDITOR
  109. m_LocalDocumentPath = Application.dataPath + "/../Local/ios/";
  110. #else
  111. m_LocalDocumentPath = m_LocalDocumentPath + "ios/";
  112. #endif
  113. #elif UNITY_ANDROID
  114. #if UNITY_EDITOR
  115. m_LocalDocumentPath = Application.dataPath + "/../Local/AssetsAndroid/";
  116. #else
  117. m_LocalDocumentPath = m_LocalDocumentPath + "AssetsAndroid/";
  118. #endif
  119. #else
  120. m_LocalDocumentPath = Application.streamingAssetsPath +"/AssetsPC/";
  121. #endif
  122. }
  123. return m_LocalDocumentPath;
  124. }
  125. }
  126. private static string m_TempDocumentPath = string.Empty;
  127. public static string TempDocumentPath
  128. {
  129. get
  130. {
  131. if (string.IsNullOrEmpty(m_TempDocumentPath))
  132. {
  133. m_TempDocumentPath = GetUserDocumentPath();
  134. #if UNITY_IPHONE
  135. #if UNITY_EDITOR
  136. m_TempDocumentPath = Application.dataPath + "/../Local/Temp/ios/";
  137. #else
  138. m_TempDocumentPath = m_TempDocumentPath + "/Temp/ios/";
  139. #endif
  140. #elif UNITY_ANDROID
  141. #if UNITY_EDITOR
  142. m_TempDocumentPath = Application.dataPath + "/../Local/Temp/AssetsAndroid/";
  143. #else
  144. m_TempDocumentPath = m_TempDocumentPath + "/Temp/AssetsAndroid/";
  145. #endif
  146. #else
  147. m_TempDocumentPath = Application.streamingAssetsPath +"/Temp/AssetsPC/";
  148. #endif
  149. }
  150. return m_TempDocumentPath;
  151. }
  152. }
  153. private static string m_PlatformPath = string.Empty;
  154. public static string PlatformPath
  155. {
  156. get
  157. {
  158. if (string.IsNullOrEmpty(m_PlatformPath))
  159. {
  160. #if UNITY_IPHONE
  161. m_PlatformPath = "ios/";
  162. #elif UNITY_ANDROID
  163. m_PlatformPath = "AssetsAndroid/";
  164. #else
  165. m_PlatformPath = "AssetsPC/";
  166. #endif
  167. }
  168. return m_PlatformPath;
  169. }
  170. }
  171. private static string m_PatchConfigFile = string.Empty;
  172. public static string PatchConfigFile
  173. {
  174. get
  175. {
  176. if (string.IsNullOrEmpty(m_PatchConfigFile))
  177. {
  178. #if UNITY_EDITOR //editor
  179. m_PatchConfigFile = string.Format("{0}/appbuildconfig.xml", Application.dataPath);
  180. #else
  181. m_PatchConfigFile = string.Format("{0}appbuildconfig.xml", FileSystem.LocalPackagePath);
  182. #endif
  183. }
  184. return m_PatchConfigFile;
  185. }
  186. }
  187. /// <summary>
  188. /// 依据不同平台,获取用户文档目录,即app可自由读写的目录
  189. /// </summary>
  190. public static string GetUserDocumentPath()
  191. {
  192. string path = null;
  193. #if UNITY_IPHONE
  194. path = GetiPhoneDocumentsPath();
  195. #elif UNITY_ANDROID
  196. path = GetAndroidDocumentsPath();
  197. #else
  198. path = GetWindowsDocumentsPath();
  199. #endif
  200. return path;
  201. }
  202. /// <summary>
  203. /// 判断指定文件是否存在
  204. /// </summary>
  205. /// <returns>true 表示存在</returns>
  206. public static bool Exists(string file_path)
  207. {
  208. bool is_exist = false;
  209. is_exist = File.Exists(file_path);
  210. return is_exist;
  211. }
  212. private static string GetiPhoneDocumentsPath()
  213. {
  214. // Your game has read+write access to /var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/Library
  215. // Application.dataPath returns
  216. // /var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/myappname.app/Data
  217. //string path = Application.dataPath.Substring(0, Application.dataPath.Length - 5);
  218. //path = path.Substring(0, path.LastIndexOf('/'));
  219. //return path + "/Library/";
  220. //return path + "/Documents/";
  221. return Application.persistentDataPath + "/";
  222. }
  223. private static string GetAndroidDocumentsPath()
  224. {
  225. return Application.persistentDataPath + "/";//"";//
  226. }
  227. private static string GetWindowsDocumentsPath()
  228. {
  229. string path = Application.dataPath + "/../";
  230. return path;
  231. }
  232. private static bool CheckEXT(string fileName )
  233. {
  234. if (fileName.EndsWith(".prefab") || fileName.EndsWith(".txt") || fileName.EndsWith(".xml") ||
  235. fileName.EndsWith(".lua") || fileName.EndsWith(".csv") ||
  236. fileName.EndsWith(".ogg") || fileName.EndsWith(".wav")||
  237. fileName.EndsWith(".TTF") || fileName.EndsWith(".ttf") ||
  238. fileName.EndsWith(".bytes") || fileName.EndsWith(".pb"))
  239. return true;
  240. else
  241. return false;
  242. }
  243. public static List<string> getAllFilesPath(string path, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
  244. {
  245. List<string> all = new List<string>();
  246. string[] allFiles = Directory.GetFiles(path, searchPattern, searchOption);
  247. for (int j = 0; j < allFiles.Length; ++j)
  248. {
  249. string fileName = allFiles[j];
  250. if (CheckEXT(fileName))
  251. {
  252. fileName = fileName.Replace(Application.dataPath, "Assets");
  253. all.Add(fileName);
  254. }
  255. }
  256. return all;
  257. }
  258. public static List<string> getAllDirPath(string path, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
  259. {
  260. List<string> all = new List<string>();
  261. string[] allFiles = Directory.GetDirectories(path, searchPattern, searchOption);
  262. for (int j = 0; j < allFiles.Length; ++j)
  263. {
  264. string fileName = allFiles[j];
  265. all.Add(fileName);
  266. }
  267. return all;
  268. }
  269. public static List<string> getAllFilesPathEX(string path,string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
  270. {
  271. List<string> all = new List<string>();
  272. string[] allFiles = Directory.GetFiles(path, searchPattern, searchOption);
  273. for (int j = 0; j < allFiles.Length; ++j)
  274. {
  275. string fileName = allFiles[j];
  276. if (CheckEXT(fileName))
  277. {
  278. all.Add(fileName);
  279. }
  280. }
  281. return all;
  282. }
  283. public static string ReadFileLineOne(string path)
  284. {
  285. string[] result = null;
  286. try
  287. {
  288. result = File.ReadAllLines(path);
  289. if(result.Length > 0)
  290. {
  291. return result[0];
  292. }
  293. }
  294. catch (Exception e)
  295. {
  296. DebugHelper.LogError(e);
  297. }
  298. return string.Empty;
  299. }
  300. public static Byte[] ReadFileByte(string path)
  301. {
  302. Byte[] result = null;
  303. try
  304. {
  305. result = File.ReadAllBytes(path);
  306. }
  307. catch (Exception e)
  308. {
  309. DebugHelper.LogError(e);
  310. }
  311. return result;
  312. }
  313. public static string[] ReadFileLines(string path)
  314. {
  315. string[] result = null;
  316. try
  317. {
  318. result = File.ReadAllLines(path);
  319. }
  320. catch (Exception e)
  321. {
  322. DebugHelper.LogError(e);
  323. }
  324. return result;
  325. }
  326. public static void WriteAllBytes(string path, byte[] data)
  327. {
  328. WriteAllBytes(path, data, 0, data.Length);
  329. }
  330. public static void WriteAllBytes(string path, byte[] data, int start, int count)
  331. {
  332. string directoryName = Path.GetDirectoryName(path);
  333. CreateDirIfNotExist(directoryName);
  334. if (File.Exists(path))
  335. {
  336. File.Delete(path);
  337. }
  338. using (FileStream fileStream = File.OpenWrite(path))
  339. {
  340. fileStream.Write(data, start, count);
  341. fileStream.Close();
  342. }
  343. }
  344. public static void CreateDirIfNotExist(string dir)
  345. {
  346. if (!Directory.Exists(dir))
  347. {
  348. Directory.CreateDirectory(dir);
  349. }
  350. }
  351. public static void ReleaseFilesFromDirToDir(string from,string to)
  352. {
  353. CopyFolder(from, to);
  354. }
  355. /// <summary>
  356. /// 获取文件名字,去掉文件的目录
  357. /// </summary>
  358. /// <param name="filePath"></param>
  359. /// <returns></returns>
  360. public static string ExtractPureName(string filePath)
  361. {
  362. filePath = filePath.Replace('\\', '/');
  363. int pos = filePath.LastIndexOf("/");
  364. if (pos == -1)
  365. return filePath;
  366. return filePath.Substring(pos + 1, filePath.Length - pos - 1);
  367. }
  368. /// <summary>
  369. /// 去掉文件后缀
  370. /// </summary>
  371. /// <param name="fileName">文件名字</param>
  372. /// <returns></returns>
  373. public static string RemoveExtension(string fileName)
  374. {
  375. int pos = fileName.LastIndexOf(".");
  376. if (pos == -1)
  377. return fileName;
  378. return fileName.Substring(0, pos);
  379. }
  380. private static void CopyFolder(string from, string to)
  381. {
  382. string[] dirs = Directory.GetDirectories(from);
  383. FileSystem.CreateDirIfNotExist(to);
  384. foreach (string sub in dirs)
  385. {
  386. string path = to + Path.GetFileName(sub);
  387. if (!Directory.Exists(path))
  388. Directory.CreateDirectory(to + Path.GetFileName(sub));
  389. CopyFolder(sub + "\\", path + "\\");
  390. }
  391. string[] files = Directory.GetFiles(from);
  392. foreach (string file in files)
  393. File.Copy(file, to + Path.GetFileName(file), true);
  394. }
  395. }