PDFJS_Library.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using UnityEngine;
  2. using System.Runtime.InteropServices;
  3. using System;
  4. using System.Collections;
  5. using Paroxe.PdfRenderer;
  6. using Paroxe.PdfRenderer.Internal;
  7. namespace Paroxe.PdfRenderer.WebGL
  8. {
  9. public class PDFJS_Library : MonoBehaviour
  10. {
  11. private Hashtable m_PromiseCoroutineMap = new Hashtable();
  12. private bool m_IsInitialized;
  13. private static PDFJS_Library s_Instance;
  14. public static PDFJS_Library Instance
  15. {
  16. get
  17. {
  18. if (s_Instance == null)
  19. s_Instance = FindObjectOfType<PDFJS_Library>();
  20. if (s_Instance == null)
  21. s_Instance = new GameObject("WebGL_JSRuntime").AddComponent<PDFJS_Library>();
  22. return s_Instance;
  23. }
  24. }
  25. public PDFJS_PromiseCoroutine PreparePromiseCoroutine(
  26. Func<PDFJS_PromiseCoroutine, IPDFJS_Promise, object, IEnumerator> coroutine, IPDFJS_Promise promise, object parameters)
  27. {
  28. m_PromiseCoroutineMap[promise.PromiseHandle] = new PDFJS_PromiseCoroutine(this, promise, coroutine, parameters);
  29. return (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promise.PromiseHandle];
  30. }
  31. private void Awake()
  32. {
  33. DontDestroyOnLoad(gameObject);
  34. }
  35. private string GetMessagePromiseHandle(string message)
  36. {
  37. string promiseHandle = message.Substring(message.IndexOf('{'));
  38. return promiseHandle.Substring(0, promiseHandle.IndexOf('}') + 1).Trim();
  39. }
  40. private string GetMessageObjectHandle(string message)
  41. {
  42. return message.Substring(message.IndexOf("objectHandle:")).Replace("objectHandle:", "").Trim();
  43. }
  44. private string GetMessageProgress(string message)
  45. {
  46. return message.Substring(message.IndexOf("progress:")).Replace("progress:", "").Trim();
  47. }
  48. private void OnPromiseProgress(string message)
  49. {
  50. string promiseHandle = GetMessagePromiseHandle(message);
  51. string progress = GetMessageProgress(message);
  52. if (m_PromiseCoroutineMap.Contains(promiseHandle))
  53. {
  54. PDFJS_PromiseCoroutine promiseCoroutine = (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promiseHandle];
  55. promiseCoroutine.Promise.Progress = float.Parse(progress);
  56. }
  57. }
  58. private void OnPromiseThen(string message)
  59. {
  60. string promiseHandle = GetMessagePromiseHandle(message);
  61. string objectHandle = GetMessageObjectHandle(message);
  62. if (m_PromiseCoroutineMap.Contains(promiseHandle))
  63. {
  64. PDFJS_PromiseCoroutine promiseCoroutine = (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promiseHandle];
  65. promiseCoroutine.Promise.JSObjectHandle = objectHandle;
  66. promiseCoroutine.Promise.HasSucceeded = true;
  67. promiseCoroutine.Promise.HasReceivedJSResponse = true;
  68. m_PromiseCoroutineMap.Remove(promiseHandle);
  69. }
  70. }
  71. private void OnPromiseCatch(string message)
  72. {
  73. string promiseHandle = GetMessagePromiseHandle(message);
  74. string objectHandle = GetMessageObjectHandle(message);
  75. if (m_PromiseCoroutineMap.Contains(promiseHandle))
  76. {
  77. PDFJS_PromiseCoroutine promiseCoroutine = (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promiseHandle];
  78. promiseCoroutine.Promise.JSObjectHandle = objectHandle;
  79. promiseCoroutine.Promise.HasSucceeded = false;
  80. promiseCoroutine.Promise.HasReceivedJSResponse = true;
  81. m_PromiseCoroutineMap.Remove(promiseHandle);
  82. }
  83. }
  84. private void OnPromiseCancel(string message)
  85. {
  86. string promiseHandle = GetMessagePromiseHandle(message);
  87. string objectHandle = GetMessageObjectHandle(message);
  88. if (m_PromiseCoroutineMap.Contains(promiseHandle))
  89. {
  90. PDFJS_PromiseCoroutine promiseCoroutine = (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promiseHandle];
  91. promiseCoroutine.Promise.JSObjectHandle = objectHandle;
  92. promiseCoroutine.Promise.HasBeenCancelled = true;
  93. promiseCoroutine.Promise.HasSucceeded = false;
  94. promiseCoroutine.Promise.HasReceivedJSResponse = true;
  95. m_PromiseCoroutineMap.Remove(promiseHandle);
  96. }
  97. }
  98. public void OnLibraryInitialized(string message)
  99. {
  100. PDFLibrary.Instance.IsInitialized = true;
  101. }
  102. public void TryTerminateRenderingWorker(string promiseHandle)
  103. {
  104. #if UNITY_WEBGL && !UNITY_EDITOR
  105. NativeMethods.PDFJS_TryTerminateRenderWorker(promiseHandle);
  106. #endif
  107. }
  108. }
  109. }