FileSystem.cs 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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. using System.Runtime.InteropServices;
  11. namespace GameFramework.FileSystem
  12. {
  13. /// <summary>
  14. /// 文件系统。
  15. /// </summary>
  16. internal sealed partial class FileSystem : IFileSystem
  17. {
  18. private const int ClusterSize = 1024 * 4;
  19. private const int CachedBytesLength = 0x1000;
  20. private static readonly string[] EmptyStringArray = new string[] { };
  21. private static readonly byte[] s_CachedBytes = new byte[CachedBytesLength];
  22. private static readonly int HeaderDataSize = Marshal.SizeOf(typeof(HeaderData));
  23. private static readonly int BlockDataSize = Marshal.SizeOf(typeof(BlockData));
  24. private static readonly int StringDataSize = Marshal.SizeOf(typeof(StringData));
  25. private readonly string m_FullPath;
  26. private readonly FileSystemAccess m_Access;
  27. private readonly FileSystemStream m_Stream;
  28. private readonly Dictionary<string, int> m_FileDatas;
  29. private readonly List<BlockData> m_BlockDatas;
  30. private readonly GameFrameworkMultiDictionary<int, int> m_FreeBlockIndexes;
  31. private readonly SortedDictionary<int, StringData> m_StringDatas;
  32. private readonly Queue<KeyValuePair<int, StringData>> m_FreeStringDatas;
  33. private HeaderData m_HeaderData;
  34. private int m_BlockDataOffset;
  35. private int m_StringDataOffset;
  36. private int m_FileDataOffset;
  37. /// <summary>
  38. /// 初始化文件系统的新实例。
  39. /// </summary>
  40. /// <param name="fullPath">文件系统完整路径。</param>
  41. /// <param name="access">文件系统访问方式。</param>
  42. /// <param name="stream">文件系统流。</param>
  43. private FileSystem(string fullPath, FileSystemAccess access, FileSystemStream stream)
  44. {
  45. if (string.IsNullOrEmpty(fullPath))
  46. {
  47. throw new GameFrameworkException("Full path is invalid.");
  48. }
  49. if (access == FileSystemAccess.Unspecified)
  50. {
  51. throw new GameFrameworkException("Access is invalid.");
  52. }
  53. if (stream == null)
  54. {
  55. throw new GameFrameworkException("Stream is invalid.");
  56. }
  57. m_FullPath = fullPath;
  58. m_Access = access;
  59. m_Stream = stream;
  60. m_FileDatas = new Dictionary<string, int>(StringComparer.Ordinal);
  61. m_BlockDatas = new List<BlockData>();
  62. m_FreeBlockIndexes = new GameFrameworkMultiDictionary<int, int>();
  63. m_StringDatas = new SortedDictionary<int, StringData>();
  64. m_FreeStringDatas = new Queue<KeyValuePair<int, StringData>>();
  65. m_HeaderData = default(HeaderData);
  66. m_BlockDataOffset = 0;
  67. m_StringDataOffset = 0;
  68. m_FileDataOffset = 0;
  69. Utility.Marshal.EnsureCachedHGlobalSize(CachedBytesLength);
  70. }
  71. /// <summary>
  72. /// 获取文件系统完整路径。
  73. /// </summary>
  74. public string FullPath
  75. {
  76. get
  77. {
  78. return m_FullPath;
  79. }
  80. }
  81. /// <summary>
  82. /// 获取文件系统访问方式。
  83. /// </summary>
  84. public FileSystemAccess Access
  85. {
  86. get
  87. {
  88. return m_Access;
  89. }
  90. }
  91. /// <summary>
  92. /// 获取文件数量。
  93. /// </summary>
  94. public int FileCount
  95. {
  96. get
  97. {
  98. return m_FileDatas.Count;
  99. }
  100. }
  101. /// <summary>
  102. /// 获取最大文件数量。
  103. /// </summary>
  104. public int MaxFileCount
  105. {
  106. get
  107. {
  108. return m_HeaderData.MaxFileCount;
  109. }
  110. }
  111. /// <summary>
  112. /// 创建文件系统。
  113. /// </summary>
  114. /// <param name="fullPath">要创建的文件系统的完整路径。</param>
  115. /// <param name="access">要创建的文件系统的访问方式。</param>
  116. /// <param name="stream">要创建的文件系统的文件系统流。</param>
  117. /// <param name="maxFileCount">要创建的文件系统的最大文件数量。</param>
  118. /// <param name="maxBlockCount">要创建的文件系统的最大块数据数量。</param>
  119. /// <returns>创建的文件系统。</returns>
  120. public static FileSystem Create(string fullPath, FileSystemAccess access, FileSystemStream stream, int maxFileCount, int maxBlockCount)
  121. {
  122. if (maxFileCount <= 0)
  123. {
  124. throw new GameFrameworkException("Max file count is invalid.");
  125. }
  126. if (maxBlockCount <= 0)
  127. {
  128. throw new GameFrameworkException("Max block count is invalid.");
  129. }
  130. if (maxFileCount > maxBlockCount)
  131. {
  132. throw new GameFrameworkException("Max file count can not larger than max block count.");
  133. }
  134. FileSystem fileSystem = new FileSystem(fullPath, access, stream);
  135. fileSystem.m_HeaderData = new HeaderData(maxFileCount, maxBlockCount);
  136. CalcOffsets(fileSystem);
  137. Utility.Marshal.StructureToBytes(fileSystem.m_HeaderData, HeaderDataSize, s_CachedBytes);
  138. try
  139. {
  140. stream.Write(s_CachedBytes, 0, HeaderDataSize);
  141. stream.SetLength(fileSystem.m_FileDataOffset);
  142. return fileSystem;
  143. }
  144. catch
  145. {
  146. fileSystem.Shutdown();
  147. return null;
  148. }
  149. }
  150. /// <summary>
  151. /// 加载文件系统。
  152. /// </summary>
  153. /// <param name="fullPath">要加载的文件系统的完整路径。</param>
  154. /// <param name="access">要加载的文件系统的访问方式。</param>
  155. /// <param name="stream">要加载的文件系统的文件系统流。</param>
  156. /// <returns>加载的文件系统。</returns>
  157. public static FileSystem Load(string fullPath, FileSystemAccess access, FileSystemStream stream)
  158. {
  159. FileSystem fileSystem = new FileSystem(fullPath, access, stream);
  160. stream.Read(s_CachedBytes, 0, HeaderDataSize);
  161. fileSystem.m_HeaderData = Utility.Marshal.BytesToStructure<HeaderData>(HeaderDataSize, s_CachedBytes);
  162. if (!fileSystem.m_HeaderData.IsValid)
  163. {
  164. throw new GameFrameworkException(Utility.Text.Format("File system '{0}' is invalid.", fullPath));
  165. }
  166. CalcOffsets(fileSystem);
  167. if (fileSystem.m_BlockDatas.Capacity < fileSystem.m_HeaderData.BlockCount)
  168. {
  169. fileSystem.m_BlockDatas.Capacity = fileSystem.m_HeaderData.BlockCount;
  170. }
  171. for (int i = 0; i < fileSystem.m_HeaderData.BlockCount; i++)
  172. {
  173. stream.Read(s_CachedBytes, 0, BlockDataSize);
  174. BlockData blockData = Utility.Marshal.BytesToStructure<BlockData>(BlockDataSize, s_CachedBytes);
  175. fileSystem.m_BlockDatas.Add(blockData);
  176. }
  177. for (int i = 0; i < fileSystem.m_BlockDatas.Count; i++)
  178. {
  179. BlockData blockData = fileSystem.m_BlockDatas[i];
  180. if (blockData.Using)
  181. {
  182. StringData stringData = fileSystem.ReadStringData(blockData.StringIndex);
  183. fileSystem.m_StringDatas.Add(blockData.StringIndex, stringData);
  184. fileSystem.m_FileDatas.Add(stringData.GetString(fileSystem.m_HeaderData.GetEncryptBytes()), i);
  185. }
  186. else
  187. {
  188. fileSystem.m_FreeBlockIndexes.Add(blockData.Length, i);
  189. }
  190. }
  191. return fileSystem;
  192. }
  193. /// <summary>
  194. /// 关闭并清理文件系统。
  195. /// </summary>
  196. public void Shutdown()
  197. {
  198. m_Stream.Close();
  199. m_FileDatas.Clear();
  200. m_BlockDatas.Clear();
  201. m_FreeBlockIndexes.Clear();
  202. m_StringDatas.Clear();
  203. m_FreeStringDatas.Clear();
  204. m_BlockDataOffset = 0;
  205. m_StringDataOffset = 0;
  206. m_FileDataOffset = 0;
  207. }
  208. /// <summary>
  209. /// 获取文件信息。
  210. /// </summary>
  211. /// <param name="name">要获取文件信息的文件名称。</param>
  212. /// <returns>获取的文件信息。</returns>
  213. public FileInfo GetFileInfo(string name)
  214. {
  215. if (string.IsNullOrEmpty(name))
  216. {
  217. throw new GameFrameworkException("Name is invalid.");
  218. }
  219. int blockIndex = 0;
  220. if (!m_FileDatas.TryGetValue(name, out blockIndex))
  221. {
  222. return default(FileInfo);
  223. }
  224. BlockData blockData = m_BlockDatas[blockIndex];
  225. return new FileInfo(name, GetClusterOffset(blockData.ClusterIndex), blockData.Length);
  226. }
  227. /// <summary>
  228. /// 获取所有文件信息。
  229. /// </summary>
  230. /// <returns>获取的所有文件信息。</returns>
  231. public FileInfo[] GetAllFileInfos()
  232. {
  233. int index = 0;
  234. FileInfo[] results = new FileInfo[m_FileDatas.Count];
  235. foreach (KeyValuePair<string, int> fileData in m_FileDatas)
  236. {
  237. BlockData blockData = m_BlockDatas[fileData.Value];
  238. results[index++] = new FileInfo(fileData.Key, GetClusterOffset(blockData.ClusterIndex), blockData.Length);
  239. }
  240. return results;
  241. }
  242. /// <summary>
  243. /// 获取所有文件信息。
  244. /// </summary>
  245. /// <param name="results">获取的所有文件信息。</param>
  246. public void GetAllFileInfos(List<FileInfo> results)
  247. {
  248. if (results == null)
  249. {
  250. throw new GameFrameworkException("Results is invalid.");
  251. }
  252. results.Clear();
  253. foreach (KeyValuePair<string, int> fileData in m_FileDatas)
  254. {
  255. BlockData blockData = m_BlockDatas[fileData.Value];
  256. results.Add(new FileInfo(fileData.Key, GetClusterOffset(blockData.ClusterIndex), blockData.Length));
  257. }
  258. }
  259. /// <summary>
  260. /// 检查是否存在指定文件。
  261. /// </summary>
  262. /// <param name="name">要检查的文件名称。</param>
  263. /// <returns>是否存在指定文件。</returns>
  264. public bool HasFile(string name)
  265. {
  266. if (string.IsNullOrEmpty(name))
  267. {
  268. throw new GameFrameworkException("Name is invalid.");
  269. }
  270. return m_FileDatas.ContainsKey(name);
  271. }
  272. /// <summary>
  273. /// 读取指定文件。
  274. /// </summary>
  275. /// <param name="name">要读取的文件名称。</param>
  276. /// <returns>存储读取文件内容的二进制流。</returns>
  277. public byte[] ReadFile(string name)
  278. {
  279. if (m_Access != FileSystemAccess.Read && m_Access != FileSystemAccess.ReadWrite)
  280. {
  281. throw new GameFrameworkException("File system is not readable.");
  282. }
  283. if (string.IsNullOrEmpty(name))
  284. {
  285. throw new GameFrameworkException("Name is invalid.");
  286. }
  287. FileInfo fileInfo = GetFileInfo(name);
  288. if (!fileInfo.IsValid)
  289. {
  290. return null;
  291. }
  292. int length = fileInfo.Length;
  293. byte[] buffer = new byte[length];
  294. if (length > 0)
  295. {
  296. m_Stream.Position = fileInfo.Offset;
  297. m_Stream.Read(buffer, 0, length);
  298. }
  299. return buffer;
  300. }
  301. /// <summary>
  302. /// 读取指定文件。
  303. /// </summary>
  304. /// <param name="name">要读取的文件名称。</param>
  305. /// <param name="buffer">存储读取文件内容的二进制流。</param>
  306. /// <returns>实际读取了多少字节。</returns>
  307. public int ReadFile(string name, byte[] buffer)
  308. {
  309. if (buffer == null)
  310. {
  311. throw new GameFrameworkException("Buffer is invalid.");
  312. }
  313. return ReadFile(name, buffer, 0, buffer.Length);
  314. }
  315. /// <summary>
  316. /// 读取指定文件。
  317. /// </summary>
  318. /// <param name="name">要读取的文件名称。</param>
  319. /// <param name="buffer">存储读取文件内容的二进制流。</param>
  320. /// <param name="startIndex">存储读取文件内容的二进制流的起始位置。</param>
  321. /// <returns>实际读取了多少字节。</returns>
  322. public int ReadFile(string name, byte[] buffer, int startIndex)
  323. {
  324. if (buffer == null)
  325. {
  326. throw new GameFrameworkException("Buffer is invalid.");
  327. }
  328. return ReadFile(name, buffer, startIndex, buffer.Length - startIndex);
  329. }
  330. /// <summary>
  331. /// 读取指定文件。
  332. /// </summary>
  333. /// <param name="name">要读取的文件名称。</param>
  334. /// <param name="buffer">存储读取文件内容的二进制流。</param>
  335. /// <param name="startIndex">存储读取文件内容的二进制流的起始位置。</param>
  336. /// <param name="length">存储读取文件内容的二进制流的长度。</param>
  337. /// <returns>实际读取了多少字节。</returns>
  338. public int ReadFile(string name, byte[] buffer, int startIndex, int length)
  339. {
  340. if (m_Access != FileSystemAccess.Read && m_Access != FileSystemAccess.ReadWrite)
  341. {
  342. throw new GameFrameworkException("File system is not readable.");
  343. }
  344. if (string.IsNullOrEmpty(name))
  345. {
  346. throw new GameFrameworkException("Name is invalid.");
  347. }
  348. if (buffer == null)
  349. {
  350. throw new GameFrameworkException("Buffer is invalid.");
  351. }
  352. if (startIndex < 0 || length < 0 || startIndex + length > buffer.Length)
  353. {
  354. throw new GameFrameworkException("Start index or length is invalid.");
  355. }
  356. FileInfo fileInfo = GetFileInfo(name);
  357. if (!fileInfo.IsValid)
  358. {
  359. return 0;
  360. }
  361. m_Stream.Position = fileInfo.Offset;
  362. if (length > fileInfo.Length)
  363. {
  364. length = fileInfo.Length;
  365. }
  366. if (length > 0)
  367. {
  368. return m_Stream.Read(buffer, startIndex, length);
  369. }
  370. return 0;
  371. }
  372. /// <summary>
  373. /// 读取指定文件。
  374. /// </summary>
  375. /// <param name="name">要读取的文件名称。</param>
  376. /// <param name="stream">存储读取文件内容的二进制流。</param>
  377. /// <returns>实际读取了多少字节。</returns>
  378. public int ReadFile(string name, Stream stream)
  379. {
  380. if (m_Access != FileSystemAccess.Read && m_Access != FileSystemAccess.ReadWrite)
  381. {
  382. throw new GameFrameworkException("File system is not readable.");
  383. }
  384. if (string.IsNullOrEmpty(name))
  385. {
  386. throw new GameFrameworkException("Name is invalid.");
  387. }
  388. if (stream == null)
  389. {
  390. throw new GameFrameworkException("Stream is invalid.");
  391. }
  392. if (!stream.CanWrite)
  393. {
  394. throw new GameFrameworkException("Stream is not writable.");
  395. }
  396. FileInfo fileInfo = GetFileInfo(name);
  397. if (!fileInfo.IsValid)
  398. {
  399. return 0;
  400. }
  401. int length = fileInfo.Length;
  402. if (length > 0)
  403. {
  404. m_Stream.Position = fileInfo.Offset;
  405. return m_Stream.Read(stream, length);
  406. }
  407. return 0;
  408. }
  409. /// <summary>
  410. /// 读取指定文件的指定片段。
  411. /// </summary>
  412. /// <param name="name">要读取片段的文件名称。</param>
  413. /// <param name="length">要读取片段的长度。</param>
  414. /// <returns>存储读取文件片段内容的二进制流。</returns>
  415. public byte[] ReadFileSegment(string name, int length)
  416. {
  417. return ReadFileSegment(name, 0, length);
  418. }
  419. /// <summary>
  420. /// 读取指定文件的指定片段。
  421. /// </summary>
  422. /// <param name="name">要读取片段的文件名称。</param>
  423. /// <param name="offset">要读取片段的偏移。</param>
  424. /// <param name="length">要读取片段的长度。</param>
  425. /// <returns>存储读取文件片段内容的二进制流。</returns>
  426. public byte[] ReadFileSegment(string name, int offset, int length)
  427. {
  428. if (m_Access != FileSystemAccess.Read && m_Access != FileSystemAccess.ReadWrite)
  429. {
  430. throw new GameFrameworkException("File system is not readable.");
  431. }
  432. if (string.IsNullOrEmpty(name))
  433. {
  434. throw new GameFrameworkException("Name is invalid.");
  435. }
  436. if (offset < 0)
  437. {
  438. throw new GameFrameworkException("Index is invalid.");
  439. }
  440. if (length < 0)
  441. {
  442. throw new GameFrameworkException("Length is invalid.");
  443. }
  444. FileInfo fileInfo = GetFileInfo(name);
  445. if (!fileInfo.IsValid)
  446. {
  447. return null;
  448. }
  449. if (offset > fileInfo.Length)
  450. {
  451. offset = fileInfo.Length;
  452. }
  453. int leftLength = fileInfo.Length - offset;
  454. if (length > leftLength)
  455. {
  456. length = leftLength;
  457. }
  458. byte[] buffer = new byte[length];
  459. if (length > 0)
  460. {
  461. m_Stream.Position = fileInfo.Offset + offset;
  462. m_Stream.Read(buffer, 0, length);
  463. }
  464. return buffer;
  465. }
  466. /// <summary>
  467. /// 读取指定文件的指定片段。
  468. /// </summary>
  469. /// <param name="name">要读取片段的文件名称。</param>
  470. /// <param name="buffer">存储读取文件片段内容的二进制流。</param>
  471. /// <returns>实际读取了多少字节。</returns>
  472. public int ReadFileSegment(string name, byte[] buffer)
  473. {
  474. if (buffer == null)
  475. {
  476. throw new GameFrameworkException("Buffer is invalid.");
  477. }
  478. return ReadFileSegment(name, 0, buffer, 0, buffer.Length);
  479. }
  480. /// <summary>
  481. /// 读取指定文件的指定片段。
  482. /// </summary>
  483. /// <param name="name">要读取片段的文件名称。</param>
  484. /// <param name="buffer">存储读取文件片段内容的二进制流。</param>
  485. /// <param name="length">要读取片段的长度。</param>
  486. /// <returns>实际读取了多少字节。</returns>
  487. public int ReadFileSegment(string name, byte[] buffer, int length)
  488. {
  489. return ReadFileSegment(name, 0, buffer, 0, length);
  490. }
  491. /// <summary>
  492. /// 读取指定文件的指定片段。
  493. /// </summary>
  494. /// <param name="name">要读取片段的文件名称。</param>
  495. /// <param name="buffer">存储读取文件片段内容的二进制流。</param>
  496. /// <param name="startIndex">存储读取文件片段内容的二进制流的起始位置。</param>
  497. /// <param name="length">要读取片段的长度。</param>
  498. /// <returns>实际读取了多少字节。</returns>
  499. public int ReadFileSegment(string name, byte[] buffer, int startIndex, int length)
  500. {
  501. return ReadFileSegment(name, 0, buffer, startIndex, length);
  502. }
  503. /// <summary>
  504. /// 读取指定文件的指定片段。
  505. /// </summary>
  506. /// <param name="name">要读取片段的文件名称。</param>
  507. /// <param name="offset">要读取片段的偏移。</param>
  508. /// <param name="buffer">存储读取文件片段内容的二进制流。</param>
  509. /// <returns>实际读取了多少字节。</returns>
  510. public int ReadFileSegment(string name, int offset, byte[] buffer)
  511. {
  512. if (buffer == null)
  513. {
  514. throw new GameFrameworkException("Buffer is invalid.");
  515. }
  516. return ReadFileSegment(name, offset, buffer, 0, buffer.Length);
  517. }
  518. /// <summary>
  519. /// 读取指定文件的指定片段。
  520. /// </summary>
  521. /// <param name="name">要读取片段的文件名称。</param>
  522. /// <param name="offset">要读取片段的偏移。</param>
  523. /// <param name="buffer">存储读取文件片段内容的二进制流。</param>
  524. /// <param name="length">要读取片段的长度。</param>
  525. /// <returns>实际读取了多少字节。</returns>
  526. public int ReadFileSegment(string name, int offset, byte[] buffer, int length)
  527. {
  528. return ReadFileSegment(name, offset, buffer, 0, length);
  529. }
  530. /// <summary>
  531. /// 读取指定文件的指定片段。
  532. /// </summary>
  533. /// <param name="name">要读取片段的文件名称。</param>
  534. /// <param name="offset">要读取片段的偏移。</param>
  535. /// <param name="buffer">存储读取文件片段内容的二进制流。</param>
  536. /// <param name="startIndex">存储读取文件片段内容的二进制流的起始位置。</param>
  537. /// <param name="length">要读取片段的长度。</param>
  538. /// <returns>实际读取了多少字节。</returns>
  539. public int ReadFileSegment(string name, int offset, byte[] buffer, int startIndex, int length)
  540. {
  541. if (m_Access != FileSystemAccess.Read && m_Access != FileSystemAccess.ReadWrite)
  542. {
  543. throw new GameFrameworkException("File system is not readable.");
  544. }
  545. if (string.IsNullOrEmpty(name))
  546. {
  547. throw new GameFrameworkException("Name is invalid.");
  548. }
  549. if (offset < 0)
  550. {
  551. throw new GameFrameworkException("Index is invalid.");
  552. }
  553. if (buffer == null)
  554. {
  555. throw new GameFrameworkException("Buffer is invalid.");
  556. }
  557. if (startIndex < 0 || length < 0 || startIndex + length > buffer.Length)
  558. {
  559. throw new GameFrameworkException("Start index or length is invalid.");
  560. }
  561. FileInfo fileInfo = GetFileInfo(name);
  562. if (!fileInfo.IsValid)
  563. {
  564. return 0;
  565. }
  566. if (offset > fileInfo.Length)
  567. {
  568. offset = fileInfo.Length;
  569. }
  570. int leftLength = fileInfo.Length - offset;
  571. if (length > leftLength)
  572. {
  573. length = leftLength;
  574. }
  575. if (length > 0)
  576. {
  577. m_Stream.Position = fileInfo.Offset + offset;
  578. return m_Stream.Read(buffer, startIndex, length);
  579. }
  580. return 0;
  581. }
  582. /// <summary>
  583. /// 读取指定文件的指定片段。
  584. /// </summary>
  585. /// <param name="name">要读取片段的文件名称。</param>
  586. /// <param name="stream">存储读取文件片段内容的二进制流。</param>
  587. /// <param name="length">要读取片段的长度。</param>
  588. /// <returns>实际读取了多少字节。</returns>
  589. public int ReadFileSegment(string name, Stream stream, int length)
  590. {
  591. return ReadFileSegment(name, 0, stream, length);
  592. }
  593. /// <summary>
  594. /// 读取指定文件的指定片段。
  595. /// </summary>
  596. /// <param name="name">要读取片段的文件名称。</param>
  597. /// <param name="offset">要读取片段的偏移。</param>
  598. /// <param name="stream">存储读取文件片段内容的二进制流。</param>
  599. /// <param name="length">要读取片段的长度。</param>
  600. /// <returns>实际读取了多少字节。</returns>
  601. public int ReadFileSegment(string name, int offset, Stream stream, int length)
  602. {
  603. if (m_Access != FileSystemAccess.Read && m_Access != FileSystemAccess.ReadWrite)
  604. {
  605. throw new GameFrameworkException("File system is not readable.");
  606. }
  607. if (string.IsNullOrEmpty(name))
  608. {
  609. throw new GameFrameworkException("Name is invalid.");
  610. }
  611. if (offset < 0)
  612. {
  613. throw new GameFrameworkException("Index is invalid.");
  614. }
  615. if (stream == null)
  616. {
  617. throw new GameFrameworkException("Stream is invalid.");
  618. }
  619. if (!stream.CanWrite)
  620. {
  621. throw new GameFrameworkException("Stream is not writable.");
  622. }
  623. if (length < 0)
  624. {
  625. throw new GameFrameworkException("Length is invalid.");
  626. }
  627. FileInfo fileInfo = GetFileInfo(name);
  628. if (!fileInfo.IsValid)
  629. {
  630. return 0;
  631. }
  632. if (offset > fileInfo.Length)
  633. {
  634. offset = fileInfo.Length;
  635. }
  636. int leftLength = fileInfo.Length - offset;
  637. if (length > leftLength)
  638. {
  639. length = leftLength;
  640. }
  641. if (length > 0)
  642. {
  643. m_Stream.Position = fileInfo.Offset + offset;
  644. return m_Stream.Read(stream, length);
  645. }
  646. return 0;
  647. }
  648. /// <summary>
  649. /// 写入指定文件。
  650. /// </summary>
  651. /// <param name="name">要写入的文件名称。</param>
  652. /// <param name="buffer">存储写入文件内容的二进制流。</param>
  653. /// <returns>是否写入指定文件成功。</returns>
  654. public bool WriteFile(string name, byte[] buffer)
  655. {
  656. if (buffer == null)
  657. {
  658. throw new GameFrameworkException("Buffer is invalid.");
  659. }
  660. return WriteFile(name, buffer, 0, buffer.Length);
  661. }
  662. /// <summary>
  663. /// 写入指定文件。
  664. /// </summary>
  665. /// <param name="name">要写入的文件名称。</param>
  666. /// <param name="buffer">存储写入文件内容的二进制流。</param>
  667. /// <param name="startIndex">存储写入文件内容的二进制流的起始位置。</param>
  668. /// <returns>是否写入指定文件成功。</returns>
  669. public bool WriteFile(string name, byte[] buffer, int startIndex)
  670. {
  671. if (buffer == null)
  672. {
  673. throw new GameFrameworkException("Buffer is invalid.");
  674. }
  675. return WriteFile(name, buffer, startIndex, buffer.Length - startIndex);
  676. }
  677. /// <summary>
  678. /// 写入指定文件。
  679. /// </summary>
  680. /// <param name="name">要写入的文件名称。</param>
  681. /// <param name="buffer">存储写入文件内容的二进制流。</param>
  682. /// <param name="startIndex">存储写入文件内容的二进制流的起始位置。</param>
  683. /// <param name="length">存储写入文件内容的二进制流的长度。</param>
  684. /// <returns>是否写入指定文件成功。</returns>
  685. public bool WriteFile(string name, byte[] buffer, int startIndex, int length)
  686. {
  687. if (m_Access != FileSystemAccess.Write && m_Access != FileSystemAccess.ReadWrite)
  688. {
  689. throw new GameFrameworkException("File system is not writable.");
  690. }
  691. if (string.IsNullOrEmpty(name))
  692. {
  693. throw new GameFrameworkException("Name is invalid.");
  694. }
  695. if (name.Length > byte.MaxValue)
  696. {
  697. throw new GameFrameworkException(Utility.Text.Format("Name '{0}' is too long.", name));
  698. }
  699. if (buffer == null)
  700. {
  701. throw new GameFrameworkException("Buffer is invalid.");
  702. }
  703. if (startIndex < 0 || length < 0 || startIndex + length > buffer.Length)
  704. {
  705. throw new GameFrameworkException("Start index or length is invalid.");
  706. }
  707. bool hasFile = false;
  708. int oldBlockIndex = -1;
  709. if (m_FileDatas.TryGetValue(name, out oldBlockIndex))
  710. {
  711. hasFile = true;
  712. }
  713. if (!hasFile && m_FileDatas.Count >= m_HeaderData.MaxFileCount)
  714. {
  715. return false;
  716. }
  717. int blockIndex = AllocBlock(length);
  718. if (blockIndex < 0)
  719. {
  720. return false;
  721. }
  722. if (length > 0)
  723. {
  724. m_Stream.Position = GetClusterOffset(m_BlockDatas[blockIndex].ClusterIndex);
  725. m_Stream.Write(buffer, startIndex, length);
  726. }
  727. ProcessWriteFile(name, hasFile, oldBlockIndex, blockIndex, length);
  728. m_Stream.Flush();
  729. return true;
  730. }
  731. /// <summary>
  732. /// 写入指定文件。
  733. /// </summary>
  734. /// <param name="name">要写入的文件名称。</param>
  735. /// <param name="stream">存储写入文件内容的二进制流。</param>
  736. /// <returns>是否写入指定文件成功。</returns>
  737. public bool WriteFile(string name, Stream stream)
  738. {
  739. if (m_Access != FileSystemAccess.Write && m_Access != FileSystemAccess.ReadWrite)
  740. {
  741. throw new GameFrameworkException("File system is not writable.");
  742. }
  743. if (string.IsNullOrEmpty(name))
  744. {
  745. throw new GameFrameworkException("Name is invalid.");
  746. }
  747. if (name.Length > byte.MaxValue)
  748. {
  749. throw new GameFrameworkException(Utility.Text.Format("Name '{0}' is too long.", name));
  750. }
  751. if (stream == null)
  752. {
  753. throw new GameFrameworkException("Stream is invalid.");
  754. }
  755. if (!stream.CanRead)
  756. {
  757. throw new GameFrameworkException("Stream is not readable.");
  758. }
  759. bool hasFile = false;
  760. int oldBlockIndex = -1;
  761. if (m_FileDatas.TryGetValue(name, out oldBlockIndex))
  762. {
  763. hasFile = true;
  764. }
  765. if (!hasFile && m_FileDatas.Count >= m_HeaderData.MaxFileCount)
  766. {
  767. return false;
  768. }
  769. int length = (int)(stream.Length - stream.Position);
  770. int blockIndex = AllocBlock(length);
  771. if (blockIndex < 0)
  772. {
  773. return false;
  774. }
  775. if (length > 0)
  776. {
  777. m_Stream.Position = GetClusterOffset(m_BlockDatas[blockIndex].ClusterIndex);
  778. m_Stream.Write(stream, length);
  779. }
  780. ProcessWriteFile(name, hasFile, oldBlockIndex, blockIndex, length);
  781. m_Stream.Flush();
  782. return true;
  783. }
  784. /// <summary>
  785. /// 写入指定文件。
  786. /// </summary>
  787. /// <param name="name">要写入的文件名称。</param>
  788. /// <param name="filePath">存储写入文件内容的文件路径。</param>
  789. /// <returns>是否写入指定文件成功。</returns>
  790. public bool WriteFile(string name, string filePath)
  791. {
  792. if (string.IsNullOrEmpty(filePath))
  793. {
  794. throw new GameFrameworkException("File path is invalid");
  795. }
  796. if (!File.Exists(filePath))
  797. {
  798. return false;
  799. }
  800. using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
  801. {
  802. return WriteFile(name, fileStream);
  803. }
  804. }
  805. /// <summary>
  806. /// 将指定文件另存为物理文件。
  807. /// </summary>
  808. /// <param name="name">要另存为的文件名称。</param>
  809. /// <param name="filePath">存储写入文件内容的文件路径。</param>
  810. /// <returns>是否将指定文件另存为物理文件成功。</returns>
  811. public bool SaveAsFile(string name, string filePath)
  812. {
  813. if (m_Access != FileSystemAccess.Read && m_Access != FileSystemAccess.ReadWrite)
  814. {
  815. throw new GameFrameworkException("File system is not readable.");
  816. }
  817. if (string.IsNullOrEmpty(name))
  818. {
  819. throw new GameFrameworkException("Name is invalid.");
  820. }
  821. if (string.IsNullOrEmpty(filePath))
  822. {
  823. throw new GameFrameworkException("File path is invalid");
  824. }
  825. FileInfo fileInfo = GetFileInfo(name);
  826. if (!fileInfo.IsValid)
  827. {
  828. return false;
  829. }
  830. try
  831. {
  832. if (File.Exists(filePath))
  833. {
  834. File.Delete(filePath);
  835. }
  836. string directory = Path.GetDirectoryName(filePath);
  837. if (!Directory.Exists(directory))
  838. {
  839. Directory.CreateDirectory(directory);
  840. }
  841. using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
  842. {
  843. int length = fileInfo.Length;
  844. if (length > 0)
  845. {
  846. m_Stream.Position = fileInfo.Offset;
  847. return m_Stream.Read(fileStream, length) == length;
  848. }
  849. return true;
  850. }
  851. }
  852. catch
  853. {
  854. return false;
  855. }
  856. }
  857. /// <summary>
  858. /// 重命名指定文件。
  859. /// </summary>
  860. /// <param name="oldName">要重命名的文件名称。</param>
  861. /// <param name="newName">重命名后的文件名称。</param>
  862. /// <returns>是否重命名指定文件成功。</returns>
  863. public bool RenameFile(string oldName, string newName)
  864. {
  865. if (m_Access != FileSystemAccess.Write && m_Access != FileSystemAccess.ReadWrite)
  866. {
  867. throw new GameFrameworkException("File system is not writable.");
  868. }
  869. if (string.IsNullOrEmpty(oldName))
  870. {
  871. throw new GameFrameworkException("Old name is invalid.");
  872. }
  873. if (string.IsNullOrEmpty(newName))
  874. {
  875. throw new GameFrameworkException("New name is invalid.");
  876. }
  877. if (newName.Length > byte.MaxValue)
  878. {
  879. throw new GameFrameworkException(Utility.Text.Format("New name '{0}' is too long.", newName));
  880. }
  881. if (oldName == newName)
  882. {
  883. return true;
  884. }
  885. if (m_FileDatas.ContainsKey(newName))
  886. {
  887. return false;
  888. }
  889. int blockIndex = 0;
  890. if (!m_FileDatas.TryGetValue(oldName, out blockIndex))
  891. {
  892. return false;
  893. }
  894. int stringIndex = m_BlockDatas[blockIndex].StringIndex;
  895. StringData stringData = m_StringDatas[stringIndex].SetString(newName, m_HeaderData.GetEncryptBytes());
  896. m_StringDatas[stringIndex] = stringData;
  897. WriteStringData(stringIndex, stringData);
  898. m_FileDatas.Add(newName, blockIndex);
  899. m_FileDatas.Remove(oldName);
  900. m_Stream.Flush();
  901. return true;
  902. }
  903. /// <summary>
  904. /// 删除指定文件。
  905. /// </summary>
  906. /// <param name="name">要删除的文件名称。</param>
  907. /// <returns>是否删除指定文件成功。</returns>
  908. public bool DeleteFile(string name)
  909. {
  910. if (m_Access != FileSystemAccess.Write && m_Access != FileSystemAccess.ReadWrite)
  911. {
  912. throw new GameFrameworkException("File system is not writable.");
  913. }
  914. if (string.IsNullOrEmpty(name))
  915. {
  916. throw new GameFrameworkException("Name is invalid.");
  917. }
  918. int blockIndex = 0;
  919. if (!m_FileDatas.TryGetValue(name, out blockIndex))
  920. {
  921. return false;
  922. }
  923. m_FileDatas.Remove(name);
  924. BlockData blockData = m_BlockDatas[blockIndex];
  925. int stringIndex = blockData.StringIndex;
  926. StringData stringData = m_StringDatas[stringIndex].Clear();
  927. m_FreeStringDatas.Enqueue(new KeyValuePair<int, StringData>(stringIndex, stringData));
  928. m_StringDatas.Remove(stringIndex);
  929. WriteStringData(stringIndex, stringData);
  930. blockData = blockData.Free();
  931. m_BlockDatas[blockIndex] = blockData;
  932. if (!TryCombineFreeBlocks(blockIndex))
  933. {
  934. m_FreeBlockIndexes.Add(blockData.Length, blockIndex);
  935. WriteBlockData(blockIndex);
  936. }
  937. m_Stream.Flush();
  938. return true;
  939. }
  940. private void ProcessWriteFile(string name, bool hasFile, int oldBlockIndex, int blockIndex, int length)
  941. {
  942. BlockData blockData = m_BlockDatas[blockIndex];
  943. if (hasFile)
  944. {
  945. BlockData oldBlockData = m_BlockDatas[oldBlockIndex];
  946. blockData = new BlockData(oldBlockData.StringIndex, blockData.ClusterIndex, length);
  947. m_BlockDatas[blockIndex] = blockData;
  948. WriteBlockData(blockIndex);
  949. oldBlockData = oldBlockData.Free();
  950. m_BlockDatas[oldBlockIndex] = oldBlockData;
  951. if (!TryCombineFreeBlocks(oldBlockIndex))
  952. {
  953. m_FreeBlockIndexes.Add(oldBlockData.Length, oldBlockIndex);
  954. WriteBlockData(oldBlockIndex);
  955. }
  956. }
  957. else
  958. {
  959. int stringIndex = AllocString(name);
  960. blockData = new BlockData(stringIndex, blockData.ClusterIndex, length);
  961. m_BlockDatas[blockIndex] = blockData;
  962. WriteBlockData(blockIndex);
  963. }
  964. if (hasFile)
  965. {
  966. m_FileDatas[name] = blockIndex;
  967. }
  968. else
  969. {
  970. m_FileDatas.Add(name, blockIndex);
  971. }
  972. }
  973. private bool TryCombineFreeBlocks(int freeBlockIndex)
  974. {
  975. BlockData freeBlockData = m_BlockDatas[freeBlockIndex];
  976. if (freeBlockData.Length <= 0)
  977. {
  978. return false;
  979. }
  980. int previousFreeBlockIndex = -1;
  981. int nextFreeBlockIndex = -1;
  982. int nextBlockDataClusterIndex = freeBlockData.ClusterIndex + GetUpBoundClusterCount(freeBlockData.Length);
  983. foreach (KeyValuePair<int, GameFrameworkLinkedListRange<int>> blockIndexes in m_FreeBlockIndexes)
  984. {
  985. if (blockIndexes.Key <= 0)
  986. {
  987. continue;
  988. }
  989. int blockDataClusterCount = GetUpBoundClusterCount(blockIndexes.Key);
  990. foreach (int blockIndex in blockIndexes.Value)
  991. {
  992. BlockData blockData = m_BlockDatas[blockIndex];
  993. if (blockData.ClusterIndex + blockDataClusterCount == freeBlockData.ClusterIndex)
  994. {
  995. previousFreeBlockIndex = blockIndex;
  996. }
  997. else if (blockData.ClusterIndex == nextBlockDataClusterIndex)
  998. {
  999. nextFreeBlockIndex = blockIndex;
  1000. }
  1001. }
  1002. }
  1003. if (previousFreeBlockIndex < 0 && nextFreeBlockIndex < 0)
  1004. {
  1005. return false;
  1006. }
  1007. m_FreeBlockIndexes.Remove(freeBlockData.Length, freeBlockIndex);
  1008. if (previousFreeBlockIndex >= 0)
  1009. {
  1010. BlockData previousFreeBlockData = m_BlockDatas[previousFreeBlockIndex];
  1011. m_FreeBlockIndexes.Remove(previousFreeBlockData.Length, previousFreeBlockIndex);
  1012. freeBlockData = new BlockData(previousFreeBlockData.ClusterIndex, previousFreeBlockData.Length + freeBlockData.Length);
  1013. m_BlockDatas[previousFreeBlockIndex] = BlockData.Empty;
  1014. m_FreeBlockIndexes.Add(0, previousFreeBlockIndex);
  1015. WriteBlockData(previousFreeBlockIndex);
  1016. }
  1017. if (nextFreeBlockIndex >= 0)
  1018. {
  1019. BlockData nextFreeBlockData = m_BlockDatas[nextFreeBlockIndex];
  1020. m_FreeBlockIndexes.Remove(nextFreeBlockData.Length, nextFreeBlockIndex);
  1021. freeBlockData = new BlockData(freeBlockData.ClusterIndex, freeBlockData.Length + nextFreeBlockData.Length);
  1022. m_BlockDatas[nextFreeBlockIndex] = BlockData.Empty;
  1023. m_FreeBlockIndexes.Add(0, nextFreeBlockIndex);
  1024. WriteBlockData(nextFreeBlockIndex);
  1025. }
  1026. m_BlockDatas[freeBlockIndex] = freeBlockData;
  1027. m_FreeBlockIndexes.Add(freeBlockData.Length, freeBlockIndex);
  1028. WriteBlockData(freeBlockIndex);
  1029. return true;
  1030. }
  1031. private int GetEmptyBlockIndex()
  1032. {
  1033. GameFrameworkLinkedListRange<int> lengthRange = default(GameFrameworkLinkedListRange<int>);
  1034. if (m_FreeBlockIndexes.TryGetValue(0, out lengthRange))
  1035. {
  1036. int blockIndex = lengthRange.First.Value;
  1037. m_FreeBlockIndexes.Remove(0, blockIndex);
  1038. return blockIndex;
  1039. }
  1040. if (m_BlockDatas.Count < m_HeaderData.MaxBlockCount)
  1041. {
  1042. int blockIndex = m_BlockDatas.Count;
  1043. m_BlockDatas.Add(BlockData.Empty);
  1044. WriteHeaderData();
  1045. return blockIndex;
  1046. }
  1047. return -1;
  1048. }
  1049. private int AllocBlock(int length)
  1050. {
  1051. if (length <= 0)
  1052. {
  1053. return GetEmptyBlockIndex();
  1054. }
  1055. length = (int)GetUpBoundClusterOffset(length);
  1056. int lengthFound = -1;
  1057. GameFrameworkLinkedListRange<int> lengthRange = default(GameFrameworkLinkedListRange<int>);
  1058. foreach (KeyValuePair<int, GameFrameworkLinkedListRange<int>> i in m_FreeBlockIndexes)
  1059. {
  1060. if (i.Key < length)
  1061. {
  1062. continue;
  1063. }
  1064. if (lengthFound >= 0 && lengthFound < i.Key)
  1065. {
  1066. continue;
  1067. }
  1068. lengthFound = i.Key;
  1069. lengthRange = i.Value;
  1070. }
  1071. if (lengthFound >= 0)
  1072. {
  1073. if (lengthFound > length && m_BlockDatas.Count >= m_HeaderData.MaxBlockCount)
  1074. {
  1075. return -1;
  1076. }
  1077. int blockIndex = lengthRange.First.Value;
  1078. m_FreeBlockIndexes.Remove(lengthFound, blockIndex);
  1079. if (lengthFound > length)
  1080. {
  1081. BlockData blockData = m_BlockDatas[blockIndex];
  1082. m_BlockDatas[blockIndex] = new BlockData(blockData.ClusterIndex, length);
  1083. WriteBlockData(blockIndex);
  1084. int deltaLength = lengthFound - length;
  1085. int anotherBlockIndex = GetEmptyBlockIndex();
  1086. m_BlockDatas[anotherBlockIndex] = new BlockData(blockData.ClusterIndex + GetUpBoundClusterCount(length), deltaLength);
  1087. m_FreeBlockIndexes.Add(deltaLength, anotherBlockIndex);
  1088. WriteBlockData(anotherBlockIndex);
  1089. }
  1090. return blockIndex;
  1091. }
  1092. else
  1093. {
  1094. int blockIndex = GetEmptyBlockIndex();
  1095. if (blockIndex < 0)
  1096. {
  1097. return -1;
  1098. }
  1099. long fileLength = m_Stream.Length;
  1100. try
  1101. {
  1102. m_Stream.SetLength(fileLength + length);
  1103. }
  1104. catch
  1105. {
  1106. return -1;
  1107. }
  1108. m_BlockDatas[blockIndex] = new BlockData(GetUpBoundClusterCount(fileLength), length);
  1109. WriteBlockData(blockIndex);
  1110. return blockIndex;
  1111. }
  1112. }
  1113. private int AllocString(string value)
  1114. {
  1115. int stringIndex = -1;
  1116. StringData stringData = default(StringData);
  1117. if (m_FreeStringDatas.Count > 0)
  1118. {
  1119. KeyValuePair<int, StringData> freeStringData = m_FreeStringDatas.Dequeue();
  1120. stringIndex = freeStringData.Key;
  1121. stringData = freeStringData.Value;
  1122. }
  1123. else
  1124. {
  1125. int index = 0;
  1126. foreach (KeyValuePair<int, StringData> i in m_StringDatas)
  1127. {
  1128. if (i.Key == index)
  1129. {
  1130. index++;
  1131. continue;
  1132. }
  1133. break;
  1134. }
  1135. if (index < m_HeaderData.MaxFileCount)
  1136. {
  1137. stringIndex = index;
  1138. byte[] bytes = new byte[byte.MaxValue];
  1139. Utility.Random.GetRandomBytes(bytes);
  1140. stringData = new StringData(0, bytes);
  1141. }
  1142. }
  1143. if (stringIndex < 0)
  1144. {
  1145. throw new GameFrameworkException("Alloc string internal error.");
  1146. }
  1147. stringData = stringData.SetString(value, m_HeaderData.GetEncryptBytes());
  1148. m_StringDatas.Add(stringIndex, stringData);
  1149. WriteStringData(stringIndex, stringData);
  1150. return stringIndex;
  1151. }
  1152. private void WriteHeaderData()
  1153. {
  1154. m_HeaderData = m_HeaderData.SetBlockCount(m_BlockDatas.Count);
  1155. Utility.Marshal.StructureToBytes(m_HeaderData, HeaderDataSize, s_CachedBytes);
  1156. m_Stream.Position = 0L;
  1157. m_Stream.Write(s_CachedBytes, 0, HeaderDataSize);
  1158. }
  1159. private void WriteBlockData(int blockIndex)
  1160. {
  1161. Utility.Marshal.StructureToBytes(m_BlockDatas[blockIndex], BlockDataSize, s_CachedBytes);
  1162. m_Stream.Position = m_BlockDataOffset + BlockDataSize * blockIndex;
  1163. m_Stream.Write(s_CachedBytes, 0, BlockDataSize);
  1164. }
  1165. private StringData ReadStringData(int stringIndex)
  1166. {
  1167. m_Stream.Position = m_StringDataOffset + StringDataSize * stringIndex;
  1168. m_Stream.Read(s_CachedBytes, 0, StringDataSize);
  1169. return Utility.Marshal.BytesToStructure<StringData>(StringDataSize, s_CachedBytes);
  1170. }
  1171. private void WriteStringData(int stringIndex, StringData stringData)
  1172. {
  1173. Utility.Marshal.StructureToBytes(stringData, StringDataSize, s_CachedBytes);
  1174. m_Stream.Position = m_StringDataOffset + StringDataSize * stringIndex;
  1175. m_Stream.Write(s_CachedBytes, 0, StringDataSize);
  1176. }
  1177. private static void CalcOffsets(FileSystem fileSystem)
  1178. {
  1179. fileSystem.m_BlockDataOffset = HeaderDataSize;
  1180. fileSystem.m_StringDataOffset = fileSystem.m_BlockDataOffset + BlockDataSize * fileSystem.m_HeaderData.MaxBlockCount;
  1181. fileSystem.m_FileDataOffset = (int)GetUpBoundClusterOffset(fileSystem.m_StringDataOffset + StringDataSize * fileSystem.m_HeaderData.MaxFileCount);
  1182. }
  1183. private static long GetUpBoundClusterOffset(long offset)
  1184. {
  1185. return (offset - 1L + ClusterSize) / ClusterSize * ClusterSize;
  1186. }
  1187. private static int GetUpBoundClusterCount(long length)
  1188. {
  1189. return (int)((length - 1L + ClusterSize) / ClusterSize);
  1190. }
  1191. private static long GetClusterOffset(int clusterIndex)
  1192. {
  1193. return (long)ClusterSize * clusterIndex;
  1194. }
  1195. }
  1196. }