/// <summary> /// 创建文件系统。 /// </summary> /// <param name="fullPath">要创建的文件系统的完整路径。</param> /// <param name="access">要创建的文件系统的访问方式。</param> /// <param name="stream">要创建的文件系统的文件系统流。</param> /// <param name="maxFileCount">要创建的文件系统的最大文件数量。</param> /// <param name="maxBlockCount">要创建的文件系统的最大块数据数量。</param> /// <returns>创建的文件系统。</returns> public static FileSystem Create(string fullPath, FileSystemAccess access, FileSystemStream stream, int maxFileCount, int maxBlockCount) { if (maxFileCount <= 0) { throw new GameFrameworkException("Max file count is invalid."); } if (maxBlockCount <= 0) { throw new GameFrameworkException("Max block count is invalid."); } if (maxFileCount > maxBlockCount) { throw new GameFrameworkException("Max file count can not larger than max block count."); } FileSystem fileSystem = new FileSystem(fullPath, access, stream); fileSystem.m_HeaderData = new HeaderData(maxFileCount, maxBlockCount); CalcOffsets(fileSystem); Utility.Marshal.StructureToBytes(fileSystem.m_HeaderData, HeaderDataSize, s_CachedBytes); try { stream.Write(s_CachedBytes, 0, HeaderDataSize); stream.SetLength(fileSystem.m_FileDataOffset); return(fileSystem); } catch { fileSystem.Shutdown(); return(null); } }
private int AllocBlock(int length) { length = (int)GetUpBoundClusterOffset(length); int lengthFound = -1; GameFrameworkLinkedListRange <int> lengthRange = default(GameFrameworkLinkedListRange <int>); foreach (KeyValuePair <int, GameFrameworkLinkedListRange <int> > i in m_FreeBlockIndexes) { if (i.Key < length) { continue; } if (lengthFound >= 0 && lengthFound < i.Key) { continue; } lengthFound = i.Key; lengthRange = i.Value; } if (lengthFound >= 0) { if (lengthFound > length && m_BlockDatas.Count >= m_HeaderData.MaxBlockCount) { return(-1); } LinkedListNode <int> blockIndexNode = lengthRange.First; int blockIndex = blockIndexNode.Value; m_FreeBlockIndexes.Remove(lengthFound, blockIndex); if (lengthFound > length) { BlockData blockData = m_BlockDatas[blockIndex]; m_BlockDatas[blockIndex] = new BlockData(blockData.ClusterIndex, length); WriteBlockData(blockIndex); int deltaLength = lengthFound - length; int anotherBlockIndex = m_BlockDatas.Count; BlockData anotherBlockData = new BlockData(blockData.ClusterIndex + GetUpBoundClusterCount(length), deltaLength); m_BlockDatas.Add(anotherBlockData); m_FreeBlockIndexes.Add(deltaLength, anotherBlockIndex); WriteBlockData(anotherBlockIndex); WriteHeaderData(); } return(blockIndex); } else { if (m_BlockDatas.Count >= m_HeaderData.MaxBlockCount) { return(-1); } long fileLength = m_Stream.Length; try { m_Stream.SetLength(fileLength + length); } catch { return(-1); } int blockIndex = m_BlockDatas.Count; BlockData blockData = new BlockData(GetUpBoundClusterCount(fileLength), length); m_BlockDatas.Add(blockData); WriteBlockData(blockIndex); WriteHeaderData(); return(blockIndex); } }