/// <summary> /// 只读目录读文件 /// </summary> /// <param name="assetName">资源名称</param> private void ReadOnlyByte(string assetName) { m_FilePath = PathUtil.GetRemotePath(GameEntry.Resource.ReadOnlyPath, PathUtil.GetResourceNameWithSuffix(assetName)); #if UNITY_5_4_OR_NEWER m_UnityWebRequest = UnityWebRequest.Get(m_FilePath); #if UNITY_2017_2_OR_NEWER m_UnityWebRequest.SendWebRequest(); #else m_UnityWebRequest.Send(); #endif #else m_WWW = new WWW(m_FilePath); #endif }
/// <summary> /// 更新版本资源列表 /// </summary> /// <param name="versionListLength">版本资源列表大小</param> /// <param name="versionListHashCode">版本资源列表哈希值</param> /// <param name="versionListZipLength">版本资源列表压缩后大小</param> /// <param name="versionListZipHashCode">版本资源列表压缩后哈希值</param> public void UpdateVersionList(int versionListLength, int versionListHashCode, int versionListZipLength, int versionListZipHashCode) { if (GameEntry.Download == null) { throw new Exception("You must set download manager first."); } m_VersionListLength = versionListLength; m_VersionListHashCode = versionListHashCode; m_VersionListZipLength = versionListZipLength; m_VersionListZipHashCode = versionListZipHashCode; string versionListFileName = PathUtil.GetResourceNameWithSuffix(VersionListFileName); string latestVersionListFileName = PathUtil.GetResourceNameWithCrc32AndSuffix(VersionListFileName, m_VersionListHashCode); string localVersionListFilePath = PathUtil.GetCombinePath(m_ResourceManager.ReadWritePath, versionListFileName); string latestVersionListFileUri = PathUtil.GetRemotePath(m_ResourceManager.UpdatePrefixUri, latestVersionListFileName); GameEntry.Download.AddDownload(localVersionListFilePath, latestVersionListFileUri, this); }
public void CheckResources() { TryRecoverReadWriteList(); if (GameEntry.Resource.ResourceHelper == null) { throw new Exception("Resource helper is invalid."); } if (string.IsNullOrEmpty(m_ResourceManager.ReadOnlyPath)) { throw new Exception("Readonly path is invalid."); } if (string.IsNullOrEmpty(m_ResourceManager.ReadWritePath)) { throw new Exception("Read-write path is invalid."); } GameEntry.Resource.ResourceHelper.LoadBytes(PathUtil.GetRemotePath(GameEntry.Resource.ReadWritePath, PathUtil.GetResourceNameWithSuffix(VersionListFileName)), ParseVersionList); GameEntry.Resource.ResourceHelper.LoadBytes(PathUtil.GetRemotePath(GameEntry.Resource.ReadOnlyPath, PathUtil.GetResourceNameWithSuffix(ResourceListFileName)), ParseReadOnlyList); GameEntry.Resource.ResourceHelper.LoadBytes(PathUtil.GetRemotePath(GameEntry.Resource.ReadWritePath, PathUtil.GetResourceNameWithSuffix(ResourceListFileName)), ParseReadWriteList); }
/// <summary> /// 尝试恢复读写区资源列表 /// </summary> /// <returns>是否恢复成功</returns> private bool TryRecoverReadWriteList() { string file = PathUtil.GetCombinePath(m_ResourceManager.ReadWritePath, PathUtil.GetResourceNameWithSuffix(ResourceListFileName)); string backupFile = file + BackupFileSuffixName; try { if (!File.Exists(backupFile)) { return(false); } if (File.Exists(file)) { File.Delete(file); } File.Move(backupFile, file); } catch { return(false); } return(true); }
private void RefreshCheckInfoStatus() { if (!m_VersionListReady || !m_ReadOnlyListReady || !m_ReadWriteListReady) { return; } int removedCount = 0; int updateCount = 0; long updateTotalLength = 0L; long updateTotalZipLength = 0L; foreach (KeyValuePair <string, CheckInfo> checkInfo in m_CheckInfos) { CheckInfo ci = checkInfo.Value; ci.RefreshStatus(); if (ci.Status == CheckInfo.CheckStatus.StorageInReadOnly) { ProcessResourceInfo(ci.ResourceName, ci.Length, ci.HashCode, true); } else if (ci.Status == CheckInfo.CheckStatus.StorageInReadWrite) { ProcessResourceInfo(ci.ResourceName, ci.Length, ci.HashCode, false); } else if (ci.Status == CheckInfo.CheckStatus.NeedUpdate) { updateCount++; updateTotalLength += ci.Length; updateTotalZipLength += ci.ZipLength; ResourceNeedUpdate(ci.ResourceName, ci.Length, ci.HashCode, ci.ZipLength, ci.ZipHashCode); } else if (ci.Status == CheckInfo.CheckStatus.Disuse) { // Do nothing. } else { throw new Exception(TextUtil.Format("Check resources '{0}' error with unknown status.", ci.ResourceName)); } if (ci.NeedRemove) { removedCount++; string path = PathUtil.GetCombinePath(m_ResourceManager.ReadWritePath, PathUtil.GetResourceNameWithSuffix(ci.ResourceName)); File.Delete(path); if (!m_ResourceManager.ReadWriteResourceInfos.ContainsKey(ci.ResourceName)) { throw new Exception(TextUtil.Format("Resource '{0}' is not exist in read-write list.", ci.ResourceName)); } m_ResourceManager.ReadWriteResourceInfos.Remove(ci.ResourceName); } } ResourceCheckComplete(removedCount, updateCount, updateTotalLength, updateTotalZipLength); }
/// <summary> /// 初始化资源 /// </summary> public void InitResources() { if (GameEntry.Resource.ResourceHelper == null) { throw new Exception("Resource helper is invalid."); } GameEntry.Resource.ResourceHelper.LoadBytes(PathUtil.GetRemotePath(GameEntry.Resource.ReadOnlyPath, PathUtil.GetResourceNameWithSuffix(VersionListFileName)), ParsePackageList); }
/// <summary> /// 预加载Shader /// </summary> /// <param name="preLoadShaderCallBack">预加载shader回调函数集</param> public void PreLoadShader(PreLoadShaderCallBack preLoadShaderCallBack) { if (m_ResourceHelper == null) { throw new Exception("Please set resourceHelper first!"); } m_PreLoadShaderCallBack = preLoadShaderCallBack; ResourceInfo resourceInfo = GameEntry.Resource.GetResourceInfo("shader"); m_ResourceHelper.LoadBytes(PathUtil.GetRemotePath(resourceInfo.StorageInReadOnly ? GameEntry.Resource.ReadOnlyPath : GameEntry.Resource.ReadWritePath, PathUtil.GetResourceNameWithSuffix(resourceInfo.ResourceName)), OnLoadShdaerBytesComplete); }
/// <summary> /// 异步加载主依赖项 /// </summary> internal void LoadManifestAsync(LoadManifestCompleteCallBack loadManifestCompleteCallBack) { LoadManifestCompleteCallBack = loadManifestCompleteCallBack; string assetName = string.Empty; #if UNITY_STANDALONE_WIN assetName = "windows"; #elif UNITY_ANDROID assetName = "android"; #elif UNITY_IPHONE assetName = "ios"; #endif ResourceInfo resourceInfo = GetResourceInfo(assetName); m_ResourceHelper.LoadBytes(PathUtil.GetRemotePath(resourceInfo.StorageInReadOnly ? GameEntry.Resource.ReadOnlyPath : GameEntry.Resource.ReadWritePath, PathUtil.GetResourceNameWithSuffix(assetName)), OnLoadManifestCallBack); }
private void OnCheckerResourceNeedUpdate(string resourceName, int length, int hashCode, int zipLength, int zipHashCode) { m_ResourceUpdater.AddResourceUpdate(resourceName, length, hashCode, zipLength, zipHashCode, PathUtil.GetCombinePath(m_ReadWritePath, PathUtil.GetResourceNameWithSuffix(resourceName))); }
private void GenerateReadWriteList() { string file = PathUtil.GetCombinePath(m_ResourceManager.ReadWritePath, PathUtil.GetResourceNameWithSuffix(ResourceListFileName)); string backupFile = null; if (File.Exists(file)) { backupFile = file + BackupFileSuffixName; if (File.Exists(backupFile)) { File.Delete(backupFile); } File.Move(file, backupFile); } try { using (FileStream fileStream = new FileStream(file, FileMode.CreateNew, FileAccess.Write)) { byte[] buffer = null; using (MMO_MemoryStream ms = new MMO_MemoryStream()) { ms.WriteInt(m_ResourceManager.ReadWriteResourceInfos.Count); foreach (var readWriteResourceInfo in m_ResourceManager.ReadWriteResourceInfos) { int length = readWriteResourceInfo.Value.Length; int hashcode = readWriteResourceInfo.Value.HashCode; ms.WriteUTF8String(readWriteResourceInfo.Key); ms.WriteInt(length); ms.WriteInt(hashcode); } buffer = ms.ToArray(); } fileStream.Write(buffer, 0, buffer.Length); } if (!string.IsNullOrEmpty(backupFile)) { File.Delete(backupFile); } } catch (Exception exception) { if (File.Exists(file)) { File.Delete(file); } if (!string.IsNullOrEmpty(backupFile)) { File.Move(backupFile, file); } throw new Exception(TextUtil.Format("Pack save exception '{0}'.", exception.Message), exception); } }
/// <summary> /// 读写目录读文件 /// </summary> /// <param name="assetName">资源名称</param> private void ReadWriteByte(string assetName) { m_FilePath = TextUtil.Format("{0}/{1}", GameEntry.Resource.ReadWritePath, PathUtil.GetResourceNameWithSuffix(assetName)); System.Threading.Tasks.Task.Factory.StartNew(ReadWriteByteAsync); }
/// <summary> /// 开始处理加载资源任务 /// </summary> /// <param name="task">要处理的加载资源任务</param> /// <returns>开始处理任务的状态</returns> public StartTaskStatus StartTask(ResourceLoaderTask task) { if (task == null) { throw new Exception("Task is invalid."); } m_Task = task; m_Task.StartTime = DateTime.Now; if (string.IsNullOrEmpty(m_Task.AssetName)) { if (IsResourceLoading(m_Task.AssetBundleName)) { m_Task.StartTime = default(DateTime); return(StartTaskStatus.HasToWait); } AssetBundleInfo bundleInfo = m_AssetBundlePool.SpawnAsset(m_Task.AssetBundleName); if (bundleInfo != null) { OnResourceObjectReady((AssetBundle)bundleInfo.Asset); return(StartTaskStatus.CanResume); } s_LoadingResourceNames.Add(m_Task.AssetBundleName); ResourceInfo resourcinfo = GameEntry.Resource.GetResourceInfo(m_Task.AssetBundleName); string fullpath = PathUtil.GetCombinePath(resourcinfo.StorageInReadOnly ? GameEntry.Resource.ReadOnlyPath : GameEntry.Resource.ReadWritePath, PathUtil.GetResourceNameWithSuffix(resourcinfo.ResourceName)); ReadBytes(fullpath); //if (resourceInfo.StorageInReadOnly) //{ // ReadOnlyByte(resourcinfo.ResourceName); //} //else //{ // ReadWriteByte(resourcinfo.ResourceName); //} return(StartTaskStatus.CanResume); } if (IsAssetLoading(m_Task.AssetName)) { m_Task.StartTime = default(DateTime); return(StartTaskStatus.HasToWait); } if (!m_Task.IsScene) { AssetInfo assetInfo = m_AssetPool.SpawnAsset(m_Task.AssetName); if (assetInfo != null) { OnAssetObjectReady((UnityEngine.Object)assetInfo.Asset); return(StartTaskStatus.Done); } } foreach (string dependencyAssetName in m_Task.GetDependencyAssetNames()) { if (!m_AssetBundlePool.CanSpawnAsset(dependencyAssetName)) { m_Task.StartTime = default(DateTime); return(StartTaskStatus.HasToWait); } } if (IsResourceLoading(m_Task.AssetBundleName)) { m_Task.StartTime = default(DateTime); return(StartTaskStatus.HasToWait); } s_LoadingAssetNames.Add(m_Task.AssetName); AssetBundleInfo assetBundleInfo = m_AssetBundlePool.SpawnAsset(m_Task.AssetBundleName); if (assetBundleInfo != null) { OnResourceObjectReady((AssetBundle)assetBundleInfo.Asset); return(StartTaskStatus.CanResume); } s_LoadingResourceNames.Add(m_Task.AssetBundleName); ResourceInfo resourceInfo = GameEntry.Resource.GetResourceInfo(m_Task.AssetBundleName); string fullPath = PathUtil.GetCombinePath(resourceInfo.StorageInReadOnly ? GameEntry.Resource.ReadOnlyPath : GameEntry.Resource.ReadWritePath, PathUtil.GetResourceNameWithSuffix(resourceInfo.ResourceName)); ReadBytes(fullPath); //if (resourceInfo.StorageInReadOnly) //{ // ReadOnlyByte(resourceInfo.ResourceName); //} //else //{ // ReadWriteByte(resourceInfo.ResourceName); //} return(StartTaskStatus.CanResume); }