//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 loyalsoft. All rights reserved.
// Homepage: http://www.game7000.com/
// Feedback: http://www.game7000.com/
//------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
namespace GameFramework.FileSystem
{
///
/// 文件系统管理器。
///
internal sealed class FileSystemManager : GameFrameworkModule, IFileSystemManager
{
private readonly Dictionary m_FileSystems;
private IFileSystemHelper m_FileSystemHelper;
///
/// 初始化文件系统管理器的新实例。
///
public FileSystemManager()
{
m_FileSystems = new Dictionary(StringComparer.Ordinal);
m_FileSystemHelper = null;
}
///
/// 获取游戏框架模块优先级。
///
/// 优先级较高的模块会优先轮询,并且关闭操作会后进行。
internal override int Priority
{
get
{
return 4;
}
}
///
/// 获取文件系统数量。
///
public int Count
{
get
{
return m_FileSystems.Count;
}
}
///
/// 文件系统管理器轮询。
///
/// 逻辑流逝时间,以秒为单位。
/// 真实流逝时间,以秒为单位。
internal override void Update(float elapseSeconds, float realElapseSeconds)
{
}
///
/// 关闭并清理文件系统管理器。
///
internal override void Shutdown()
{
while (m_FileSystems.Count > 0)
{
foreach (KeyValuePair fileSystem in m_FileSystems)
{
DestroyFileSystem(fileSystem.Value, false);
break;
}
}
}
///
/// 设置文件系统辅助器。
///
/// 文件系统辅助器。
public void SetFileSystemHelper(IFileSystemHelper fileSystemHelper)
{
if (fileSystemHelper == null)
{
throw new GameFrameworkException("File system helper is invalid.");
}
m_FileSystemHelper = fileSystemHelper;
}
///
/// 检查是否存在文件系统。
///
/// 要检查的文件系统的完整路径。
/// 是否存在文件系统。
public bool HasFileSystem(string fullPath)
{
if (string.IsNullOrEmpty(fullPath))
{
throw new GameFrameworkException("Full path is invalid.");
}
return m_FileSystems.ContainsKey(Utility.Path.GetRegularPath(fullPath));
}
///
/// 获取文件系统。
///
/// 要获取的文件系统的完整路径。
/// 获取的文件系统。
public IFileSystem GetFileSystem(string fullPath)
{
if (string.IsNullOrEmpty(fullPath))
{
throw new GameFrameworkException("Full path is invalid.");
}
FileSystem fileSystem = null;
if (m_FileSystems.TryGetValue(Utility.Path.GetRegularPath(fullPath), out fileSystem))
{
return fileSystem;
}
return null;
}
///
/// 创建文件系统。
///
/// 要创建的文件系统的完整路径。
/// 要创建的文件系统的访问方式。
/// 要创建的文件系统的最大文件数量。
/// 要创建的文件系统的最大块数据数量。
/// 创建的文件系统。
public IFileSystem CreateFileSystem(string fullPath, FileSystemAccess access, int maxFileCount, int maxBlockCount)
{
if (m_FileSystemHelper == null)
{
throw new GameFrameworkException("File system helper is invalid.");
}
if (string.IsNullOrEmpty(fullPath))
{
throw new GameFrameworkException("Full path is invalid.");
}
if (access == FileSystemAccess.Unspecified)
{
throw new GameFrameworkException("Access is invalid.");
}
if (access == FileSystemAccess.Read)
{
throw new GameFrameworkException("Access read is invalid.");
}
fullPath = Utility.Path.GetRegularPath(fullPath);
if (m_FileSystems.ContainsKey(fullPath))
{
throw new GameFrameworkException(Utility.Text.Format("File system '{0}' is already exist.", fullPath));
}
FileSystemStream fileSystemStream = m_FileSystemHelper.CreateFileSystemStream(fullPath, access, true);
if (fileSystemStream == null)
{
throw new GameFrameworkException(Utility.Text.Format("Create file system stream for '{0}' failure.", fullPath));
}
FileSystem fileSystem = FileSystem.Create(fullPath, access, fileSystemStream, maxFileCount, maxBlockCount);
if (fileSystem == null)
{
throw new GameFrameworkException(Utility.Text.Format("Create file system '{0}' failure.", fullPath));
}
m_FileSystems.Add(fullPath, fileSystem);
return fileSystem;
}
///
/// 加载文件系统。
///
/// 要加载的文件系统的完整路径。
/// 要加载的文件系统的访问方式。
/// 加载的文件系统。
public IFileSystem LoadFileSystem(string fullPath, FileSystemAccess access)
{
if (m_FileSystemHelper == null)
{
throw new GameFrameworkException("File system helper is invalid.");
}
if (string.IsNullOrEmpty(fullPath))
{
throw new GameFrameworkException("Full path is invalid.");
}
if (access == FileSystemAccess.Unspecified)
{
throw new GameFrameworkException("Access is invalid.");
}
fullPath = Utility.Path.GetRegularPath(fullPath);
if (m_FileSystems.ContainsKey(fullPath))
{
throw new GameFrameworkException(Utility.Text.Format("File system '{0}' is already exist.", fullPath));
}
FileSystemStream fileSystemStream = m_FileSystemHelper.CreateFileSystemStream(fullPath, access, false);
if (fileSystemStream == null)
{
throw new GameFrameworkException(Utility.Text.Format("Create file system stream for '{0}' failure.", fullPath));
}
FileSystem fileSystem = FileSystem.Load(fullPath, access, fileSystemStream);
if (fileSystem == null)
{
throw new GameFrameworkException(Utility.Text.Format("Load file system '{0}' failure.", fullPath));
}
m_FileSystems.Add(fullPath, fileSystem);
return fileSystem;
}
///
/// 销毁文件系统。
///
/// 要销毁的文件系统。
/// 是否删除文件系统对应的物理文件。
public void DestroyFileSystem(IFileSystem fileSystem, bool deletePhysicalFile)
{
if (fileSystem == null)
{
throw new GameFrameworkException("File system is invalid.");
}
string fullPath = fileSystem.FullPath;
((FileSystem)fileSystem).Shutdown();
m_FileSystems.Remove(fullPath);
if (deletePhysicalFile && File.Exists(fullPath))
{
File.Delete(fullPath);
}
}
///
/// 获取所有文件系统集合。
///
/// 获取的所有文件系统集合。
public IFileSystem[] GetAllFileSystems()
{
int index = 0;
IFileSystem[] results = new IFileSystem[m_FileSystems.Count];
foreach (KeyValuePair fileSystem in m_FileSystems)
{
results[index++] = fileSystem.Value;
}
return results;
}
///
/// 获取所有文件系统集合。
///
/// 获取的所有文件系统集合。
public void GetAllFileSystems(List results)
{
if (results == null)
{
throw new GameFrameworkException("Results is invalid.");
}
results.Clear();
foreach (KeyValuePair fileSystem in m_FileSystems)
{
results.Add(fileSystem.Value);
}
}
}
}