FileSystemManager.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. namespace GameFramework.FileSystem
  11. {
  12. /// <summary>
  13. /// 文件系统管理器。
  14. /// </summary>
  15. internal sealed class FileSystemManager : GameFrameworkModule, IFileSystemManager
  16. {
  17. private readonly Dictionary<string, FileSystem> m_FileSystems;
  18. private IFileSystemHelper m_FileSystemHelper;
  19. /// <summary>
  20. /// 初始化文件系统管理器的新实例。
  21. /// </summary>
  22. public FileSystemManager()
  23. {
  24. m_FileSystems = new Dictionary<string, FileSystem>(StringComparer.Ordinal);
  25. m_FileSystemHelper = null;
  26. }
  27. /// <summary>
  28. /// 获取游戏框架模块优先级。
  29. /// </summary>
  30. /// <remarks>优先级较高的模块会优先轮询,并且关闭操作会后进行。</remarks>
  31. internal override int Priority
  32. {
  33. get
  34. {
  35. return 4;
  36. }
  37. }
  38. /// <summary>
  39. /// 获取文件系统数量。
  40. /// </summary>
  41. public int Count
  42. {
  43. get
  44. {
  45. return m_FileSystems.Count;
  46. }
  47. }
  48. /// <summary>
  49. /// 文件系统管理器轮询。
  50. /// </summary>
  51. /// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
  52. /// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
  53. internal override void Update(float elapseSeconds, float realElapseSeconds)
  54. {
  55. }
  56. /// <summary>
  57. /// 关闭并清理文件系统管理器。
  58. /// </summary>
  59. internal override void Shutdown()
  60. {
  61. while (m_FileSystems.Count > 0)
  62. {
  63. foreach (KeyValuePair<string, FileSystem> fileSystem in m_FileSystems)
  64. {
  65. DestroyFileSystem(fileSystem.Value, false);
  66. break;
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// 设置文件系统辅助器。
  72. /// </summary>
  73. /// <param name="fileSystemHelper">文件系统辅助器。</param>
  74. public void SetFileSystemHelper(IFileSystemHelper fileSystemHelper)
  75. {
  76. if (fileSystemHelper == null)
  77. {
  78. throw new GameFrameworkException("File system helper is invalid.");
  79. }
  80. m_FileSystemHelper = fileSystemHelper;
  81. }
  82. /// <summary>
  83. /// 检查是否存在文件系统。
  84. /// </summary>
  85. /// <param name="fullPath">要检查的文件系统的完整路径。</param>
  86. /// <returns>是否存在文件系统。</returns>
  87. public bool HasFileSystem(string fullPath)
  88. {
  89. if (string.IsNullOrEmpty(fullPath))
  90. {
  91. throw new GameFrameworkException("Full path is invalid.");
  92. }
  93. return m_FileSystems.ContainsKey(Utility.Path.GetRegularPath(fullPath));
  94. }
  95. /// <summary>
  96. /// 获取文件系统。
  97. /// </summary>
  98. /// <param name="fullPath">要获取的文件系统的完整路径。</param>
  99. /// <returns>获取的文件系统。</returns>
  100. public IFileSystem GetFileSystem(string fullPath)
  101. {
  102. if (string.IsNullOrEmpty(fullPath))
  103. {
  104. throw new GameFrameworkException("Full path is invalid.");
  105. }
  106. FileSystem fileSystem = null;
  107. if (m_FileSystems.TryGetValue(Utility.Path.GetRegularPath(fullPath), out fileSystem))
  108. {
  109. return fileSystem;
  110. }
  111. return null;
  112. }
  113. /// <summary>
  114. /// 创建文件系统。
  115. /// </summary>
  116. /// <param name="fullPath">要创建的文件系统的完整路径。</param>
  117. /// <param name="access">要创建的文件系统的访问方式。</param>
  118. /// <param name="maxFileCount">要创建的文件系统的最大文件数量。</param>
  119. /// <param name="maxBlockCount">要创建的文件系统的最大块数据数量。</param>
  120. /// <returns>创建的文件系统。</returns>
  121. public IFileSystem CreateFileSystem(string fullPath, FileSystemAccess access, int maxFileCount, int maxBlockCount)
  122. {
  123. if (m_FileSystemHelper == null)
  124. {
  125. throw new GameFrameworkException("File system helper is invalid.");
  126. }
  127. if (string.IsNullOrEmpty(fullPath))
  128. {
  129. throw new GameFrameworkException("Full path is invalid.");
  130. }
  131. if (access == FileSystemAccess.Unspecified)
  132. {
  133. throw new GameFrameworkException("Access is invalid.");
  134. }
  135. if (access == FileSystemAccess.Read)
  136. {
  137. throw new GameFrameworkException("Access read is invalid.");
  138. }
  139. fullPath = Utility.Path.GetRegularPath(fullPath);
  140. if (m_FileSystems.ContainsKey(fullPath))
  141. {
  142. throw new GameFrameworkException(Utility.Text.Format("File system '{0}' is already exist.", fullPath));
  143. }
  144. FileSystemStream fileSystemStream = m_FileSystemHelper.CreateFileSystemStream(fullPath, access, true);
  145. if (fileSystemStream == null)
  146. {
  147. throw new GameFrameworkException(Utility.Text.Format("Create file system stream for '{0}' failure.", fullPath));
  148. }
  149. FileSystem fileSystem = FileSystem.Create(fullPath, access, fileSystemStream, maxFileCount, maxBlockCount);
  150. if (fileSystem == null)
  151. {
  152. throw new GameFrameworkException(Utility.Text.Format("Create file system '{0}' failure.", fullPath));
  153. }
  154. m_FileSystems.Add(fullPath, fileSystem);
  155. return fileSystem;
  156. }
  157. /// <summary>
  158. /// 加载文件系统。
  159. /// </summary>
  160. /// <param name="fullPath">要加载的文件系统的完整路径。</param>
  161. /// <param name="access">要加载的文件系统的访问方式。</param>
  162. /// <returns>加载的文件系统。</returns>
  163. public IFileSystem LoadFileSystem(string fullPath, FileSystemAccess access)
  164. {
  165. if (m_FileSystemHelper == null)
  166. {
  167. throw new GameFrameworkException("File system helper is invalid.");
  168. }
  169. if (string.IsNullOrEmpty(fullPath))
  170. {
  171. throw new GameFrameworkException("Full path is invalid.");
  172. }
  173. if (access == FileSystemAccess.Unspecified)
  174. {
  175. throw new GameFrameworkException("Access is invalid.");
  176. }
  177. fullPath = Utility.Path.GetRegularPath(fullPath);
  178. if (m_FileSystems.ContainsKey(fullPath))
  179. {
  180. throw new GameFrameworkException(Utility.Text.Format("File system '{0}' is already exist.", fullPath));
  181. }
  182. FileSystemStream fileSystemStream = m_FileSystemHelper.CreateFileSystemStream(fullPath, access, false);
  183. if (fileSystemStream == null)
  184. {
  185. throw new GameFrameworkException(Utility.Text.Format("Create file system stream for '{0}' failure.", fullPath));
  186. }
  187. FileSystem fileSystem = FileSystem.Load(fullPath, access, fileSystemStream);
  188. if (fileSystem == null)
  189. {
  190. throw new GameFrameworkException(Utility.Text.Format("Load file system '{0}' failure.", fullPath));
  191. }
  192. m_FileSystems.Add(fullPath, fileSystem);
  193. return fileSystem;
  194. }
  195. /// <summary>
  196. /// 销毁文件系统。
  197. /// </summary>
  198. /// <param name="fileSystem">要销毁的文件系统。</param>
  199. /// <param name="deletePhysicalFile">是否删除文件系统对应的物理文件。</param>
  200. public void DestroyFileSystem(IFileSystem fileSystem, bool deletePhysicalFile)
  201. {
  202. if (fileSystem == null)
  203. {
  204. throw new GameFrameworkException("File system is invalid.");
  205. }
  206. string fullPath = fileSystem.FullPath;
  207. ((FileSystem)fileSystem).Shutdown();
  208. m_FileSystems.Remove(fullPath);
  209. if (deletePhysicalFile && File.Exists(fullPath))
  210. {
  211. File.Delete(fullPath);
  212. }
  213. }
  214. /// <summary>
  215. /// 获取所有文件系统集合。
  216. /// </summary>
  217. /// <returns>获取的所有文件系统集合。</returns>
  218. public IFileSystem[] GetAllFileSystems()
  219. {
  220. int index = 0;
  221. IFileSystem[] results = new IFileSystem[m_FileSystems.Count];
  222. foreach (KeyValuePair<string, FileSystem> fileSystem in m_FileSystems)
  223. {
  224. results[index++] = fileSystem.Value;
  225. }
  226. return results;
  227. }
  228. /// <summary>
  229. /// 获取所有文件系统集合。
  230. /// </summary>
  231. /// <param name="results">获取的所有文件系统集合。</param>
  232. public void GetAllFileSystems(List<IFileSystem> results)
  233. {
  234. if (results == null)
  235. {
  236. throw new GameFrameworkException("Results is invalid.");
  237. }
  238. results.Clear();
  239. foreach (KeyValuePair<string, FileSystem> fileSystem in m_FileSystems)
  240. {
  241. results.Add(fileSystem.Value);
  242. }
  243. }
  244. }
  245. }