PDFJS_Library.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. http://www.cgsoso.com/forum-211-1.html
  3. CG搜搜 Unity3d 每日Unity3d插件免费更新 更有VIP资源!
  4. CGSOSO 主打游戏开发,影视设计等CG资源素材。
  5. 插件如若商用,请务必官网购买!
  6. daily assets update for try.
  7. U should buy the asset from home store if u use it in your project!
  8. */
  9. using UnityEngine;
  10. using System.Runtime.InteropServices;
  11. using System;
  12. using System.Collections;
  13. using Paroxe.PdfRenderer;
  14. namespace Paroxe.PdfRenderer.WebGL
  15. {
  16. public class PDFJS_Library : MonoBehaviour
  17. {
  18. private Hashtable m_PromiseCoroutineMap = new Hashtable();
  19. private bool m_IsInitialized;
  20. private static PDFJS_Library s_Instance;
  21. public static PDFJS_Library Instance
  22. {
  23. get
  24. {
  25. if (s_Instance == null)
  26. s_Instance = FindObjectOfType<PDFJS_Library>();
  27. if (s_Instance == null)
  28. s_Instance = new GameObject("WebGL_JSRuntime").AddComponent<PDFJS_Library>();
  29. return s_Instance;
  30. }
  31. }
  32. public PDFJS_PromiseCoroutine PreparePromiseCoroutine(
  33. Func<PDFJS_PromiseCoroutine, IPDFJS_Promise, object, IEnumerator> coroutine, IPDFJS_Promise promise, object parameters)
  34. {
  35. m_PromiseCoroutineMap[promise.PromiseHandle] = new PDFJS_PromiseCoroutine(this, promise, coroutine, parameters);
  36. return (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promise.PromiseHandle];
  37. }
  38. private string GetMessagePromiseHandle(string message)
  39. {
  40. string promiseHandle = message.Substring(message.IndexOf('{'));
  41. return promiseHandle.Substring(0, promiseHandle.IndexOf('}') + 1).Trim();
  42. }
  43. private string GetMessageObjectHandle(string message)
  44. {
  45. return message.Substring(message.IndexOf("objectHandle:")).Replace("objectHandle:", "").Trim();
  46. }
  47. private void OnPromiseThen(string message)
  48. {
  49. string promiseHandle = GetMessagePromiseHandle(message);
  50. string objectHandle = GetMessageObjectHandle(message);
  51. if (m_PromiseCoroutineMap.Contains(promiseHandle))
  52. {
  53. PDFJS_PromiseCoroutine promiseCoroutine = (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promiseHandle];
  54. promiseCoroutine.Promise.JSObjectHandle = objectHandle;
  55. promiseCoroutine.Promise.HasSucceeded = true;
  56. promiseCoroutine.Promise.HasReceivedJSResponse = true;
  57. m_PromiseCoroutineMap.Remove(promiseHandle);
  58. }
  59. }
  60. private void OnPromiseCatch(string message)
  61. {
  62. string promiseHandle = GetMessagePromiseHandle(message);
  63. string objectHandle = GetMessageObjectHandle(message);
  64. if (m_PromiseCoroutineMap.Contains(promiseHandle))
  65. {
  66. PDFJS_PromiseCoroutine promiseCoroutine = (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promiseHandle];
  67. promiseCoroutine.Promise.JSObjectHandle = objectHandle;
  68. promiseCoroutine.Promise.HasSucceeded = false;
  69. promiseCoroutine.Promise.HasReceivedJSResponse = true;
  70. m_PromiseCoroutineMap.Remove(promiseHandle);
  71. }
  72. }
  73. private void OnPromiseCancel(string message)
  74. {
  75. string promiseHandle = GetMessagePromiseHandle(message);
  76. string objectHandle = GetMessageObjectHandle(message);
  77. if (m_PromiseCoroutineMap.Contains(promiseHandle))
  78. {
  79. PDFJS_PromiseCoroutine promiseCoroutine = (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promiseHandle];
  80. promiseCoroutine.Promise.JSObjectHandle = objectHandle;
  81. promiseCoroutine.Promise.HasBeenCancelled = true;
  82. promiseCoroutine.Promise.HasSucceeded = false;
  83. promiseCoroutine.Promise.HasReceivedJSResponse = true;
  84. m_PromiseCoroutineMap.Remove(promiseHandle);
  85. }
  86. }
  87. public void OnLibraryInitialized(string message)
  88. {
  89. PDFLibrary.Instance.IsInitialized = true;
  90. }
  91. public void TryTerminateRenderingWorker(string promiseHandle)
  92. {
  93. #if UNITY_WEBGL && !UNITY_EDITOR
  94. PDFJS_TryTerminateRenderWorker(promiseHandle);
  95. #endif
  96. }
  97. #if UNITY_WEBGL && !UNITY_EDITOR
  98. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  99. private static extern void PDFJS_TryTerminateRenderWorker(string promiseHandle);
  100. #endif
  101. }
  102. }