DefaultWebServerFileSystem.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace YooAsset
  6. {
  7. /// <summary>
  8. /// Web文件系统
  9. /// </summary>
  10. internal class DefaultWebServerFileSystem : IFileSystem
  11. {
  12. public class FileWrapper
  13. {
  14. public string FileName { private set; get; }
  15. public FileWrapper(string fileName)
  16. {
  17. FileName = fileName;
  18. }
  19. }
  20. protected readonly Dictionary<string, FileWrapper> _wrappers = new Dictionary<string, FileWrapper>(10000);
  21. protected readonly Dictionary<string, string> _webFilePathMapping = new Dictionary<string, string>(10000);
  22. protected string _webPackageRoot = string.Empty;
  23. /// <summary>
  24. /// 包裹名称
  25. /// </summary>
  26. public string PackageName { private set; get; }
  27. /// <summary>
  28. /// 文件根目录
  29. /// </summary>
  30. public string FileRoot
  31. {
  32. get
  33. {
  34. return _webPackageRoot;
  35. }
  36. }
  37. /// <summary>
  38. /// 文件数量
  39. /// </summary>
  40. public int FileCount
  41. {
  42. get
  43. {
  44. return 0;
  45. }
  46. }
  47. #region 自定义参数
  48. /// <summary>
  49. /// 禁用Unity的网络缓存
  50. /// </summary>
  51. public bool DisableUnityWebCache { private set; get; } = false;
  52. /// <summary>
  53. /// 自定义参数:解密方法类
  54. /// </summary>
  55. public IWebDecryptionServices DecryptionServices { private set; get; }
  56. #endregion
  57. public DefaultWebServerFileSystem()
  58. {
  59. }
  60. public virtual FSInitializeFileSystemOperation InitializeFileSystemAsync()
  61. {
  62. var operation = new DWSFSInitializeOperation(this);
  63. return operation;
  64. }
  65. public virtual FSLoadPackageManifestOperation LoadPackageManifestAsync(string packageVersion, int timeout)
  66. {
  67. var operation = new DWSFSLoadPackageManifestOperation(this, packageVersion, timeout);
  68. return operation;
  69. }
  70. public virtual FSRequestPackageVersionOperation RequestPackageVersionAsync(bool appendTimeTicks, int timeout)
  71. {
  72. var operation = new DWSFSRequestPackageVersionOperation(this, timeout);
  73. return operation;
  74. }
  75. public virtual FSClearCacheFilesOperation ClearCacheFilesAsync(PackageManifest manifest, string clearMode, object clearParam)
  76. {
  77. var operation = new FSClearCacheFilesCompleteOperation();
  78. return operation;
  79. }
  80. public virtual FSDownloadFileOperation DownloadFileAsync(PackageBundle bundle, DownloadParam param)
  81. {
  82. throw new System.NotImplementedException();
  83. }
  84. public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
  85. {
  86. if (bundle.BundleType == (int)EBuildBundleType.AssetBundle)
  87. {
  88. var operation = new DWSFSLoadAssetBundleOperation(this, bundle);
  89. return operation;
  90. }
  91. else
  92. {
  93. string error = $"{nameof(DefaultWebServerFileSystem)} not support load bundle type : {bundle.BundleType}";
  94. var operation = new FSLoadBundleCompleteOperation(error);
  95. return operation;
  96. }
  97. }
  98. public virtual void SetParameter(string name, object value)
  99. {
  100. if (name == FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE)
  101. {
  102. DisableUnityWebCache = Convert.ToBoolean(value);
  103. }
  104. else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
  105. {
  106. DecryptionServices = (IWebDecryptionServices)value;
  107. }
  108. else
  109. {
  110. YooLogger.Warning($"Invalid parameter : {name}");
  111. }
  112. }
  113. public virtual void OnCreate(string packageName, string packageRoot)
  114. {
  115. PackageName = packageName;
  116. if (string.IsNullOrEmpty(packageRoot))
  117. _webPackageRoot = GetDefaultWebPackageRoot(packageName);
  118. else
  119. _webPackageRoot = packageRoot;
  120. }
  121. public virtual void OnDestroy()
  122. {
  123. }
  124. public virtual bool Belong(PackageBundle bundle)
  125. {
  126. return _wrappers.ContainsKey(bundle.BundleGUID);
  127. }
  128. public virtual bool Exists(PackageBundle bundle)
  129. {
  130. return _wrappers.ContainsKey(bundle.BundleGUID);
  131. }
  132. public virtual bool NeedDownload(PackageBundle bundle)
  133. {
  134. return false;
  135. }
  136. public virtual bool NeedUnpack(PackageBundle bundle)
  137. {
  138. return false;
  139. }
  140. public virtual bool NeedImport(PackageBundle bundle)
  141. {
  142. return false;
  143. }
  144. public virtual string GetBundleFilePath(PackageBundle bundle)
  145. {
  146. throw new System.NotImplementedException();
  147. }
  148. public virtual byte[] ReadBundleFileData(PackageBundle bundle)
  149. {
  150. throw new System.NotImplementedException();
  151. }
  152. public virtual string ReadBundleFileText(PackageBundle bundle)
  153. {
  154. throw new System.NotImplementedException();
  155. }
  156. #region 内部方法
  157. protected string GetDefaultWebPackageRoot(string packageName)
  158. {
  159. string rootDirectory = YooAssetSettingsData.GetYooDefaultBuildinRoot();
  160. return PathUtility.Combine(rootDirectory, packageName);
  161. }
  162. public string GetWebFileLoadPath(PackageBundle bundle)
  163. {
  164. if (_webFilePathMapping.TryGetValue(bundle.BundleGUID, out string filePath) == false)
  165. {
  166. filePath = PathUtility.Combine(_webPackageRoot, bundle.FileName);
  167. _webFilePathMapping.Add(bundle.BundleGUID, filePath);
  168. }
  169. return filePath;
  170. }
  171. public string GetWebPackageVersionFilePath()
  172. {
  173. string fileName = YooAssetSettingsData.GetPackageVersionFileName(PackageName);
  174. return PathUtility.Combine(FileRoot, fileName);
  175. }
  176. public string GetWebPackageHashFilePath(string packageVersion)
  177. {
  178. string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion);
  179. return PathUtility.Combine(FileRoot, fileName);
  180. }
  181. public string GetWebPackageManifestFilePath(string packageVersion)
  182. {
  183. string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion);
  184. return PathUtility.Combine(FileRoot, fileName);
  185. }
  186. public string GetCatalogBinaryFileLoadPath()
  187. {
  188. return PathUtility.Combine(_webPackageRoot, DefaultBuildinFileSystemDefine.BuildinCatalogBinaryFileName);
  189. }
  190. /// <summary>
  191. /// 记录内置文件信息
  192. /// </summary>
  193. public bool RecordCatalogFile(string bundleGUID, FileWrapper wrapper)
  194. {
  195. if (_wrappers.ContainsKey(bundleGUID))
  196. {
  197. YooLogger.Error($"{nameof(DefaultWebServerFileSystem)} has element : {bundleGUID}");
  198. return false;
  199. }
  200. _wrappers.Add(bundleGUID, wrapper);
  201. return true;
  202. }
  203. #endregion
  204. }
  205. }