ObservableWWW.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. #if !UniRxLibrary
  5. using ObservableUnity = UniRx.Observable;
  6. #endif
  7. #if UNITY_2018_3_OR_NEWER
  8. #pragma warning disable CS0618
  9. #endif
  10. namespace UniRx
  11. {
  12. using System.Threading;
  13. #if !(UNITY_METRO || UNITY_WP8) && (UNITY_4_4 || UNITY_4_3 || UNITY_4_2 || UNITY_4_1 || UNITY_4_0_1 || UNITY_4_0 || UNITY_3_5 || UNITY_3_4 || UNITY_3_3 || UNITY_3_2 || UNITY_3_1 || UNITY_3_0_0 || UNITY_3_0 || UNITY_2_6_1 || UNITY_2_6)
  14. // Fallback for Unity versions below 4.5
  15. using Hash = System.Collections.Hashtable;
  16. using HashEntry = System.Collections.DictionaryEntry;
  17. #else
  18. // Unity 4.5 release notes:
  19. // WWW: deprecated 'WWW(string url, byte[] postData, Hashtable headers)',
  20. // use 'public WWW(string url, byte[] postData, Dictionary<string, string> headers)' instead.
  21. using Hash = System.Collections.Generic.Dictionary<string, string>;
  22. using HashEntry = System.Collections.Generic.KeyValuePair<string, string>;
  23. #endif
  24. #if UNITY_2018_3_OR_NEWER
  25. [Obsolete("Use UnityWebRequest, a fully featured replacement which is more efficient and has additional features")]
  26. #endif
  27. public static partial class ObservableWWW
  28. {
  29. public static IObservable<string> Get(string url, Hash headers = null, IProgress<float> progress = null)
  30. {
  31. return ObservableUnity.FromCoroutine<string>((observer, cancellation) => FetchText(new WWW(url, null, (headers ?? new Hash())), observer, progress, cancellation));
  32. }
  33. public static IObservable<byte[]> GetAndGetBytes(string url, Hash headers = null, IProgress<float> progress = null)
  34. {
  35. return ObservableUnity.FromCoroutine<byte[]>((observer, cancellation) => FetchBytes(new WWW(url, null, (headers ?? new Hash())), observer, progress, cancellation));
  36. }
  37. public static IObservable<WWW> GetWWW(string url, Hash headers = null, IProgress<float> progress = null)
  38. {
  39. return ObservableUnity.FromCoroutine<WWW>((observer, cancellation) => Fetch(new WWW(url, null, (headers ?? new Hash())), observer, progress, cancellation));
  40. }
  41. public static IObservable<string> Post(string url, byte[] postData, IProgress<float> progress = null)
  42. {
  43. return ObservableUnity.FromCoroutine<string>((observer, cancellation) => FetchText(new WWW(url, postData), observer, progress, cancellation));
  44. }
  45. public static IObservable<string> Post(string url, byte[] postData, Hash headers, IProgress<float> progress = null)
  46. {
  47. return ObservableUnity.FromCoroutine<string>((observer, cancellation) => FetchText(new WWW(url, postData, headers), observer, progress, cancellation));
  48. }
  49. public static IObservable<string> Post(string url, WWWForm content, IProgress<float> progress = null)
  50. {
  51. return ObservableUnity.FromCoroutine<string>((observer, cancellation) => FetchText(new WWW(url, content), observer, progress, cancellation));
  52. }
  53. public static IObservable<string> Post(string url, WWWForm content, Hash headers, IProgress<float> progress = null)
  54. {
  55. var contentHeaders = content.headers;
  56. return ObservableUnity.FromCoroutine<string>((observer, cancellation) => FetchText(new WWW(url, content.data, MergeHash(contentHeaders, headers)), observer, progress, cancellation));
  57. }
  58. public static IObservable<byte[]> PostAndGetBytes(string url, byte[] postData, IProgress<float> progress = null)
  59. {
  60. return ObservableUnity.FromCoroutine<byte[]>((observer, cancellation) => FetchBytes(new WWW(url, postData), observer, progress, cancellation));
  61. }
  62. public static IObservable<byte[]> PostAndGetBytes(string url, byte[] postData, Hash headers, IProgress<float> progress = null)
  63. {
  64. return ObservableUnity.FromCoroutine<byte[]>((observer, cancellation) => FetchBytes(new WWW(url, postData, headers), observer, progress, cancellation));
  65. }
  66. public static IObservable<byte[]> PostAndGetBytes(string url, WWWForm content, IProgress<float> progress = null)
  67. {
  68. return ObservableUnity.FromCoroutine<byte[]>((observer, cancellation) => FetchBytes(new WWW(url, content), observer, progress, cancellation));
  69. }
  70. public static IObservable<byte[]> PostAndGetBytes(string url, WWWForm content, Hash headers, IProgress<float> progress = null)
  71. {
  72. var contentHeaders = content.headers;
  73. return ObservableUnity.FromCoroutine<byte[]>((observer, cancellation) => FetchBytes(new WWW(url, content.data, MergeHash(contentHeaders, headers)), observer, progress, cancellation));
  74. }
  75. public static IObservable<WWW> PostWWW(string url, byte[] postData, IProgress<float> progress = null)
  76. {
  77. return ObservableUnity.FromCoroutine<WWW>((observer, cancellation) => Fetch(new WWW(url, postData), observer, progress, cancellation));
  78. }
  79. public static IObservable<WWW> PostWWW(string url, byte[] postData, Hash headers, IProgress<float> progress = null)
  80. {
  81. return ObservableUnity.FromCoroutine<WWW>((observer, cancellation) => Fetch(new WWW(url, postData, headers), observer, progress, cancellation));
  82. }
  83. public static IObservable<WWW> PostWWW(string url, WWWForm content, IProgress<float> progress = null)
  84. {
  85. return ObservableUnity.FromCoroutine<WWW>((observer, cancellation) => Fetch(new WWW(url, content), observer, progress, cancellation));
  86. }
  87. public static IObservable<WWW> PostWWW(string url, WWWForm content, Hash headers, IProgress<float> progress = null)
  88. {
  89. var contentHeaders = content.headers;
  90. return ObservableUnity.FromCoroutine<WWW>((observer, cancellation) => Fetch(new WWW(url, content.data, MergeHash(contentHeaders, headers)), observer, progress, cancellation));
  91. }
  92. public static IObservable<AssetBundle> LoadFromCacheOrDownload(string url, int version, IProgress<float> progress = null)
  93. {
  94. return ObservableUnity.FromCoroutine<AssetBundle>((observer, cancellation) => FetchAssetBundle(WWW.LoadFromCacheOrDownload(url, version), observer, progress, cancellation));
  95. }
  96. public static IObservable<AssetBundle> LoadFromCacheOrDownload(string url, int version, uint crc, IProgress<float> progress = null)
  97. {
  98. return ObservableUnity.FromCoroutine<AssetBundle>((observer, cancellation) => FetchAssetBundle(WWW.LoadFromCacheOrDownload(url, version, crc), observer, progress, cancellation));
  99. }
  100. // over Unity5 supports Hash128
  101. #if !(UNITY_4_7 || UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3 || UNITY_4_2 || UNITY_4_1 || UNITY_4_0_1 || UNITY_4_0 || UNITY_3_5 || UNITY_3_4 || UNITY_3_3 || UNITY_3_2 || UNITY_3_1 || UNITY_3_0_0 || UNITY_3_0 || UNITY_2_6_1 || UNITY_2_6)
  102. public static IObservable<AssetBundle> LoadFromCacheOrDownload(string url, Hash128 hash128, IProgress<float> progress = null)
  103. {
  104. return ObservableUnity.FromCoroutine<AssetBundle>((observer, cancellation) => FetchAssetBundle(WWW.LoadFromCacheOrDownload(url, hash128), observer, progress, cancellation));
  105. }
  106. public static IObservable<AssetBundle> LoadFromCacheOrDownload(string url, Hash128 hash128, uint crc, IProgress<float> progress = null)
  107. {
  108. return ObservableUnity.FromCoroutine<AssetBundle>((observer, cancellation) => FetchAssetBundle(WWW.LoadFromCacheOrDownload(url, hash128, crc), observer, progress, cancellation));
  109. }
  110. #endif
  111. // over 4.5, Hash define is Dictionary.
  112. // below Unity 4.5, WWW only supports Hashtable.
  113. // Unity 4.5, 4.6 WWW supports Dictionary and [Obsolete]Hashtable but WWWForm.content is Hashtable.
  114. // Unity 5.0 WWW only supports Dictionary and WWWForm.content is also Dictionary.
  115. #if !(UNITY_METRO || UNITY_WP8) && (UNITY_4_5 || UNITY_4_6 || UNITY_4_7)
  116. static Hash MergeHash(Hashtable wwwFormHeaders, Hash externalHeaders)
  117. {
  118. var newHeaders = new Hash();
  119. foreach (DictionaryEntry item in wwwFormHeaders)
  120. {
  121. newHeaders[item.Key.ToString()] = item.Value.ToString();
  122. }
  123. foreach (HashEntry item in externalHeaders)
  124. {
  125. newHeaders[item.Key] = item.Value;
  126. }
  127. return newHeaders;
  128. }
  129. #else
  130. static Hash MergeHash(Hash wwwFormHeaders, Hash externalHeaders)
  131. {
  132. foreach (HashEntry item in externalHeaders)
  133. {
  134. wwwFormHeaders[item.Key] = item.Value;
  135. }
  136. return wwwFormHeaders;
  137. }
  138. #endif
  139. static IEnumerator Fetch(WWW www, IObserver<WWW> observer, IProgress<float> reportProgress, CancellationToken cancel)
  140. {
  141. using (www)
  142. {
  143. if (reportProgress != null)
  144. {
  145. while (!www.isDone && !cancel.IsCancellationRequested)
  146. {
  147. try
  148. {
  149. reportProgress.Report(www.progress);
  150. }
  151. catch (Exception ex)
  152. {
  153. observer.OnError(ex);
  154. yield break;
  155. }
  156. yield return null;
  157. }
  158. }
  159. else
  160. {
  161. if (!www.isDone)
  162. {
  163. yield return www;
  164. }
  165. }
  166. if (cancel.IsCancellationRequested)
  167. {
  168. yield break;
  169. }
  170. if (reportProgress != null)
  171. {
  172. try
  173. {
  174. reportProgress.Report(www.progress);
  175. }
  176. catch (Exception ex)
  177. {
  178. observer.OnError(ex);
  179. yield break;
  180. }
  181. }
  182. if (!string.IsNullOrEmpty(www.error))
  183. {
  184. observer.OnError(new WWWErrorException(www, www.text));
  185. }
  186. else
  187. {
  188. observer.OnNext(www);
  189. observer.OnCompleted();
  190. }
  191. }
  192. }
  193. static IEnumerator FetchText(WWW www, IObserver<string> observer, IProgress<float> reportProgress, CancellationToken cancel)
  194. {
  195. using (www)
  196. {
  197. if (reportProgress != null)
  198. {
  199. while (!www.isDone && !cancel.IsCancellationRequested)
  200. {
  201. try
  202. {
  203. reportProgress.Report(www.progress);
  204. }
  205. catch (Exception ex)
  206. {
  207. observer.OnError(ex);
  208. yield break;
  209. }
  210. yield return null;
  211. }
  212. }
  213. else
  214. {
  215. if (!www.isDone)
  216. {
  217. yield return www;
  218. }
  219. }
  220. if (cancel.IsCancellationRequested)
  221. {
  222. yield break;
  223. }
  224. if (reportProgress != null)
  225. {
  226. try
  227. {
  228. reportProgress.Report(www.progress);
  229. }
  230. catch (Exception ex)
  231. {
  232. observer.OnError(ex);
  233. yield break;
  234. }
  235. }
  236. if (!string.IsNullOrEmpty(www.error))
  237. {
  238. observer.OnError(new WWWErrorException(www, www.text));
  239. }
  240. else
  241. {
  242. observer.OnNext(www.text);
  243. observer.OnCompleted();
  244. }
  245. }
  246. }
  247. static IEnumerator FetchBytes(WWW www, IObserver<byte[]> observer, IProgress<float> reportProgress, CancellationToken cancel)
  248. {
  249. using (www)
  250. {
  251. if (reportProgress != null)
  252. {
  253. while (!www.isDone && !cancel.IsCancellationRequested)
  254. {
  255. try
  256. {
  257. reportProgress.Report(www.progress);
  258. }
  259. catch (Exception ex)
  260. {
  261. observer.OnError(ex);
  262. yield break;
  263. }
  264. yield return null;
  265. }
  266. }
  267. else
  268. {
  269. if (!www.isDone)
  270. {
  271. yield return www;
  272. }
  273. }
  274. if (cancel.IsCancellationRequested)
  275. {
  276. yield break;
  277. }
  278. if (reportProgress != null)
  279. {
  280. try
  281. {
  282. reportProgress.Report(www.progress);
  283. }
  284. catch (Exception ex)
  285. {
  286. observer.OnError(ex);
  287. yield break;
  288. }
  289. }
  290. if (!string.IsNullOrEmpty(www.error))
  291. {
  292. observer.OnError(new WWWErrorException(www, www.text));
  293. }
  294. else
  295. {
  296. observer.OnNext(www.bytes);
  297. observer.OnCompleted();
  298. }
  299. }
  300. }
  301. static IEnumerator FetchAssetBundle(WWW www, IObserver<AssetBundle> observer, IProgress<float> reportProgress, CancellationToken cancel)
  302. {
  303. using (www)
  304. {
  305. if (reportProgress != null)
  306. {
  307. while (!www.isDone && !cancel.IsCancellationRequested)
  308. {
  309. try
  310. {
  311. reportProgress.Report(www.progress);
  312. }
  313. catch (Exception ex)
  314. {
  315. observer.OnError(ex);
  316. yield break;
  317. }
  318. yield return null;
  319. }
  320. }
  321. else
  322. {
  323. if (!www.isDone)
  324. {
  325. yield return www;
  326. }
  327. }
  328. if (cancel.IsCancellationRequested)
  329. {
  330. yield break;
  331. }
  332. if (reportProgress != null)
  333. {
  334. try
  335. {
  336. reportProgress.Report(www.progress);
  337. }
  338. catch (Exception ex)
  339. {
  340. observer.OnError(ex);
  341. yield break;
  342. }
  343. }
  344. if (!string.IsNullOrEmpty(www.error))
  345. {
  346. observer.OnError(new WWWErrorException(www, ""));
  347. }
  348. else
  349. {
  350. observer.OnNext(www.assetBundle);
  351. observer.OnCompleted();
  352. }
  353. }
  354. }
  355. }
  356. public class WWWErrorException : Exception
  357. {
  358. public string RawErrorMessage { get; private set; }
  359. public bool HasResponse { get; private set; }
  360. public string Text { get; private set; }
  361. public System.Net.HttpStatusCode StatusCode { get; private set; }
  362. public System.Collections.Generic.Dictionary<string, string> ResponseHeaders { get; private set; }
  363. public WWW WWW { get; private set; }
  364. // cache the text because if www was disposed, can't access it.
  365. public WWWErrorException(WWW www, string text)
  366. {
  367. this.WWW = www;
  368. this.RawErrorMessage = www.error;
  369. this.ResponseHeaders = www.responseHeaders;
  370. this.HasResponse = false;
  371. this.Text = text;
  372. var splitted = RawErrorMessage.Split(' ', ':');
  373. if (splitted.Length != 0)
  374. {
  375. int statusCode;
  376. if (int.TryParse(splitted[0], out statusCode))
  377. {
  378. this.HasResponse = true;
  379. this.StatusCode = (System.Net.HttpStatusCode)statusCode;
  380. }
  381. }
  382. }
  383. public override string ToString()
  384. {
  385. var text = this.Text;
  386. if (string.IsNullOrEmpty(text))
  387. {
  388. return RawErrorMessage;
  389. }
  390. else
  391. {
  392. return RawErrorMessage + " " + text;
  393. }
  394. }
  395. }
  396. }
  397. #if UNITY_2018_3_OR_NEWER
  398. #pragma warning restore CS0618
  399. #endif