//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 loyalsoft. All rights reserved.
// Homepage: http://www.game7000.com/
// Feedback: http://www.game7000.com/
//------------------------------------------------------------
using GameFramework.Download;
using GameFramework.FileSystem;
using GameFramework.ObjectPool;
using System;
using System.Collections.Generic;
using System.IO;
namespace GameFramework.Resource
{
///
/// 资源管理器。
///
internal sealed partial class ResourceManager : GameFrameworkModule, IResourceManager
{
private const string RemoteVersionListFileName = "GameFrameworkVersion.dat";
private const string LocalVersionListFileName = "GameFrameworkList.dat";
private const string DefaultExtension = "dat";
private const string TempExtension = "tmp";
private const int FileSystemMaxFileCount = 1024 * 16;
private const int FileSystemMaxBlockCount = 1024 * 256;
private Dictionary m_AssetInfos;
private Dictionary m_ResourceInfos;
private SortedDictionary m_ReadWriteResourceInfos;
private readonly Dictionary m_ReadOnlyFileSystems;
private readonly Dictionary m_ReadWriteFileSystems;
private readonly Dictionary m_ResourceGroups;
private PackageVersionListSerializer m_PackageVersionListSerializer;
private UpdatableVersionListSerializer m_UpdatableVersionListSerializer;
private ReadOnlyVersionListSerializer m_ReadOnlyVersionListSerializer;
private ReadWriteVersionListSerializer m_ReadWriteVersionListSerializer;
private ResourcePackVersionListSerializer m_ResourcePackVersionListSerializer;
private IFileSystemManager m_FileSystemManager;
private ResourceIniter m_ResourceIniter;
private VersionListProcessor m_VersionListProcessor;
private ResourceChecker m_ResourceChecker;
private ResourceUpdater m_ResourceUpdater;
private ResourceLoader m_ResourceLoader;
private IResourceHelper m_ResourceHelper;
private string m_ReadOnlyPath;
private string m_ReadWritePath;
private ResourceMode m_ResourceMode;
private bool m_RefuseSetFlag;
private string m_CurrentVariant;
private string m_UpdatePrefixUri;
private string m_ApplicableGameVersion;
private int m_InternalResourceVersion;
private MemoryStream m_DecompressCachedStream;
private DecryptResourceCallback m_DecryptResourceCallback;
private InitResourcesCompleteCallback m_InitResourcesCompleteCallback;
private UpdateVersionListCallbacks m_UpdateVersionListCallbacks;
private CheckResourcesCompleteCallback m_CheckResourcesCompleteCallback;
private ApplyResourcesCompleteCallback m_ApplyResourcesCompleteCallback;
private UpdateResourcesCompleteCallback m_UpdateResourcesCompleteCallback;
private EventHandler m_ResourceApplySuccessEventHandler;
private EventHandler m_ResourceApplyFailureEventHandler;
private EventHandler m_ResourceUpdateStartEventHandler;
private EventHandler m_ResourceUpdateChangedEventHandler;
private EventHandler m_ResourceUpdateSuccessEventHandler;
private EventHandler m_ResourceUpdateFailureEventHandler;
private EventHandler m_ResourceUpdateAllCompleteEventHandler;
///
/// 初始化资源管理器的新实例。
///
public ResourceManager()
{
m_AssetInfos = null;
m_ResourceInfos = null;
m_ReadWriteResourceInfos = null;
m_ReadOnlyFileSystems = new Dictionary(StringComparer.Ordinal);
m_ReadWriteFileSystems = new Dictionary(StringComparer.Ordinal);
m_ResourceGroups = new Dictionary(StringComparer.Ordinal);
m_PackageVersionListSerializer = null;
m_UpdatableVersionListSerializer = null;
m_ReadOnlyVersionListSerializer = null;
m_ReadWriteVersionListSerializer = null;
m_ResourcePackVersionListSerializer = null;
m_ResourceIniter = null;
m_VersionListProcessor = null;
m_ResourceChecker = null;
m_ResourceUpdater = null;
m_ResourceLoader = new ResourceLoader(this);
m_ResourceHelper = null;
m_ReadOnlyPath = null;
m_ReadWritePath = null;
m_ResourceMode = ResourceMode.Unspecified;
m_RefuseSetFlag = false;
m_CurrentVariant = null;
m_UpdatePrefixUri = null;
m_ApplicableGameVersion = null;
m_InternalResourceVersion = 0;
m_DecompressCachedStream = null;
m_DecryptResourceCallback = null;
m_InitResourcesCompleteCallback = null;
m_UpdateVersionListCallbacks = null;
m_CheckResourcesCompleteCallback = null;
m_ApplyResourcesCompleteCallback = null;
m_UpdateResourcesCompleteCallback = null;
m_ResourceApplySuccessEventHandler = null;
m_ResourceApplyFailureEventHandler = null;
m_ResourceUpdateStartEventHandler = null;
m_ResourceUpdateChangedEventHandler = null;
m_ResourceUpdateSuccessEventHandler = null;
m_ResourceUpdateFailureEventHandler = null;
m_ResourceUpdateAllCompleteEventHandler = null;
}
///
/// 获取游戏框架模块优先级。
///
/// 优先级较高的模块会优先轮询,并且关闭操作会后进行。
internal override int Priority
{
get
{
return 3;
}
}
///
/// 获取资源只读区路径。
///
public string ReadOnlyPath
{
get
{
return m_ReadOnlyPath;
}
}
///
/// 获取资源读写区路径。
///
public string ReadWritePath
{
get
{
return m_ReadWritePath;
}
}
///
/// 获取资源模式。
///
public ResourceMode ResourceMode
{
get
{
return m_ResourceMode;
}
}
///
/// 获取当前变体。
///
public string CurrentVariant
{
get
{
return m_CurrentVariant;
}
}
///
/// 获取单机模式版本资源列表序列化器。
///
public PackageVersionListSerializer PackageVersionListSerializer
{
get
{
return m_PackageVersionListSerializer;
}
}
///
/// 获取可更新模式版本资源列表序列化器。
///
public UpdatableVersionListSerializer UpdatableVersionListSerializer
{
get
{
return m_UpdatableVersionListSerializer;
}
}
///
/// 获取本地只读区版本资源列表序列化器。
///
public ReadOnlyVersionListSerializer ReadOnlyVersionListSerializer
{
get
{
return m_ReadOnlyVersionListSerializer;
}
}
///
/// 获取本地读写区版本资源列表序列化器。
///
public ReadWriteVersionListSerializer ReadWriteVersionListSerializer
{
get
{
return m_ReadWriteVersionListSerializer;
}
}
///
/// 获取资源包版本资源列表序列化器。
///
public ResourcePackVersionListSerializer ResourcePackVersionListSerializer
{
get
{
return m_ResourcePackVersionListSerializer;
}
}
///
/// 获取当前资源适用的游戏版本号。
///
public string ApplicableGameVersion
{
get
{
return m_ApplicableGameVersion;
}
}
///
/// 获取当前内部资源版本号。
///
public int InternalResourceVersion
{
get
{
return m_InternalResourceVersion;
}
}
///
/// 获取资源数量。
///
public int AssetCount
{
get
{
return m_AssetInfos != null ? m_AssetInfos.Count : 0;
}
}
///
/// 获取资源数量。
///
public int ResourceCount
{
get
{
return m_ResourceInfos != null ? m_ResourceInfos.Count : 0;
}
}
///
/// 获取资源组数量。
///
public int ResourceGroupCount
{
get
{
return m_ResourceGroups.Count;
}
}
///
/// 获取或设置资源更新下载地址前缀。
///
public string UpdatePrefixUri
{
get
{
return m_UpdatePrefixUri;
}
set
{
m_UpdatePrefixUri = value;
}
}
///
/// 获取或设置每更新多少字节的资源,重新生成一次版本资源列表。
///
public int GenerateReadWriteVersionListLength
{
get
{
return m_ResourceUpdater != null ? m_ResourceUpdater.GenerateReadWriteVersionListLength : 0;
}
set
{
if (m_ResourceUpdater == null)
{
throw new GameFrameworkException("You can not use GenerateReadWriteVersionListLength at this time.");
}
m_ResourceUpdater.GenerateReadWriteVersionListLength = value;
}
}
///
/// 获取正在应用的资源包路径。
///
public string ApplyingResourcePackPath
{
get
{
return m_ResourceUpdater != null ? m_ResourceUpdater.ApplyingResourcePackPath : null;
}
}
///
/// 获取等待应用资源数量。
///
public int ApplyWaitingCount
{
get
{
return m_ResourceUpdater != null ? m_ResourceUpdater.ApplyWaitingCount : 0;
}
}
///
/// 获取或设置资源更新重试次数。
///
public int UpdateRetryCount
{
get
{
return m_ResourceUpdater != null ? m_ResourceUpdater.UpdateRetryCount : 0;
}
set
{
if (m_ResourceUpdater == null)
{
throw new GameFrameworkException("You can not use UpdateRetryCount at this time.");
}
m_ResourceUpdater.UpdateRetryCount = value;
}
}
///
/// 获取正在更新的资源组。
///
public IResourceGroup UpdatingResourceGroup
{
get
{
return m_ResourceUpdater != null ? m_ResourceUpdater.UpdatingResourceGroup : null;
}
}
///
/// 获取等待更新资源数量。
///
public int UpdateWaitingCount
{
get
{
return m_ResourceUpdater != null ? m_ResourceUpdater.UpdateWaitingCount : 0;
}
}
///
/// 获取使用时下载的等待更新资源数量。
///
public int UpdateWaitingWhilePlayingCount
{
get
{
return m_ResourceUpdater != null ? m_ResourceUpdater.UpdateWaitingWhilePlayingCount : 0;
}
}
///
/// 获取候选更新资源数量。
///
public int UpdateCandidateCount
{
get
{
return m_ResourceUpdater != null ? m_ResourceUpdater.UpdateCandidateCount : 0;
}
}
///
/// 获取加载资源代理总数量。
///
public int LoadTotalAgentCount
{
get
{
return m_ResourceLoader.TotalAgentCount;
}
}
///
/// 获取可用加载资源代理数量。
///
public int LoadFreeAgentCount
{
get
{
return m_ResourceLoader.FreeAgentCount;
}
}
///
/// 获取工作中加载资源代理数量。
///
public int LoadWorkingAgentCount
{
get
{
return m_ResourceLoader.WorkingAgentCount;
}
}
///
/// 获取等待加载资源任务数量。
///
public int LoadWaitingTaskCount
{
get
{
return m_ResourceLoader.WaitingTaskCount;
}
}
///
/// 获取或设置资源对象池自动释放可释放对象的间隔秒数。
///
public float AssetAutoReleaseInterval
{
get
{
return m_ResourceLoader.AssetAutoReleaseInterval;
}
set
{
m_ResourceLoader.AssetAutoReleaseInterval = value;
}
}
///
/// 获取或设置资源对象池的容量。
///
public int AssetCapacity
{
get
{
return m_ResourceLoader.AssetCapacity;
}
set
{
m_ResourceLoader.AssetCapacity = value;
}
}
///
/// 获取或设置资源对象池对象过期秒数。
///
public float AssetExpireTime
{
get
{
return m_ResourceLoader.AssetExpireTime;
}
set
{
m_ResourceLoader.AssetExpireTime = value;
}
}
///
/// 获取或设置资源对象池的优先级。
///
public int AssetPriority
{
get
{
return m_ResourceLoader.AssetPriority;
}
set
{
m_ResourceLoader.AssetPriority = value;
}
}
///
/// 获取或设置资源对象池自动释放可释放对象的间隔秒数。
///
public float ResourceAutoReleaseInterval
{
get
{
return m_ResourceLoader.ResourceAutoReleaseInterval;
}
set
{
m_ResourceLoader.ResourceAutoReleaseInterval = value;
}
}
///
/// 获取或设置资源对象池的容量。
///
public int ResourceCapacity
{
get
{
return m_ResourceLoader.ResourceCapacity;
}
set
{
m_ResourceLoader.ResourceCapacity = value;
}
}
///
/// 获取或设置资源对象池对象过期秒数。
///
public float ResourceExpireTime
{
get
{
return m_ResourceLoader.ResourceExpireTime;
}
set
{
m_ResourceLoader.ResourceExpireTime = value;
}
}
///
/// 获取或设置资源对象池的优先级。
///
public int ResourcePriority
{
get
{
return m_ResourceLoader.ResourcePriority;
}
set
{
m_ResourceLoader.ResourcePriority = value;
}
}
///
/// 资源应用成功事件。
///
public event EventHandler ResourceApplySuccess
{
add
{
m_ResourceApplySuccessEventHandler += value;
}
remove
{
m_ResourceApplySuccessEventHandler -= value;
}
}
///
/// 资源应用失败事件。
///
public event EventHandler ResourceApplyFailure
{
add
{
m_ResourceApplyFailureEventHandler += value;
}
remove
{
m_ResourceApplyFailureEventHandler -= value;
}
}
///
/// 资源更新开始事件。
///
public event EventHandler ResourceUpdateStart
{
add
{
m_ResourceUpdateStartEventHandler += value;
}
remove
{
m_ResourceUpdateStartEventHandler -= value;
}
}
///
/// 资源更新改变事件。
///
public event EventHandler ResourceUpdateChanged
{
add
{
m_ResourceUpdateChangedEventHandler += value;
}
remove
{
m_ResourceUpdateChangedEventHandler -= value;
}
}
///
/// 资源更新成功事件。
///
public event EventHandler ResourceUpdateSuccess
{
add
{
m_ResourceUpdateSuccessEventHandler += value;
}
remove
{
m_ResourceUpdateSuccessEventHandler -= value;
}
}
///
/// 资源更新失败事件。
///
public event EventHandler ResourceUpdateFailure
{
add
{
m_ResourceUpdateFailureEventHandler += value;
}
remove
{
m_ResourceUpdateFailureEventHandler -= value;
}
}
///
/// 资源更新全部完成事件。
///
public event EventHandler ResourceUpdateAllComplete
{
add
{
m_ResourceUpdateAllCompleteEventHandler += value;
}
remove
{
m_ResourceUpdateAllCompleteEventHandler -= value;
}
}
///
/// 资源管理器轮询。
///
/// 逻辑流逝时间,以秒为单位。
/// 真实流逝时间,以秒为单位。
internal override void Update(float elapseSeconds, float realElapseSeconds)
{
if (m_ResourceUpdater != null)
{
m_ResourceUpdater.Update(elapseSeconds, realElapseSeconds);
}
m_ResourceLoader.Update(elapseSeconds, realElapseSeconds);
}
///
/// 关闭并清理资源管理器。
///
internal override void Shutdown()
{
if (m_ResourceIniter != null)
{
m_ResourceIniter.Shutdown();
m_ResourceIniter = null;
}
if (m_VersionListProcessor != null)
{
m_VersionListProcessor.VersionListUpdateSuccess -= OnVersionListProcessorUpdateSuccess;
m_VersionListProcessor.VersionListUpdateFailure -= OnVersionListProcessorUpdateFailure;
m_VersionListProcessor.Shutdown();
m_VersionListProcessor = null;
}
if (m_ResourceChecker != null)
{
m_ResourceChecker.ResourceNeedUpdate -= OnCheckerResourceNeedUpdate;
m_ResourceChecker.ResourceCheckComplete -= OnCheckerResourceCheckComplete;
m_ResourceChecker.Shutdown();
m_ResourceChecker = null;
}
if (m_ResourceUpdater != null)
{
m_ResourceUpdater.ResourceApplySuccess -= OnUpdaterResourceApplySuccess;
m_ResourceUpdater.ResourceApplyFailure -= OnUpdaterResourceApplyFailure;
m_ResourceUpdater.ResourceApplyComplete -= OnUpdaterResourceApplyComplete;
m_ResourceUpdater.ResourceUpdateStart -= OnUpdaterResourceUpdateStart;
m_ResourceUpdater.ResourceUpdateChanged -= OnUpdaterResourceUpdateChanged;
m_ResourceUpdater.ResourceUpdateSuccess -= OnUpdaterResourceUpdateSuccess;
m_ResourceUpdater.ResourceUpdateFailure -= OnUpdaterResourceUpdateFailure;
m_ResourceUpdater.ResourceUpdateComplete -= OnUpdaterResourceUpdateComplete;
m_ResourceUpdater.ResourceUpdateAllComplete -= OnUpdaterResourceUpdateAllComplete;
m_ResourceUpdater.Shutdown();
m_ResourceUpdater = null;
if (m_ReadWriteResourceInfos != null)
{
m_ReadWriteResourceInfos.Clear();
m_ReadWriteResourceInfos = null;
}
if (m_DecompressCachedStream != null)
{
m_DecompressCachedStream.Dispose();
m_DecompressCachedStream = null;
}
}
if (m_ResourceLoader != null)
{
m_ResourceLoader.Shutdown();
m_ResourceLoader = null;
}
if (m_AssetInfos != null)
{
m_AssetInfos.Clear();
m_AssetInfos = null;
}
if (m_ResourceInfos != null)
{
m_ResourceInfos.Clear();
m_ResourceInfos = null;
}
m_ReadOnlyFileSystems.Clear();
m_ReadWriteFileSystems.Clear();
m_ResourceGroups.Clear();
}
///
/// 设置资源只读区路径。
///
/// 资源只读区路径。
public void SetReadOnlyPath(string readOnlyPath)
{
if (string.IsNullOrEmpty(readOnlyPath))
{
throw new GameFrameworkException("Read-only path is invalid.");
}
if (m_RefuseSetFlag)
{
throw new GameFrameworkException("You can not set read-only path at this time.");
}
if (m_ResourceLoader.TotalAgentCount > 0)
{
throw new GameFrameworkException("You must set read-only path before add load resource agent helper.");
}
m_ReadOnlyPath = readOnlyPath;
}
///
/// 设置资源读写区路径。
///
/// 资源读写区路径。
public void SetReadWritePath(string readWritePath)
{
if (string.IsNullOrEmpty(readWritePath))
{
throw new GameFrameworkException("Read-write path is invalid.");
}
if (m_RefuseSetFlag)
{
throw new GameFrameworkException("You can not set read-write path at this time.");
}
if (m_ResourceLoader.TotalAgentCount > 0)
{
throw new GameFrameworkException("You must set read-write path before add load resource agent helper.");
}
m_ReadWritePath = readWritePath;
}
///
/// 设置资源模式。
///
/// 资源模式。
public void SetResourceMode(ResourceMode resourceMode)
{
if (resourceMode == ResourceMode.Unspecified)
{
throw new GameFrameworkException("Resource mode is invalid.");
}
if (m_RefuseSetFlag)
{
throw new GameFrameworkException("You can not set resource mode at this time.");
}
if (m_ResourceMode == ResourceMode.Unspecified)
{
m_ResourceMode = resourceMode;
if (m_ResourceMode == ResourceMode.Package)
{
m_PackageVersionListSerializer = new PackageVersionListSerializer();
m_ResourceIniter = new ResourceIniter(this);
m_ResourceIniter.ResourceInitComplete += OnIniterResourceInitComplete;
}
else if (m_ResourceMode == ResourceMode.Updatable || m_ResourceMode == ResourceMode.UpdatableWhilePlaying)
{
m_UpdatableVersionListSerializer = new UpdatableVersionListSerializer();
m_ReadOnlyVersionListSerializer = new ReadOnlyVersionListSerializer();
m_ReadWriteVersionListSerializer = new ReadWriteVersionListSerializer();
m_ResourcePackVersionListSerializer = new ResourcePackVersionListSerializer();
m_VersionListProcessor = new VersionListProcessor(this);
m_VersionListProcessor.VersionListUpdateSuccess += OnVersionListProcessorUpdateSuccess;
m_VersionListProcessor.VersionListUpdateFailure += OnVersionListProcessorUpdateFailure;
m_ResourceChecker = new ResourceChecker(this);
m_ResourceChecker.ResourceNeedUpdate += OnCheckerResourceNeedUpdate;
m_ResourceChecker.ResourceCheckComplete += OnCheckerResourceCheckComplete;
m_ResourceUpdater = new ResourceUpdater(this);
m_ResourceUpdater.ResourceApplySuccess += OnUpdaterResourceApplySuccess;
m_ResourceUpdater.ResourceApplyFailure += OnUpdaterResourceApplyFailure;
m_ResourceUpdater.ResourceApplyComplete += OnUpdaterResourceApplyComplete;
m_ResourceUpdater.ResourceUpdateStart += OnUpdaterResourceUpdateStart;
m_ResourceUpdater.ResourceUpdateChanged += OnUpdaterResourceUpdateChanged;
m_ResourceUpdater.ResourceUpdateSuccess += OnUpdaterResourceUpdateSuccess;
m_ResourceUpdater.ResourceUpdateFailure += OnUpdaterResourceUpdateFailure;
m_ResourceUpdater.ResourceUpdateComplete += OnUpdaterResourceUpdateComplete;
m_ResourceUpdater.ResourceUpdateAllComplete += OnUpdaterResourceUpdateAllComplete;
}
}
else if (m_ResourceMode != resourceMode)
{
throw new GameFrameworkException("You can not change resource mode at this time.");
}
}
///
/// 设置当前变体。
///
/// 当前变体。
public void SetCurrentVariant(string currentVariant)
{
if (m_RefuseSetFlag)
{
throw new GameFrameworkException("You can not set current variant at this time.");
}
m_CurrentVariant = currentVariant;
}
///
/// 设置对象池管理器。
///
/// 对象池管理器。
public void SetObjectPoolManager(IObjectPoolManager objectPoolManager)
{
if (objectPoolManager == null)
{
throw new GameFrameworkException("Object pool manager is invalid.");
}
m_ResourceLoader.SetObjectPoolManager(objectPoolManager);
}
///
/// 设置文件系统管理器。
///
/// 文件系统管理器。
public void SetFileSystemManager(IFileSystemManager fileSystemManager)
{
if (fileSystemManager == null)
{
throw new GameFrameworkException("File system manager is invalid.");
}
m_FileSystemManager = fileSystemManager;
}
///
/// 设置下载管理器。
///
/// 下载管理器。
public void SetDownloadManager(IDownloadManager downloadManager)
{
if (downloadManager == null)
{
throw new GameFrameworkException("Download manager is invalid.");
}
if (m_VersionListProcessor != null)
{
m_VersionListProcessor.SetDownloadManager(downloadManager);
}
if (m_ResourceUpdater != null)
{
m_ResourceUpdater.SetDownloadManager(downloadManager);
}
}
///
/// 设置解密资源回调函数。
///
/// 要设置的解密资源回调函数。
/// 如果不设置,将使用默认的解密资源回调函数。
public void SetDecryptResourceCallback(DecryptResourceCallback decryptResourceCallback)
{
if (m_ResourceLoader.TotalAgentCount > 0)
{
throw new GameFrameworkException("You must set decrypt resource callback before add load resource agent helper.");
}
m_DecryptResourceCallback = decryptResourceCallback;
}
///
/// 设置资源辅助器。
///
/// 资源辅助器。
public void SetResourceHelper(IResourceHelper resourceHelper)
{
if (resourceHelper == null)
{
throw new GameFrameworkException("Resource helper is invalid.");
}
if (m_ResourceLoader.TotalAgentCount > 0)
{
throw new GameFrameworkException("You must set resource helper before add load resource agent helper.");
}
m_ResourceHelper = resourceHelper;
}
///
/// 增加加载资源代理辅助器。
///
/// 要增加的加载资源代理辅助器。
public void AddLoadResourceAgentHelper(ILoadResourceAgentHelper loadResourceAgentHelper)
{
if (m_ResourceHelper == null)
{
throw new GameFrameworkException("Resource helper is invalid.");
}
if (string.IsNullOrEmpty(m_ReadOnlyPath))
{
throw new GameFrameworkException("Read-only path is invalid.");
}
if (string.IsNullOrEmpty(m_ReadWritePath))
{
throw new GameFrameworkException("Read-write path is invalid.");
}
m_ResourceLoader.AddLoadResourceAgentHelper(loadResourceAgentHelper, m_ResourceHelper, m_ReadOnlyPath, m_ReadWritePath, m_DecryptResourceCallback);
}
///
/// 使用单机模式并初始化资源。
///
/// 使用单机模式并初始化资源完成时的回调函数。
public void InitResources(InitResourcesCompleteCallback initResourcesCompleteCallback)
{
if (initResourcesCompleteCallback == null)
{
throw new GameFrameworkException("Init resources complete callback is invalid.");
}
if (m_ResourceMode == ResourceMode.Unspecified)
{
throw new GameFrameworkException("You must set resource mode first.");
}
if (m_ResourceMode != ResourceMode.Package)
{
throw new GameFrameworkException("You can not use InitResources without package resource mode.");
}
if (m_ResourceIniter == null)
{
throw new GameFrameworkException("You can not use InitResources at this time.");
}
m_RefuseSetFlag = true;
m_InitResourcesCompleteCallback = initResourcesCompleteCallback;
m_ResourceIniter.InitResources(m_CurrentVariant);
}
///
/// 使用可更新模式并检查版本资源列表。
///
/// 最新的内部资源版本号。
/// 检查版本资源列表结果。
public CheckVersionListResult CheckVersionList(int latestInternalResourceVersion)
{
if (m_ResourceMode == ResourceMode.Unspecified)
{
throw new GameFrameworkException("You must set resource mode first.");
}
if (m_ResourceMode != ResourceMode.Updatable && m_ResourceMode != ResourceMode.UpdatableWhilePlaying)
{
throw new GameFrameworkException("You can not use CheckVersionList without updatable resource mode.");
}
if (m_VersionListProcessor == null)
{
throw new GameFrameworkException("You can not use CheckVersionList at this time.");
}
return m_VersionListProcessor.CheckVersionList(latestInternalResourceVersion);
}
///
/// 使用可更新模式并更新版本资源列表。
///
/// 版本资源列表大小。
/// 版本资源列表哈希值。
/// 版本资源列表压缩后大小。
/// 版本资源列表压缩后哈希值。
/// 版本资源列表更新回调函数集。
public void UpdateVersionList(int versionListLength, int versionListHashCode, int versionListCompressedLength, int versionListCompressedHashCode, UpdateVersionListCallbacks updateVersionListCallbacks)
{
if (updateVersionListCallbacks == null)
{
throw new GameFrameworkException("Update version list callbacks is invalid.");
}
if (m_ResourceMode == ResourceMode.Unspecified)
{
throw new GameFrameworkException("You must set resource mode first.");
}
if (m_ResourceMode != ResourceMode.Updatable && m_ResourceMode != ResourceMode.UpdatableWhilePlaying)
{
throw new GameFrameworkException("You can not use UpdateVersionList without updatable resource mode.");
}
if (m_VersionListProcessor == null)
{
throw new GameFrameworkException("You can not use UpdateVersionList at this time.");
}
m_UpdateVersionListCallbacks = updateVersionListCallbacks;
m_VersionListProcessor.UpdateVersionList(versionListLength, versionListHashCode, versionListCompressedLength, versionListCompressedHashCode);
}
///
/// 使用可更新模式并检查资源。
///
/// 是否忽略处理其它变体的资源,若不忽略,将会移除其它变体的资源。
/// 使用可更新模式并检查资源完成时的回调函数。
public void CheckResources(bool ignoreOtherVariant, CheckResourcesCompleteCallback checkResourcesCompleteCallback)
{
if (checkResourcesCompleteCallback == null)
{
throw new GameFrameworkException("Check resources complete callback is invalid.");
}
if (m_ResourceMode == ResourceMode.Unspecified)
{
throw new GameFrameworkException("You must set resource mode first.");
}
if (m_ResourceMode != ResourceMode.Updatable && m_ResourceMode != ResourceMode.UpdatableWhilePlaying)
{
throw new GameFrameworkException("You can not use CheckResources without updatable resource mode.");
}
if (m_ResourceChecker == null)
{
throw new GameFrameworkException("You can not use CheckResources at this time.");
}
m_RefuseSetFlag = true;
m_CheckResourcesCompleteCallback = checkResourcesCompleteCallback;
m_ResourceChecker.CheckResources(m_CurrentVariant, ignoreOtherVariant);
}
///
/// 使用可更新模式并应用资源包资源。
///
/// 要应用的资源包路径。
/// 使用可更新模式并应用资源包资源完成时的回调函数。
public void ApplyResources(string resourcePackPath, ApplyResourcesCompleteCallback applyResourcesCompleteCallback)
{
if (string.IsNullOrEmpty(resourcePackPath))
{
throw new GameFrameworkException("Resource pack path is invalid.");
}
if (!File.Exists(resourcePackPath))
{
throw new GameFrameworkException(Utility.Text.Format("Resource pack '{0}' is not exist.", resourcePackPath));
}
if (applyResourcesCompleteCallback == null)
{
throw new GameFrameworkException("Apply resources complete callback is invalid.");
}
if (m_ResourceMode == ResourceMode.Unspecified)
{
throw new GameFrameworkException("You must set resource mode first.");
}
if (m_ResourceMode != ResourceMode.Updatable && m_ResourceMode != ResourceMode.UpdatableWhilePlaying)
{
throw new GameFrameworkException("You can not use ApplyResources without updatable resource mode.");
}
if (m_ResourceUpdater == null)
{
throw new GameFrameworkException("You can not use ApplyResources at this time.");
}
m_ApplyResourcesCompleteCallback = applyResourcesCompleteCallback;
m_ResourceUpdater.ApplyResources(resourcePackPath);
}
///
/// 使用可更新模式并更新所有资源。
///
/// 使用可更新模式并更新默认资源组完成时的回调函数。
public void UpdateResources(UpdateResourcesCompleteCallback updateResourcesCompleteCallback)
{
UpdateResources(string.Empty, updateResourcesCompleteCallback);
}
///
/// 使用可更新模式并更新指定资源组的资源。
///
/// 要更新的资源组名称。
/// 使用可更新模式并更新指定资源组完成时的回调函数。
public void UpdateResources(string resourceGroupName, UpdateResourcesCompleteCallback updateResourcesCompleteCallback)
{
if (updateResourcesCompleteCallback == null)
{
throw new GameFrameworkException("Update resources complete callback is invalid.");
}
if (m_ResourceMode == ResourceMode.Unspecified)
{
throw new GameFrameworkException("You must set resource mode first.");
}
if (m_ResourceMode != ResourceMode.Updatable && m_ResourceMode != ResourceMode.UpdatableWhilePlaying)
{
throw new GameFrameworkException("You can not use UpdateResources without updatable resource mode.");
}
if (m_ResourceUpdater == null)
{
throw new GameFrameworkException("You can not use UpdateResources at this time.");
}
ResourceGroup resourceGroup = (ResourceGroup)GetResourceGroup(resourceGroupName);
if (resourceGroup == null)
{
throw new GameFrameworkException(Utility.Text.Format("Can not find resource group '{0}'.", resourceGroupName));
}
m_UpdateResourcesCompleteCallback = updateResourcesCompleteCallback;
m_ResourceUpdater.UpdateResources(resourceGroup);
}
///
/// 停止更新资源。
///
public void StopUpdateResources()
{
if (m_ResourceMode == ResourceMode.Unspecified)
{
throw new GameFrameworkException("You must set resource mode first.");
}
if (m_ResourceMode != ResourceMode.Updatable && m_ResourceMode != ResourceMode.UpdatableWhilePlaying)
{
throw new GameFrameworkException("You can not use StopUpdateResources without updatable resource mode.");
}
if (m_ResourceUpdater == null)
{
throw new GameFrameworkException("You can not use StopUpdateResources at this time.");
}
m_ResourceUpdater.StopUpdateResources();
m_UpdateResourcesCompleteCallback = null;
}
///
/// 校验资源包。
///
/// 要校验的资源包路径。
/// 是否校验资源包成功。
public bool VerifyResourcePack(string resourcePackPath)
{
if (string.IsNullOrEmpty(resourcePackPath))
{
throw new GameFrameworkException("Resource pack path is invalid.");
}
if (!File.Exists(resourcePackPath))
{
throw new GameFrameworkException(Utility.Text.Format("Resource pack '{0}' is not exist.", resourcePackPath));
}
if (m_ResourceMode == ResourceMode.Unspecified)
{
throw new GameFrameworkException("You must set resource mode first.");
}
if (m_ResourceMode != ResourceMode.Updatable && m_ResourceMode != ResourceMode.UpdatableWhilePlaying)
{
throw new GameFrameworkException("You can not use VerifyResourcePack without updatable resource mode.");
}
if (m_ResourcePackVersionListSerializer == null)
{
throw new GameFrameworkException("You can not use VerifyResourcePack at this time.");
}
try
{
long length = 0L;
ResourcePackVersionList versionList = default(ResourcePackVersionList);
using (FileStream fileStream = new FileStream(resourcePackPath, FileMode.Open, FileAccess.Read))
{
length = fileStream.Length;
versionList = m_ResourcePackVersionListSerializer.Deserialize(fileStream);
}
if (!versionList.IsValid)
{
return false;
}
if (versionList.Offset + versionList.Length != length)
{
return false;
}
int hashCode = 0;
using (FileStream fileStream = new FileStream(resourcePackPath, FileMode.Open, FileAccess.Read))
{
fileStream.Position = versionList.Offset;
hashCode = Utility.Verifier.GetCrc32(fileStream);
}
if (versionList.HashCode != hashCode)
{
return false;
}
return true;
}
catch
{
return false;
}
}
///
/// 获取所有加载资源任务的信息。
///
/// 所有加载资源任务的信息。
public TaskInfo[] GetAllLoadAssetInfos()
{
return m_ResourceLoader.GetAllLoadAssetInfos();
}
///
/// 获取所有加载资源任务的信息。
///
/// 所有加载资源任务的信息。
public void GetAllLoadAssetInfos(List results)
{
m_ResourceLoader.GetAllLoadAssetInfos(results);
}
///
/// 检查资源是否存在。
///
/// 要检查资源的名称。
/// 检查资源是否存在的结果。
public HasAssetResult HasAsset(string assetName)
{
if (string.IsNullOrEmpty(assetName))
{
throw new GameFrameworkException("Asset name is invalid.");
}
return m_ResourceLoader.HasAsset(assetName);
}
///
/// 异步加载资源。
///
/// 要加载资源的名称。
/// 加载资源回调函数集。
public void LoadAsset(string assetName, LoadAssetCallbacks loadAssetCallbacks)
{
if (string.IsNullOrEmpty(assetName))
{
throw new GameFrameworkException("Asset name is invalid.");
}
if (loadAssetCallbacks == null)
{
throw new GameFrameworkException("Load asset callbacks is invalid.");
}
m_ResourceLoader.LoadAsset(assetName, null, Constant.DefaultPriority, loadAssetCallbacks, null);
}
///
/// 异步加载资源。
///
/// 要加载资源的名称。
/// 要加载资源的类型。
/// 加载资源回调函数集。
public void LoadAsset(string assetName, Type assetType, LoadAssetCallbacks loadAssetCallbacks)
{
if (string.IsNullOrEmpty(assetName))
{
throw new GameFrameworkException("Asset name is invalid.");
}
if (loadAssetCallbacks == null)
{
throw new GameFrameworkException("Load asset callbacks is invalid.");
}
m_ResourceLoader.LoadAsset(assetName, assetType, Constant.DefaultPriority, loadAssetCallbacks, null);
}
///
/// 异步加载资源。
///
/// 要加载资源的名称。
/// 加载资源的优先级。
/// 加载资源回调函数集。
public void LoadAsset(string assetName, int priority, LoadAssetCallbacks loadAssetCallbacks)
{
if (string.IsNullOrEmpty(assetName))
{
throw new GameFrameworkException("Asset name is invalid.");
}
if (loadAssetCallbacks == null)
{
throw new GameFrameworkException("Load asset callbacks is invalid.");
}
m_ResourceLoader.LoadAsset(assetName, null, priority, loadAssetCallbacks, null);
}
///
/// 异步加载资源。
///
/// 要加载资源的名称。
/// 加载资源回调函数集。
/// 用户自定义数据。
public void LoadAsset(string assetName, LoadAssetCallbacks loadAssetCallbacks, object userData)
{
if (string.IsNullOrEmpty(assetName))
{
throw new GameFrameworkException("Asset name is invalid.");
}
if (loadAssetCallbacks == null)
{
throw new GameFrameworkException("Load asset callbacks is invalid.");
}
m_ResourceLoader.LoadAsset(assetName, null, Constant.DefaultPriority, loadAssetCallbacks, userData);
}
///
/// 异步加载资源。
///
/// 要加载资源的名称。
/// 要加载资源的类型。
/// 加载资源的优先级。
/// 加载资源回调函数集。
public void LoadAsset(string assetName, Type assetType, int priority, LoadAssetCallbacks loadAssetCallbacks)
{
if (string.IsNullOrEmpty(assetName))
{
throw new GameFrameworkException("Asset name is invalid.");
}
if (loadAssetCallbacks == null)
{
throw new GameFrameworkException("Load asset callbacks is invalid.");
}
m_ResourceLoader.LoadAsset(assetName, assetType, priority, loadAssetCallbacks, null);
}
///
/// 异步加载资源。
///
/// 要加载资源的名称。
/// 要加载资源的类型。
/// 加载资源回调函数集。
/// 用户自定义数据。
public void LoadAsset(string assetName, Type assetType, LoadAssetCallbacks loadAssetCallbacks, object userData)
{
if (string.IsNullOrEmpty(assetName))
{
throw new GameFrameworkException("Asset name is invalid.");
}
if (loadAssetCallbacks == null)
{
throw new GameFrameworkException("Load asset callbacks is invalid.");
}
m_ResourceLoader.LoadAsset(assetName, assetType, Constant.DefaultPriority, loadAssetCallbacks, userData);
}
///
/// 异步加载资源。
///
/// 要加载资源的名称。
/// 加载资源的优先级。
/// 加载资源回调函数集。
/// 用户自定义数据。
public void LoadAsset(string assetName, int priority, LoadAssetCallbacks loadAssetCallbacks, object userData)
{
if (string.IsNullOrEmpty(assetName))
{
throw new GameFrameworkException("Asset name is invalid.");
}
if (loadAssetCallbacks == null)
{
throw new GameFrameworkException("Load asset callbacks is invalid.");
}
m_ResourceLoader.LoadAsset(assetName, null, priority, loadAssetCallbacks, userData);
}
///
/// 异步加载资源。
///
/// 要加载资源的名称。
/// 要加载资源的类型。
/// 加载资源的优先级。
/// 加载资源回调函数集。
/// 用户自定义数据。
public void LoadAsset(string assetName, Type assetType, int priority, LoadAssetCallbacks loadAssetCallbacks, object userData)
{
if (string.IsNullOrEmpty(assetName))
{
throw new GameFrameworkException("Asset name is invalid.");
}
if (loadAssetCallbacks == null)
{
throw new GameFrameworkException("Load asset callbacks is invalid.");
}
m_ResourceLoader.LoadAsset(assetName, assetType, priority, loadAssetCallbacks, userData);
}
///
/// 卸载资源。
///
/// 要卸载的资源。
public void UnloadAsset(object asset)
{
if (asset == null)
{
throw new GameFrameworkException("Asset is invalid.");
}
if (m_ResourceLoader == null)
{
return;
}
m_ResourceLoader.UnloadAsset(asset);
}
///
/// 异步加载场景。
///
/// 要加载场景资源的名称。
/// 加载场景回调函数集。
public void LoadScene(string sceneAssetName, LoadSceneCallbacks loadSceneCallbacks)
{
if (string.IsNullOrEmpty(sceneAssetName))
{
throw new GameFrameworkException("Scene asset name is invalid.");
}
if (loadSceneCallbacks == null)
{
throw new GameFrameworkException("Load scene callbacks is invalid.");
}
m_ResourceLoader.LoadScene(sceneAssetName, Constant.DefaultPriority, loadSceneCallbacks, null);
}
///
/// 异步加载场景。
///
/// 要加载场景资源的名称。
/// 加载场景资源的优先级。
/// 加载场景回调函数集。
public void LoadScene(string sceneAssetName, int priority, LoadSceneCallbacks loadSceneCallbacks)
{
if (string.IsNullOrEmpty(sceneAssetName))
{
throw new GameFrameworkException("Scene asset name is invalid.");
}
if (loadSceneCallbacks == null)
{
throw new GameFrameworkException("Load scene callbacks is invalid.");
}
m_ResourceLoader.LoadScene(sceneAssetName, priority, loadSceneCallbacks, null);
}
///
/// 异步加载场景。
///
/// 要加载场景资源的名称。
/// 加载场景回调函数集。
/// 用户自定义数据。
public void LoadScene(string sceneAssetName, LoadSceneCallbacks loadSceneCallbacks, object userData)
{
if (string.IsNullOrEmpty(sceneAssetName))
{
throw new GameFrameworkException("Scene asset name is invalid.");
}
if (loadSceneCallbacks == null)
{
throw new GameFrameworkException("Load scene callbacks is invalid.");
}
m_ResourceLoader.LoadScene(sceneAssetName, Constant.DefaultPriority, loadSceneCallbacks, userData);
}
///
/// 异步加载场景。
///
/// 要加载场景资源的名称。
/// 加载场景资源的优先级。
/// 加载场景回调函数集。
/// 用户自定义数据。
public void LoadScene(string sceneAssetName, int priority, LoadSceneCallbacks loadSceneCallbacks, object userData)
{
if (string.IsNullOrEmpty(sceneAssetName))
{
throw new GameFrameworkException("Scene asset name is invalid.");
}
if (loadSceneCallbacks == null)
{
throw new GameFrameworkException("Load scene callbacks is invalid.");
}
m_ResourceLoader.LoadScene(sceneAssetName, priority, loadSceneCallbacks, userData);
}
///
/// 异步卸载场景。
///
/// 要卸载场景资源的名称。
/// 卸载场景回调函数集。
public void UnloadScene(string sceneAssetName, UnloadSceneCallbacks unloadSceneCallbacks)
{
if (string.IsNullOrEmpty(sceneAssetName))
{
throw new GameFrameworkException("Scene asset name is invalid.");
}
if (unloadSceneCallbacks == null)
{
throw new GameFrameworkException("Unload scene callbacks is invalid.");
}
m_ResourceLoader.UnloadScene(sceneAssetName, unloadSceneCallbacks, null);
}
///
/// 异步卸载场景。
///
/// 要卸载场景资源的名称。
/// 卸载场景回调函数集。
/// 用户自定义数据。
public void UnloadScene(string sceneAssetName, UnloadSceneCallbacks unloadSceneCallbacks, object userData)
{
if (string.IsNullOrEmpty(sceneAssetName))
{
throw new GameFrameworkException("Scene asset name is invalid.");
}
if (unloadSceneCallbacks == null)
{
throw new GameFrameworkException("Unload scene callbacks is invalid.");
}
m_ResourceLoader.UnloadScene(sceneAssetName, unloadSceneCallbacks, userData);
}
///
/// 获取二进制资源的实际路径。
///
/// 要获取实际路径的二进制资源的名称。
/// 二进制资源的实际路径。
/// 此方法仅适用于二进制资源存储在磁盘(而非文件系统)中的情况。若二进制资源存储在文件系统中时,返回值将始终为空。
public string GetBinaryPath(string binaryAssetName)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
return m_ResourceLoader.GetBinaryPath(binaryAssetName);
}
///
/// 获取二进制资源的实际路径。
///
/// 要获取实际路径的二进制资源的名称。
/// 二进制资源是否存储在只读区中。
/// 二进制资源是否存储在文件系统中。
/// 二进制资源或存储二进制资源的文件系统,相对于只读区或者读写区的相对路径。
/// 若二进制资源存储在文件系统中,则指示二进制资源在文件系统中的名称,否则此参数返回空。
/// 是否获取二进制资源的实际路径成功。
public bool GetBinaryPath(string binaryAssetName, out bool storageInReadOnly, out bool storageInFileSystem, out string relativePath, out string fileName)
{
return m_ResourceLoader.GetBinaryPath(binaryAssetName, out storageInReadOnly, out storageInFileSystem, out relativePath, out fileName);
}
///
/// 获取二进制资源的长度。
///
/// 要获取长度的二进制资源的名称。
/// 二进制资源的长度。
public int GetBinaryLength(string binaryAssetName)
{
return m_ResourceLoader.GetBinaryLength(binaryAssetName);
}
///
/// 异步加载二进制资源。
///
/// 要加载二进制资源的名称。
/// 加载二进制资源回调函数集。
public void LoadBinary(string binaryAssetName, LoadBinaryCallbacks loadBinaryCallbacks)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
if (loadBinaryCallbacks == null)
{
throw new GameFrameworkException("Load binary callbacks is invalid.");
}
m_ResourceLoader.LoadBinary(binaryAssetName, loadBinaryCallbacks, null);
}
///
/// 异步加载二进制资源。
///
/// 要加载二进制资源的名称。
/// 加载二进制资源回调函数集。
/// 用户自定义数据。
public void LoadBinary(string binaryAssetName, LoadBinaryCallbacks loadBinaryCallbacks, object userData)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
if (loadBinaryCallbacks == null)
{
throw new GameFrameworkException("Load binary callbacks is invalid.");
}
m_ResourceLoader.LoadBinary(binaryAssetName, loadBinaryCallbacks, userData);
}
///
/// 从文件系统中加载二进制资源。
///
/// 要加载二进制资源的名称。
/// 存储加载二进制资源的二进制流。
public byte[] LoadBinaryFromFileSystem(string binaryAssetName)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
return m_ResourceLoader.LoadBinaryFromFileSystem(binaryAssetName);
}
///
/// 从文件系统中加载二进制资源。
///
/// 要加载二进制资源的名称。
/// 存储加载二进制资源的二进制流。
/// 实际加载了多少字节。
public int LoadBinaryFromFileSystem(string binaryAssetName, byte[] buffer)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
return m_ResourceLoader.LoadBinaryFromFileSystem(binaryAssetName, buffer, 0, buffer.Length);
}
///
/// 从文件系统中加载二进制资源。
///
/// 要加载二进制资源的名称。
/// 存储加载二进制资源的二进制流。
/// 存储加载二进制资源的二进制流的起始位置。
/// 实际加载了多少字节。
public int LoadBinaryFromFileSystem(string binaryAssetName, byte[] buffer, int startIndex)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
return m_ResourceLoader.LoadBinaryFromFileSystem(binaryAssetName, buffer, startIndex, buffer.Length - startIndex);
}
///
/// 从文件系统中加载二进制资源。
///
/// 要加载二进制资源的名称。
/// 存储加载二进制资源的二进制流。
/// 存储加载二进制资源的二进制流的起始位置。
/// 存储加载二进制资源的二进制流的长度。
/// 实际加载了多少字节。
public int LoadBinaryFromFileSystem(string binaryAssetName, byte[] buffer, int startIndex, int length)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
return m_ResourceLoader.LoadBinaryFromFileSystem(binaryAssetName, buffer, startIndex, length);
}
///
/// 从文件系统中加载二进制资源的片段。
///
/// 要加载片段的二进制资源的名称。
/// 要加载片段的长度。
/// 存储加载二进制资源片段内容的二进制流。
public byte[] LoadBinarySegmentFromFileSystem(string binaryAssetName, int length)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
return m_ResourceLoader.LoadBinarySegmentFromFileSystem(binaryAssetName, 0, length);
}
///
/// 从文件系统中加载二进制资源的片段。
///
/// 要加载片段的二进制资源的名称。
/// 要加载片段的偏移。
/// 要加载片段的长度。
/// 存储加载二进制资源片段内容的二进制流。
public byte[] LoadBinarySegmentFromFileSystem(string binaryAssetName, int offset, int length)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
return m_ResourceLoader.LoadBinarySegmentFromFileSystem(binaryAssetName, offset, length);
}
///
/// 从文件系统中加载二进制资源的片段。
///
/// 要加载片段的二进制资源的名称。
/// 存储加载二进制资源片段内容的二进制流。
/// 实际加载了多少字节。
public int LoadBinarySegmentFromFileSystem(string binaryAssetName, byte[] buffer)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
return m_ResourceLoader.LoadBinarySegmentFromFileSystem(binaryAssetName, 0, buffer, 0, buffer.Length);
}
///
/// 从文件系统中加载二进制资源的片段。
///
/// 要加载片段的二进制资源的名称。
/// 存储加载二进制资源片段内容的二进制流。
/// 要加载片段的长度。
/// 实际加载了多少字节。
public int LoadBinarySegmentFromFileSystem(string binaryAssetName, byte[] buffer, int length)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
return m_ResourceLoader.LoadBinarySegmentFromFileSystem(binaryAssetName, 0, buffer, 0, length);
}
///
/// 从文件系统中加载二进制资源的片段。
///
/// 要加载片段的二进制资源的名称。
/// 存储加载二进制资源片段内容的二进制流。
/// 存储加载二进制资源片段内容的二进制流的起始位置。
/// 要加载片段的长度。
/// 实际加载了多少字节。
public int LoadBinarySegmentFromFileSystem(string binaryAssetName, byte[] buffer, int startIndex, int length)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
return m_ResourceLoader.LoadBinarySegmentFromFileSystem(binaryAssetName, 0, buffer, startIndex, length);
}
///
/// 从文件系统中加载二进制资源的片段。
///
/// 要加载片段的二进制资源的名称。
/// 要加载片段的偏移。
/// 存储加载二进制资源片段内容的二进制流。
/// 实际加载了多少字节。
public int LoadBinarySegmentFromFileSystem(string binaryAssetName, int offset, byte[] buffer)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
return m_ResourceLoader.LoadBinarySegmentFromFileSystem(binaryAssetName, offset, buffer, 0, buffer.Length);
}
///
/// 从文件系统中加载二进制资源的片段。
///
/// 要加载片段的二进制资源的名称。
/// 要加载片段的偏移。
/// 存储加载二进制资源片段内容的二进制流。
/// 要加载片段的长度。
/// 实际加载了多少字节。
public int LoadBinarySegmentFromFileSystem(string binaryAssetName, int offset, byte[] buffer, int length)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
return m_ResourceLoader.LoadBinarySegmentFromFileSystem(binaryAssetName, offset, buffer, 0, length);
}
///
/// 从文件系统中加载二进制资源的片段。
///
/// 要加载片段的二进制资源的名称。
/// 要加载片段的偏移。
/// 存储加载二进制资源片段内容的二进制流。
/// 存储加载二进制资源片段内容的二进制流的起始位置。
/// 要加载片段的长度。
/// 实际加载了多少字节。
public int LoadBinarySegmentFromFileSystem(string binaryAssetName, int offset, byte[] buffer, int startIndex, int length)
{
if (string.IsNullOrEmpty(binaryAssetName))
{
throw new GameFrameworkException("Binary asset name is invalid.");
}
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
return m_ResourceLoader.LoadBinarySegmentFromFileSystem(binaryAssetName, offset, buffer, startIndex, length);
}
///
/// 检查资源组是否存在。
///
/// 要检查资源组的名称。
/// 资源组是否存在。
public bool HasResourceGroup(string resourceGroupName)
{
return m_ResourceGroups.ContainsKey(resourceGroupName ?? string.Empty);
}
///
/// 获取默认资源组。
///
/// 默认资源组。
public IResourceGroup GetResourceGroup()
{
return GetResourceGroup(string.Empty);
}
///
/// 获取资源组。
///
/// 要获取的资源组名称。
/// 要获取的资源组。
public IResourceGroup GetResourceGroup(string resourceGroupName)
{
ResourceGroup resourceGroup = null;
if (m_ResourceGroups.TryGetValue(resourceGroupName ?? string.Empty, out resourceGroup))
{
return resourceGroup;
}
return null;
}
///
/// 获取所有资源组。
///
/// 所有资源组。
public IResourceGroup[] GetAllResourceGroups()
{
int index = 0;
IResourceGroup[] results = new IResourceGroup[m_ResourceGroups.Count];
foreach (KeyValuePair resourceGroup in m_ResourceGroups)
{
results[index++] = resourceGroup.Value;
}
return results;
}
///
/// 获取所有资源组。
///
/// 所有资源组。
public void GetAllResourceGroups(List results)
{
if (results == null)
{
throw new GameFrameworkException("Results is invalid.");
}
results.Clear();
foreach (KeyValuePair resourceGroup in m_ResourceGroups)
{
results.Add(resourceGroup.Value);
}
}
///
/// 获取资源组集合。
///
/// 要获取的资源组名称的集合。
/// 要获取的资源组集合。
public IResourceGroupCollection GetResourceGroupCollection(params string[] resourceGroupNames)
{
if (resourceGroupNames == null || resourceGroupNames.Length < 1)
{
throw new GameFrameworkException("Resource group names is invalid.");
}
ResourceGroup[] resourceGroups = new ResourceGroup[resourceGroupNames.Length];
for (int i = 0; i < resourceGroupNames.Length; i++)
{
if (string.IsNullOrEmpty(resourceGroupNames[i]))
{
throw new GameFrameworkException("Resource group name is invalid.");
}
resourceGroups[i] = (ResourceGroup)GetResourceGroup(resourceGroupNames[i]);
if (resourceGroups[i] == null)
{
throw new GameFrameworkException(Utility.Text.Format("Resource group '{0}' is not exist.", resourceGroupNames[i]));
}
}
return new ResourceGroupCollection(resourceGroups, m_ResourceInfos);
}
///
/// 获取资源组集合。
///
/// 要获取的资源组名称的集合。
/// 要获取的资源组集合。
public IResourceGroupCollection GetResourceGroupCollection(List resourceGroupNames)
{
if (resourceGroupNames == null || resourceGroupNames.Count < 1)
{
throw new GameFrameworkException("Resource group names is invalid.");
}
ResourceGroup[] resourceGroups = new ResourceGroup[resourceGroupNames.Count];
for (int i = 0; i < resourceGroupNames.Count; i++)
{
if (string.IsNullOrEmpty(resourceGroupNames[i]))
{
throw new GameFrameworkException("Resource group name is invalid.");
}
resourceGroups[i] = (ResourceGroup)GetResourceGroup(resourceGroupNames[i]);
if (resourceGroups[i] == null)
{
throw new GameFrameworkException(Utility.Text.Format("Resource group '{0}' is not exist.", resourceGroupNames[i]));
}
}
return new ResourceGroupCollection(resourceGroups, m_ResourceInfos);
}
private void UpdateResource(ResourceName resourceName)
{
m_ResourceUpdater.UpdateResource(resourceName);
}
private ResourceGroup GetOrAddResourceGroup(string resourceGroupName)
{
if (resourceGroupName == null)
{
resourceGroupName = string.Empty;
}
ResourceGroup resourceGroup = null;
if (!m_ResourceGroups.TryGetValue(resourceGroupName, out resourceGroup))
{
resourceGroup = new ResourceGroup(resourceGroupName, m_ResourceInfos);
m_ResourceGroups.Add(resourceGroupName, resourceGroup);
}
return resourceGroup;
}
private AssetInfo GetAssetInfo(string assetName)
{
if (string.IsNullOrEmpty(assetName))
{
throw new GameFrameworkException("Asset name is invalid.");
}
if (m_AssetInfos == null)
{
return null;
}
AssetInfo assetInfo = null;
if (m_AssetInfos.TryGetValue(assetName, out assetInfo))
{
return assetInfo;
}
return null;
}
private ResourceInfo GetResourceInfo(ResourceName resourceName)
{
if (m_ResourceInfos == null)
{
return null;
}
ResourceInfo resourceInfo = null;
if (m_ResourceInfos.TryGetValue(resourceName, out resourceInfo))
{
return resourceInfo;
}
return null;
}
private IFileSystem GetFileSystem(string fileSystemName, bool storageInReadOnly)
{
if (string.IsNullOrEmpty(fileSystemName))
{
throw new GameFrameworkException("File system name is invalid.");
}
IFileSystem fileSystem = null;
if (storageInReadOnly)
{
if (!m_ReadOnlyFileSystems.TryGetValue(fileSystemName, out fileSystem))
{
string fullPath = Utility.Path.GetRegularPath(Path.Combine(m_ReadOnlyPath, Utility.Text.Format("{0}.{1}", fileSystemName, DefaultExtension)));
fileSystem = m_FileSystemManager.GetFileSystem(fullPath);
if (fileSystem == null)
{
fileSystem = m_FileSystemManager.LoadFileSystem(fullPath, FileSystemAccess.Read);
m_ReadOnlyFileSystems.Add(fileSystemName, fileSystem);
}
}
}
else
{
if (!m_ReadWriteFileSystems.TryGetValue(fileSystemName, out fileSystem))
{
string fullPath = Utility.Path.GetRegularPath(Path.Combine(m_ReadWritePath, Utility.Text.Format("{0}.{1}", fileSystemName, DefaultExtension)));
fileSystem = m_FileSystemManager.GetFileSystem(fullPath);
if (fileSystem == null)
{
if (File.Exists(fullPath))
{
fileSystem = m_FileSystemManager.LoadFileSystem(fullPath, FileSystemAccess.ReadWrite);
}
else
{
string directory = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
fileSystem = m_FileSystemManager.CreateFileSystem(fullPath, FileSystemAccess.ReadWrite, FileSystemMaxFileCount, FileSystemMaxBlockCount);
}
m_ReadWriteFileSystems.Add(fileSystemName, fileSystem);
}
}
}
return fileSystem;
}
private void OnIniterResourceInitComplete()
{
m_ResourceIniter.ResourceInitComplete -= OnIniterResourceInitComplete;
m_ResourceIniter.Shutdown();
m_ResourceIniter = null;
m_InitResourcesCompleteCallback();
m_InitResourcesCompleteCallback = null;
}
private void OnVersionListProcessorUpdateSuccess(string downloadPath, string downloadUri)
{
m_UpdateVersionListCallbacks.UpdateVersionListSuccessCallback(downloadPath, downloadUri);
}
private void OnVersionListProcessorUpdateFailure(string downloadUri, string errorMessage)
{
if (m_UpdateVersionListCallbacks.UpdateVersionListFailureCallback != null)
{
m_UpdateVersionListCallbacks.UpdateVersionListFailureCallback(downloadUri, errorMessage);
}
}
private void OnCheckerResourceNeedUpdate(ResourceName resourceName, string fileSystemName, LoadType loadType, int length, int hashCode, int compressedLength, int compressedHashCode)
{
m_ResourceUpdater.AddResourceUpdate(resourceName, fileSystemName, loadType, length, hashCode, compressedLength, compressedHashCode, Utility.Path.GetRegularPath(Path.Combine(m_ReadWritePath, resourceName.FullName)));
}
private void OnCheckerResourceCheckComplete(int movedCount, int removedCount, int updateCount, long updateTotalLength, long updateTotalCompressedLength)
{
m_VersionListProcessor.VersionListUpdateSuccess -= OnVersionListProcessorUpdateSuccess;
m_VersionListProcessor.VersionListUpdateFailure -= OnVersionListProcessorUpdateFailure;
m_VersionListProcessor.Shutdown();
m_VersionListProcessor = null;
m_UpdateVersionListCallbacks = null;
m_ResourceChecker.ResourceNeedUpdate -= OnCheckerResourceNeedUpdate;
m_ResourceChecker.ResourceCheckComplete -= OnCheckerResourceCheckComplete;
m_ResourceChecker.Shutdown();
m_ResourceChecker = null;
m_ResourceUpdater.CheckResourceComplete(movedCount > 0 || removedCount > 0);
if (updateCount <= 0)
{
m_ResourceUpdater.ResourceApplySuccess -= OnUpdaterResourceApplySuccess;
m_ResourceUpdater.ResourceApplyFailure -= OnUpdaterResourceApplyFailure;
m_ResourceUpdater.ResourceApplyComplete -= OnUpdaterResourceApplyComplete;
m_ResourceUpdater.ResourceUpdateStart -= OnUpdaterResourceUpdateStart;
m_ResourceUpdater.ResourceUpdateChanged -= OnUpdaterResourceUpdateChanged;
m_ResourceUpdater.ResourceUpdateSuccess -= OnUpdaterResourceUpdateSuccess;
m_ResourceUpdater.ResourceUpdateFailure -= OnUpdaterResourceUpdateFailure;
m_ResourceUpdater.ResourceUpdateComplete -= OnUpdaterResourceUpdateComplete;
m_ResourceUpdater.ResourceUpdateAllComplete -= OnUpdaterResourceUpdateAllComplete;
m_ResourceUpdater.Shutdown();
m_ResourceUpdater = null;
m_ReadWriteResourceInfos.Clear();
m_ReadWriteResourceInfos = null;
if (m_DecompressCachedStream != null)
{
m_DecompressCachedStream.Dispose();
m_DecompressCachedStream = null;
}
}
m_CheckResourcesCompleteCallback(movedCount, removedCount, updateCount, updateTotalLength, updateTotalCompressedLength);
m_CheckResourcesCompleteCallback = null;
}
private void OnUpdaterResourceApplySuccess(ResourceName resourceName, string applyPath, string resourcePackPath, int length, int compressedLength)
{
if (m_ResourceApplySuccessEventHandler != null)
{
ResourceApplySuccessEventArgs resourceApplySuccessEventArgs = ResourceApplySuccessEventArgs.Create(resourceName.FullName, applyPath, resourcePackPath, length, compressedLength);
m_ResourceApplySuccessEventHandler(this, resourceApplySuccessEventArgs);
ReferencePool.Release(resourceApplySuccessEventArgs);
}
}
private void OnUpdaterResourceApplyFailure(ResourceName resourceName, string resourcePackPath, string errorMessage)
{
if (m_ResourceApplyFailureEventHandler != null)
{
ResourceApplyFailureEventArgs resourceApplyFailureEventArgs = ResourceApplyFailureEventArgs.Create(resourceName.FullName, resourcePackPath, errorMessage);
m_ResourceApplyFailureEventHandler(this, resourceApplyFailureEventArgs);
ReferencePool.Release(resourceApplyFailureEventArgs);
}
}
private void OnUpdaterResourceApplyComplete(string resourcePackPath, bool result)
{
ApplyResourcesCompleteCallback applyResourcesCompleteCallback = m_ApplyResourcesCompleteCallback;
m_ApplyResourcesCompleteCallback = null;
applyResourcesCompleteCallback(resourcePackPath, result);
}
private void OnUpdaterResourceUpdateStart(ResourceName resourceName, string downloadPath, string downloadUri, int currentLength, int compressedLength, int retryCount)
{
if (m_ResourceUpdateStartEventHandler != null)
{
ResourceUpdateStartEventArgs resourceUpdateStartEventArgs = ResourceUpdateStartEventArgs.Create(resourceName.FullName, downloadPath, downloadUri, currentLength, compressedLength, retryCount);
m_ResourceUpdateStartEventHandler(this, resourceUpdateStartEventArgs);
ReferencePool.Release(resourceUpdateStartEventArgs);
}
}
private void OnUpdaterResourceUpdateChanged(ResourceName resourceName, string downloadPath, string downloadUri, int currentLength, int compressedLength)
{
if (m_ResourceUpdateChangedEventHandler != null)
{
ResourceUpdateChangedEventArgs resourceUpdateChangedEventArgs = ResourceUpdateChangedEventArgs.Create(resourceName.FullName, downloadPath, downloadUri, currentLength, compressedLength);
m_ResourceUpdateChangedEventHandler(this, resourceUpdateChangedEventArgs);
ReferencePool.Release(resourceUpdateChangedEventArgs);
}
}
private void OnUpdaterResourceUpdateSuccess(ResourceName resourceName, string downloadPath, string downloadUri, int length, int compressedLength)
{
if (m_ResourceUpdateSuccessEventHandler != null)
{
ResourceUpdateSuccessEventArgs resourceUpdateSuccessEventArgs = ResourceUpdateSuccessEventArgs.Create(resourceName.FullName, downloadPath, downloadUri, length, compressedLength);
m_ResourceUpdateSuccessEventHandler(this, resourceUpdateSuccessEventArgs);
ReferencePool.Release(resourceUpdateSuccessEventArgs);
}
}
private void OnUpdaterResourceUpdateFailure(ResourceName resourceName, string downloadUri, int retryCount, int totalRetryCount, string errorMessage)
{
if (m_ResourceUpdateFailureEventHandler != null)
{
ResourceUpdateFailureEventArgs resourceUpdateFailureEventArgs = ResourceUpdateFailureEventArgs.Create(resourceName.FullName, downloadUri, retryCount, totalRetryCount, errorMessage);
m_ResourceUpdateFailureEventHandler(this, resourceUpdateFailureEventArgs);
ReferencePool.Release(resourceUpdateFailureEventArgs);
}
}
private void OnUpdaterResourceUpdateComplete(ResourceGroup resourceGroup, bool result)
{
Utility.Path.RemoveEmptyDirectory(m_ReadWritePath);
UpdateResourcesCompleteCallback updateResourcesCompleteCallback = m_UpdateResourcesCompleteCallback;
m_UpdateResourcesCompleteCallback = null;
updateResourcesCompleteCallback(resourceGroup, result);
}
private void OnUpdaterResourceUpdateAllComplete()
{
m_ResourceUpdater.ResourceApplySuccess -= OnUpdaterResourceApplySuccess;
m_ResourceUpdater.ResourceApplyFailure -= OnUpdaterResourceApplyFailure;
m_ResourceUpdater.ResourceApplyComplete -= OnUpdaterResourceApplyComplete;
m_ResourceUpdater.ResourceUpdateStart -= OnUpdaterResourceUpdateStart;
m_ResourceUpdater.ResourceUpdateChanged -= OnUpdaterResourceUpdateChanged;
m_ResourceUpdater.ResourceUpdateSuccess -= OnUpdaterResourceUpdateSuccess;
m_ResourceUpdater.ResourceUpdateFailure -= OnUpdaterResourceUpdateFailure;
m_ResourceUpdater.ResourceUpdateComplete -= OnUpdaterResourceUpdateComplete;
m_ResourceUpdater.ResourceUpdateAllComplete -= OnUpdaterResourceUpdateAllComplete;
m_ResourceUpdater.Shutdown();
m_ResourceUpdater = null;
m_ReadWriteResourceInfos.Clear();
m_ReadWriteResourceInfos = null;
if (m_DecompressCachedStream != null)
{
m_DecompressCachedStream.Dispose();
m_DecompressCachedStream = null;
}
Utility.Path.RemoveEmptyDirectory(m_ReadWritePath);
if (m_ResourceUpdateAllCompleteEventHandler != null)
{
ResourceUpdateAllCompleteEventArgs resourceUpdateAllCompleteEventArgs = ResourceUpdateAllCompleteEventArgs.Create();
m_ResourceUpdateAllCompleteEventHandler(this, resourceUpdateAllCompleteEventArgs);
ReferencePool.Release(resourceUpdateAllCompleteEventArgs);
}
}
}
}